blob: 03897b1c22745e359c5884b5f1ebd68e5f7835ab [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
Eric Fiselier3a0e4302015-06-13 07:08:02 +00001985 && !__libcpp_is_final<_T1>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00001986 bool = is_empty<_T2>::value
Eric Fiselier3a0e4302015-06-13 07:08:02 +00001987 && !__libcpp_is_final<_T2>::value
Howard Hinnantd4cf2152011-12-11 20:31:33 +00001988 >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001989struct __libcpp_compressed_pair_switch;
1990
1991template <class _T1, class _T2, bool IsSame>
1992struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1993
1994template <class _T1, class _T2, bool IsSame>
1995struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1996
1997template <class _T1, class _T2, bool IsSame>
1998struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1999
2000template <class _T1, class _T2>
2001struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
2002
2003template <class _T1, class _T2>
2004struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
2005
2006template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
2007class __libcpp_compressed_pair_imp;
2008
2009template <class _T1, class _T2>
2010class __libcpp_compressed_pair_imp<_T1, _T2, 0>
2011{
2012private:
2013 _T1 __first_;
2014 _T2 __second_;
2015public:
2016 typedef _T1 _T1_param;
2017 typedef _T2 _T2_param;
2018
2019 typedef typename remove_reference<_T1>::type& _T1_reference;
2020 typedef typename remove_reference<_T2>::type& _T2_reference;
2021
2022 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2023 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2024
2025 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002026 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002027 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002028 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002029 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002030 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002031 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002032
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002033#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002034
2035 _LIBCPP_INLINE_VISIBILITY
2036 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2037 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2038 is_nothrow_copy_constructible<_T2>::value)
2039 : __first_(__p.first()),
2040 __second_(__p.second()) {}
2041
2042 _LIBCPP_INLINE_VISIBILITY
2043 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2044 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2045 is_nothrow_copy_assignable<_T2>::value)
2046 {
2047 __first_ = __p.first();
2048 __second_ = __p.second();
2049 return *this;
2050 }
2051
Howard Hinnant61aa6012011-07-01 19:24:36 +00002052 _LIBCPP_INLINE_VISIBILITY
2053 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002054 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2055 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002056 : __first_(_VSTD::forward<_T1>(__p.first())),
2057 __second_(_VSTD::forward<_T2>(__p.second())) {}
2058
2059 _LIBCPP_INLINE_VISIBILITY
2060 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2061 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2062 is_nothrow_move_assignable<_T2>::value)
2063 {
2064 __first_ = _VSTD::forward<_T1>(__p.first());
2065 __second_ = _VSTD::forward<_T2>(__p.second());
2066 return *this;
2067 }
2068
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002069#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002070
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002071#ifndef _LIBCPP_HAS_NO_VARIADICS
2072
2073 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2074 _LIBCPP_INLINE_VISIBILITY
2075 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2076 tuple<_Args1...> __first_args,
2077 tuple<_Args2...> __second_args,
2078 __tuple_indices<_I1...>,
2079 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002080 : __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2081 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002082 {}
2083
2084#endif // _LIBCPP_HAS_NO_VARIADICS
2085
Howard Hinnant1694d232011-05-28 14:41:13 +00002086 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2087 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002088
Howard Hinnant1694d232011-05-28 14:41:13 +00002089 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2090 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002091
2092 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002093 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002094 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002095 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002096 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002097 swap(__first_, __x.__first_);
2098 swap(__second_, __x.__second_);
2099 }
2100};
2101
2102template <class _T1, class _T2>
2103class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2104 : private _T1
2105{
2106private:
2107 _T2 __second_;
2108public:
2109 typedef _T1 _T1_param;
2110 typedef _T2 _T2_param;
2111
2112 typedef _T1& _T1_reference;
2113 typedef typename remove_reference<_T2>::type& _T2_reference;
2114
2115 typedef const _T1& _T1_const_reference;
2116 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2117
2118 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002119 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002120 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002121 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002122 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002123 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002124 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002125
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002126#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002127
2128 _LIBCPP_INLINE_VISIBILITY
2129 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2130 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2131 is_nothrow_copy_constructible<_T2>::value)
2132 : _T1(__p.first()), __second_(__p.second()) {}
2133
2134 _LIBCPP_INLINE_VISIBILITY
2135 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2136 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2137 is_nothrow_copy_assignable<_T2>::value)
2138 {
2139 _T1::operator=(__p.first());
2140 __second_ = __p.second();
2141 return *this;
2142 }
2143
Howard Hinnant61aa6012011-07-01 19:24:36 +00002144 _LIBCPP_INLINE_VISIBILITY
2145 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002146 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2147 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002148 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002149
2150 _LIBCPP_INLINE_VISIBILITY
2151 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2152 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2153 is_nothrow_move_assignable<_T2>::value)
2154 {
2155 _T1::operator=(_VSTD::move(__p.first()));
2156 __second_ = _VSTD::forward<_T2>(__p.second());
2157 return *this;
2158 }
2159
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002160#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002161
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002162#ifndef _LIBCPP_HAS_NO_VARIADICS
2163
2164 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2165 _LIBCPP_INLINE_VISIBILITY
2166 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2167 tuple<_Args1...> __first_args,
2168 tuple<_Args2...> __second_args,
2169 __tuple_indices<_I1...>,
2170 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002171 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2172 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002173 {}
2174
2175#endif // _LIBCPP_HAS_NO_VARIADICS
2176
Howard Hinnant1694d232011-05-28 14:41:13 +00002177 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2178 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002179
Howard Hinnant1694d232011-05-28 14:41:13 +00002180 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2181 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002182
2183 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002184 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002185 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002186 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002187 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002188 swap(__second_, __x.__second_);
2189 }
2190};
2191
2192template <class _T1, class _T2>
2193class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2194 : private _T2
2195{
2196private:
2197 _T1 __first_;
2198public:
2199 typedef _T1 _T1_param;
2200 typedef _T2 _T2_param;
2201
2202 typedef typename remove_reference<_T1>::type& _T1_reference;
2203 typedef _T2& _T2_reference;
2204
2205 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2206 typedef const _T2& _T2_const_reference;
2207
2208 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2209 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002210 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002211 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002212 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002213 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00002214 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2215 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002216 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002217
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002218#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002219
2220 _LIBCPP_INLINE_VISIBILITY
2221 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2222 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2223 is_nothrow_copy_constructible<_T2>::value)
2224 : _T2(__p.second()), __first_(__p.first()) {}
2225
2226 _LIBCPP_INLINE_VISIBILITY
2227 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2228 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2229 is_nothrow_copy_assignable<_T2>::value)
2230 {
2231 _T2::operator=(__p.second());
2232 __first_ = __p.first();
2233 return *this;
2234 }
2235
Howard Hinnant61aa6012011-07-01 19:24:36 +00002236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002237 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002238 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2239 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002240 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002241
2242 _LIBCPP_INLINE_VISIBILITY
2243 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2244 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2245 is_nothrow_move_assignable<_T2>::value)
2246 {
2247 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2248 __first_ = _VSTD::move(__p.first());
2249 return *this;
2250 }
2251
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002252#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002253
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002254#ifndef _LIBCPP_HAS_NO_VARIADICS
2255
2256 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2257 _LIBCPP_INLINE_VISIBILITY
2258 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2259 tuple<_Args1...> __first_args,
2260 tuple<_Args2...> __second_args,
2261 __tuple_indices<_I1...>,
2262 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002263 : _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...),
2264 __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002265
2266 {}
2267
2268#endif // _LIBCPP_HAS_NO_VARIADICS
2269
Howard Hinnant1694d232011-05-28 14:41:13 +00002270 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2271 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002272
Howard Hinnant1694d232011-05-28 14:41:13 +00002273 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2274 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002275
2276 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002277 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002278 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002279 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002280 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002281 swap(__first_, __x.__first_);
2282 }
2283};
2284
2285template <class _T1, class _T2>
2286class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2287 : private _T1,
2288 private _T2
2289{
2290public:
2291 typedef _T1 _T1_param;
2292 typedef _T2 _T2_param;
2293
2294 typedef _T1& _T1_reference;
2295 typedef _T2& _T2_reference;
2296
2297 typedef const _T1& _T1_const_reference;
2298 typedef const _T2& _T2_const_reference;
2299
2300 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2301 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002302 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002303 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002304 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002305 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002306 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002307
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002308#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002309
2310 _LIBCPP_INLINE_VISIBILITY
2311 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2312 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2313 is_nothrow_copy_constructible<_T2>::value)
2314 : _T1(__p.first()), _T2(__p.second()) {}
2315
2316 _LIBCPP_INLINE_VISIBILITY
2317 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2318 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2319 is_nothrow_copy_assignable<_T2>::value)
2320 {
2321 _T1::operator=(__p.first());
2322 _T2::operator=(__p.second());
2323 return *this;
2324 }
2325
Howard Hinnant61aa6012011-07-01 19:24:36 +00002326 _LIBCPP_INLINE_VISIBILITY
2327 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002328 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2329 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002330 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002331
2332 _LIBCPP_INLINE_VISIBILITY
2333 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2334 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2335 is_nothrow_move_assignable<_T2>::value)
2336 {
2337 _T1::operator=(_VSTD::move(__p.first()));
2338 _T2::operator=(_VSTD::move(__p.second()));
2339 return *this;
2340 }
2341
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002342#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002343
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002344#ifndef _LIBCPP_HAS_NO_VARIADICS
2345
2346 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2347 _LIBCPP_INLINE_VISIBILITY
2348 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2349 tuple<_Args1...> __first_args,
2350 tuple<_Args2...> __second_args,
2351 __tuple_indices<_I1...>,
2352 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002353 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2354 _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002355 {}
2356
2357#endif // _LIBCPP_HAS_NO_VARIADICS
2358
Howard Hinnant1694d232011-05-28 14:41:13 +00002359 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2360 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002361
Howard Hinnant1694d232011-05-28 14:41:13 +00002362 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2363 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002364
Howard Hinnantec3773c2011-12-01 20:21:04 +00002365 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant1694d232011-05-28 14:41:13 +00002366 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002367 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002368 {
2369 }
2370};
2371
2372template <class _T1, class _T2>
2373class __compressed_pair
2374 : private __libcpp_compressed_pair_imp<_T1, _T2>
2375{
2376 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2377public:
2378 typedef typename base::_T1_param _T1_param;
2379 typedef typename base::_T2_param _T2_param;
2380
2381 typedef typename base::_T1_reference _T1_reference;
2382 typedef typename base::_T2_reference _T2_reference;
2383
2384 typedef typename base::_T1_const_reference _T1_const_reference;
2385 typedef typename base::_T2_const_reference _T2_const_reference;
2386
2387 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002388 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002389 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002390 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002391 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002392 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002393 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002394
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002395#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002396
2397 _LIBCPP_INLINE_VISIBILITY
2398 __compressed_pair(const __compressed_pair& __p)
2399 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2400 is_nothrow_copy_constructible<_T2>::value)
2401 : base(__p) {}
2402
2403 _LIBCPP_INLINE_VISIBILITY
2404 __compressed_pair& operator=(const __compressed_pair& __p)
2405 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2406 is_nothrow_copy_assignable<_T2>::value)
2407 {
2408 base::operator=(__p);
2409 return *this;
2410 }
2411
Howard Hinnant61aa6012011-07-01 19:24:36 +00002412 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002413 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002414 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2415 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002416 : base(_VSTD::move(__p)) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002417
2418 _LIBCPP_INLINE_VISIBILITY
2419 __compressed_pair& operator=(__compressed_pair&& __p)
2420 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2421 is_nothrow_move_assignable<_T2>::value)
2422 {
2423 base::operator=(_VSTD::move(__p));
2424 return *this;
2425 }
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002426
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002427#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002428
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002429#ifndef _LIBCPP_HAS_NO_VARIADICS
2430
2431 template <class... _Args1, class... _Args2>
2432 _LIBCPP_INLINE_VISIBILITY
2433 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2434 tuple<_Args2...> __second_args)
Howard Hinnant57199402012-01-02 17:56:02 +00002435 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002436 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2437 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2438 {}
2439
2440#endif // _LIBCPP_HAS_NO_VARIADICS
2441
Howard Hinnant1694d232011-05-28 14:41:13 +00002442 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2443 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002444
Howard Hinnant1694d232011-05-28 14:41:13 +00002445 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2446 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002447
Howard Hinnant1694d232011-05-28 14:41:13 +00002448 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2449 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002450 __is_nothrow_swappable<_T2>::value)
Howard Hinnant1694d232011-05-28 14:41:13 +00002451 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002452};
2453
2454template <class _T1, class _T2>
2455inline _LIBCPP_INLINE_VISIBILITY
2456void
2457swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002458 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002459 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002460 {__x.swap(__y);}
2461
Howard Hinnant57199402012-01-02 17:56:02 +00002462// __same_or_less_cv_qualified
2463
2464template <class _Ptr1, class _Ptr2,
2465 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2466 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2467 >::value
2468 >
2469struct __same_or_less_cv_qualified_imp
2470 : is_convertible<_Ptr1, _Ptr2> {};
2471
2472template <class _Ptr1, class _Ptr2>
2473struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2474 : false_type {};
2475
Marshall Clow5f64a2b2014-04-26 05:19:48 +00002476template <class _Ptr1, class _Ptr2, bool = is_pointer<_Ptr1>::value ||
2477 is_same<_Ptr1, _Ptr2>::value ||
2478 __has_element_type<_Ptr1>::value>
Howard Hinnant57199402012-01-02 17:56:02 +00002479struct __same_or_less_cv_qualified
2480 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2481
2482template <class _Ptr1, class _Ptr2>
Marshall Clow5f64a2b2014-04-26 05:19:48 +00002483struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, false>
Howard Hinnant57199402012-01-02 17:56:02 +00002484 : false_type {};
2485
2486// default_delete
2487
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002488template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002489struct _LIBCPP_TYPE_VIS_ONLY default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002490{
Howard Hinnant46e94932012-07-07 20:56:04 +00002491#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2492 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2493#else
2494 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2495#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002496 template <class _Up>
2497 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002498 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2499 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002500 {
2501 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant05e7d242013-04-24 19:44:26 +00002502 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002503 delete __ptr;
2504 }
2505};
2506
2507template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002508struct _LIBCPP_TYPE_VIS_ONLY default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002509{
Howard Hinnant8e843502011-12-18 21:19:44 +00002510public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002511#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2512 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2513#else
2514 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2515#endif
Howard Hinnant8e843502011-12-18 21:19:44 +00002516 template <class _Up>
2517 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant57199402012-01-02 17:56:02 +00002518 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant8e843502011-12-18 21:19:44 +00002519 template <class _Up>
2520 _LIBCPP_INLINE_VISIBILITY
2521 void operator() (_Up* __ptr,
Howard Hinnant57199402012-01-02 17:56:02 +00002522 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002523 {
2524 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant05e7d242013-04-24 19:44:26 +00002525 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002526 delete [] __ptr;
2527 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002528};
2529
2530template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002531class _LIBCPP_TYPE_VIS_ONLY unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002532{
2533public:
2534 typedef _Tp element_type;
2535 typedef _Dp deleter_type;
2536 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2537private:
2538 __compressed_pair<pointer, deleter_type> __ptr_;
2539
Howard Hinnant57199402012-01-02 17:56:02 +00002540#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002541 unique_ptr(unique_ptr&);
2542 template <class _Up, class _Ep>
2543 unique_ptr(unique_ptr<_Up, _Ep>&);
2544 unique_ptr& operator=(unique_ptr&);
2545 template <class _Up, class _Ep>
2546 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002547#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002548
2549 struct __nat {int __for_bool_;};
2550
2551 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2552 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2553public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002554 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002555 : __ptr_(pointer())
2556 {
2557 static_assert(!is_pointer<deleter_type>::value,
2558 "unique_ptr constructed with null function pointer deleter");
2559 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002560 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002561 : __ptr_(pointer())
2562 {
2563 static_assert(!is_pointer<deleter_type>::value,
2564 "unique_ptr constructed with null function pointer deleter");
2565 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002566 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002567 : __ptr_(_VSTD::move(__p))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002568 {
2569 static_assert(!is_pointer<deleter_type>::value,
2570 "unique_ptr constructed with null function pointer deleter");
2571 }
2572
Howard Hinnant73d21a42010-09-04 23:28:19 +00002573#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002574 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2575 is_reference<deleter_type>::value,
2576 deleter_type,
2577 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002578 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002579 : __ptr_(__p, __d) {}
2580
2581 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002582 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002583 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002584 {
2585 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2586 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002587 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002588 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002589 template <class _Up, class _Ep>
2590 _LIBCPP_INLINE_VISIBILITY
2591 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2592 typename enable_if
2593 <
2594 !is_array<_Up>::value &&
2595 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2596 is_convertible<_Ep, deleter_type>::value &&
2597 (
2598 !is_reference<deleter_type>::value ||
2599 is_same<deleter_type, _Ep>::value
2600 ),
2601 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002602 >::type = __nat()) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002603 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002604
2605 template <class _Up>
2606 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2607 typename enable_if<
2608 is_convertible<_Up*, _Tp*>::value &&
2609 is_same<_Dp, default_delete<_Tp> >::value,
2610 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002611 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002612 : __ptr_(__p.release())
2613 {
2614 }
2615
Howard Hinnant1694d232011-05-28 14:41:13 +00002616 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002617 {
2618 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002619 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002620 return *this;
2621 }
2622
2623 template <class _Up, class _Ep>
2624 _LIBCPP_INLINE_VISIBILITY
2625 typename enable_if
2626 <
Howard Hinnant57199402012-01-02 17:56:02 +00002627 !is_array<_Up>::value &&
2628 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2629 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002630 unique_ptr&
2631 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002632 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002633 {
2634 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002635 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002636 return *this;
2637 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002638#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002639
2640 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2641 {
2642 return __rv<unique_ptr>(*this);
2643 }
2644
2645 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002646 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002647
2648 template <class _Up, class _Ep>
2649 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2650 {
2651 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002652 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002653 return *this;
2654 }
2655
2656 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002657 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002658
2659 template <class _Up>
2660 _LIBCPP_INLINE_VISIBILITY
2661 typename enable_if<
2662 is_convertible<_Up*, _Tp*>::value &&
2663 is_same<_Dp, default_delete<_Tp> >::value,
2664 unique_ptr&
2665 >::type
2666 operator=(auto_ptr<_Up> __p)
2667 {reset(__p.release()); return *this;}
2668
Howard Hinnant73d21a42010-09-04 23:28:19 +00002669#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002670 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2671
Howard Hinnant1694d232011-05-28 14:41:13 +00002672 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002673 {
2674 reset();
2675 return *this;
2676 }
2677
2678 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2679 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002680 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2681 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2682 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2683 {return __ptr_.second();}
2684 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2685 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002686 _LIBCPP_INLINE_VISIBILITY
2687 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2688 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002689
Howard Hinnant1694d232011-05-28 14:41:13 +00002690 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002691 {
2692 pointer __t = __ptr_.first();
2693 __ptr_.first() = pointer();
2694 return __t;
2695 }
2696
Howard Hinnant1694d232011-05-28 14:41:13 +00002697 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002698 {
2699 pointer __tmp = __ptr_.first();
2700 __ptr_.first() = __p;
2701 if (__tmp)
2702 __ptr_.second()(__tmp);
2703 }
2704
Howard Hinnant1694d232011-05-28 14:41:13 +00002705 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2706 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002707};
2708
2709template <class _Tp, class _Dp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002710class _LIBCPP_TYPE_VIS_ONLY unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002711{
2712public:
2713 typedef _Tp element_type;
2714 typedef _Dp deleter_type;
2715 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2716private:
2717 __compressed_pair<pointer, deleter_type> __ptr_;
2718
Howard Hinnant57199402012-01-02 17:56:02 +00002719#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002720 unique_ptr(unique_ptr&);
2721 template <class _Up>
2722 unique_ptr(unique_ptr<_Up>&);
2723 unique_ptr& operator=(unique_ptr&);
2724 template <class _Up>
2725 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002726#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002727
2728 struct __nat {int __for_bool_;};
2729
2730 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2731 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2732public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002733 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002734 : __ptr_(pointer())
2735 {
2736 static_assert(!is_pointer<deleter_type>::value,
2737 "unique_ptr constructed with null function pointer deleter");
2738 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002739 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002740 : __ptr_(pointer())
2741 {
2742 static_assert(!is_pointer<deleter_type>::value,
2743 "unique_ptr constructed with null function pointer deleter");
2744 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002745#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00002746 template <class _Pp>
2747 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p,
2748 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002749 : __ptr_(__p)
2750 {
2751 static_assert(!is_pointer<deleter_type>::value,
2752 "unique_ptr constructed with null function pointer deleter");
2753 }
2754
Logan Chiene1678a12014-01-31 09:30:46 +00002755 template <class _Pp>
Howard Hinnant99968442011-11-29 18:15:50 +00002756 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002757 is_reference<deleter_type>::value,
2758 deleter_type,
Logan Chiene1678a12014-01-31 09:30:46 +00002759 typename add_lvalue_reference<const deleter_type>::type>::type __d,
2760 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant1694d232011-05-28 14:41:13 +00002761 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002762 : __ptr_(__p, __d) {}
2763
2764 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2765 is_reference<deleter_type>::value,
2766 deleter_type,
2767 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002768 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002769 : __ptr_(pointer(), __d) {}
2770
Logan Chiene1678a12014-01-31 09:30:46 +00002771 template <class _Pp>
2772 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p,
2773 typename remove_reference<deleter_type>::type&& __d,
2774 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant1694d232011-05-28 14:41:13 +00002775 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002776 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002777 {
2778 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2779 }
2780
2781 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002782 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002783 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002784 {
2785 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2786 }
2787
Howard Hinnant1694d232011-05-28 14:41:13 +00002788 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002789 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002790
Howard Hinnant1694d232011-05-28 14:41:13 +00002791 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002792 {
2793 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002794 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002795 return *this;
2796 }
Howard Hinnant8e843502011-12-18 21:19:44 +00002797
2798 template <class _Up, class _Ep>
2799 _LIBCPP_INLINE_VISIBILITY
2800 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2801 typename enable_if
2802 <
2803 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002804 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant8e843502011-12-18 21:19:44 +00002805 && is_convertible<_Ep, deleter_type>::value &&
2806 (
2807 !is_reference<deleter_type>::value ||
2808 is_same<deleter_type, _Ep>::value
2809 ),
2810 __nat
2811 >::type = __nat()
2812 ) _NOEXCEPT
2813 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2814
2815
2816 template <class _Up, class _Ep>
2817 _LIBCPP_INLINE_VISIBILITY
2818 typename enable_if
2819 <
2820 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002821 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2822 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant8e843502011-12-18 21:19:44 +00002823 unique_ptr&
2824 >::type
2825 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2826 {
2827 reset(__u.release());
2828 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2829 return *this;
2830 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002831#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002832
2833 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2834 : __ptr_(__p)
2835 {
2836 static_assert(!is_pointer<deleter_type>::value,
2837 "unique_ptr constructed with null function pointer deleter");
2838 }
2839
2840 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002841 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002842
2843 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002844 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002845
2846 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2847 {
2848 return __rv<unique_ptr>(*this);
2849 }
2850
2851 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002852 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002853
2854 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2855 {
2856 reset(__u->release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002857 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002858 return *this;
2859 }
2860
Howard Hinnant73d21a42010-09-04 23:28:19 +00002861#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002862 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2863
Howard Hinnant1694d232011-05-28 14:41:13 +00002864 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002865 {
2866 reset();
2867 return *this;
2868 }
2869
2870 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2871 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002872 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2873 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2874 {return __ptr_.second();}
2875 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2876 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002877 _LIBCPP_INLINE_VISIBILITY
2878 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2879 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002880
Howard Hinnant1694d232011-05-28 14:41:13 +00002881 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002882 {
2883 pointer __t = __ptr_.first();
2884 __ptr_.first() = pointer();
2885 return __t;
2886 }
2887
Howard Hinnant73d21a42010-09-04 23:28:19 +00002888#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00002889 template <class _Pp>
2890 _LIBCPP_INLINE_VISIBILITY
2891 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, void>::type
2892 reset(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002893 {
2894 pointer __tmp = __ptr_.first();
2895 __ptr_.first() = __p;
2896 if (__tmp)
2897 __ptr_.second()(__tmp);
2898 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002899 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002900 {
2901 pointer __tmp = __ptr_.first();
2902 __ptr_.first() = nullptr;
2903 if (__tmp)
2904 __ptr_.second()(__tmp);
2905 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002906 _LIBCPP_INLINE_VISIBILITY void reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002907 {
2908 pointer __tmp = __ptr_.first();
2909 __ptr_.first() = nullptr;
2910 if (__tmp)
2911 __ptr_.second()(__tmp);
2912 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002913#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002914 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2915 {
2916 pointer __tmp = __ptr_.first();
2917 __ptr_.first() = __p;
2918 if (__tmp)
2919 __ptr_.second()(__tmp);
2920 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002921#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002922
2923 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2924private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002925
Howard Hinnant73d21a42010-09-04 23:28:19 +00002926#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002927 template <class _Up>
2928 explicit unique_ptr(_Up);
2929 template <class _Up>
2930 unique_ptr(_Up __u,
2931 typename conditional<
2932 is_reference<deleter_type>::value,
2933 deleter_type,
2934 typename add_lvalue_reference<const deleter_type>::type>::type,
2935 typename enable_if
2936 <
2937 is_convertible<_Up, pointer>::value,
2938 __nat
2939 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002940#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002941};
2942
2943template <class _Tp, class _Dp>
2944inline _LIBCPP_INLINE_VISIBILITY
2945void
Howard Hinnant1694d232011-05-28 14:41:13 +00002946swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002947
2948template <class _T1, class _D1, class _T2, class _D2>
2949inline _LIBCPP_INLINE_VISIBILITY
2950bool
2951operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2952
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 == __y);}
2957
2958template <class _T1, class _D1, class _T2, class _D2>
2959inline _LIBCPP_INLINE_VISIBILITY
2960bool
Howard Hinnant3fadda32012-02-21 21:02:58 +00002961operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2962{
2963 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2964 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselier8492cd82015-02-05 23:01:40 +00002965 typedef typename common_type<_P1, _P2>::type _Vp;
2966 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnant3fadda32012-02-21 21:02:58 +00002967}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002968
2969template <class _T1, class _D1, class _T2, class _D2>
2970inline _LIBCPP_INLINE_VISIBILITY
2971bool
2972operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2973
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 !(__x < __y);}
2983
Howard Hinnant3fadda32012-02-21 21:02:58 +00002984template <class _T1, class _D1>
2985inline _LIBCPP_INLINE_VISIBILITY
2986bool
Howard Hinnant46e94932012-07-07 20:56:04 +00002987operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00002988{
2989 return !__x;
2990}
2991
2992template <class _T1, class _D1>
2993inline _LIBCPP_INLINE_VISIBILITY
2994bool
Howard Hinnant46e94932012-07-07 20:56:04 +00002995operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00002996{
2997 return !__x;
2998}
2999
3000template <class _T1, class _D1>
3001inline _LIBCPP_INLINE_VISIBILITY
3002bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003003operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003004{
3005 return static_cast<bool>(__x);
3006}
3007
3008template <class _T1, class _D1>
3009inline _LIBCPP_INLINE_VISIBILITY
3010bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003011operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003012{
3013 return static_cast<bool>(__x);
3014}
3015
3016template <class _T1, class _D1>
3017inline _LIBCPP_INLINE_VISIBILITY
3018bool
3019operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3020{
3021 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3022 return less<_P1>()(__x.get(), nullptr);
3023}
3024
3025template <class _T1, class _D1>
3026inline _LIBCPP_INLINE_VISIBILITY
3027bool
3028operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3029{
3030 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3031 return less<_P1>()(nullptr, __x.get());
3032}
3033
3034template <class _T1, class _D1>
3035inline _LIBCPP_INLINE_VISIBILITY
3036bool
3037operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3038{
3039 return nullptr < __x;
3040}
3041
3042template <class _T1, class _D1>
3043inline _LIBCPP_INLINE_VISIBILITY
3044bool
3045operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3046{
3047 return __x < nullptr;
3048}
3049
3050template <class _T1, class _D1>
3051inline _LIBCPP_INLINE_VISIBILITY
3052bool
3053operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3054{
3055 return !(nullptr < __x);
3056}
3057
3058template <class _T1, class _D1>
3059inline _LIBCPP_INLINE_VISIBILITY
3060bool
3061operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3062{
3063 return !(__x < nullptr);
3064}
3065
3066template <class _T1, class _D1>
3067inline _LIBCPP_INLINE_VISIBILITY
3068bool
3069operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3070{
3071 return !(__x < nullptr);
3072}
3073
3074template <class _T1, class _D1>
3075inline _LIBCPP_INLINE_VISIBILITY
3076bool
3077operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3078{
3079 return !(nullptr < __x);
3080}
3081
Howard Hinnant87073e42012-05-01 15:37:54 +00003082#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3083
3084template <class _Tp, class _Dp>
3085inline _LIBCPP_INLINE_VISIBILITY
3086unique_ptr<_Tp, _Dp>
3087move(unique_ptr<_Tp, _Dp>& __t)
3088{
3089 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3090}
3091
3092#endif
3093
Marshall Clowfd7481e2013-07-01 18:16:03 +00003094#if _LIBCPP_STD_VER > 11
3095
3096template<class _Tp>
3097struct __unique_if
3098{
3099 typedef unique_ptr<_Tp> __unique_single;
3100};
3101
3102template<class _Tp>
3103struct __unique_if<_Tp[]>
3104{
3105 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3106};
3107
3108template<class _Tp, size_t _Np>
3109struct __unique_if<_Tp[_Np]>
3110{
3111 typedef void __unique_array_known_bound;
3112};
3113
3114template<class _Tp, class... _Args>
Marshall Clowfb551102013-07-02 20:06:09 +00003115inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003116typename __unique_if<_Tp>::__unique_single
3117make_unique(_Args&&... __args)
3118{
3119 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3120}
3121
3122template<class _Tp>
Marshall Clowfb551102013-07-02 20:06:09 +00003123inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003124typename __unique_if<_Tp>::__unique_array_unknown_bound
3125make_unique(size_t __n)
3126{
3127 typedef typename remove_extent<_Tp>::type _Up;
3128 return unique_ptr<_Tp>(new _Up[__n]());
3129}
3130
3131template<class _Tp, class... _Args>
3132 typename __unique_if<_Tp>::__unique_array_known_bound
3133 make_unique(_Args&&...) = delete;
3134
3135#endif // _LIBCPP_STD_VER > 11
3136
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00003137template <class _Tp> struct hash;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003138
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003139template <class _Size>
3140inline _LIBCPP_INLINE_VISIBILITY
3141_Size
3142__loadword(const void* __p)
3143{
3144 _Size __r;
3145 std::memcpy(&__r, __p, sizeof(__r));
3146 return __r;
3147}
3148
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003149// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
3150// is 64 bits. This is because cityhash64 uses 64bit x 64bit
3151// multiplication, which can be very slow on 32-bit systems.
Howard Hinnant40c13d32011-12-05 00:08:45 +00003152template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003153struct __murmur2_or_cityhash;
Howard Hinnant40c13d32011-12-05 00:08:45 +00003154
3155template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003156struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003157{
3158 _Size operator()(const void* __key, _Size __len);
3159};
3160
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003161// murmur2
Howard Hinnant40c13d32011-12-05 00:08:45 +00003162template <class _Size>
3163_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003164__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003165{
3166 const _Size __m = 0x5bd1e995;
3167 const _Size __r = 24;
3168 _Size __h = __len;
3169 const unsigned char* __data = static_cast<const unsigned char*>(__key);
3170 for (; __len >= 4; __data += 4, __len -= 4)
3171 {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003172 _Size __k = __loadword<_Size>(__data);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003173 __k *= __m;
3174 __k ^= __k >> __r;
3175 __k *= __m;
3176 __h *= __m;
3177 __h ^= __k;
3178 }
3179 switch (__len)
3180 {
3181 case 3:
3182 __h ^= __data[2] << 16;
3183 case 2:
3184 __h ^= __data[1] << 8;
3185 case 1:
3186 __h ^= __data[0];
3187 __h *= __m;
3188 }
3189 __h ^= __h >> 13;
3190 __h *= __m;
3191 __h ^= __h >> 15;
3192 return __h;
3193}
3194
3195template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003196struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003197{
3198 _Size operator()(const void* __key, _Size __len);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003199
3200 private:
3201 // Some primes between 2^63 and 2^64.
3202 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
3203 static const _Size __k1 = 0xb492b66fbe98f273ULL;
3204 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
3205 static const _Size __k3 = 0xc949d7c7509e6557ULL;
3206
3207 static _Size __rotate(_Size __val, int __shift) {
3208 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
3209 }
3210
3211 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
3212 return (__val >> __shift) | (__val << (64 - __shift));
3213 }
3214
3215 static _Size __shift_mix(_Size __val) {
3216 return __val ^ (__val >> 47);
3217 }
3218
3219 static _Size __hash_len_16(_Size __u, _Size __v) {
3220 const _Size __mul = 0x9ddfea08eb382d69ULL;
3221 _Size __a = (__u ^ __v) * __mul;
3222 __a ^= (__a >> 47);
3223 _Size __b = (__v ^ __a) * __mul;
3224 __b ^= (__b >> 47);
3225 __b *= __mul;
3226 return __b;
3227 }
3228
3229 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
3230 if (__len > 8) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003231 const _Size __a = __loadword<_Size>(__s);
3232 const _Size __b = __loadword<_Size>(__s + __len - 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003233 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
3234 }
3235 if (__len >= 4) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003236 const uint32_t __a = __loadword<uint32_t>(__s);
3237 const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003238 return __hash_len_16(__len + (__a << 3), __b);
3239 }
3240 if (__len > 0) {
3241 const unsigned char __a = __s[0];
3242 const unsigned char __b = __s[__len >> 1];
3243 const unsigned char __c = __s[__len - 1];
3244 const uint32_t __y = static_cast<uint32_t>(__a) +
3245 (static_cast<uint32_t>(__b) << 8);
3246 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3247 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3248 }
3249 return __k2;
3250 }
3251
3252 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003253 const _Size __a = __loadword<_Size>(__s) * __k1;
3254 const _Size __b = __loadword<_Size>(__s + 8);
3255 const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;
3256 const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003257 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3258 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3259 }
3260
3261 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3262 // Callers do best to use "random-looking" values for a and b.
3263 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3264 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3265 __a += __w;
3266 __b = __rotate(__b + __a + __z, 21);
3267 const _Size __c = __a;
3268 __a += __x;
3269 __a += __y;
3270 __b += __rotate(__a, 44);
3271 return pair<_Size, _Size>(__a + __z, __b + __c);
3272 }
3273
3274 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3275 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3276 const char* __s, _Size __a, _Size __b) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003277 return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),
3278 __loadword<_Size>(__s + 8),
3279 __loadword<_Size>(__s + 16),
3280 __loadword<_Size>(__s + 24),
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003281 __a,
3282 __b);
3283 }
3284
3285 // Return an 8-byte hash for 33 to 64 bytes.
3286 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003287 _Size __z = __loadword<_Size>(__s + 24);
3288 _Size __a = __loadword<_Size>(__s) +
3289 (__len + __loadword<_Size>(__s + __len - 16)) * __k0;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003290 _Size __b = __rotate(__a + __z, 52);
3291 _Size __c = __rotate(__a, 37);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003292 __a += __loadword<_Size>(__s + 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003293 __c += __rotate(__a, 7);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003294 __a += __loadword<_Size>(__s + 16);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003295 _Size __vf = __a + __z;
3296 _Size __vs = __b + __rotate(__a, 31) + __c;
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003297 __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);
3298 __z += __loadword<_Size>(__s + __len - 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003299 __b = __rotate(__a + __z, 52);
3300 __c = __rotate(__a, 37);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003301 __a += __loadword<_Size>(__s + __len - 24);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003302 __c += __rotate(__a, 7);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003303 __a += __loadword<_Size>(__s + __len - 16);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003304 _Size __wf = __a + __z;
3305 _Size __ws = __b + __rotate(__a, 31) + __c;
3306 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3307 return __shift_mix(__r * __k0 + __vs) * __k2;
3308 }
Howard Hinnant40c13d32011-12-05 00:08:45 +00003309};
3310
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003311// cityhash64
Howard Hinnant40c13d32011-12-05 00:08:45 +00003312template <class _Size>
3313_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003314__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003315{
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003316 const char* __s = static_cast<const char*>(__key);
3317 if (__len <= 32) {
3318 if (__len <= 16) {
3319 return __hash_len_0_to_16(__s, __len);
3320 } else {
3321 return __hash_len_17_to_32(__s, __len);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003322 }
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003323 } else if (__len <= 64) {
3324 return __hash_len_33_to_64(__s, __len);
3325 }
3326
3327 // For strings over 64 bytes we hash the end first, and then as we
3328 // loop we keep 56 bytes of state: v, w, x, y, and z.
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003329 _Size __x = __loadword<_Size>(__s + __len - 40);
3330 _Size __y = __loadword<_Size>(__s + __len - 16) +
3331 __loadword<_Size>(__s + __len - 56);
3332 _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,
3333 __loadword<_Size>(__s + __len - 24));
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003334 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3335 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003336 __x = __x * __k1 + __loadword<_Size>(__s);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003337
3338 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3339 __len = (__len - 1) & ~static_cast<_Size>(63);
3340 do {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003341 __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;
3342 __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003343 __x ^= __w.second;
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003344 __y += __v.first + __loadword<_Size>(__s + 40);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003345 __z = __rotate(__z + __w.first, 33) * __k1;
3346 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3347 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003348 __y + __loadword<_Size>(__s + 16));
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003349 std::swap(__z, __x);
3350 __s += 64;
3351 __len -= 64;
3352 } while (__len != 0);
3353 return __hash_len_16(
3354 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3355 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003356}
3357
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003358template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3359struct __scalar_hash;
3360
3361template <class _Tp>
3362struct __scalar_hash<_Tp, 0>
3363 : public unary_function<_Tp, size_t>
Howard Hinnant21aefc32010-06-03 16:42:57 +00003364{
Howard Hinnant82894812010-09-22 16:48:34 +00003365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003366 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003367 {
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003368 union
3369 {
3370 _Tp __t;
3371 size_t __a;
3372 } __u;
3373 __u.__a = 0;
3374 __u.__t = __v;
3375 return __u.__a;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003376 }
3377};
3378
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003379template <class _Tp>
3380struct __scalar_hash<_Tp, 1>
3381 : public unary_function<_Tp, size_t>
3382{
3383 _LIBCPP_INLINE_VISIBILITY
3384 size_t operator()(_Tp __v) const _NOEXCEPT
3385 {
3386 union
3387 {
3388 _Tp __t;
3389 size_t __a;
3390 } __u;
3391 __u.__t = __v;
3392 return __u.__a;
3393 }
3394};
3395
3396template <class _Tp>
3397struct __scalar_hash<_Tp, 2>
3398 : public unary_function<_Tp, size_t>
3399{
3400 _LIBCPP_INLINE_VISIBILITY
3401 size_t operator()(_Tp __v) const _NOEXCEPT
3402 {
3403 union
3404 {
3405 _Tp __t;
3406 struct
3407 {
3408 size_t __a;
3409 size_t __b;
3410 };
3411 } __u;
3412 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003413 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003414 }
3415};
3416
3417template <class _Tp>
3418struct __scalar_hash<_Tp, 3>
3419 : public unary_function<_Tp, size_t>
3420{
3421 _LIBCPP_INLINE_VISIBILITY
3422 size_t operator()(_Tp __v) const _NOEXCEPT
3423 {
3424 union
3425 {
3426 _Tp __t;
3427 struct
3428 {
3429 size_t __a;
3430 size_t __b;
3431 size_t __c;
3432 };
3433 } __u;
3434 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003435 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003436 }
3437};
3438
3439template <class _Tp>
3440struct __scalar_hash<_Tp, 4>
3441 : public unary_function<_Tp, size_t>
3442{
3443 _LIBCPP_INLINE_VISIBILITY
3444 size_t operator()(_Tp __v) const _NOEXCEPT
3445 {
3446 union
3447 {
3448 _Tp __t;
3449 struct
3450 {
3451 size_t __a;
3452 size_t __b;
3453 size_t __c;
3454 size_t __d;
3455 };
3456 } __u;
3457 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003458 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003459 }
3460};
3461
3462template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003463struct _LIBCPP_TYPE_VIS_ONLY hash<_Tp*>
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003464 : public unary_function<_Tp*, size_t>
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003465{
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003466 _LIBCPP_INLINE_VISIBILITY
3467 size_t operator()(_Tp* __v) const _NOEXCEPT
3468 {
3469 union
3470 {
3471 _Tp* __t;
3472 size_t __a;
3473 } __u;
3474 __u.__t = __v;
3475 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
3476 }
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003477};
3478
Howard Hinnant21aefc32010-06-03 16:42:57 +00003479template <class _Tp, class _Dp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003480struct _LIBCPP_TYPE_VIS_ONLY hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003481{
3482 typedef unique_ptr<_Tp, _Dp> argument_type;
3483 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003484 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003485 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003486 {
3487 typedef typename argument_type::pointer pointer;
3488 return hash<pointer>()(__ptr.get());
3489 }
3490};
3491
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003492struct __destruct_n
3493{
3494private:
3495 size_t size;
3496
3497 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003498 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003499 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3500
3501 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003502 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003503 {}
3504
Howard Hinnant1694d232011-05-28 14:41:13 +00003505 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003506 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003507 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003508 {}
3509
Howard Hinnant1694d232011-05-28 14:41:13 +00003510 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003511 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003512 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003513 {}
3514public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003515 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3516 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003517
3518 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003519 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003520 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003521
3522 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003523 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003524 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003525
3526 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003527 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003528 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003529};
3530
3531template <class _Alloc>
3532class __allocator_destructor
3533{
3534 typedef allocator_traits<_Alloc> __alloc_traits;
3535public:
3536 typedef typename __alloc_traits::pointer pointer;
3537 typedef typename __alloc_traits::size_type size_type;
3538private:
3539 _Alloc& __alloc_;
3540 size_type __s_;
3541public:
3542 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00003543 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003544 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003546 void operator()(pointer __p) _NOEXCEPT
3547 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003548};
3549
3550template <class _InputIterator, class _ForwardIterator>
3551_ForwardIterator
3552uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3553{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003554 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003555#ifndef _LIBCPP_NO_EXCEPTIONS
3556 _ForwardIterator __s = __r;
3557 try
3558 {
3559#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003560 for (; __f != __l; ++__f, (void) ++__r)
3561 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant8292d742011-12-29 17:45:35 +00003562#ifndef _LIBCPP_NO_EXCEPTIONS
3563 }
3564 catch (...)
3565 {
3566 for (; __s != __r; ++__s)
3567 __s->~value_type();
3568 throw;
3569 }
3570#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003571 return __r;
3572}
3573
3574template <class _InputIterator, class _Size, class _ForwardIterator>
3575_ForwardIterator
3576uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3577{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003578 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003579#ifndef _LIBCPP_NO_EXCEPTIONS
3580 _ForwardIterator __s = __r;
3581 try
3582 {
3583#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003584 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3585 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant8292d742011-12-29 17:45:35 +00003586#ifndef _LIBCPP_NO_EXCEPTIONS
3587 }
3588 catch (...)
3589 {
3590 for (; __s != __r; ++__s)
3591 __s->~value_type();
3592 throw;
3593 }
3594#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003595 return __r;
3596}
3597
3598template <class _ForwardIterator, class _Tp>
3599void
3600uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3601{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003602 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003603#ifndef _LIBCPP_NO_EXCEPTIONS
3604 _ForwardIterator __s = __f;
3605 try
3606 {
3607#endif
3608 for (; __f != __l; ++__f)
Marshall Clow5dce73d2015-05-19 15:01:48 +00003609 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant8292d742011-12-29 17:45:35 +00003610#ifndef _LIBCPP_NO_EXCEPTIONS
3611 }
3612 catch (...)
3613 {
3614 for (; __s != __f; ++__s)
3615 __s->~value_type();
3616 throw;
3617 }
3618#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003619}
3620
3621template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003622_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003623uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3624{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003625 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003626#ifndef _LIBCPP_NO_EXCEPTIONS
3627 _ForwardIterator __s = __f;
3628 try
3629 {
3630#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003631 for (; __n > 0; ++__f, (void) --__n)
3632 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant8292d742011-12-29 17:45:35 +00003633#ifndef _LIBCPP_NO_EXCEPTIONS
3634 }
3635 catch (...)
3636 {
3637 for (; __s != __f; ++__s)
3638 __s->~value_type();
3639 throw;
3640 }
3641#endif
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003642 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003643}
3644
Howard Hinnant82894812010-09-22 16:48:34 +00003645class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003646 : public std::exception
3647{
3648public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003649 virtual ~bad_weak_ptr() _NOEXCEPT;
3650 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003651};
3652
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003653template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003654
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003655class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003656{
3657 __shared_count(const __shared_count&);
3658 __shared_count& operator=(const __shared_count&);
3659
3660protected:
3661 long __shared_owners_;
3662 virtual ~__shared_count();
3663private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003664 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003665
3666public:
Howard Hinnant82894812010-09-22 16:48:34 +00003667 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003668 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003669 : __shared_owners_(__refs) {}
3670
Howard Hinnant1694d232011-05-28 14:41:13 +00003671 void __add_shared() _NOEXCEPT;
3672 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003673 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003674 long use_count() const _NOEXCEPT {return __shared_owners_ + 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003675};
3676
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003677class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003678 : private __shared_count
3679{
3680 long __shared_weak_owners_;
3681
3682public:
Howard Hinnant82894812010-09-22 16:48:34 +00003683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003684 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003685 : __shared_count(__refs),
3686 __shared_weak_owners_(__refs) {}
3687protected:
3688 virtual ~__shared_weak_count();
3689
3690public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003691 void __add_shared() _NOEXCEPT;
3692 void __add_weak() _NOEXCEPT;
3693 void __release_shared() _NOEXCEPT;
3694 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003695 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003696 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3697 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003698
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003699 // Define the function out only if we build static libc++ without RTTI.
3700 // Otherwise we may break clients who need to compile their projects with
3701 // -fno-rtti and yet link against a libc++.dylib compiled
3702 // without -fno-rtti.
3703#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant1694d232011-05-28 14:41:13 +00003704 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003705#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003706private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003707 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003708};
3709
3710template <class _Tp, class _Dp, class _Alloc>
3711class __shared_ptr_pointer
3712 : public __shared_weak_count
3713{
3714 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3715public:
Howard Hinnant82894812010-09-22 16:48:34 +00003716 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003717 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003718 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003719
Howard Hinnantd4444702010-08-11 17:04:31 +00003720#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00003721 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00003722#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003723
3724private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003725 virtual void __on_zero_shared() _NOEXCEPT;
3726 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003727};
3728
Howard Hinnantd4444702010-08-11 17:04:31 +00003729#ifndef _LIBCPP_NO_RTTI
3730
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003731template <class _Tp, class _Dp, class _Alloc>
3732const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00003733__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003734{
Marshall Clow4b3ca8c2014-11-17 19:05:50 +00003735 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003736}
3737
Howard Hinnant324bb032010-08-22 00:02:43 +00003738#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003739
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003740template <class _Tp, class _Dp, class _Alloc>
3741void
Howard Hinnant1694d232011-05-28 14:41:13 +00003742__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003743{
3744 __data_.first().second()(__data_.first().first());
3745 __data_.first().second().~_Dp();
3746}
3747
3748template <class _Tp, class _Dp, class _Alloc>
3749void
Howard Hinnant1694d232011-05-28 14:41:13 +00003750__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003751{
Eric Fiselier8492cd82015-02-05 23:01:40 +00003752 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3753 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003754 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3755
Eric Fiselier8492cd82015-02-05 23:01:40 +00003756 _Al __a(__data_.second());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003757 __data_.second().~_Alloc();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003758 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003759}
3760
3761template <class _Tp, class _Alloc>
3762class __shared_ptr_emplace
3763 : public __shared_weak_count
3764{
3765 __compressed_pair<_Alloc, _Tp> __data_;
3766public:
3767#ifndef _LIBCPP_HAS_NO_VARIADICS
3768
Howard Hinnant82894812010-09-22 16:48:34 +00003769 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003770 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003771 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003772
3773 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00003774 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003775 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00003776 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3777 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003778
3779#else // _LIBCPP_HAS_NO_VARIADICS
3780
Howard Hinnant82894812010-09-22 16:48:34 +00003781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003782 __shared_ptr_emplace(_Alloc __a)
3783 : __data_(__a) {}
3784
3785 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00003786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003787 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3788 : __data_(__a, _Tp(__a0)) {}
3789
3790 template <class _A0, class _A1>
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, _A1& __a1)
3793 : __data_(__a, _Tp(__a0, __a1)) {}
3794
3795 template <class _A0, class _A1, class _A2>
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, _A2& __a2)
3798 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3799
3800#endif // _LIBCPP_HAS_NO_VARIADICS
3801
3802private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003803 virtual void __on_zero_shared() _NOEXCEPT;
3804 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003805public:
Howard Hinnant82894812010-09-22 16:48:34 +00003806 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003807 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003808};
3809
3810template <class _Tp, class _Alloc>
3811void
Howard Hinnant1694d232011-05-28 14:41:13 +00003812__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003813{
3814 __data_.second().~_Tp();
3815}
3816
3817template <class _Tp, class _Alloc>
3818void
Howard Hinnant1694d232011-05-28 14:41:13 +00003819__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003820{
Eric Fiselier8492cd82015-02-05 23:01:40 +00003821 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3822 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003823 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40 +00003824 _Al __a(__data_.first());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003825 __data_.first().~_Alloc();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003826 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003827}
3828
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003829template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003830
3831template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003832class _LIBCPP_TYPE_VIS_ONLY shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003833{
Howard Hinnant324bb032010-08-22 00:02:43 +00003834public:
3835 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003836private:
3837 element_type* __ptr_;
3838 __shared_weak_count* __cntrl_;
3839
3840 struct __nat {int __for_bool_;};
3841public:
Howard Hinnant46e94932012-07-07 20:56:04 +00003842 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
3843 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiene1678a12014-01-31 09:30:46 +00003844 template<class _Yp>
3845 explicit shared_ptr(_Yp* __p,
3846 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3847 template<class _Yp, class _Dp>
3848 shared_ptr(_Yp* __p, _Dp __d,
3849 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3850 template<class _Yp, class _Dp, class _Alloc>
3851 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3852 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003853 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3854 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant1694d232011-05-28 14:41:13 +00003855 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3856 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003857 template<class _Yp>
3858 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003859 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3860 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003861#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003862 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003863 template<class _Yp> shared_ptr(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#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003867 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003868 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003869#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003870 template<class _Yp>
3871 shared_ptr(auto_ptr<_Yp>&& __r,
3872 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant324bb032010-08-22 00:02:43 +00003873#else
Logan Chiene1678a12014-01-31 09:30:46 +00003874 template<class _Yp>
3875 shared_ptr(auto_ptr<_Yp> __r,
3876 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003877#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003878#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003879 template <class _Yp, class _Dp>
3880 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3881 typename enable_if
3882 <
3883 !is_lvalue_reference<_Dp>::value &&
3884 !is_array<_Yp>::value &&
3885 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3886 __nat
3887 >::type = __nat());
3888 template <class _Yp, class _Dp>
3889 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3890 typename enable_if
3891 <
3892 is_lvalue_reference<_Dp>::value &&
3893 !is_array<_Yp>::value &&
3894 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3895 __nat
3896 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003897#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003898 template <class _Yp, class _Dp>
3899 shared_ptr(unique_ptr<_Yp, _Dp>,
3900 typename enable_if
3901 <
3902 !is_lvalue_reference<_Dp>::value &&
3903 !is_array<_Yp>::value &&
3904 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3905 __nat
3906 >::type = __nat());
3907 template <class _Yp, class _Dp>
3908 shared_ptr(unique_ptr<_Yp, _Dp>,
3909 typename enable_if
3910 <
3911 is_lvalue_reference<_Dp>::value &&
3912 !is_array<_Yp>::value &&
3913 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3914 __nat
3915 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003916#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003917
3918 ~shared_ptr();
3919
Howard Hinnant1694d232011-05-28 14:41:13 +00003920 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003921 template<class _Yp>
3922 typename enable_if
3923 <
3924 is_convertible<_Yp*, element_type*>::value,
3925 shared_ptr&
3926 >::type
3927 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003928#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003929 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003930 template<class _Yp>
3931 typename enable_if
3932 <
3933 is_convertible<_Yp*, element_type*>::value,
3934 shared_ptr<_Tp>&
3935 >::type
3936 operator=(shared_ptr<_Yp>&& __r);
3937 template<class _Yp>
3938 typename enable_if
3939 <
3940 !is_array<_Yp>::value &&
3941 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant37c4acf2013-09-13 23:56:00 +00003942 shared_ptr
3943 >::type&
Howard Hinnant57199402012-01-02 17:56:02 +00003944 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003945#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003946 template<class _Yp>
3947 typename enable_if
3948 <
3949 !is_array<_Yp>::value &&
3950 is_convertible<_Yp*, element_type*>::value,
3951 shared_ptr&
3952 >::type
3953 operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003954#endif
Howard Hinnant57199402012-01-02 17:56:02 +00003955 template <class _Yp, class _Dp>
3956 typename enable_if
3957 <
3958 !is_array<_Yp>::value &&
3959 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3960 shared_ptr&
3961 >::type
Howard Hinnant73d21a42010-09-04 23:28:19 +00003962#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003963 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003964#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003965 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003966#endif
3967
Howard Hinnant1694d232011-05-28 14:41:13 +00003968 void swap(shared_ptr& __r) _NOEXCEPT;
3969 void reset() _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003970 template<class _Yp>
3971 typename enable_if
3972 <
3973 is_convertible<_Yp*, element_type*>::value,
3974 void
3975 >::type
3976 reset(_Yp* __p);
3977 template<class _Yp, class _Dp>
3978 typename enable_if
3979 <
3980 is_convertible<_Yp*, element_type*>::value,
3981 void
3982 >::type
3983 reset(_Yp* __p, _Dp __d);
3984 template<class _Yp, class _Dp, class _Alloc>
3985 typename enable_if
3986 <
3987 is_convertible<_Yp*, element_type*>::value,
3988 void
3989 >::type
3990 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003991
Howard Hinnant82894812010-09-22 16:48:34 +00003992 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003993 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003994 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003995 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3996 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003997 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003998 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003999 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004000 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00004001 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004002 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00004003 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant77861882012-02-21 21:46:43 +00004004 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant99968442011-11-29 18:15:50 +00004005 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00004006 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00004007 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004008 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant99968442011-11-29 18:15:50 +00004009 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00004010 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00004011 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004012 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant5fec82d2012-07-30 01:40:57 +00004013 _LIBCPP_INLINE_VISIBILITY
4014 bool
4015 __owner_equivalent(const shared_ptr& __p) const
4016 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004017
Howard Hinnantd4444702010-08-11 17:04:31 +00004018#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004019 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00004020 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004021 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004022 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00004023#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004024
4025#ifndef _LIBCPP_HAS_NO_VARIADICS
4026
4027 template<class ..._Args>
4028 static
4029 shared_ptr<_Tp>
4030 make_shared(_Args&& ...__args);
4031
4032 template<class _Alloc, class ..._Args>
4033 static
4034 shared_ptr<_Tp>
4035 allocate_shared(const _Alloc& __a, _Args&& ...__args);
4036
4037#else // _LIBCPP_HAS_NO_VARIADICS
4038
4039 static shared_ptr<_Tp> make_shared();
4040
4041 template<class _A0>
4042 static shared_ptr<_Tp> make_shared(_A0&);
4043
4044 template<class _A0, class _A1>
4045 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
4046
4047 template<class _A0, class _A1, class _A2>
4048 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
4049
4050 template<class _Alloc>
4051 static shared_ptr<_Tp>
4052 allocate_shared(const _Alloc& __a);
4053
4054 template<class _Alloc, class _A0>
4055 static shared_ptr<_Tp>
4056 allocate_shared(const _Alloc& __a, _A0& __a0);
4057
4058 template<class _Alloc, class _A0, class _A1>
4059 static shared_ptr<_Tp>
4060 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
4061
4062 template<class _Alloc, class _A0, class _A1, class _A2>
4063 static shared_ptr<_Tp>
4064 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
4065
4066#endif // _LIBCPP_HAS_NO_VARIADICS
4067
4068private:
4069
4070 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00004071 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004072 void
Marshall Clowfc3a3ff2015-05-27 20:36:14 +00004073 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004074 {
4075 if (__e)
4076 __e->__weak_this_ = *this;
4077 }
4078
Howard Hinnant82894812010-09-22 16:48:34 +00004079 _LIBCPP_INLINE_VISIBILITY
Marshall Clow60784f62015-05-27 22:44:47 +00004080 void __enable_weak_this(const volatile void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004081
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004082 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
4083 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004084};
4085
4086template<class _Tp>
4087inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00004088_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004089shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004090 : __ptr_(0),
4091 __cntrl_(0)
4092{
4093}
4094
4095template<class _Tp>
4096inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00004097_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004098shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004099 : __ptr_(0),
4100 __cntrl_(0)
4101{
4102}
4103
4104template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004105template<class _Yp>
4106shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4107 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004108 : __ptr_(__p)
4109{
4110 unique_ptr<_Yp> __hold(__p);
4111 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4112 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
4113 __hold.release();
4114 __enable_weak_this(__p);
4115}
4116
4117template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004118template<class _Yp, class _Dp>
4119shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4120 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004121 : __ptr_(__p)
4122{
4123#ifndef _LIBCPP_NO_EXCEPTIONS
4124 try
4125 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004126#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004127 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4128 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
4129 __enable_weak_this(__p);
4130#ifndef _LIBCPP_NO_EXCEPTIONS
4131 }
4132 catch (...)
4133 {
4134 __d(__p);
4135 throw;
4136 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004137#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004138}
4139
4140template<class _Tp>
4141template<class _Dp>
4142shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4143 : __ptr_(0)
4144{
4145#ifndef _LIBCPP_NO_EXCEPTIONS
4146 try
4147 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004148#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004149 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4150 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4151#ifndef _LIBCPP_NO_EXCEPTIONS
4152 }
4153 catch (...)
4154 {
4155 __d(__p);
4156 throw;
4157 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004158#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004159}
4160
4161template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004162template<class _Yp, class _Dp, class _Alloc>
4163shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4164 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004165 : __ptr_(__p)
4166{
4167#ifndef _LIBCPP_NO_EXCEPTIONS
4168 try
4169 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004170#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004171 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004172 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004173 typedef __allocator_destructor<_A2> _D2;
4174 _A2 __a2(__a);
4175 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004176 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4177 _CntrlBlk(__p, __d, __a);
4178 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004179 __enable_weak_this(__p);
4180#ifndef _LIBCPP_NO_EXCEPTIONS
4181 }
4182 catch (...)
4183 {
4184 __d(__p);
4185 throw;
4186 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004187#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004188}
4189
4190template<class _Tp>
4191template<class _Dp, class _Alloc>
4192shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4193 : __ptr_(0)
4194{
4195#ifndef _LIBCPP_NO_EXCEPTIONS
4196 try
4197 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004198#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004199 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004200 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004201 typedef __allocator_destructor<_A2> _D2;
4202 _A2 __a2(__a);
4203 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004204 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4205 _CntrlBlk(__p, __d, __a);
4206 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004207#ifndef _LIBCPP_NO_EXCEPTIONS
4208 }
4209 catch (...)
4210 {
4211 __d(__p);
4212 throw;
4213 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004214#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004215}
4216
4217template<class _Tp>
4218template<class _Yp>
4219inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004220shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004221 : __ptr_(__p),
4222 __cntrl_(__r.__cntrl_)
4223{
4224 if (__cntrl_)
4225 __cntrl_->__add_shared();
4226}
4227
4228template<class _Tp>
4229inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004230shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004231 : __ptr_(__r.__ptr_),
4232 __cntrl_(__r.__cntrl_)
4233{
4234 if (__cntrl_)
4235 __cntrl_->__add_shared();
4236}
4237
4238template<class _Tp>
4239template<class _Yp>
4240inline _LIBCPP_INLINE_VISIBILITY
4241shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4242 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004243 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004244 : __ptr_(__r.__ptr_),
4245 __cntrl_(__r.__cntrl_)
4246{
4247 if (__cntrl_)
4248 __cntrl_->__add_shared();
4249}
4250
Howard Hinnant73d21a42010-09-04 23:28:19 +00004251#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004252
4253template<class _Tp>
4254inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004255shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004256 : __ptr_(__r.__ptr_),
4257 __cntrl_(__r.__cntrl_)
4258{
4259 __r.__ptr_ = 0;
4260 __r.__cntrl_ = 0;
4261}
4262
4263template<class _Tp>
4264template<class _Yp>
4265inline _LIBCPP_INLINE_VISIBILITY
4266shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4267 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004268 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004269 : __ptr_(__r.__ptr_),
4270 __cntrl_(__r.__cntrl_)
4271{
4272 __r.__ptr_ = 0;
4273 __r.__cntrl_ = 0;
4274}
4275
Howard Hinnant73d21a42010-09-04 23:28:19 +00004276#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004277
4278template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004279template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004280#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00004281shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004282#else
Logan Chiene1678a12014-01-31 09:30:46 +00004283shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004284#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004285 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004286 : __ptr_(__r.get())
4287{
4288 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4289 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
4290 __enable_weak_this(__r.get());
4291 __r.release();
4292}
4293
4294template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004295template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004296#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004297shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4298#else
4299shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4300#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004301 typename enable_if
4302 <
4303 !is_lvalue_reference<_Dp>::value &&
4304 !is_array<_Yp>::value &&
4305 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4306 __nat
4307 >::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004308 : __ptr_(__r.get())
4309{
Marshall Clow0ad232a2015-05-10 13:59:45 +00004310#if _LIBCPP_STD_VER > 11
4311 if (__ptr_ == nullptr)
4312 __cntrl_ = nullptr;
4313 else
4314#endif
4315 {
4316 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4317 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
4318 __enable_weak_this(__r.get());
4319 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004320 __r.release();
4321}
4322
4323template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004324template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004325#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004326shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4327#else
4328shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4329#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004330 typename enable_if
4331 <
4332 is_lvalue_reference<_Dp>::value &&
4333 !is_array<_Yp>::value &&
4334 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4335 __nat
4336 >::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004337 : __ptr_(__r.get())
4338{
Marshall Clow0ad232a2015-05-10 13:59:45 +00004339#if _LIBCPP_STD_VER > 11
4340 if (__ptr_ == nullptr)
4341 __cntrl_ = nullptr;
4342 else
4343#endif
4344 {
4345 typedef __shared_ptr_pointer<_Yp*,
4346 reference_wrapper<typename remove_reference<_Dp>::type>,
4347 allocator<_Yp> > _CntrlBlk;
4348 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
4349 __enable_weak_this(__r.get());
4350 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004351 __r.release();
4352}
4353
4354#ifndef _LIBCPP_HAS_NO_VARIADICS
4355
4356template<class _Tp>
4357template<class ..._Args>
4358shared_ptr<_Tp>
4359shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4360{
4361 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4362 typedef allocator<_CntrlBlk> _A2;
4363 typedef __allocator_destructor<_A2> _D2;
4364 _A2 __a2;
4365 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004366 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004367 shared_ptr<_Tp> __r;
4368 __r.__ptr_ = __hold2.get()->get();
4369 __r.__cntrl_ = __hold2.release();
4370 __r.__enable_weak_this(__r.__ptr_);
4371 return __r;
4372}
4373
4374template<class _Tp>
4375template<class _Alloc, class ..._Args>
4376shared_ptr<_Tp>
4377shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4378{
4379 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004380 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004381 typedef __allocator_destructor<_A2> _D2;
4382 _A2 __a2(__a);
4383 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004384 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4385 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004386 shared_ptr<_Tp> __r;
4387 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004388 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004389 __r.__enable_weak_this(__r.__ptr_);
4390 return __r;
4391}
4392
4393#else // _LIBCPP_HAS_NO_VARIADICS
4394
4395template<class _Tp>
4396shared_ptr<_Tp>
4397shared_ptr<_Tp>::make_shared()
4398{
4399 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4400 typedef allocator<_CntrlBlk> _Alloc2;
4401 typedef __allocator_destructor<_Alloc2> _D2;
4402 _Alloc2 __alloc2;
4403 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4404 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4405 shared_ptr<_Tp> __r;
4406 __r.__ptr_ = __hold2.get()->get();
4407 __r.__cntrl_ = __hold2.release();
4408 __r.__enable_weak_this(__r.__ptr_);
4409 return __r;
4410}
4411
4412template<class _Tp>
4413template<class _A0>
4414shared_ptr<_Tp>
4415shared_ptr<_Tp>::make_shared(_A0& __a0)
4416{
4417 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4418 typedef allocator<_CntrlBlk> _Alloc2;
4419 typedef __allocator_destructor<_Alloc2> _D2;
4420 _Alloc2 __alloc2;
4421 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4422 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4423 shared_ptr<_Tp> __r;
4424 __r.__ptr_ = __hold2.get()->get();
4425 __r.__cntrl_ = __hold2.release();
4426 __r.__enable_weak_this(__r.__ptr_);
4427 return __r;
4428}
4429
4430template<class _Tp>
4431template<class _A0, class _A1>
4432shared_ptr<_Tp>
4433shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4434{
4435 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4436 typedef allocator<_CntrlBlk> _Alloc2;
4437 typedef __allocator_destructor<_Alloc2> _D2;
4438 _Alloc2 __alloc2;
4439 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4440 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4441 shared_ptr<_Tp> __r;
4442 __r.__ptr_ = __hold2.get()->get();
4443 __r.__cntrl_ = __hold2.release();
4444 __r.__enable_weak_this(__r.__ptr_);
4445 return __r;
4446}
4447
4448template<class _Tp>
4449template<class _A0, class _A1, class _A2>
4450shared_ptr<_Tp>
4451shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4452{
4453 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4454 typedef allocator<_CntrlBlk> _Alloc2;
4455 typedef __allocator_destructor<_Alloc2> _D2;
4456 _Alloc2 __alloc2;
4457 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4458 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4459 shared_ptr<_Tp> __r;
4460 __r.__ptr_ = __hold2.get()->get();
4461 __r.__cntrl_ = __hold2.release();
4462 __r.__enable_weak_this(__r.__ptr_);
4463 return __r;
4464}
4465
4466template<class _Tp>
4467template<class _Alloc>
4468shared_ptr<_Tp>
4469shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4470{
4471 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004472 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004473 typedef __allocator_destructor<_Alloc2> _D2;
4474 _Alloc2 __alloc2(__a);
4475 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004476 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4477 _CntrlBlk(__a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004478 shared_ptr<_Tp> __r;
4479 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004480 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004481 __r.__enable_weak_this(__r.__ptr_);
4482 return __r;
4483}
4484
4485template<class _Tp>
4486template<class _Alloc, class _A0>
4487shared_ptr<_Tp>
4488shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4489{
4490 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004491 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004492 typedef __allocator_destructor<_Alloc2> _D2;
4493 _Alloc2 __alloc2(__a);
4494 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004495 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4496 _CntrlBlk(__a, __a0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004497 shared_ptr<_Tp> __r;
4498 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004499 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004500 __r.__enable_weak_this(__r.__ptr_);
4501 return __r;
4502}
4503
4504template<class _Tp>
4505template<class _Alloc, class _A0, class _A1>
4506shared_ptr<_Tp>
4507shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4508{
4509 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004510 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004511 typedef __allocator_destructor<_Alloc2> _D2;
4512 _Alloc2 __alloc2(__a);
4513 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004514 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4515 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004516 shared_ptr<_Tp> __r;
4517 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004518 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004519 __r.__enable_weak_this(__r.__ptr_);
4520 return __r;
4521}
4522
4523template<class _Tp>
4524template<class _Alloc, class _A0, class _A1, class _A2>
4525shared_ptr<_Tp>
4526shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4527{
4528 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004529 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004530 typedef __allocator_destructor<_Alloc2> _D2;
4531 _Alloc2 __alloc2(__a);
4532 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004533 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4534 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004535 shared_ptr<_Tp> __r;
4536 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004537 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004538 __r.__enable_weak_this(__r.__ptr_);
4539 return __r;
4540}
4541
4542#endif // _LIBCPP_HAS_NO_VARIADICS
4543
4544template<class _Tp>
4545shared_ptr<_Tp>::~shared_ptr()
4546{
4547 if (__cntrl_)
4548 __cntrl_->__release_shared();
4549}
4550
4551template<class _Tp>
4552inline _LIBCPP_INLINE_VISIBILITY
4553shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004554shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004555{
4556 shared_ptr(__r).swap(*this);
4557 return *this;
4558}
4559
4560template<class _Tp>
4561template<class _Yp>
4562inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004563typename enable_if
4564<
4565 is_convertible<_Yp*, _Tp*>::value,
4566 shared_ptr<_Tp>&
4567>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004568shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004569{
4570 shared_ptr(__r).swap(*this);
4571 return *this;
4572}
4573
Howard Hinnant73d21a42010-09-04 23:28:19 +00004574#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004575
4576template<class _Tp>
4577inline _LIBCPP_INLINE_VISIBILITY
4578shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004579shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004580{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004581 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004582 return *this;
4583}
4584
4585template<class _Tp>
4586template<class _Yp>
4587inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004588typename enable_if
4589<
4590 is_convertible<_Yp*, _Tp*>::value,
4591 shared_ptr<_Tp>&
4592>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004593shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4594{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004595 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004596 return *this;
4597}
4598
4599template<class _Tp>
4600template<class _Yp>
4601inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004602typename enable_if
4603<
4604 !is_array<_Yp>::value &&
4605 is_convertible<_Yp*, _Tp*>::value,
Howard Hinnant37c4acf2013-09-13 23:56:00 +00004606 shared_ptr<_Tp>
4607>::type&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004608shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4609{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004610 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004611 return *this;
4612}
4613
4614template<class _Tp>
4615template <class _Yp, class _Dp>
4616inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004617typename enable_if
4618<
4619 !is_array<_Yp>::value &&
4620 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4621 shared_ptr<_Tp>&
4622>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004623shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4624{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004625 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004626 return *this;
4627}
4628
Howard Hinnant73d21a42010-09-04 23:28:19 +00004629#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004630
4631template<class _Tp>
4632template<class _Yp>
4633inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004634typename enable_if
4635<
4636 !is_array<_Yp>::value &&
4637 is_convertible<_Yp*, _Tp*>::value,
4638 shared_ptr<_Tp>&
4639>::type
Howard Hinnant324bb032010-08-22 00:02:43 +00004640shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004641{
4642 shared_ptr(__r).swap(*this);
4643 return *this;
4644}
4645
4646template<class _Tp>
4647template <class _Yp, class _Dp>
4648inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004649typename enable_if
4650<
4651 !is_array<_Yp>::value &&
4652 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4653 shared_ptr<_Tp>&
4654>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004655shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4656{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004657 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004658 return *this;
4659}
4660
Howard Hinnant73d21a42010-09-04 23:28:19 +00004661#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004662
4663template<class _Tp>
4664inline _LIBCPP_INLINE_VISIBILITY
4665void
Howard Hinnant1694d232011-05-28 14:41:13 +00004666shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004667{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004668 _VSTD::swap(__ptr_, __r.__ptr_);
4669 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004670}
4671
4672template<class _Tp>
4673inline _LIBCPP_INLINE_VISIBILITY
4674void
Howard Hinnant1694d232011-05-28 14:41:13 +00004675shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004676{
4677 shared_ptr().swap(*this);
4678}
4679
4680template<class _Tp>
4681template<class _Yp>
4682inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004683typename enable_if
4684<
4685 is_convertible<_Yp*, _Tp*>::value,
4686 void
4687>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004688shared_ptr<_Tp>::reset(_Yp* __p)
4689{
4690 shared_ptr(__p).swap(*this);
4691}
4692
4693template<class _Tp>
4694template<class _Yp, class _Dp>
4695inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004696typename enable_if
4697<
4698 is_convertible<_Yp*, _Tp*>::value,
4699 void
4700>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004701shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4702{
4703 shared_ptr(__p, __d).swap(*this);
4704}
4705
4706template<class _Tp>
4707template<class _Yp, class _Dp, class _Alloc>
4708inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004709typename enable_if
4710<
4711 is_convertible<_Yp*, _Tp*>::value,
4712 void
4713>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004714shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4715{
4716 shared_ptr(__p, __d, __a).swap(*this);
4717}
4718
4719#ifndef _LIBCPP_HAS_NO_VARIADICS
4720
Howard Hinnant324bb032010-08-22 00:02:43 +00004721template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004722inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004723typename enable_if
4724<
4725 !is_array<_Tp>::value,
4726 shared_ptr<_Tp>
4727>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004728make_shared(_Args&& ...__args)
4729{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004730 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004731}
4732
Howard Hinnant324bb032010-08-22 00:02:43 +00004733template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004734inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004735typename enable_if
4736<
4737 !is_array<_Tp>::value,
4738 shared_ptr<_Tp>
4739>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004740allocate_shared(const _Alloc& __a, _Args&& ...__args)
4741{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004742 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004743}
4744
4745#else // _LIBCPP_HAS_NO_VARIADICS
4746
4747template<class _Tp>
4748inline _LIBCPP_INLINE_VISIBILITY
4749shared_ptr<_Tp>
4750make_shared()
4751{
4752 return shared_ptr<_Tp>::make_shared();
4753}
4754
4755template<class _Tp, class _A0>
4756inline _LIBCPP_INLINE_VISIBILITY
4757shared_ptr<_Tp>
4758make_shared(_A0& __a0)
4759{
4760 return shared_ptr<_Tp>::make_shared(__a0);
4761}
4762
4763template<class _Tp, class _A0, class _A1>
4764inline _LIBCPP_INLINE_VISIBILITY
4765shared_ptr<_Tp>
4766make_shared(_A0& __a0, _A1& __a1)
4767{
4768 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4769}
4770
Howard Hinnant324bb032010-08-22 00:02:43 +00004771template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004772inline _LIBCPP_INLINE_VISIBILITY
4773shared_ptr<_Tp>
4774make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4775{
4776 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4777}
4778
4779template<class _Tp, class _Alloc>
4780inline _LIBCPP_INLINE_VISIBILITY
4781shared_ptr<_Tp>
4782allocate_shared(const _Alloc& __a)
4783{
4784 return shared_ptr<_Tp>::allocate_shared(__a);
4785}
4786
4787template<class _Tp, class _Alloc, class _A0>
4788inline _LIBCPP_INLINE_VISIBILITY
4789shared_ptr<_Tp>
4790allocate_shared(const _Alloc& __a, _A0& __a0)
4791{
4792 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4793}
4794
4795template<class _Tp, class _Alloc, class _A0, class _A1>
4796inline _LIBCPP_INLINE_VISIBILITY
4797shared_ptr<_Tp>
4798allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4799{
4800 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4801}
4802
4803template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4804inline _LIBCPP_INLINE_VISIBILITY
4805shared_ptr<_Tp>
4806allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4807{
4808 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4809}
4810
4811#endif // _LIBCPP_HAS_NO_VARIADICS
4812
4813template<class _Tp, class _Up>
4814inline _LIBCPP_INLINE_VISIBILITY
4815bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004816operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004817{
4818 return __x.get() == __y.get();
4819}
4820
4821template<class _Tp, class _Up>
4822inline _LIBCPP_INLINE_VISIBILITY
4823bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004824operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004825{
4826 return !(__x == __y);
4827}
4828
4829template<class _Tp, class _Up>
4830inline _LIBCPP_INLINE_VISIBILITY
4831bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004832operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004833{
Eric Fiselier8492cd82015-02-05 23:01:40 +00004834 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4835 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnant3fadda32012-02-21 21:02:58 +00004836}
4837
4838template<class _Tp, class _Up>
4839inline _LIBCPP_INLINE_VISIBILITY
4840bool
4841operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4842{
4843 return __y < __x;
4844}
4845
4846template<class _Tp, class _Up>
4847inline _LIBCPP_INLINE_VISIBILITY
4848bool
4849operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4850{
4851 return !(__y < __x);
4852}
4853
4854template<class _Tp, class _Up>
4855inline _LIBCPP_INLINE_VISIBILITY
4856bool
4857operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4858{
4859 return !(__x < __y);
4860}
4861
4862template<class _Tp>
4863inline _LIBCPP_INLINE_VISIBILITY
4864bool
4865operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4866{
4867 return !__x;
4868}
4869
4870template<class _Tp>
4871inline _LIBCPP_INLINE_VISIBILITY
4872bool
4873operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4874{
4875 return !__x;
4876}
4877
4878template<class _Tp>
4879inline _LIBCPP_INLINE_VISIBILITY
4880bool
4881operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4882{
4883 return static_cast<bool>(__x);
4884}
4885
4886template<class _Tp>
4887inline _LIBCPP_INLINE_VISIBILITY
4888bool
4889operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4890{
4891 return static_cast<bool>(__x);
4892}
4893
4894template<class _Tp>
4895inline _LIBCPP_INLINE_VISIBILITY
4896bool
4897operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4898{
4899 return less<_Tp*>()(__x.get(), nullptr);
4900}
4901
4902template<class _Tp>
4903inline _LIBCPP_INLINE_VISIBILITY
4904bool
4905operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4906{
4907 return less<_Tp*>()(nullptr, __x.get());
4908}
4909
4910template<class _Tp>
4911inline _LIBCPP_INLINE_VISIBILITY
4912bool
4913operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4914{
4915 return nullptr < __x;
4916}
4917
4918template<class _Tp>
4919inline _LIBCPP_INLINE_VISIBILITY
4920bool
4921operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4922{
4923 return __x < nullptr;
4924}
4925
4926template<class _Tp>
4927inline _LIBCPP_INLINE_VISIBILITY
4928bool
4929operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4930{
4931 return !(nullptr < __x);
4932}
4933
4934template<class _Tp>
4935inline _LIBCPP_INLINE_VISIBILITY
4936bool
4937operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4938{
4939 return !(__x < nullptr);
4940}
4941
4942template<class _Tp>
4943inline _LIBCPP_INLINE_VISIBILITY
4944bool
4945operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4946{
4947 return !(__x < nullptr);
4948}
4949
4950template<class _Tp>
4951inline _LIBCPP_INLINE_VISIBILITY
4952bool
4953operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4954{
4955 return !(nullptr < __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004956}
4957
4958template<class _Tp>
4959inline _LIBCPP_INLINE_VISIBILITY
4960void
Howard Hinnant1694d232011-05-28 14:41:13 +00004961swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004962{
4963 __x.swap(__y);
4964}
4965
4966template<class _Tp, class _Up>
4967inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004968typename enable_if
4969<
4970 !is_array<_Tp>::value && !is_array<_Up>::value,
4971 shared_ptr<_Tp>
4972>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004973static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004974{
4975 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4976}
4977
4978template<class _Tp, class _Up>
4979inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004980typename enable_if
4981<
4982 !is_array<_Tp>::value && !is_array<_Up>::value,
4983 shared_ptr<_Tp>
4984>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004985dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004986{
4987 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4988 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4989}
4990
4991template<class _Tp, class _Up>
Howard Hinnant57199402012-01-02 17:56:02 +00004992typename enable_if
4993<
4994 is_array<_Tp>::value == is_array<_Up>::value,
4995 shared_ptr<_Tp>
4996>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004997const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004998{
Howard Hinnant57199402012-01-02 17:56:02 +00004999 typedef typename remove_extent<_Tp>::type _RTp;
5000 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005001}
5002
Howard Hinnantd4444702010-08-11 17:04:31 +00005003#ifndef _LIBCPP_NO_RTTI
5004
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005005template<class _Dp, class _Tp>
5006inline _LIBCPP_INLINE_VISIBILITY
5007_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00005008get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005009{
5010 return __p.template __get_deleter<_Dp>();
5011}
5012
Howard Hinnant324bb032010-08-22 00:02:43 +00005013#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00005014
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005015template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005016class _LIBCPP_TYPE_VIS_ONLY weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005017{
Howard Hinnant324bb032010-08-22 00:02:43 +00005018public:
5019 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005020private:
5021 element_type* __ptr_;
5022 __shared_weak_count* __cntrl_;
5023
Howard Hinnant324bb032010-08-22 00:02:43 +00005024public:
Howard Hinnant46e94932012-07-07 20:56:04 +00005025 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005026 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00005027 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5028 _NOEXCEPT;
5029 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005030 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00005031 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5032 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005033
Howard Hinnant57199402012-01-02 17:56:02 +00005034#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5035 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
5036 template<class _Yp> weak_ptr(weak_ptr<_Yp>&& __r,
5037 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5038 _NOEXCEPT;
5039#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00005040 ~weak_ptr();
5041
Howard Hinnant1694d232011-05-28 14:41:13 +00005042 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00005043 template<class _Yp>
5044 typename enable_if
5045 <
5046 is_convertible<_Yp*, element_type*>::value,
5047 weak_ptr&
5048 >::type
5049 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
5050
5051#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5052
5053 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
5054 template<class _Yp>
5055 typename enable_if
5056 <
5057 is_convertible<_Yp*, element_type*>::value,
5058 weak_ptr&
5059 >::type
5060 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
5061
5062#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5063
5064 template<class _Yp>
5065 typename enable_if
5066 <
5067 is_convertible<_Yp*, element_type*>::value,
5068 weak_ptr&
5069 >::type
5070 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005071
Howard Hinnant1694d232011-05-28 14:41:13 +00005072 void swap(weak_ptr& __r) _NOEXCEPT;
5073 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005074
Howard Hinnant82894812010-09-22 16:48:34 +00005075 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005076 long use_count() const _NOEXCEPT
5077 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00005078 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005079 bool expired() const _NOEXCEPT
5080 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5081 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00005082 template<class _Up>
5083 _LIBCPP_INLINE_VISIBILITY
5084 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005085 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00005086 template<class _Up>
5087 _LIBCPP_INLINE_VISIBILITY
5088 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005089 {return __cntrl_ < __r.__cntrl_;}
5090
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005091 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
5092 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005093};
5094
5095template<class _Tp>
5096inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00005097_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005098weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005099 : __ptr_(0),
5100 __cntrl_(0)
5101{
5102}
5103
5104template<class _Tp>
5105inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005106weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005107 : __ptr_(__r.__ptr_),
5108 __cntrl_(__r.__cntrl_)
5109{
5110 if (__cntrl_)
5111 __cntrl_->__add_weak();
5112}
5113
5114template<class _Tp>
5115template<class _Yp>
5116inline _LIBCPP_INLINE_VISIBILITY
5117weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005118 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005119 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005120 : __ptr_(__r.__ptr_),
5121 __cntrl_(__r.__cntrl_)
5122{
5123 if (__cntrl_)
5124 __cntrl_->__add_weak();
5125}
5126
5127template<class _Tp>
5128template<class _Yp>
5129inline _LIBCPP_INLINE_VISIBILITY
5130weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005131 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005132 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005133 : __ptr_(__r.__ptr_),
5134 __cntrl_(__r.__cntrl_)
5135{
5136 if (__cntrl_)
5137 __cntrl_->__add_weak();
5138}
5139
Howard Hinnant57199402012-01-02 17:56:02 +00005140#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5141
5142template<class _Tp>
5143inline _LIBCPP_INLINE_VISIBILITY
5144weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5145 : __ptr_(__r.__ptr_),
5146 __cntrl_(__r.__cntrl_)
5147{
5148 __r.__ptr_ = 0;
5149 __r.__cntrl_ = 0;
5150}
5151
5152template<class _Tp>
5153template<class _Yp>
5154inline _LIBCPP_INLINE_VISIBILITY
5155weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5156 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5157 _NOEXCEPT
5158 : __ptr_(__r.__ptr_),
5159 __cntrl_(__r.__cntrl_)
5160{
5161 __r.__ptr_ = 0;
5162 __r.__cntrl_ = 0;
5163}
5164
5165#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5166
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005167template<class _Tp>
5168weak_ptr<_Tp>::~weak_ptr()
5169{
5170 if (__cntrl_)
5171 __cntrl_->__release_weak();
5172}
5173
5174template<class _Tp>
5175inline _LIBCPP_INLINE_VISIBILITY
5176weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00005177weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005178{
5179 weak_ptr(__r).swap(*this);
5180 return *this;
5181}
5182
5183template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005184template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005185inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005186typename enable_if
5187<
5188 is_convertible<_Yp*, _Tp*>::value,
5189 weak_ptr<_Tp>&
5190>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005191weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005192{
5193 weak_ptr(__r).swap(*this);
5194 return *this;
5195}
5196
Howard Hinnant57199402012-01-02 17:56:02 +00005197#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5198
5199template<class _Tp>
5200inline _LIBCPP_INLINE_VISIBILITY
5201weak_ptr<_Tp>&
5202weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5203{
5204 weak_ptr(_VSTD::move(__r)).swap(*this);
5205 return *this;
5206}
5207
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005208template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005209template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005210inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005211typename enable_if
5212<
5213 is_convertible<_Yp*, _Tp*>::value,
5214 weak_ptr<_Tp>&
5215>::type
5216weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5217{
5218 weak_ptr(_VSTD::move(__r)).swap(*this);
5219 return *this;
5220}
5221
5222#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5223
5224template<class _Tp>
5225template<class _Yp>
5226inline _LIBCPP_INLINE_VISIBILITY
5227typename enable_if
5228<
5229 is_convertible<_Yp*, _Tp*>::value,
5230 weak_ptr<_Tp>&
5231>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005232weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005233{
5234 weak_ptr(__r).swap(*this);
5235 return *this;
5236}
5237
5238template<class _Tp>
5239inline _LIBCPP_INLINE_VISIBILITY
5240void
Howard Hinnant1694d232011-05-28 14:41:13 +00005241weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005242{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005243 _VSTD::swap(__ptr_, __r.__ptr_);
5244 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005245}
5246
5247template<class _Tp>
5248inline _LIBCPP_INLINE_VISIBILITY
5249void
Howard Hinnant1694d232011-05-28 14:41:13 +00005250swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005251{
5252 __x.swap(__y);
5253}
5254
5255template<class _Tp>
5256inline _LIBCPP_INLINE_VISIBILITY
5257void
Howard Hinnant1694d232011-05-28 14:41:13 +00005258weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005259{
5260 weak_ptr().swap(*this);
5261}
5262
5263template<class _Tp>
5264template<class _Yp>
5265shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5266 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
5267 : __ptr_(__r.__ptr_),
5268 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5269{
5270 if (__cntrl_ == 0)
5271#ifndef _LIBCPP_NO_EXCEPTIONS
5272 throw bad_weak_ptr();
5273#else
5274 assert(!"bad_weak_ptr");
5275#endif
5276}
5277
5278template<class _Tp>
5279shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00005280weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005281{
5282 shared_ptr<_Tp> __r;
5283 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5284 if (__r.__cntrl_)
5285 __r.__ptr_ = __ptr_;
5286 return __r;
5287}
5288
Howard Hinnant324bb032010-08-22 00:02:43 +00005289template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005290
5291template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005292struct _LIBCPP_TYPE_VIS_ONLY owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005293 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00005294{
5295 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005297 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5298 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005300 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5301 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005302 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005303 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5304 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00005305};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005306
5307template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005308struct _LIBCPP_TYPE_VIS_ONLY owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005309 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5310{
Howard Hinnant324bb032010-08-22 00:02:43 +00005311 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005312 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005313 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5314 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005316 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5317 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005318 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005319 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5320 {return __x.owner_before(__y);}
5321};
5322
5323template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005324class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005325{
5326 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00005327protected:
Howard Hinnant46e94932012-07-07 20:56:04 +00005328 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005329 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005330 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005331 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005333 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5334 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00005335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005336 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00005337public:
Howard Hinnant82894812010-09-22 16:48:34 +00005338 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005339 shared_ptr<_Tp> shared_from_this()
5340 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00005341 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005342 shared_ptr<_Tp const> shared_from_this() const
5343 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005344
5345 template <class _Up> friend class shared_ptr;
5346};
5347
Howard Hinnant21aefc32010-06-03 16:42:57 +00005348template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005349struct _LIBCPP_TYPE_VIS_ONLY hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00005350{
5351 typedef shared_ptr<_Tp> argument_type;
5352 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005353 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005354 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00005355 {
5356 return hash<_Tp*>()(__ptr.get());
5357 }
5358};
5359
Howard Hinnant99968442011-11-29 18:15:50 +00005360template<class _CharT, class _Traits, class _Yp>
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005361inline _LIBCPP_INLINE_VISIBILITY
5362basic_ostream<_CharT, _Traits>&
Howard Hinnant99968442011-11-29 18:15:50 +00005363operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005364
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +00005365#if __has_feature(cxx_atomic) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005366
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005367class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005368{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005369 void* __lx;
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005370public:
5371 void lock() _NOEXCEPT;
5372 void unlock() _NOEXCEPT;
5373
5374private:
5375 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5376 __sp_mut(const __sp_mut&);
5377 __sp_mut& operator=(const __sp_mut&);
5378
Howard Hinnant83eade62013-03-06 23:30:19 +00005379 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005380};
5381
Howard Hinnant83eade62013-03-06 23:30:19 +00005382_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005383
5384template <class _Tp>
5385inline _LIBCPP_INLINE_VISIBILITY
5386bool
5387atomic_is_lock_free(const shared_ptr<_Tp>*)
5388{
5389 return false;
5390}
5391
5392template <class _Tp>
5393shared_ptr<_Tp>
5394atomic_load(const shared_ptr<_Tp>* __p)
5395{
5396 __sp_mut& __m = __get_sp_mut(__p);
5397 __m.lock();
5398 shared_ptr<_Tp> __q = *__p;
5399 __m.unlock();
5400 return __q;
5401}
5402
5403template <class _Tp>
5404inline _LIBCPP_INLINE_VISIBILITY
5405shared_ptr<_Tp>
5406atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5407{
5408 return atomic_load(__p);
5409}
5410
5411template <class _Tp>
5412void
5413atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5414{
5415 __sp_mut& __m = __get_sp_mut(__p);
5416 __m.lock();
5417 __p->swap(__r);
5418 __m.unlock();
5419}
5420
5421template <class _Tp>
5422inline _LIBCPP_INLINE_VISIBILITY
5423void
5424atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5425{
5426 atomic_store(__p, __r);
5427}
5428
5429template <class _Tp>
5430shared_ptr<_Tp>
5431atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5432{
5433 __sp_mut& __m = __get_sp_mut(__p);
5434 __m.lock();
5435 __p->swap(__r);
5436 __m.unlock();
5437 return __r;
5438}
5439
5440template <class _Tp>
5441inline _LIBCPP_INLINE_VISIBILITY
5442shared_ptr<_Tp>
5443atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5444{
5445 return atomic_exchange(__p, __r);
5446}
5447
5448template <class _Tp>
5449bool
5450atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5451{
5452 __sp_mut& __m = __get_sp_mut(__p);
5453 __m.lock();
5454 if (__p->__owner_equivalent(*__v))
5455 {
5456 *__p = __w;
5457 __m.unlock();
5458 return true;
5459 }
5460 *__v = *__p;
5461 __m.unlock();
5462 return false;
5463}
5464
5465template <class _Tp>
5466inline _LIBCPP_INLINE_VISIBILITY
5467bool
5468atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5469{
5470 return atomic_compare_exchange_strong(__p, __v, __w);
5471}
5472
5473template <class _Tp>
5474inline _LIBCPP_INLINE_VISIBILITY
5475bool
5476atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5477 shared_ptr<_Tp> __w, memory_order, memory_order)
5478{
5479 return atomic_compare_exchange_strong(__p, __v, __w);
5480}
5481
5482template <class _Tp>
5483inline _LIBCPP_INLINE_VISIBILITY
5484bool
5485atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5486 shared_ptr<_Tp> __w, memory_order, memory_order)
5487{
5488 return atomic_compare_exchange_weak(__p, __v, __w);
5489}
5490
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +00005491#endif // __has_feature(cxx_atomic) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005492
Howard Hinnant324bb032010-08-22 00:02:43 +00005493//enum class
Howard Hinnant83eade62013-03-06 23:30:19 +00005494struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005495{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005496 enum __lx
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005497 {
5498 relaxed,
5499 preferred,
5500 strict
5501 };
5502
Howard Hinnant9c0df142012-10-30 19:06:59 +00005503 __lx __v_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005504
Howard Hinnant82894812010-09-22 16:48:34 +00005505 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9c0df142012-10-30 19:06:59 +00005506 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00005507 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005508 operator int() const {return __v_;}
5509};
5510
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005511_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5512_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5513_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
5514_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5515_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005516
5517template <class _Tp>
5518inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005519_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005520undeclare_reachable(_Tp* __p)
5521{
5522 return static_cast<_Tp*>(__undeclare_reachable(__p));
5523}
5524
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005525_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005526
5527_LIBCPP_END_NAMESPACE_STD
5528
5529#endif // _LIBCPP_MEMORY