blob: 69702c6192f48e2be299dd3db251baa02fa9a483 [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)
Marshall Clowc4113372015-06-19 15:54:13 +00004076 {
4077 __e->__weak_this_.__ptr_ = const_cast<_Yp*>(static_cast<const _Yp*>(__e));
4078 __e->__weak_this_.__cntrl_ = __cntrl_;
Marshall Clow46d06b92015-06-19 19:32:06 +00004079 __cntrl_->__add_weak();
Marshall Clowc4113372015-06-19 15:54:13 +00004080 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004081 }
4082
Howard Hinnant82894812010-09-22 16:48:34 +00004083 _LIBCPP_INLINE_VISIBILITY
Marshall Clow60784f62015-05-27 22:44:47 +00004084 void __enable_weak_this(const volatile void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004085
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004086 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
4087 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004088};
4089
4090template<class _Tp>
4091inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00004092_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004093shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004094 : __ptr_(0),
4095 __cntrl_(0)
4096{
4097}
4098
4099template<class _Tp>
4100inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00004101_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004102shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004103 : __ptr_(0),
4104 __cntrl_(0)
4105{
4106}
4107
4108template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004109template<class _Yp>
4110shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4111 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004112 : __ptr_(__p)
4113{
4114 unique_ptr<_Yp> __hold(__p);
4115 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4116 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
4117 __hold.release();
4118 __enable_weak_this(__p);
4119}
4120
4121template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004122template<class _Yp, class _Dp>
4123shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4124 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004125 : __ptr_(__p)
4126{
4127#ifndef _LIBCPP_NO_EXCEPTIONS
4128 try
4129 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004130#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004131 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4132 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
4133 __enable_weak_this(__p);
4134#ifndef _LIBCPP_NO_EXCEPTIONS
4135 }
4136 catch (...)
4137 {
4138 __d(__p);
4139 throw;
4140 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004141#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004142}
4143
4144template<class _Tp>
4145template<class _Dp>
4146shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4147 : __ptr_(0)
4148{
4149#ifndef _LIBCPP_NO_EXCEPTIONS
4150 try
4151 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004152#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004153 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4154 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4155#ifndef _LIBCPP_NO_EXCEPTIONS
4156 }
4157 catch (...)
4158 {
4159 __d(__p);
4160 throw;
4161 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004162#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004163}
4164
4165template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004166template<class _Yp, class _Dp, class _Alloc>
4167shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4168 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004169 : __ptr_(__p)
4170{
4171#ifndef _LIBCPP_NO_EXCEPTIONS
4172 try
4173 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004174#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004175 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004176 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004177 typedef __allocator_destructor<_A2> _D2;
4178 _A2 __a2(__a);
4179 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004180 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4181 _CntrlBlk(__p, __d, __a);
4182 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004183 __enable_weak_this(__p);
4184#ifndef _LIBCPP_NO_EXCEPTIONS
4185 }
4186 catch (...)
4187 {
4188 __d(__p);
4189 throw;
4190 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004191#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004192}
4193
4194template<class _Tp>
4195template<class _Dp, class _Alloc>
4196shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4197 : __ptr_(0)
4198{
4199#ifndef _LIBCPP_NO_EXCEPTIONS
4200 try
4201 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004202#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004203 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004204 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004205 typedef __allocator_destructor<_A2> _D2;
4206 _A2 __a2(__a);
4207 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004208 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4209 _CntrlBlk(__p, __d, __a);
4210 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004211#ifndef _LIBCPP_NO_EXCEPTIONS
4212 }
4213 catch (...)
4214 {
4215 __d(__p);
4216 throw;
4217 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004218#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004219}
4220
4221template<class _Tp>
4222template<class _Yp>
4223inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004224shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004225 : __ptr_(__p),
4226 __cntrl_(__r.__cntrl_)
4227{
4228 if (__cntrl_)
4229 __cntrl_->__add_shared();
4230}
4231
4232template<class _Tp>
4233inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004234shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004235 : __ptr_(__r.__ptr_),
4236 __cntrl_(__r.__cntrl_)
4237{
4238 if (__cntrl_)
4239 __cntrl_->__add_shared();
4240}
4241
4242template<class _Tp>
4243template<class _Yp>
4244inline _LIBCPP_INLINE_VISIBILITY
4245shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4246 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004247 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004248 : __ptr_(__r.__ptr_),
4249 __cntrl_(__r.__cntrl_)
4250{
4251 if (__cntrl_)
4252 __cntrl_->__add_shared();
4253}
4254
Howard Hinnant73d21a42010-09-04 23:28:19 +00004255#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004256
4257template<class _Tp>
4258inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004259shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004260 : __ptr_(__r.__ptr_),
4261 __cntrl_(__r.__cntrl_)
4262{
4263 __r.__ptr_ = 0;
4264 __r.__cntrl_ = 0;
4265}
4266
4267template<class _Tp>
4268template<class _Yp>
4269inline _LIBCPP_INLINE_VISIBILITY
4270shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4271 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004272 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004273 : __ptr_(__r.__ptr_),
4274 __cntrl_(__r.__cntrl_)
4275{
4276 __r.__ptr_ = 0;
4277 __r.__cntrl_ = 0;
4278}
4279
Howard Hinnant73d21a42010-09-04 23:28:19 +00004280#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004281
4282template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004283template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004284#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00004285shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004286#else
Logan Chiene1678a12014-01-31 09:30:46 +00004287shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004288#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004289 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004290 : __ptr_(__r.get())
4291{
4292 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4293 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
4294 __enable_weak_this(__r.get());
4295 __r.release();
4296}
4297
4298template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004299template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004300#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004301shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4302#else
4303shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4304#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004305 typename enable_if
4306 <
4307 !is_lvalue_reference<_Dp>::value &&
4308 !is_array<_Yp>::value &&
4309 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4310 __nat
4311 >::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004312 : __ptr_(__r.get())
4313{
Marshall Clow0ad232a2015-05-10 13:59:45 +00004314#if _LIBCPP_STD_VER > 11
4315 if (__ptr_ == nullptr)
4316 __cntrl_ = nullptr;
4317 else
4318#endif
4319 {
4320 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4321 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
4322 __enable_weak_this(__r.get());
4323 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004324 __r.release();
4325}
4326
4327template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004328template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004329#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004330shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4331#else
4332shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4333#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004334 typename enable_if
4335 <
4336 is_lvalue_reference<_Dp>::value &&
4337 !is_array<_Yp>::value &&
4338 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4339 __nat
4340 >::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004341 : __ptr_(__r.get())
4342{
Marshall Clow0ad232a2015-05-10 13:59:45 +00004343#if _LIBCPP_STD_VER > 11
4344 if (__ptr_ == nullptr)
4345 __cntrl_ = nullptr;
4346 else
4347#endif
4348 {
4349 typedef __shared_ptr_pointer<_Yp*,
4350 reference_wrapper<typename remove_reference<_Dp>::type>,
4351 allocator<_Yp> > _CntrlBlk;
4352 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
4353 __enable_weak_this(__r.get());
4354 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004355 __r.release();
4356}
4357
4358#ifndef _LIBCPP_HAS_NO_VARIADICS
4359
4360template<class _Tp>
4361template<class ..._Args>
4362shared_ptr<_Tp>
4363shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4364{
4365 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4366 typedef allocator<_CntrlBlk> _A2;
4367 typedef __allocator_destructor<_A2> _D2;
4368 _A2 __a2;
4369 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004370 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004371 shared_ptr<_Tp> __r;
4372 __r.__ptr_ = __hold2.get()->get();
4373 __r.__cntrl_ = __hold2.release();
4374 __r.__enable_weak_this(__r.__ptr_);
4375 return __r;
4376}
4377
4378template<class _Tp>
4379template<class _Alloc, class ..._Args>
4380shared_ptr<_Tp>
4381shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4382{
4383 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004384 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004385 typedef __allocator_destructor<_A2> _D2;
4386 _A2 __a2(__a);
4387 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004388 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4389 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004390 shared_ptr<_Tp> __r;
4391 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004392 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004393 __r.__enable_weak_this(__r.__ptr_);
4394 return __r;
4395}
4396
4397#else // _LIBCPP_HAS_NO_VARIADICS
4398
4399template<class _Tp>
4400shared_ptr<_Tp>
4401shared_ptr<_Tp>::make_shared()
4402{
4403 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4404 typedef allocator<_CntrlBlk> _Alloc2;
4405 typedef __allocator_destructor<_Alloc2> _D2;
4406 _Alloc2 __alloc2;
4407 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4408 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4409 shared_ptr<_Tp> __r;
4410 __r.__ptr_ = __hold2.get()->get();
4411 __r.__cntrl_ = __hold2.release();
4412 __r.__enable_weak_this(__r.__ptr_);
4413 return __r;
4414}
4415
4416template<class _Tp>
4417template<class _A0>
4418shared_ptr<_Tp>
4419shared_ptr<_Tp>::make_shared(_A0& __a0)
4420{
4421 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4422 typedef allocator<_CntrlBlk> _Alloc2;
4423 typedef __allocator_destructor<_Alloc2> _D2;
4424 _Alloc2 __alloc2;
4425 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4426 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4427 shared_ptr<_Tp> __r;
4428 __r.__ptr_ = __hold2.get()->get();
4429 __r.__cntrl_ = __hold2.release();
4430 __r.__enable_weak_this(__r.__ptr_);
4431 return __r;
4432}
4433
4434template<class _Tp>
4435template<class _A0, class _A1>
4436shared_ptr<_Tp>
4437shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4438{
4439 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4440 typedef allocator<_CntrlBlk> _Alloc2;
4441 typedef __allocator_destructor<_Alloc2> _D2;
4442 _Alloc2 __alloc2;
4443 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4444 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4445 shared_ptr<_Tp> __r;
4446 __r.__ptr_ = __hold2.get()->get();
4447 __r.__cntrl_ = __hold2.release();
4448 __r.__enable_weak_this(__r.__ptr_);
4449 return __r;
4450}
4451
4452template<class _Tp>
4453template<class _A0, class _A1, class _A2>
4454shared_ptr<_Tp>
4455shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4456{
4457 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4458 typedef allocator<_CntrlBlk> _Alloc2;
4459 typedef __allocator_destructor<_Alloc2> _D2;
4460 _Alloc2 __alloc2;
4461 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4462 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4463 shared_ptr<_Tp> __r;
4464 __r.__ptr_ = __hold2.get()->get();
4465 __r.__cntrl_ = __hold2.release();
4466 __r.__enable_weak_this(__r.__ptr_);
4467 return __r;
4468}
4469
4470template<class _Tp>
4471template<class _Alloc>
4472shared_ptr<_Tp>
4473shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4474{
4475 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004476 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004477 typedef __allocator_destructor<_Alloc2> _D2;
4478 _Alloc2 __alloc2(__a);
4479 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004480 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4481 _CntrlBlk(__a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004482 shared_ptr<_Tp> __r;
4483 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004484 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004485 __r.__enable_weak_this(__r.__ptr_);
4486 return __r;
4487}
4488
4489template<class _Tp>
4490template<class _Alloc, class _A0>
4491shared_ptr<_Tp>
4492shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4493{
4494 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004495 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004496 typedef __allocator_destructor<_Alloc2> _D2;
4497 _Alloc2 __alloc2(__a);
4498 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004499 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4500 _CntrlBlk(__a, __a0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004501 shared_ptr<_Tp> __r;
4502 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004503 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004504 __r.__enable_weak_this(__r.__ptr_);
4505 return __r;
4506}
4507
4508template<class _Tp>
4509template<class _Alloc, class _A0, class _A1>
4510shared_ptr<_Tp>
4511shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4512{
4513 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004514 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004515 typedef __allocator_destructor<_Alloc2> _D2;
4516 _Alloc2 __alloc2(__a);
4517 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004518 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4519 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004520 shared_ptr<_Tp> __r;
4521 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004522 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004523 __r.__enable_weak_this(__r.__ptr_);
4524 return __r;
4525}
4526
4527template<class _Tp>
4528template<class _Alloc, class _A0, class _A1, class _A2>
4529shared_ptr<_Tp>
4530shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4531{
4532 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004533 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004534 typedef __allocator_destructor<_Alloc2> _D2;
4535 _Alloc2 __alloc2(__a);
4536 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004537 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4538 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004539 shared_ptr<_Tp> __r;
4540 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004541 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004542 __r.__enable_weak_this(__r.__ptr_);
4543 return __r;
4544}
4545
4546#endif // _LIBCPP_HAS_NO_VARIADICS
4547
4548template<class _Tp>
4549shared_ptr<_Tp>::~shared_ptr()
4550{
4551 if (__cntrl_)
4552 __cntrl_->__release_shared();
4553}
4554
4555template<class _Tp>
4556inline _LIBCPP_INLINE_VISIBILITY
4557shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004558shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004559{
4560 shared_ptr(__r).swap(*this);
4561 return *this;
4562}
4563
4564template<class _Tp>
4565template<class _Yp>
4566inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004567typename enable_if
4568<
4569 is_convertible<_Yp*, _Tp*>::value,
4570 shared_ptr<_Tp>&
4571>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004572shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004573{
4574 shared_ptr(__r).swap(*this);
4575 return *this;
4576}
4577
Howard Hinnant73d21a42010-09-04 23:28:19 +00004578#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004579
4580template<class _Tp>
4581inline _LIBCPP_INLINE_VISIBILITY
4582shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004583shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004584{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004585 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004586 return *this;
4587}
4588
4589template<class _Tp>
4590template<class _Yp>
4591inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004592typename enable_if
4593<
4594 is_convertible<_Yp*, _Tp*>::value,
4595 shared_ptr<_Tp>&
4596>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004597shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4598{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004599 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004600 return *this;
4601}
4602
4603template<class _Tp>
4604template<class _Yp>
4605inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004606typename enable_if
4607<
4608 !is_array<_Yp>::value &&
4609 is_convertible<_Yp*, _Tp*>::value,
Howard Hinnant37c4acf2013-09-13 23:56:00 +00004610 shared_ptr<_Tp>
4611>::type&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004612shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4613{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004614 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004615 return *this;
4616}
4617
4618template<class _Tp>
4619template <class _Yp, class _Dp>
4620inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004621typename enable_if
4622<
4623 !is_array<_Yp>::value &&
4624 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4625 shared_ptr<_Tp>&
4626>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004627shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4628{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004629 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004630 return *this;
4631}
4632
Howard Hinnant73d21a42010-09-04 23:28:19 +00004633#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004634
4635template<class _Tp>
4636template<class _Yp>
4637inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004638typename enable_if
4639<
4640 !is_array<_Yp>::value &&
4641 is_convertible<_Yp*, _Tp*>::value,
4642 shared_ptr<_Tp>&
4643>::type
Howard Hinnant324bb032010-08-22 00:02:43 +00004644shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004645{
4646 shared_ptr(__r).swap(*this);
4647 return *this;
4648}
4649
4650template<class _Tp>
4651template <class _Yp, class _Dp>
4652inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004653typename enable_if
4654<
4655 !is_array<_Yp>::value &&
4656 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4657 shared_ptr<_Tp>&
4658>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004659shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4660{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004661 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004662 return *this;
4663}
4664
Howard Hinnant73d21a42010-09-04 23:28:19 +00004665#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004666
4667template<class _Tp>
4668inline _LIBCPP_INLINE_VISIBILITY
4669void
Howard Hinnant1694d232011-05-28 14:41:13 +00004670shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004671{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004672 _VSTD::swap(__ptr_, __r.__ptr_);
4673 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004674}
4675
4676template<class _Tp>
4677inline _LIBCPP_INLINE_VISIBILITY
4678void
Howard Hinnant1694d232011-05-28 14:41:13 +00004679shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004680{
4681 shared_ptr().swap(*this);
4682}
4683
4684template<class _Tp>
4685template<class _Yp>
4686inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004687typename enable_if
4688<
4689 is_convertible<_Yp*, _Tp*>::value,
4690 void
4691>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004692shared_ptr<_Tp>::reset(_Yp* __p)
4693{
4694 shared_ptr(__p).swap(*this);
4695}
4696
4697template<class _Tp>
4698template<class _Yp, class _Dp>
4699inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004700typename enable_if
4701<
4702 is_convertible<_Yp*, _Tp*>::value,
4703 void
4704>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004705shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4706{
4707 shared_ptr(__p, __d).swap(*this);
4708}
4709
4710template<class _Tp>
4711template<class _Yp, class _Dp, class _Alloc>
4712inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004713typename enable_if
4714<
4715 is_convertible<_Yp*, _Tp*>::value,
4716 void
4717>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004718shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4719{
4720 shared_ptr(__p, __d, __a).swap(*this);
4721}
4722
4723#ifndef _LIBCPP_HAS_NO_VARIADICS
4724
Howard Hinnant324bb032010-08-22 00:02:43 +00004725template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004726inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004727typename enable_if
4728<
4729 !is_array<_Tp>::value,
4730 shared_ptr<_Tp>
4731>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004732make_shared(_Args&& ...__args)
4733{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004734 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004735}
4736
Howard Hinnant324bb032010-08-22 00:02:43 +00004737template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004738inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004739typename enable_if
4740<
4741 !is_array<_Tp>::value,
4742 shared_ptr<_Tp>
4743>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004744allocate_shared(const _Alloc& __a, _Args&& ...__args)
4745{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004746 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004747}
4748
4749#else // _LIBCPP_HAS_NO_VARIADICS
4750
4751template<class _Tp>
4752inline _LIBCPP_INLINE_VISIBILITY
4753shared_ptr<_Tp>
4754make_shared()
4755{
4756 return shared_ptr<_Tp>::make_shared();
4757}
4758
4759template<class _Tp, class _A0>
4760inline _LIBCPP_INLINE_VISIBILITY
4761shared_ptr<_Tp>
4762make_shared(_A0& __a0)
4763{
4764 return shared_ptr<_Tp>::make_shared(__a0);
4765}
4766
4767template<class _Tp, class _A0, class _A1>
4768inline _LIBCPP_INLINE_VISIBILITY
4769shared_ptr<_Tp>
4770make_shared(_A0& __a0, _A1& __a1)
4771{
4772 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4773}
4774
Howard Hinnant324bb032010-08-22 00:02:43 +00004775template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004776inline _LIBCPP_INLINE_VISIBILITY
4777shared_ptr<_Tp>
4778make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4779{
4780 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4781}
4782
4783template<class _Tp, class _Alloc>
4784inline _LIBCPP_INLINE_VISIBILITY
4785shared_ptr<_Tp>
4786allocate_shared(const _Alloc& __a)
4787{
4788 return shared_ptr<_Tp>::allocate_shared(__a);
4789}
4790
4791template<class _Tp, class _Alloc, class _A0>
4792inline _LIBCPP_INLINE_VISIBILITY
4793shared_ptr<_Tp>
4794allocate_shared(const _Alloc& __a, _A0& __a0)
4795{
4796 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4797}
4798
4799template<class _Tp, class _Alloc, class _A0, class _A1>
4800inline _LIBCPP_INLINE_VISIBILITY
4801shared_ptr<_Tp>
4802allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4803{
4804 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4805}
4806
4807template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4808inline _LIBCPP_INLINE_VISIBILITY
4809shared_ptr<_Tp>
4810allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4811{
4812 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4813}
4814
4815#endif // _LIBCPP_HAS_NO_VARIADICS
4816
4817template<class _Tp, class _Up>
4818inline _LIBCPP_INLINE_VISIBILITY
4819bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004820operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004821{
4822 return __x.get() == __y.get();
4823}
4824
4825template<class _Tp, class _Up>
4826inline _LIBCPP_INLINE_VISIBILITY
4827bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004828operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004829{
4830 return !(__x == __y);
4831}
4832
4833template<class _Tp, class _Up>
4834inline _LIBCPP_INLINE_VISIBILITY
4835bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004836operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004837{
Eric Fiselier8492cd82015-02-05 23:01:40 +00004838 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4839 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnant3fadda32012-02-21 21:02:58 +00004840}
4841
4842template<class _Tp, class _Up>
4843inline _LIBCPP_INLINE_VISIBILITY
4844bool
4845operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4846{
4847 return __y < __x;
4848}
4849
4850template<class _Tp, class _Up>
4851inline _LIBCPP_INLINE_VISIBILITY
4852bool
4853operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4854{
4855 return !(__y < __x);
4856}
4857
4858template<class _Tp, class _Up>
4859inline _LIBCPP_INLINE_VISIBILITY
4860bool
4861operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4862{
4863 return !(__x < __y);
4864}
4865
4866template<class _Tp>
4867inline _LIBCPP_INLINE_VISIBILITY
4868bool
4869operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4870{
4871 return !__x;
4872}
4873
4874template<class _Tp>
4875inline _LIBCPP_INLINE_VISIBILITY
4876bool
4877operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4878{
4879 return !__x;
4880}
4881
4882template<class _Tp>
4883inline _LIBCPP_INLINE_VISIBILITY
4884bool
4885operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4886{
4887 return static_cast<bool>(__x);
4888}
4889
4890template<class _Tp>
4891inline _LIBCPP_INLINE_VISIBILITY
4892bool
4893operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4894{
4895 return static_cast<bool>(__x);
4896}
4897
4898template<class _Tp>
4899inline _LIBCPP_INLINE_VISIBILITY
4900bool
4901operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4902{
4903 return less<_Tp*>()(__x.get(), nullptr);
4904}
4905
4906template<class _Tp>
4907inline _LIBCPP_INLINE_VISIBILITY
4908bool
4909operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4910{
4911 return less<_Tp*>()(nullptr, __x.get());
4912}
4913
4914template<class _Tp>
4915inline _LIBCPP_INLINE_VISIBILITY
4916bool
4917operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4918{
4919 return nullptr < __x;
4920}
4921
4922template<class _Tp>
4923inline _LIBCPP_INLINE_VISIBILITY
4924bool
4925operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4926{
4927 return __x < nullptr;
4928}
4929
4930template<class _Tp>
4931inline _LIBCPP_INLINE_VISIBILITY
4932bool
4933operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4934{
4935 return !(nullptr < __x);
4936}
4937
4938template<class _Tp>
4939inline _LIBCPP_INLINE_VISIBILITY
4940bool
4941operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4942{
4943 return !(__x < nullptr);
4944}
4945
4946template<class _Tp>
4947inline _LIBCPP_INLINE_VISIBILITY
4948bool
4949operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4950{
4951 return !(__x < nullptr);
4952}
4953
4954template<class _Tp>
4955inline _LIBCPP_INLINE_VISIBILITY
4956bool
4957operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4958{
4959 return !(nullptr < __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004960}
4961
4962template<class _Tp>
4963inline _LIBCPP_INLINE_VISIBILITY
4964void
Howard Hinnant1694d232011-05-28 14:41:13 +00004965swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004966{
4967 __x.swap(__y);
4968}
4969
4970template<class _Tp, class _Up>
4971inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004972typename enable_if
4973<
4974 !is_array<_Tp>::value && !is_array<_Up>::value,
4975 shared_ptr<_Tp>
4976>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004977static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004978{
4979 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4980}
4981
4982template<class _Tp, class _Up>
4983inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004984typename enable_if
4985<
4986 !is_array<_Tp>::value && !is_array<_Up>::value,
4987 shared_ptr<_Tp>
4988>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004989dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004990{
4991 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4992 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4993}
4994
4995template<class _Tp, class _Up>
Howard Hinnant57199402012-01-02 17:56:02 +00004996typename enable_if
4997<
4998 is_array<_Tp>::value == is_array<_Up>::value,
4999 shared_ptr<_Tp>
5000>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005001const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005002{
Howard Hinnant57199402012-01-02 17:56:02 +00005003 typedef typename remove_extent<_Tp>::type _RTp;
5004 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005005}
5006
Howard Hinnantd4444702010-08-11 17:04:31 +00005007#ifndef _LIBCPP_NO_RTTI
5008
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005009template<class _Dp, class _Tp>
5010inline _LIBCPP_INLINE_VISIBILITY
5011_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00005012get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005013{
5014 return __p.template __get_deleter<_Dp>();
5015}
5016
Howard Hinnant324bb032010-08-22 00:02:43 +00005017#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00005018
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005019template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005020class _LIBCPP_TYPE_VIS_ONLY weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005021{
Howard Hinnant324bb032010-08-22 00:02:43 +00005022public:
5023 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005024private:
5025 element_type* __ptr_;
5026 __shared_weak_count* __cntrl_;
5027
Howard Hinnant324bb032010-08-22 00:02:43 +00005028public:
Howard Hinnant46e94932012-07-07 20:56:04 +00005029 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005030 template<class _Yp> weak_ptr(shared_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;
5033 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005034 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00005035 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5036 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005037
Howard Hinnant57199402012-01-02 17:56:02 +00005038#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5039 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
5040 template<class _Yp> weak_ptr(weak_ptr<_Yp>&& __r,
5041 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5042 _NOEXCEPT;
5043#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00005044 ~weak_ptr();
5045
Howard Hinnant1694d232011-05-28 14:41:13 +00005046 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00005047 template<class _Yp>
5048 typename enable_if
5049 <
5050 is_convertible<_Yp*, element_type*>::value,
5051 weak_ptr&
5052 >::type
5053 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
5054
5055#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5056
5057 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
5058 template<class _Yp>
5059 typename enable_if
5060 <
5061 is_convertible<_Yp*, element_type*>::value,
5062 weak_ptr&
5063 >::type
5064 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
5065
5066#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5067
5068 template<class _Yp>
5069 typename enable_if
5070 <
5071 is_convertible<_Yp*, element_type*>::value,
5072 weak_ptr&
5073 >::type
5074 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005075
Howard Hinnant1694d232011-05-28 14:41:13 +00005076 void swap(weak_ptr& __r) _NOEXCEPT;
5077 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005078
Howard Hinnant82894812010-09-22 16:48:34 +00005079 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005080 long use_count() const _NOEXCEPT
5081 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00005082 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005083 bool expired() const _NOEXCEPT
5084 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5085 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00005086 template<class _Up>
5087 _LIBCPP_INLINE_VISIBILITY
5088 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005089 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00005090 template<class _Up>
5091 _LIBCPP_INLINE_VISIBILITY
5092 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005093 {return __cntrl_ < __r.__cntrl_;}
5094
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005095 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
5096 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005097};
5098
5099template<class _Tp>
5100inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00005101_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005102weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005103 : __ptr_(0),
5104 __cntrl_(0)
5105{
5106}
5107
5108template<class _Tp>
5109inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005110weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005111 : __ptr_(__r.__ptr_),
5112 __cntrl_(__r.__cntrl_)
5113{
5114 if (__cntrl_)
5115 __cntrl_->__add_weak();
5116}
5117
5118template<class _Tp>
5119template<class _Yp>
5120inline _LIBCPP_INLINE_VISIBILITY
5121weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005122 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005123 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005124 : __ptr_(__r.__ptr_),
5125 __cntrl_(__r.__cntrl_)
5126{
5127 if (__cntrl_)
5128 __cntrl_->__add_weak();
5129}
5130
5131template<class _Tp>
5132template<class _Yp>
5133inline _LIBCPP_INLINE_VISIBILITY
5134weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005135 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005136 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005137 : __ptr_(__r.__ptr_),
5138 __cntrl_(__r.__cntrl_)
5139{
5140 if (__cntrl_)
5141 __cntrl_->__add_weak();
5142}
5143
Howard Hinnant57199402012-01-02 17:56:02 +00005144#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5145
5146template<class _Tp>
5147inline _LIBCPP_INLINE_VISIBILITY
5148weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5149 : __ptr_(__r.__ptr_),
5150 __cntrl_(__r.__cntrl_)
5151{
5152 __r.__ptr_ = 0;
5153 __r.__cntrl_ = 0;
5154}
5155
5156template<class _Tp>
5157template<class _Yp>
5158inline _LIBCPP_INLINE_VISIBILITY
5159weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5160 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5161 _NOEXCEPT
5162 : __ptr_(__r.__ptr_),
5163 __cntrl_(__r.__cntrl_)
5164{
5165 __r.__ptr_ = 0;
5166 __r.__cntrl_ = 0;
5167}
5168
5169#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5170
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005171template<class _Tp>
5172weak_ptr<_Tp>::~weak_ptr()
5173{
5174 if (__cntrl_)
5175 __cntrl_->__release_weak();
5176}
5177
5178template<class _Tp>
5179inline _LIBCPP_INLINE_VISIBILITY
5180weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00005181weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005182{
5183 weak_ptr(__r).swap(*this);
5184 return *this;
5185}
5186
5187template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005188template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005189inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005190typename enable_if
5191<
5192 is_convertible<_Yp*, _Tp*>::value,
5193 weak_ptr<_Tp>&
5194>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005195weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005196{
5197 weak_ptr(__r).swap(*this);
5198 return *this;
5199}
5200
Howard Hinnant57199402012-01-02 17:56:02 +00005201#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5202
5203template<class _Tp>
5204inline _LIBCPP_INLINE_VISIBILITY
5205weak_ptr<_Tp>&
5206weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5207{
5208 weak_ptr(_VSTD::move(__r)).swap(*this);
5209 return *this;
5210}
5211
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005212template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005213template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005214inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005215typename enable_if
5216<
5217 is_convertible<_Yp*, _Tp*>::value,
5218 weak_ptr<_Tp>&
5219>::type
5220weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5221{
5222 weak_ptr(_VSTD::move(__r)).swap(*this);
5223 return *this;
5224}
5225
5226#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5227
5228template<class _Tp>
5229template<class _Yp>
5230inline _LIBCPP_INLINE_VISIBILITY
5231typename enable_if
5232<
5233 is_convertible<_Yp*, _Tp*>::value,
5234 weak_ptr<_Tp>&
5235>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005236weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005237{
5238 weak_ptr(__r).swap(*this);
5239 return *this;
5240}
5241
5242template<class _Tp>
5243inline _LIBCPP_INLINE_VISIBILITY
5244void
Howard Hinnant1694d232011-05-28 14:41:13 +00005245weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005246{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005247 _VSTD::swap(__ptr_, __r.__ptr_);
5248 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005249}
5250
5251template<class _Tp>
5252inline _LIBCPP_INLINE_VISIBILITY
5253void
Howard Hinnant1694d232011-05-28 14:41:13 +00005254swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005255{
5256 __x.swap(__y);
5257}
5258
5259template<class _Tp>
5260inline _LIBCPP_INLINE_VISIBILITY
5261void
Howard Hinnant1694d232011-05-28 14:41:13 +00005262weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005263{
5264 weak_ptr().swap(*this);
5265}
5266
5267template<class _Tp>
5268template<class _Yp>
5269shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5270 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
5271 : __ptr_(__r.__ptr_),
5272 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5273{
5274 if (__cntrl_ == 0)
5275#ifndef _LIBCPP_NO_EXCEPTIONS
5276 throw bad_weak_ptr();
5277#else
5278 assert(!"bad_weak_ptr");
5279#endif
5280}
5281
5282template<class _Tp>
5283shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00005284weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005285{
5286 shared_ptr<_Tp> __r;
5287 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5288 if (__r.__cntrl_)
5289 __r.__ptr_ = __ptr_;
5290 return __r;
5291}
5292
Howard Hinnant324bb032010-08-22 00:02:43 +00005293template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005294
5295template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005296struct _LIBCPP_TYPE_VIS_ONLY owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005297 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00005298{
5299 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005300 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005301 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5302 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005304 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5305 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005307 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5308 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00005309};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005310
5311template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005312struct _LIBCPP_TYPE_VIS_ONLY owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005313 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5314{
Howard Hinnant324bb032010-08-22 00:02:43 +00005315 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005316 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005317 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5318 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005320 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5321 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005322 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005323 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5324 {return __x.owner_before(__y);}
5325};
5326
5327template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005328class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005329{
5330 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00005331protected:
Howard Hinnant46e94932012-07-07 20:56:04 +00005332 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005333 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005335 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005336 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005337 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5338 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00005339 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005340 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00005341public:
Howard Hinnant82894812010-09-22 16:48:34 +00005342 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005343 shared_ptr<_Tp> shared_from_this()
5344 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00005345 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005346 shared_ptr<_Tp const> shared_from_this() const
5347 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005348
5349 template <class _Up> friend class shared_ptr;
5350};
5351
Howard Hinnant21aefc32010-06-03 16:42:57 +00005352template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005353struct _LIBCPP_TYPE_VIS_ONLY hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00005354{
5355 typedef shared_ptr<_Tp> argument_type;
5356 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005358 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00005359 {
5360 return hash<_Tp*>()(__ptr.get());
5361 }
5362};
5363
Howard Hinnant99968442011-11-29 18:15:50 +00005364template<class _CharT, class _Traits, class _Yp>
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005365inline _LIBCPP_INLINE_VISIBILITY
5366basic_ostream<_CharT, _Traits>&
Howard Hinnant99968442011-11-29 18:15:50 +00005367operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005368
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +00005369#if __has_feature(cxx_atomic) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005370
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005371class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005372{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005373 void* __lx;
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005374public:
5375 void lock() _NOEXCEPT;
5376 void unlock() _NOEXCEPT;
5377
5378private:
5379 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5380 __sp_mut(const __sp_mut&);
5381 __sp_mut& operator=(const __sp_mut&);
5382
Howard Hinnant83eade62013-03-06 23:30:19 +00005383 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005384};
5385
Howard Hinnant83eade62013-03-06 23:30:19 +00005386_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005387
5388template <class _Tp>
5389inline _LIBCPP_INLINE_VISIBILITY
5390bool
5391atomic_is_lock_free(const shared_ptr<_Tp>*)
5392{
5393 return false;
5394}
5395
5396template <class _Tp>
5397shared_ptr<_Tp>
5398atomic_load(const shared_ptr<_Tp>* __p)
5399{
5400 __sp_mut& __m = __get_sp_mut(__p);
5401 __m.lock();
5402 shared_ptr<_Tp> __q = *__p;
5403 __m.unlock();
5404 return __q;
5405}
5406
5407template <class _Tp>
5408inline _LIBCPP_INLINE_VISIBILITY
5409shared_ptr<_Tp>
5410atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5411{
5412 return atomic_load(__p);
5413}
5414
5415template <class _Tp>
5416void
5417atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5418{
5419 __sp_mut& __m = __get_sp_mut(__p);
5420 __m.lock();
5421 __p->swap(__r);
5422 __m.unlock();
5423}
5424
5425template <class _Tp>
5426inline _LIBCPP_INLINE_VISIBILITY
5427void
5428atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5429{
5430 atomic_store(__p, __r);
5431}
5432
5433template <class _Tp>
5434shared_ptr<_Tp>
5435atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5436{
5437 __sp_mut& __m = __get_sp_mut(__p);
5438 __m.lock();
5439 __p->swap(__r);
5440 __m.unlock();
5441 return __r;
5442}
5443
5444template <class _Tp>
5445inline _LIBCPP_INLINE_VISIBILITY
5446shared_ptr<_Tp>
5447atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5448{
5449 return atomic_exchange(__p, __r);
5450}
5451
5452template <class _Tp>
5453bool
5454atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5455{
5456 __sp_mut& __m = __get_sp_mut(__p);
5457 __m.lock();
5458 if (__p->__owner_equivalent(*__v))
5459 {
5460 *__p = __w;
5461 __m.unlock();
5462 return true;
5463 }
5464 *__v = *__p;
5465 __m.unlock();
5466 return false;
5467}
5468
5469template <class _Tp>
5470inline _LIBCPP_INLINE_VISIBILITY
5471bool
5472atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5473{
5474 return atomic_compare_exchange_strong(__p, __v, __w);
5475}
5476
5477template <class _Tp>
5478inline _LIBCPP_INLINE_VISIBILITY
5479bool
5480atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5481 shared_ptr<_Tp> __w, memory_order, memory_order)
5482{
5483 return atomic_compare_exchange_strong(__p, __v, __w);
5484}
5485
5486template <class _Tp>
5487inline _LIBCPP_INLINE_VISIBILITY
5488bool
5489atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5490 shared_ptr<_Tp> __w, memory_order, memory_order)
5491{
5492 return atomic_compare_exchange_weak(__p, __v, __w);
5493}
5494
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +00005495#endif // __has_feature(cxx_atomic) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005496
Howard Hinnant324bb032010-08-22 00:02:43 +00005497//enum class
Howard Hinnant83eade62013-03-06 23:30:19 +00005498struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005499{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005500 enum __lx
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005501 {
5502 relaxed,
5503 preferred,
5504 strict
5505 };
5506
Howard Hinnant9c0df142012-10-30 19:06:59 +00005507 __lx __v_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005508
Howard Hinnant82894812010-09-22 16:48:34 +00005509 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9c0df142012-10-30 19:06:59 +00005510 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00005511 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005512 operator int() const {return __v_;}
5513};
5514
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005515_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5516_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5517_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
5518_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5519_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005520
5521template <class _Tp>
5522inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005523_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005524undeclare_reachable(_Tp* __p)
5525{
5526 return static_cast<_Tp*>(__undeclare_reachable(__p));
5527}
5528
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005529_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005530
5531_LIBCPP_END_NAMESPACE_STD
5532
5533#endif // _LIBCPP_MEMORY