blob: e59fd5d38a4156d6c8f9535c1561722d6d8697ab [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
Eric Fiselier00f4a492015-08-19 17:21:46 +0000615#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant5fec82d2012-07-30 01:40:57 +0000616# include <atomic>
617#endif
618
Howard Hinnant66c6f972011-11-29 16:45:27 +0000619#include <__undef_min_max>
Saleem Abdulrasoolf1b30c42015-02-13 22:15:32 +0000620#include <__undef___deallocate>
Howard Hinnant66c6f972011-11-29 16:45:27 +0000621
Howard Hinnant08e17472011-10-17 20:05:10 +0000622#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000623#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000624#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000625
626_LIBCPP_BEGIN_NAMESPACE_STD
627
Eric Fiselierc6e46692015-07-07 00:27:16 +0000628template <class _ValueType>
629inline _LIBCPP_ALWAYS_INLINE
630_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
631#if !defined(_LIBCPP_HAS_NO_THREADS) && \
632 defined(__ATOMIC_RELAXED) && \
633 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
634 return __atomic_load_n(__value, __ATOMIC_RELAXED);
635#else
636 return *__value;
637#endif
638}
639
Howard Hinnanta4e87ab2013-08-08 18:38:55 +0000640// addressof moved to <__functional_base>
Douglas Gregor35d2fcf2011-06-22 22:17:44 +0000641
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000642template <class _Tp> class allocator;
643
644template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000645class _LIBCPP_TYPE_VIS_ONLY allocator<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000646{
647public:
648 typedef void* pointer;
649 typedef const void* const_pointer;
650 typedef void value_type;
651
652 template <class _Up> struct rebind {typedef allocator<_Up> other;};
653};
654
Howard Hinnanta1877872012-01-19 23:15:22 +0000655template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000656class _LIBCPP_TYPE_VIS_ONLY allocator<const void>
Howard Hinnanta1877872012-01-19 23:15:22 +0000657{
658public:
659 typedef const void* pointer;
660 typedef const void* const_pointer;
661 typedef const void value_type;
662
663 template <class _Up> struct rebind {typedef allocator<_Up> other;};
664};
665
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000666// pointer_traits
667
668template <class _Tp>
669struct __has_element_type
670{
671private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000672 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000673 template <class _Up> static __two __test(...);
674 template <class _Up> static char __test(typename _Up::element_type* = 0);
675public:
676 static const bool value = sizeof(__test<_Tp>(0)) == 1;
677};
678
679template <class _Ptr, bool = __has_element_type<_Ptr>::value>
680struct __pointer_traits_element_type;
681
682template <class _Ptr>
683struct __pointer_traits_element_type<_Ptr, true>
684{
685 typedef typename _Ptr::element_type type;
686};
687
688#ifndef _LIBCPP_HAS_NO_VARIADICS
689
690template <template <class, class...> class _Sp, class _Tp, class ..._Args>
691struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
692{
693 typedef typename _Sp<_Tp, _Args...>::element_type type;
694};
695
696template <template <class, class...> class _Sp, class _Tp, class ..._Args>
697struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
698{
699 typedef _Tp type;
700};
701
Howard Hinnant324bb032010-08-22 00:02:43 +0000702#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000703
704template <template <class> class _Sp, class _Tp>
705struct __pointer_traits_element_type<_Sp<_Tp>, true>
706{
707 typedef typename _Sp<_Tp>::element_type type;
708};
709
710template <template <class> class _Sp, class _Tp>
711struct __pointer_traits_element_type<_Sp<_Tp>, false>
712{
713 typedef _Tp type;
714};
715
716template <template <class, class> class _Sp, class _Tp, class _A0>
717struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
718{
719 typedef typename _Sp<_Tp, _A0>::element_type type;
720};
721
722template <template <class, class> class _Sp, class _Tp, class _A0>
723struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
724{
725 typedef _Tp type;
726};
727
728template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
729struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
730{
731 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
732};
733
734template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
735struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
736{
737 typedef _Tp type;
738};
739
740template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
741 class _A1, class _A2>
742struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
743{
744 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
745};
746
747template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
748 class _A1, class _A2>
749struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
750{
751 typedef _Tp type;
752};
753
Howard Hinnant324bb032010-08-22 00:02:43 +0000754#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000755
756template <class _Tp>
757struct __has_difference_type
758{
759private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000760 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000761 template <class _Up> static __two __test(...);
762 template <class _Up> static char __test(typename _Up::difference_type* = 0);
763public:
764 static const bool value = sizeof(__test<_Tp>(0)) == 1;
765};
766
767template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
768struct __pointer_traits_difference_type
769{
770 typedef ptrdiff_t type;
771};
772
773template <class _Ptr>
774struct __pointer_traits_difference_type<_Ptr, true>
775{
776 typedef typename _Ptr::difference_type type;
777};
778
779template <class _Tp, class _Up>
780struct __has_rebind
781{
782private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000783 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000784 template <class _Xp> static __two __test(...);
785 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
786public:
787 static const bool value = sizeof(__test<_Tp>(0)) == 1;
788};
789
790template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
791struct __pointer_traits_rebind
792{
793#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
794 typedef typename _Tp::template rebind<_Up> type;
795#else
796 typedef typename _Tp::template rebind<_Up>::other type;
797#endif
798};
799
800#ifndef _LIBCPP_HAS_NO_VARIADICS
801
802template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
803struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
804{
805#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
806 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
807#else
808 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
809#endif
810};
811
812template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
813struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
814{
815 typedef _Sp<_Up, _Args...> type;
816};
817
Howard Hinnant324bb032010-08-22 00:02:43 +0000818#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000819
820template <template <class> class _Sp, class _Tp, class _Up>
821struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
822{
823#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
824 typedef typename _Sp<_Tp>::template rebind<_Up> type;
825#else
826 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
827#endif
828};
829
830template <template <class> class _Sp, class _Tp, class _Up>
831struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
832{
833 typedef _Sp<_Up> type;
834};
835
836template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
837struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
838{
839#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
840 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
841#else
842 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
843#endif
844};
845
846template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
847struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
848{
849 typedef _Sp<_Up, _A0> type;
850};
851
852template <template <class, class, class> class _Sp, class _Tp, class _A0,
853 class _A1, class _Up>
854struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
855{
856#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
857 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
858#else
859 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
860#endif
861};
862
863template <template <class, class, class> class _Sp, class _Tp, class _A0,
864 class _A1, class _Up>
865struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
866{
867 typedef _Sp<_Up, _A0, _A1> type;
868};
869
870template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
871 class _A1, class _A2, class _Up>
872struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
873{
874#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
875 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
876#else
877 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
878#endif
879};
880
881template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
882 class _A1, class _A2, class _Up>
883struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
884{
885 typedef _Sp<_Up, _A0, _A1, _A2> type;
886};
887
Howard Hinnant324bb032010-08-22 00:02:43 +0000888#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000889
890template <class _Ptr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000891struct _LIBCPP_TYPE_VIS_ONLY pointer_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000892{
893 typedef _Ptr pointer;
894 typedef typename __pointer_traits_element_type<pointer>::type element_type;
895 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
896
897#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant6b41c602011-05-11 20:21:19 +0000898 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000899#else
900 template <class _Up> struct rebind
901 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant324bb032010-08-22 00:02:43 +0000902#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000903
904private:
905 struct __nat {};
906public:
Howard Hinnant82894812010-09-22 16:48:34 +0000907 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000908 static pointer pointer_to(typename conditional<is_void<element_type>::value,
909 __nat, element_type>::type& __r)
910 {return pointer::pointer_to(__r);}
911};
912
913template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000914struct _LIBCPP_TYPE_VIS_ONLY pointer_traits<_Tp*>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000915{
916 typedef _Tp* pointer;
917 typedef _Tp element_type;
918 typedef ptrdiff_t difference_type;
919
920#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
921 template <class _Up> using rebind = _Up*;
922#else
923 template <class _Up> struct rebind {typedef _Up* other;};
924#endif
925
926private:
927 struct __nat {};
928public:
Howard Hinnant82894812010-09-22 16:48:34 +0000929 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000930 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant1694d232011-05-28 14:41:13 +0000931 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000932 {return _VSTD::addressof(__r);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000933};
934
Eric Fiselierbb2f28e2015-08-23 02:56:05 +0000935template <class _From, class _To>
936struct __rebind_pointer {
937#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
938 typedef typename pointer_traits<_From>::template rebind<_To> type;
939#else
940 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
941#endif
942};
943
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000944// allocator_traits
945
946namespace __has_pointer_type_imp
947{
Howard Hinnant81277582013-09-21 01:45:05 +0000948 template <class _Up> static __two __test(...);
949 template <class _Up> static char __test(typename _Up::pointer* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000950}
951
952template <class _Tp>
953struct __has_pointer_type
Howard Hinnant81277582013-09-21 01:45:05 +0000954 : public integral_constant<bool, sizeof(__has_pointer_type_imp::__test<_Tp>(0)) == 1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000955{
956};
957
958namespace __pointer_type_imp
959{
960
961template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
962struct __pointer_type
963{
964 typedef typename _Dp::pointer type;
965};
966
967template <class _Tp, class _Dp>
968struct __pointer_type<_Tp, _Dp, false>
969{
970 typedef _Tp* type;
971};
972
Howard Hinnant47761072010-11-18 01:40:00 +0000973} // __pointer_type_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000974
975template <class _Tp, class _Dp>
976struct __pointer_type
977{
978 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
979};
980
981template <class _Tp>
982struct __has_const_pointer
983{
984private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000985 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000986 template <class _Up> static __two __test(...);
987 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
988public:
989 static const bool value = sizeof(__test<_Tp>(0)) == 1;
990};
991
992template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
993struct __const_pointer
994{
995 typedef typename _Alloc::const_pointer type;
996};
997
998template <class _Tp, class _Ptr, class _Alloc>
999struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1000{
1001#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1002 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1003#else
1004 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1005#endif
1006};
1007
1008template <class _Tp>
1009struct __has_void_pointer
1010{
1011private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001012 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001013 template <class _Up> static __two __test(...);
1014 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1015public:
1016 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1017};
1018
1019template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1020struct __void_pointer
1021{
1022 typedef typename _Alloc::void_pointer type;
1023};
1024
1025template <class _Ptr, class _Alloc>
1026struct __void_pointer<_Ptr, _Alloc, false>
1027{
1028#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1029 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1030#else
1031 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1032#endif
1033};
1034
1035template <class _Tp>
1036struct __has_const_void_pointer
1037{
1038private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001039 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001040 template <class _Up> static __two __test(...);
1041 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1042public:
1043 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1044};
1045
1046template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1047struct __const_void_pointer
1048{
1049 typedef typename _Alloc::const_void_pointer type;
1050};
1051
1052template <class _Ptr, class _Alloc>
1053struct __const_void_pointer<_Ptr, _Alloc, false>
1054{
1055#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1056 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1057#else
1058 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1059#endif
1060};
1061
Howard Hinnant99968442011-11-29 18:15:50 +00001062template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001063inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001064_Tp*
1065__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001066{
1067 return __p;
1068}
1069
1070template <class _Pointer>
1071inline _LIBCPP_INLINE_VISIBILITY
1072typename pointer_traits<_Pointer>::element_type*
Howard Hinnant1694d232011-05-28 14:41:13 +00001073__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001075 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001076}
1077
1078template <class _Tp>
1079struct __has_size_type
1080{
1081private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001082 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001083 template <class _Up> static __two __test(...);
1084 template <class _Up> static char __test(typename _Up::size_type* = 0);
1085public:
1086 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1087};
1088
Howard Hinnant47761072010-11-18 01:40:00 +00001089template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001090struct __size_type
1091{
Howard Hinnant47761072010-11-18 01:40:00 +00001092 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001093};
1094
Howard Hinnant47761072010-11-18 01:40:00 +00001095template <class _Alloc, class _DiffType>
1096struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001097{
1098 typedef typename _Alloc::size_type type;
1099};
1100
1101template <class _Tp>
1102struct __has_propagate_on_container_copy_assignment
1103{
1104private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001105 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001106 template <class _Up> static __two __test(...);
1107 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1108public:
1109 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1110};
1111
1112template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1113struct __propagate_on_container_copy_assignment
1114{
1115 typedef false_type type;
1116};
1117
1118template <class _Alloc>
1119struct __propagate_on_container_copy_assignment<_Alloc, true>
1120{
1121 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1122};
1123
1124template <class _Tp>
1125struct __has_propagate_on_container_move_assignment
1126{
1127private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001128 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001129 template <class _Up> static __two __test(...);
1130 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1131public:
1132 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1133};
1134
1135template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1136struct __propagate_on_container_move_assignment
1137{
1138 typedef false_type type;
1139};
1140
1141template <class _Alloc>
1142struct __propagate_on_container_move_assignment<_Alloc, true>
1143{
1144 typedef typename _Alloc::propagate_on_container_move_assignment type;
1145};
1146
1147template <class _Tp>
1148struct __has_propagate_on_container_swap
1149{
1150private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001151 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001152 template <class _Up> static __two __test(...);
1153 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1154public:
1155 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1156};
1157
1158template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1159struct __propagate_on_container_swap
1160{
1161 typedef false_type type;
1162};
1163
1164template <class _Alloc>
1165struct __propagate_on_container_swap<_Alloc, true>
1166{
1167 typedef typename _Alloc::propagate_on_container_swap type;
1168};
1169
Marshall Clowf0324bc2015-06-02 16:34:03 +00001170template <class _Tp>
1171struct __has_is_always_equal
1172{
1173private:
1174 struct __two {char __lx; char __lxx;};
1175 template <class _Up> static __two __test(...);
1176 template <class _Up> static char __test(typename _Up::is_always_equal* = 0);
1177public:
1178 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1179};
1180
1181template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1182struct __is_always_equal
1183{
1184 typedef typename _VSTD::is_empty<_Alloc>::type type;
1185};
1186
1187template <class _Alloc>
1188struct __is_always_equal<_Alloc, true>
1189{
1190 typedef typename _Alloc::is_always_equal type;
1191};
1192
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001193template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1194struct __has_rebind_other
1195{
1196private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001197 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001198 template <class _Xp> static __two __test(...);
1199 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1200public:
1201 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1202};
1203
1204template <class _Tp, class _Up>
1205struct __has_rebind_other<_Tp, _Up, false>
1206{
1207 static const bool value = false;
1208};
1209
1210template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1211struct __allocator_traits_rebind
1212{
1213 typedef typename _Tp::template rebind<_Up>::other type;
1214};
1215
1216#ifndef _LIBCPP_HAS_NO_VARIADICS
1217
1218template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1219struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1220{
1221 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1222};
1223
1224template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1225struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1226{
1227 typedef _Alloc<_Up, _Args...> type;
1228};
1229
Howard Hinnant324bb032010-08-22 00:02:43 +00001230#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001231
1232template <template <class> class _Alloc, class _Tp, class _Up>
1233struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1234{
1235 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1236};
1237
1238template <template <class> class _Alloc, class _Tp, class _Up>
1239struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1240{
1241 typedef _Alloc<_Up> type;
1242};
1243
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001244template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1245struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1246{
1247 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1248};
1249
1250template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1251struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1252{
1253 typedef _Alloc<_Up, _A0> type;
1254};
1255
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001256template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1257 class _A1, class _Up>
1258struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1259{
1260 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1261};
1262
1263template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1264 class _A1, class _Up>
1265struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1266{
1267 typedef _Alloc<_Up, _A0, _A1> type;
1268};
1269
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001270template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1271 class _A1, class _A2, class _Up>
1272struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1273{
1274 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1275};
1276
1277template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1278 class _A1, class _A2, class _Up>
1279struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1280{
1281 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1282};
1283
Howard Hinnant324bb032010-08-22 00:02:43 +00001284#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001285
1286#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1287
1288template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1289auto
1290__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1291 -> decltype(__a.allocate(__sz, __p), true_type());
1292
1293template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1294auto
1295__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1296 -> false_type;
1297
1298template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1299struct __has_allocate_hint
1300 : integral_constant<bool,
1301 is_same<
1302 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1303 declval<_SizeType>(),
1304 declval<_ConstVoidPtr>())),
1305 true_type>::value>
1306{
1307};
1308
Howard Hinnant324bb032010-08-22 00:02:43 +00001309#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001310
1311template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1312struct __has_allocate_hint
1313 : true_type
1314{
1315};
1316
Howard Hinnant324bb032010-08-22 00:02:43 +00001317#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001318
Howard Hinnant23369ee2011-07-29 21:35:53 +00001319#if !defined(_LIBCPP_HAS_NO_ADVANCED_SFINAE) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001320
1321template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnant0949eed2011-06-30 21:18:19 +00001322decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1323 _VSTD::declval<_Args>()...),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001324 true_type())
1325__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1326
1327template <class _Alloc, class _Pointer, class ..._Args>
1328false_type
1329__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1330
1331template <class _Alloc, class _Pointer, class ..._Args>
1332struct __has_construct
1333 : integral_constant<bool,
1334 is_same<
1335 decltype(__has_construct_test(declval<_Alloc>(),
1336 declval<_Pointer>(),
1337 declval<_Args>()...)),
1338 true_type>::value>
1339{
1340};
1341
1342template <class _Alloc, class _Pointer>
1343auto
1344__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1345 -> decltype(__a.destroy(__p), true_type());
1346
1347template <class _Alloc, class _Pointer>
1348auto
1349__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1350 -> false_type;
1351
1352template <class _Alloc, class _Pointer>
1353struct __has_destroy
1354 : integral_constant<bool,
1355 is_same<
1356 decltype(__has_destroy_test(declval<_Alloc>(),
1357 declval<_Pointer>())),
1358 true_type>::value>
1359{
1360};
1361
1362template <class _Alloc>
1363auto
1364__has_max_size_test(_Alloc&& __a)
1365 -> decltype(__a.max_size(), true_type());
1366
1367template <class _Alloc>
1368auto
1369__has_max_size_test(const volatile _Alloc& __a)
1370 -> false_type;
1371
1372template <class _Alloc>
1373struct __has_max_size
1374 : integral_constant<bool,
1375 is_same<
1376 decltype(__has_max_size_test(declval<_Alloc&>())),
1377 true_type>::value>
1378{
1379};
1380
1381template <class _Alloc>
1382auto
1383__has_select_on_container_copy_construction_test(_Alloc&& __a)
1384 -> decltype(__a.select_on_container_copy_construction(), true_type());
1385
1386template <class _Alloc>
1387auto
1388__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1389 -> false_type;
1390
1391template <class _Alloc>
1392struct __has_select_on_container_copy_construction
1393 : integral_constant<bool,
1394 is_same<
1395 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1396 true_type>::value>
1397{
1398};
1399
Howard Hinnant324bb032010-08-22 00:02:43 +00001400#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001401
1402#ifndef _LIBCPP_HAS_NO_VARIADICS
1403
1404template <class _Alloc, class _Pointer, class ..._Args>
1405struct __has_construct
1406 : false_type
1407{
1408};
1409
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001410#else // _LIBCPP_HAS_NO_VARIADICS
1411
1412template <class _Alloc, class _Pointer, class _Args>
1413struct __has_construct
1414 : false_type
1415{
1416};
1417
Howard Hinnant324bb032010-08-22 00:02:43 +00001418#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001419
1420template <class _Alloc, class _Pointer>
1421struct __has_destroy
1422 : false_type
1423{
1424};
1425
1426template <class _Alloc>
1427struct __has_max_size
1428 : true_type
1429{
1430};
1431
1432template <class _Alloc>
1433struct __has_select_on_container_copy_construction
1434 : false_type
1435{
1436};
1437
Howard Hinnant324bb032010-08-22 00:02:43 +00001438#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001439
Howard Hinnant47761072010-11-18 01:40:00 +00001440template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1441struct __alloc_traits_difference_type
1442{
1443 typedef typename pointer_traits<_Ptr>::difference_type type;
1444};
1445
1446template <class _Alloc, class _Ptr>
1447struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1448{
1449 typedef typename _Alloc::difference_type type;
1450};
1451
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001452template <class _Alloc>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001453struct _LIBCPP_TYPE_VIS_ONLY allocator_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001454{
1455 typedef _Alloc allocator_type;
1456 typedef typename allocator_type::value_type value_type;
1457
1458 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1459 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1460 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1461 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1462
Howard Hinnant47761072010-11-18 01:40:00 +00001463 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1464 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001465
1466 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1467 propagate_on_container_copy_assignment;
1468 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1469 propagate_on_container_move_assignment;
1470 typedef typename __propagate_on_container_swap<allocator_type>::type
1471 propagate_on_container_swap;
Marshall Clowf0324bc2015-06-02 16:34:03 +00001472 typedef typename __is_always_equal<allocator_type>::type
1473 is_always_equal;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001474
1475#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1476 template <class _Tp> using rebind_alloc =
Howard Hinnant6b41c602011-05-11 20:21:19 +00001477 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001478 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant324bb032010-08-22 00:02:43 +00001479#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001480 template <class _Tp> struct rebind_alloc
1481 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1482 template <class _Tp> struct rebind_traits
1483 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant324bb032010-08-22 00:02:43 +00001484#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001485
Howard Hinnant82894812010-09-22 16:48:34 +00001486 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001487 static pointer allocate(allocator_type& __a, size_type __n)
1488 {return __a.allocate(__n);}
Howard Hinnant82894812010-09-22 16:48:34 +00001489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001490 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1491 {return allocate(__a, __n, __hint,
1492 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1493
Howard Hinnant82894812010-09-22 16:48:34 +00001494 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001495 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001496 {__a.deallocate(__p, __n);}
1497
1498#ifndef _LIBCPP_HAS_NO_VARIADICS
1499 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001500 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001501 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3f5579f2014-11-11 19:22:33 +00001502 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001503 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001504#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001505 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001506 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001507 static void construct(allocator_type& __a, _Tp* __p)
1508 {
1509 ::new ((void*)__p) _Tp();
1510 }
1511 template <class _Tp, class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00001512 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001513 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1514 {
1515 ::new ((void*)__p) _Tp(__a0);
1516 }
1517 template <class _Tp, class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00001518 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001519 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1520 const _A1& __a1)
1521 {
1522 ::new ((void*)__p) _Tp(__a0, __a1);
1523 }
1524 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00001525 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001526 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1527 const _A1& __a1, const _A2& __a2)
1528 {
1529 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1530 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001531#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001532
1533 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001534 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001535 static void destroy(allocator_type& __a, _Tp* __p)
1536 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1537
Howard Hinnant82894812010-09-22 16:48:34 +00001538 _LIBCPP_INLINE_VISIBILITY
Marshall Clow08b4f3f2013-08-27 20:22:15 +00001539 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001540 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1541
Howard Hinnant82894812010-09-22 16:48:34 +00001542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001543 static allocator_type
1544 select_on_container_copy_construction(const allocator_type& __a)
1545 {return select_on_container_copy_construction(
1546 __has_select_on_container_copy_construction<const allocator_type>(),
1547 __a);}
1548
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001549 template <class _Ptr>
1550 _LIBCPP_INLINE_VISIBILITY
1551 static
1552 void
1553 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1554 {
1555 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1556 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1557 }
1558
1559 template <class _Tp>
1560 _LIBCPP_INLINE_VISIBILITY
1561 static
1562 typename enable_if
1563 <
1564 (is_same<allocator_type, allocator<_Tp> >::value
1565 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1566 is_trivially_move_constructible<_Tp>::value,
1567 void
1568 >::type
1569 __construct_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1570 {
1571 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clowbf0460e2015-05-31 03:13:31 +00001572 if (_Np > 0)
Marshall Clow56523ff2015-06-02 13:04:18 +00001573 {
Marshall Clowbf0460e2015-05-31 03:13:31 +00001574 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow56523ff2015-06-02 13:04:18 +00001575 __begin2 += _Np;
1576 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001577 }
1578
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001579 template <class _Iter, class _Ptr>
1580 _LIBCPP_INLINE_VISIBILITY
1581 static
1582 void
1583 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1584 {
1585 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1586 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1587 }
1588
1589 template <class _Tp>
1590 _LIBCPP_INLINE_VISIBILITY
1591 static
1592 typename enable_if
1593 <
1594 (is_same<allocator_type, allocator<_Tp> >::value
1595 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1596 is_trivially_move_constructible<_Tp>::value,
1597 void
1598 >::type
1599 __construct_range_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1600 {
1601 typedef typename remove_const<_Tp>::type _Vp;
1602 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clowbf0460e2015-05-31 03:13:31 +00001603 if (_Np > 0)
Marshall Clow56523ff2015-06-02 13:04:18 +00001604 {
Marshall Clowbf0460e2015-05-31 03:13:31 +00001605 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
Marshall Clow56523ff2015-06-02 13:04:18 +00001606 __begin2 += _Np;
1607 }
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001608 }
1609
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001610 template <class _Ptr>
1611 _LIBCPP_INLINE_VISIBILITY
1612 static
1613 void
1614 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1615 {
1616 while (__end1 != __begin1)
Howard Hinnantf619e232013-01-11 20:36:59 +00001617 {
1618 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1619 --__end2;
1620 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001621 }
1622
1623 template <class _Tp>
1624 _LIBCPP_INLINE_VISIBILITY
1625 static
1626 typename enable_if
1627 <
1628 (is_same<allocator_type, allocator<_Tp> >::value
1629 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1630 is_trivially_move_constructible<_Tp>::value,
1631 void
1632 >::type
1633 __construct_backward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
1634 {
1635 ptrdiff_t _Np = __end1 - __begin1;
1636 __end2 -= _Np;
Marshall Clowbf0460e2015-05-31 03:13:31 +00001637 if (_Np > 0)
1638 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001639 }
1640
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001641private:
1642
Howard Hinnant82894812010-09-22 16:48:34 +00001643 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001644 static pointer allocate(allocator_type& __a, size_type __n,
1645 const_void_pointer __hint, true_type)
1646 {return __a.allocate(__n, __hint);}
Howard Hinnant82894812010-09-22 16:48:34 +00001647 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001648 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnantec3773c2011-12-01 20:21:04 +00001649 const_void_pointer, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001650 {return __a.allocate(__n);}
1651
1652#ifndef _LIBCPP_HAS_NO_VARIADICS
1653 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001655 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001656 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001657 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001659 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1660 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001661 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001662 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001663#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001664
1665 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001666 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001667 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1668 {__a.destroy(__p);}
1669 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001670 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001671 static void __destroy(false_type, allocator_type&, _Tp* __p)
1672 {
1673 __p->~_Tp();
1674 }
1675
Howard Hinnant82894812010-09-22 16:48:34 +00001676 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001677 static size_type __max_size(true_type, const allocator_type& __a)
1678 {return __a.max_size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001679 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001680 static size_type __max_size(false_type, const allocator_type&)
Marshall Clow88fa03a2015-10-25 19:34:04 +00001681 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001682
Howard Hinnant82894812010-09-22 16:48:34 +00001683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001684 static allocator_type
1685 select_on_container_copy_construction(true_type, const allocator_type& __a)
1686 {return __a.select_on_container_copy_construction();}
Howard Hinnant82894812010-09-22 16:48:34 +00001687 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001688 static allocator_type
1689 select_on_container_copy_construction(false_type, const allocator_type& __a)
1690 {return __a;}
1691};
1692
Marshall Clow66302c62015-04-07 05:21:38 +00001693template <class _Traits, class _Tp>
1694struct __rebind_alloc_helper
1695{
1696#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Marshall Clow0ad232a2015-05-10 13:59:45 +00001697 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow66302c62015-04-07 05:21:38 +00001698#else
1699 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1700#endif
1701};
1702
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001703// allocator
1704
1705template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001706class _LIBCPP_TYPE_VIS_ONLY allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001707{
1708public:
1709 typedef size_t size_type;
1710 typedef ptrdiff_t difference_type;
1711 typedef _Tp* pointer;
1712 typedef const _Tp* const_pointer;
1713 typedef _Tp& reference;
1714 typedef const _Tp& const_reference;
1715 typedef _Tp value_type;
1716
Howard Hinnant18884f42011-06-02 21:38:57 +00001717 typedef true_type propagate_on_container_move_assignment;
Marshall Clowf0324bc2015-06-02 16:34:03 +00001718 typedef true_type is_always_equal;
Howard Hinnant18884f42011-06-02 21:38:57 +00001719
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001720 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1721
Howard Hinnant1694d232011-05-28 14:41:13 +00001722 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1723 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1724 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001725 {return _VSTD::addressof(__x);}
Howard Hinnant1694d232011-05-28 14:41:13 +00001726 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001727 {return _VSTD::addressof(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001728 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow4951a482016-03-03 12:04:39 +00001729 {
1730 if (__n > max_size())
1731#ifndef _LIBCPP_NO_EXCEPTIONS
1732 throw bad_alloc();
1733#else
1734 assert(!"allocator<T>::allocate::bad_alloc");
1735#endif
1736 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
1737 }
Howard Hinnant1694d232011-05-28 14:41:13 +00001738 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith73c1fce2014-06-04 19:54:15 +00001739 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant1694d232011-05-28 14:41:13 +00001740 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1741 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001742#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001743 template <class _Up, class... _Args>
1744 _LIBCPP_INLINE_VISIBILITY
1745 void
1746 construct(_Up* __p, _Args&&... __args)
1747 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001748 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001749 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001750#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001751 _LIBCPP_INLINE_VISIBILITY
1752 void
1753 construct(pointer __p)
1754 {
1755 ::new((void*)__p) _Tp();
1756 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001757# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001758
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001759 template <class _A0>
1760 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001761 void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001762 construct(pointer __p, _A0& __a0)
1763 {
1764 ::new((void*)__p) _Tp(__a0);
1765 }
1766 template <class _A0>
1767 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001768 void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001769 construct(pointer __p, const _A0& __a0)
1770 {
1771 ::new((void*)__p) _Tp(__a0);
1772 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001773# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001774 template <class _A0, class _A1>
1775 _LIBCPP_INLINE_VISIBILITY
1776 void
1777 construct(pointer __p, _A0& __a0, _A1& __a1)
1778 {
1779 ::new((void*)__p) _Tp(__a0, __a1);
1780 }
1781 template <class _A0, class _A1>
1782 _LIBCPP_INLINE_VISIBILITY
1783 void
1784 construct(pointer __p, const _A0& __a0, _A1& __a1)
1785 {
1786 ::new((void*)__p) _Tp(__a0, __a1);
1787 }
1788 template <class _A0, class _A1>
1789 _LIBCPP_INLINE_VISIBILITY
1790 void
1791 construct(pointer __p, _A0& __a0, const _A1& __a1)
1792 {
1793 ::new((void*)__p) _Tp(__a0, __a1);
1794 }
1795 template <class _A0, class _A1>
1796 _LIBCPP_INLINE_VISIBILITY
1797 void
1798 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1799 {
1800 ::new((void*)__p) _Tp(__a0, __a1);
1801 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001802#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001803 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1804};
1805
Howard Hinnant57199402012-01-02 17:56:02 +00001806template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001807class _LIBCPP_TYPE_VIS_ONLY allocator<const _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00001808{
1809public:
1810 typedef size_t size_type;
1811 typedef ptrdiff_t difference_type;
1812 typedef const _Tp* pointer;
1813 typedef const _Tp* const_pointer;
1814 typedef const _Tp& reference;
1815 typedef const _Tp& const_reference;
Howard Hinnant9360e9f2013-06-07 01:56:37 +00001816 typedef const _Tp value_type;
Howard Hinnant57199402012-01-02 17:56:02 +00001817
1818 typedef true_type propagate_on_container_move_assignment;
Marshall Clowb81d6f52015-07-01 21:23:40 +00001819 typedef true_type is_always_equal;
Howard Hinnant57199402012-01-02 17:56:02 +00001820
1821 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1822
1823 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1824 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1825 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1826 {return _VSTD::addressof(__x);}
1827 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow4951a482016-03-03 12:04:39 +00001828 {
1829 if (__n > max_size())
1830#ifndef _LIBCPP_NO_EXCEPTIONS
1831 throw bad_alloc();
1832#else
1833 assert(!"allocator<const T>::allocate::bad_alloc");
1834#endif
1835 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
1836 }
Howard Hinnant57199402012-01-02 17:56:02 +00001837 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith73c1fce2014-06-04 19:54:15 +00001838 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant57199402012-01-02 17:56:02 +00001839 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1840 {return size_type(~0) / sizeof(_Tp);}
1841#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1842 template <class _Up, class... _Args>
1843 _LIBCPP_INLINE_VISIBILITY
1844 void
1845 construct(_Up* __p, _Args&&... __args)
1846 {
1847 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1848 }
1849#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1850 _LIBCPP_INLINE_VISIBILITY
1851 void
1852 construct(pointer __p)
1853 {
1854 ::new((void*)__p) _Tp();
1855 }
1856# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001857
Howard Hinnant57199402012-01-02 17:56:02 +00001858 template <class _A0>
1859 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001860 void
Howard Hinnant57199402012-01-02 17:56:02 +00001861 construct(pointer __p, _A0& __a0)
1862 {
1863 ::new((void*)__p) _Tp(__a0);
1864 }
1865 template <class _A0>
1866 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001867 void
Howard Hinnant57199402012-01-02 17:56:02 +00001868 construct(pointer __p, const _A0& __a0)
1869 {
1870 ::new((void*)__p) _Tp(__a0);
1871 }
Howard Hinnant57199402012-01-02 17:56:02 +00001872# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1873 template <class _A0, class _A1>
1874 _LIBCPP_INLINE_VISIBILITY
1875 void
1876 construct(pointer __p, _A0& __a0, _A1& __a1)
1877 {
1878 ::new((void*)__p) _Tp(__a0, __a1);
1879 }
1880 template <class _A0, class _A1>
1881 _LIBCPP_INLINE_VISIBILITY
1882 void
1883 construct(pointer __p, const _A0& __a0, _A1& __a1)
1884 {
1885 ::new((void*)__p) _Tp(__a0, __a1);
1886 }
1887 template <class _A0, class _A1>
1888 _LIBCPP_INLINE_VISIBILITY
1889 void
1890 construct(pointer __p, _A0& __a0, const _A1& __a1)
1891 {
1892 ::new((void*)__p) _Tp(__a0, __a1);
1893 }
1894 template <class _A0, class _A1>
1895 _LIBCPP_INLINE_VISIBILITY
1896 void
1897 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1898 {
1899 ::new((void*)__p) _Tp(__a0, __a1);
1900 }
1901#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1902 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1903};
1904
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001905template <class _Tp, class _Up>
1906inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001907bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001908
1909template <class _Tp, class _Up>
1910inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001911bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001912
1913template <class _OutputIterator, class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001914class _LIBCPP_TYPE_VIS_ONLY raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001915 : public iterator<output_iterator_tag,
1916 _Tp, // purposefully not C++03
1917 ptrdiff_t, // purposefully not C++03
1918 _Tp*, // purposefully not C++03
1919 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1920{
1921private:
1922 _OutputIterator __x_;
1923public:
1924 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1925 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1926 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1927 {::new(&*__x_) _Tp(__element); return *this;}
Marshall Clow332ab912015-10-25 18:58:07 +00001928#if _LIBCPP_STD_VER >= 14
1929 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1930 {::new(&*__x_) _Tp(_VSTD::move(__element)); return *this;}
1931#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001932 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1933 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1934 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowdbaf7a02015-05-10 13:14:08 +00001935#if _LIBCPP_STD_VER >= 14
1936 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1937#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001938};
1939
1940template <class _Tp>
1941pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00001942get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001943{
1944 pair<_Tp*, ptrdiff_t> __r(0, 0);
1945 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1946 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1947 / sizeof(_Tp);
1948 if (__n > __m)
1949 __n = __m;
1950 while (__n > 0)
1951 {
1952 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1953 if (__r.first)
1954 {
1955 __r.second = __n;
1956 break;
1957 }
1958 __n /= 2;
1959 }
1960 return __r;
1961}
1962
1963template <class _Tp>
1964inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001965void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001966
1967template <class _Tp>
1968struct auto_ptr_ref
1969{
1970 _Tp* __ptr_;
1971};
1972
1973template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001974class _LIBCPP_TYPE_VIS_ONLY auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001975{
1976private:
1977 _Tp* __ptr_;
1978public:
1979 typedef _Tp element_type;
1980
1981 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1982 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1983 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1984 : __ptr_(__p.release()) {}
1985 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1986 {reset(__p.release()); return *this;}
1987 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1988 {reset(__p.release()); return *this;}
1989 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1990 {reset(__p.__ptr_); return *this;}
1991 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1992
1993 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1994 {return *__ptr_;}
1995 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1996 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1997 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1998 {
1999 _Tp* __t = __ptr_;
2000 __ptr_ = 0;
2001 return __t;
2002 }
2003 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2004 {
2005 if (__ptr_ != __p)
2006 delete __ptr_;
2007 __ptr_ = __p;
2008 }
2009
2010 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2011 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2012 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2013 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2014 {return auto_ptr<_Up>(release());}
2015};
2016
2017template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002018class _LIBCPP_TYPE_VIS_ONLY auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002019{
2020public:
2021 typedef void element_type;
2022};
2023
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002024template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
2025 typename remove_cv<_T2>::type>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00002026 bool = is_empty<_T1>::value
Eric Fiselier3a0e4302015-06-13 07:08:02 +00002027 && !__libcpp_is_final<_T1>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00002028 bool = is_empty<_T2>::value
Eric Fiselier3a0e4302015-06-13 07:08:02 +00002029 && !__libcpp_is_final<_T2>::value
Howard Hinnantd4cf2152011-12-11 20:31:33 +00002030 >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002031struct __libcpp_compressed_pair_switch;
2032
2033template <class _T1, class _T2, bool IsSame>
2034struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
2035
2036template <class _T1, class _T2, bool IsSame>
2037struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
2038
2039template <class _T1, class _T2, bool IsSame>
2040struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
2041
2042template <class _T1, class _T2>
2043struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
2044
2045template <class _T1, class _T2>
2046struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
2047
2048template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
2049class __libcpp_compressed_pair_imp;
2050
2051template <class _T1, class _T2>
2052class __libcpp_compressed_pair_imp<_T1, _T2, 0>
2053{
2054private:
2055 _T1 __first_;
2056 _T2 __second_;
2057public:
2058 typedef _T1 _T1_param;
2059 typedef _T2 _T2_param;
2060
2061 typedef typename remove_reference<_T1>::type& _T1_reference;
2062 typedef typename remove_reference<_T2>::type& _T2_reference;
2063
2064 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2065 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2066
Marshall Clowcd6ed542015-07-16 03:05:06 +00002067 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_(), __second_() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002068 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowcd6ed542015-07-16 03:05:06 +00002069 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002070 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowcd6ed542015-07-16 03:05:06 +00002071 : __first_(), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002072 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002073 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002074
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002075#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002076
2077 _LIBCPP_INLINE_VISIBILITY
2078 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2079 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2080 is_nothrow_copy_constructible<_T2>::value)
2081 : __first_(__p.first()),
2082 __second_(__p.second()) {}
2083
2084 _LIBCPP_INLINE_VISIBILITY
2085 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2086 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2087 is_nothrow_copy_assignable<_T2>::value)
2088 {
2089 __first_ = __p.first();
2090 __second_ = __p.second();
2091 return *this;
2092 }
2093
Howard Hinnant61aa6012011-07-01 19:24:36 +00002094 _LIBCPP_INLINE_VISIBILITY
2095 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002096 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2097 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002098 : __first_(_VSTD::forward<_T1>(__p.first())),
2099 __second_(_VSTD::forward<_T2>(__p.second())) {}
2100
2101 _LIBCPP_INLINE_VISIBILITY
2102 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2103 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2104 is_nothrow_move_assignable<_T2>::value)
2105 {
2106 __first_ = _VSTD::forward<_T1>(__p.first());
2107 __second_ = _VSTD::forward<_T2>(__p.second());
2108 return *this;
2109 }
2110
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002111#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002112
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002113#ifndef _LIBCPP_HAS_NO_VARIADICS
2114
2115 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2116 _LIBCPP_INLINE_VISIBILITY
2117 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2118 tuple<_Args1...> __first_args,
2119 tuple<_Args2...> __second_args,
2120 __tuple_indices<_I1...>,
2121 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002122 : __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2123 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002124 {}
2125
2126#endif // _LIBCPP_HAS_NO_VARIADICS
2127
Howard Hinnant1694d232011-05-28 14:41:13 +00002128 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2129 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002130
Howard Hinnant1694d232011-05-28 14:41:13 +00002131 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2132 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002133
2134 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002135 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002136 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002137 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002138 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002139 swap(__first_, __x.__first_);
2140 swap(__second_, __x.__second_);
2141 }
2142};
2143
2144template <class _T1, class _T2>
2145class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2146 : private _T1
2147{
2148private:
2149 _T2 __second_;
2150public:
2151 typedef _T1 _T1_param;
2152 typedef _T2 _T2_param;
2153
2154 typedef _T1& _T1_reference;
2155 typedef typename remove_reference<_T2>::type& _T2_reference;
2156
2157 typedef const _T1& _T1_const_reference;
2158 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2159
Marshall Clowcd6ed542015-07-16 03:05:06 +00002160 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __second_() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002161 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowcd6ed542015-07-16 03:05:06 +00002162 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002163 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002164 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002165 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002166 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002167
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002168#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002169
2170 _LIBCPP_INLINE_VISIBILITY
2171 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2172 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2173 is_nothrow_copy_constructible<_T2>::value)
2174 : _T1(__p.first()), __second_(__p.second()) {}
2175
2176 _LIBCPP_INLINE_VISIBILITY
2177 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2178 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2179 is_nothrow_copy_assignable<_T2>::value)
2180 {
2181 _T1::operator=(__p.first());
2182 __second_ = __p.second();
2183 return *this;
2184 }
2185
Howard Hinnant61aa6012011-07-01 19:24:36 +00002186 _LIBCPP_INLINE_VISIBILITY
2187 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002188 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2189 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002190 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002191
2192 _LIBCPP_INLINE_VISIBILITY
2193 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2194 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2195 is_nothrow_move_assignable<_T2>::value)
2196 {
2197 _T1::operator=(_VSTD::move(__p.first()));
2198 __second_ = _VSTD::forward<_T2>(__p.second());
2199 return *this;
2200 }
2201
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002202#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002203
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002204#ifndef _LIBCPP_HAS_NO_VARIADICS
2205
2206 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2207 _LIBCPP_INLINE_VISIBILITY
2208 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2209 tuple<_Args1...> __first_args,
2210 tuple<_Args2...> __second_args,
2211 __tuple_indices<_I1...>,
2212 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002213 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2214 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002215 {}
2216
2217#endif // _LIBCPP_HAS_NO_VARIADICS
2218
Howard Hinnant1694d232011-05-28 14:41:13 +00002219 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2220 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002221
Howard Hinnant1694d232011-05-28 14:41:13 +00002222 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2223 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002224
2225 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002226 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002227 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002228 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002229 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002230 swap(__second_, __x.__second_);
2231 }
2232};
2233
2234template <class _T1, class _T2>
2235class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2236 : private _T2
2237{
2238private:
2239 _T1 __first_;
2240public:
2241 typedef _T1 _T1_param;
2242 typedef _T2 _T2_param;
2243
2244 typedef typename remove_reference<_T1>::type& _T1_reference;
2245 typedef _T2& _T2_reference;
2246
2247 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2248 typedef const _T2& _T2_const_reference;
2249
Marshall Clowcd6ed542015-07-16 03:05:06 +00002250 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002251 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002252 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002253 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowcd6ed542015-07-16 03:05:06 +00002254 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002255 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00002256 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2257 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002258 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002259
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002260#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002261
2262 _LIBCPP_INLINE_VISIBILITY
2263 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2264 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2265 is_nothrow_copy_constructible<_T2>::value)
2266 : _T2(__p.second()), __first_(__p.first()) {}
2267
2268 _LIBCPP_INLINE_VISIBILITY
2269 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2270 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2271 is_nothrow_copy_assignable<_T2>::value)
2272 {
2273 _T2::operator=(__p.second());
2274 __first_ = __p.first();
2275 return *this;
2276 }
2277
Howard Hinnant61aa6012011-07-01 19:24:36 +00002278 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002279 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002280 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2281 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002282 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002283
2284 _LIBCPP_INLINE_VISIBILITY
2285 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2286 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2287 is_nothrow_move_assignable<_T2>::value)
2288 {
2289 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2290 __first_ = _VSTD::move(__p.first());
2291 return *this;
2292 }
2293
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002294#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002295
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002296#ifndef _LIBCPP_HAS_NO_VARIADICS
2297
2298 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2299 _LIBCPP_INLINE_VISIBILITY
2300 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2301 tuple<_Args1...> __first_args,
2302 tuple<_Args2...> __second_args,
2303 __tuple_indices<_I1...>,
2304 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002305 : _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...),
2306 __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002307
2308 {}
2309
2310#endif // _LIBCPP_HAS_NO_VARIADICS
2311
Howard Hinnant1694d232011-05-28 14:41:13 +00002312 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2313 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002314
Howard Hinnant1694d232011-05-28 14:41:13 +00002315 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2316 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002317
2318 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002319 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002320 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002321 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002322 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002323 swap(__first_, __x.__first_);
2324 }
2325};
2326
2327template <class _T1, class _T2>
2328class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2329 : private _T1,
2330 private _T2
2331{
2332public:
2333 typedef _T1 _T1_param;
2334 typedef _T2 _T2_param;
2335
2336 typedef _T1& _T1_reference;
2337 typedef _T2& _T2_reference;
2338
2339 typedef const _T1& _T1_const_reference;
2340 typedef const _T2& _T2_const_reference;
2341
2342 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2343 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002344 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002345 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002346 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002347 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002348 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002349
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002350#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002351
2352 _LIBCPP_INLINE_VISIBILITY
2353 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2354 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2355 is_nothrow_copy_constructible<_T2>::value)
2356 : _T1(__p.first()), _T2(__p.second()) {}
2357
2358 _LIBCPP_INLINE_VISIBILITY
2359 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2360 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2361 is_nothrow_copy_assignable<_T2>::value)
2362 {
2363 _T1::operator=(__p.first());
2364 _T2::operator=(__p.second());
2365 return *this;
2366 }
2367
Howard Hinnant61aa6012011-07-01 19:24:36 +00002368 _LIBCPP_INLINE_VISIBILITY
2369 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002370 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2371 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002372 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002373
2374 _LIBCPP_INLINE_VISIBILITY
2375 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2376 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2377 is_nothrow_move_assignable<_T2>::value)
2378 {
2379 _T1::operator=(_VSTD::move(__p.first()));
2380 _T2::operator=(_VSTD::move(__p.second()));
2381 return *this;
2382 }
2383
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002384#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002385
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002386#ifndef _LIBCPP_HAS_NO_VARIADICS
2387
2388 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2389 _LIBCPP_INLINE_VISIBILITY
2390 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2391 tuple<_Args1...> __first_args,
2392 tuple<_Args2...> __second_args,
2393 __tuple_indices<_I1...>,
2394 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002395 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2396 _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002397 {}
2398
2399#endif // _LIBCPP_HAS_NO_VARIADICS
2400
Howard Hinnant1694d232011-05-28 14:41:13 +00002401 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2402 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002403
Howard Hinnant1694d232011-05-28 14:41:13 +00002404 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2405 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002406
Howard Hinnantec3773c2011-12-01 20:21:04 +00002407 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant1694d232011-05-28 14:41:13 +00002408 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002409 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002410 {
2411 }
2412};
2413
2414template <class _T1, class _T2>
2415class __compressed_pair
2416 : private __libcpp_compressed_pair_imp<_T1, _T2>
2417{
2418 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2419public:
2420 typedef typename base::_T1_param _T1_param;
2421 typedef typename base::_T2_param _T2_param;
2422
2423 typedef typename base::_T1_reference _T1_reference;
2424 typedef typename base::_T2_reference _T2_reference;
2425
2426 typedef typename base::_T1_const_reference _T1_const_reference;
2427 typedef typename base::_T2_const_reference _T2_const_reference;
2428
2429 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002430 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002431 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002432 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002433 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002434 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002435 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002436
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002437#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002438
2439 _LIBCPP_INLINE_VISIBILITY
2440 __compressed_pair(const __compressed_pair& __p)
2441 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2442 is_nothrow_copy_constructible<_T2>::value)
2443 : base(__p) {}
2444
2445 _LIBCPP_INLINE_VISIBILITY
2446 __compressed_pair& operator=(const __compressed_pair& __p)
2447 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2448 is_nothrow_copy_assignable<_T2>::value)
2449 {
2450 base::operator=(__p);
2451 return *this;
2452 }
2453
Howard Hinnant61aa6012011-07-01 19:24:36 +00002454 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002455 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002456 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2457 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002458 : base(_VSTD::move(__p)) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002459
2460 _LIBCPP_INLINE_VISIBILITY
2461 __compressed_pair& operator=(__compressed_pair&& __p)
2462 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2463 is_nothrow_move_assignable<_T2>::value)
2464 {
2465 base::operator=(_VSTD::move(__p));
2466 return *this;
2467 }
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002468
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002469#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002470
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002471#ifndef _LIBCPP_HAS_NO_VARIADICS
2472
2473 template <class... _Args1, class... _Args2>
2474 _LIBCPP_INLINE_VISIBILITY
2475 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2476 tuple<_Args2...> __second_args)
Howard Hinnant57199402012-01-02 17:56:02 +00002477 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002478 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2479 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2480 {}
2481
2482#endif // _LIBCPP_HAS_NO_VARIADICS
2483
Howard Hinnant1694d232011-05-28 14:41:13 +00002484 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2485 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002486
Howard Hinnant1694d232011-05-28 14:41:13 +00002487 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2488 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002489
Howard Hinnant1694d232011-05-28 14:41:13 +00002490 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2491 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002492 __is_nothrow_swappable<_T2>::value)
Howard Hinnant1694d232011-05-28 14:41:13 +00002493 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002494};
2495
2496template <class _T1, class _T2>
2497inline _LIBCPP_INLINE_VISIBILITY
2498void
2499swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002500 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002501 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002502 {__x.swap(__y);}
2503
Howard Hinnant57199402012-01-02 17:56:02 +00002504// __same_or_less_cv_qualified
2505
2506template <class _Ptr1, class _Ptr2,
2507 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2508 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2509 >::value
2510 >
2511struct __same_or_less_cv_qualified_imp
2512 : is_convertible<_Ptr1, _Ptr2> {};
2513
2514template <class _Ptr1, class _Ptr2>
2515struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2516 : false_type {};
2517
Marshall Clow5f64a2b2014-04-26 05:19:48 +00002518template <class _Ptr1, class _Ptr2, bool = is_pointer<_Ptr1>::value ||
2519 is_same<_Ptr1, _Ptr2>::value ||
2520 __has_element_type<_Ptr1>::value>
Howard Hinnant57199402012-01-02 17:56:02 +00002521struct __same_or_less_cv_qualified
2522 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2523
2524template <class _Ptr1, class _Ptr2>
Marshall Clow5f64a2b2014-04-26 05:19:48 +00002525struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, false>
Howard Hinnant57199402012-01-02 17:56:02 +00002526 : false_type {};
2527
2528// default_delete
2529
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002530template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002531struct _LIBCPP_TYPE_VIS_ONLY default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002532{
Howard Hinnant46e94932012-07-07 20:56:04 +00002533#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2534 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2535#else
2536 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2537#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002538 template <class _Up>
2539 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002540 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2541 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002542 {
2543 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant05e7d242013-04-24 19:44:26 +00002544 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002545 delete __ptr;
2546 }
2547};
2548
2549template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002550struct _LIBCPP_TYPE_VIS_ONLY default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002551{
Howard Hinnant8e843502011-12-18 21:19:44 +00002552public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002553#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2554 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2555#else
2556 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2557#endif
Howard Hinnant8e843502011-12-18 21:19:44 +00002558 template <class _Up>
2559 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant57199402012-01-02 17:56:02 +00002560 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant8e843502011-12-18 21:19:44 +00002561 template <class _Up>
2562 _LIBCPP_INLINE_VISIBILITY
2563 void operator() (_Up* __ptr,
Howard Hinnant57199402012-01-02 17:56:02 +00002564 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002565 {
2566 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Marshall Clow61d4dd02016-02-25 16:50:51 +00002567 static_assert(!is_void<_Tp>::value, "default_delete can not delete void type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002568 delete [] __ptr;
2569 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002570};
2571
2572template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002573class _LIBCPP_TYPE_VIS_ONLY unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002574{
2575public:
2576 typedef _Tp element_type;
2577 typedef _Dp deleter_type;
2578 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2579private:
2580 __compressed_pair<pointer, deleter_type> __ptr_;
2581
Howard Hinnant57199402012-01-02 17:56:02 +00002582#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002583 unique_ptr(unique_ptr&);
2584 template <class _Up, class _Ep>
2585 unique_ptr(unique_ptr<_Up, _Ep>&);
2586 unique_ptr& operator=(unique_ptr&);
2587 template <class _Up, class _Ep>
2588 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002589#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002590
2591 struct __nat {int __for_bool_;};
2592
2593 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2594 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2595public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002596 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002597 : __ptr_(pointer())
2598 {
2599 static_assert(!is_pointer<deleter_type>::value,
2600 "unique_ptr constructed with null function pointer deleter");
2601 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002602 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002603 : __ptr_(pointer())
2604 {
2605 static_assert(!is_pointer<deleter_type>::value,
2606 "unique_ptr constructed with null function pointer deleter");
2607 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002608 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002609 : __ptr_(_VSTD::move(__p))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002610 {
2611 static_assert(!is_pointer<deleter_type>::value,
2612 "unique_ptr constructed with null function pointer deleter");
2613 }
2614
Howard Hinnant73d21a42010-09-04 23:28:19 +00002615#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002616 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2617 is_reference<deleter_type>::value,
2618 deleter_type,
2619 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002620 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002621 : __ptr_(__p, __d) {}
2622
2623 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002624 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002625 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002626 {
2627 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2628 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002629 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002630 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002631 template <class _Up, class _Ep>
2632 _LIBCPP_INLINE_VISIBILITY
2633 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2634 typename enable_if
2635 <
2636 !is_array<_Up>::value &&
2637 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2638 is_convertible<_Ep, deleter_type>::value &&
2639 (
2640 !is_reference<deleter_type>::value ||
2641 is_same<deleter_type, _Ep>::value
2642 ),
2643 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002644 >::type = __nat()) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002645 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002646
2647 template <class _Up>
2648 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2649 typename enable_if<
2650 is_convertible<_Up*, _Tp*>::value &&
2651 is_same<_Dp, default_delete<_Tp> >::value,
2652 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002653 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002654 : __ptr_(__p.release())
2655 {
2656 }
2657
Howard Hinnant1694d232011-05-28 14:41:13 +00002658 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002659 {
2660 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002661 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002662 return *this;
2663 }
2664
2665 template <class _Up, class _Ep>
2666 _LIBCPP_INLINE_VISIBILITY
2667 typename enable_if
2668 <
Howard Hinnant57199402012-01-02 17:56:02 +00002669 !is_array<_Up>::value &&
2670 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2671 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002672 unique_ptr&
2673 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002674 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002675 {
2676 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002677 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002678 return *this;
2679 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002680#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002681
2682 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2683 {
2684 return __rv<unique_ptr>(*this);
2685 }
2686
2687 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002688 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002689
2690 template <class _Up, class _Ep>
Eric Fiselieraff153a2015-08-28 05:07:06 +00002691 _LIBCPP_INLINE_VISIBILITY
2692 typename enable_if<
2693 !is_array<_Up>::value &&
2694 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2695 is_assignable<deleter_type&, _Ep&>::value,
2696 unique_ptr&
2697 >::type
2698 operator=(unique_ptr<_Up, _Ep> __u)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002699 {
2700 reset(__u.release());
Eric Fiselieraff153a2015-08-28 05:07:06 +00002701 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002702 return *this;
2703 }
2704
2705 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002706 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002707
2708 template <class _Up>
2709 _LIBCPP_INLINE_VISIBILITY
2710 typename enable_if<
2711 is_convertible<_Up*, _Tp*>::value &&
2712 is_same<_Dp, default_delete<_Tp> >::value,
2713 unique_ptr&
2714 >::type
2715 operator=(auto_ptr<_Up> __p)
2716 {reset(__p.release()); return *this;}
2717
Howard Hinnant73d21a42010-09-04 23:28:19 +00002718#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002719 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2720
Howard Hinnant1694d232011-05-28 14:41:13 +00002721 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002722 {
2723 reset();
2724 return *this;
2725 }
2726
2727 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2728 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002729 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2730 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2731 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2732 {return __ptr_.second();}
2733 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2734 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002735 _LIBCPP_INLINE_VISIBILITY
2736 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2737 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002738
Howard Hinnant1694d232011-05-28 14:41:13 +00002739 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002740 {
2741 pointer __t = __ptr_.first();
2742 __ptr_.first() = pointer();
2743 return __t;
2744 }
2745
Howard Hinnant1694d232011-05-28 14:41:13 +00002746 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002747 {
2748 pointer __tmp = __ptr_.first();
2749 __ptr_.first() = __p;
2750 if (__tmp)
2751 __ptr_.second()(__tmp);
2752 }
2753
Howard Hinnant1694d232011-05-28 14:41:13 +00002754 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2755 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002756};
2757
2758template <class _Tp, class _Dp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002759class _LIBCPP_TYPE_VIS_ONLY unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002760{
2761public:
2762 typedef _Tp element_type;
2763 typedef _Dp deleter_type;
2764 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2765private:
2766 __compressed_pair<pointer, deleter_type> __ptr_;
2767
Howard Hinnant57199402012-01-02 17:56:02 +00002768#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002769 unique_ptr(unique_ptr&);
2770 template <class _Up>
2771 unique_ptr(unique_ptr<_Up>&);
2772 unique_ptr& operator=(unique_ptr&);
2773 template <class _Up>
2774 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002775#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002776
2777 struct __nat {int __for_bool_;};
2778
2779 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2780 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2781public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002782 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002783 : __ptr_(pointer())
2784 {
2785 static_assert(!is_pointer<deleter_type>::value,
2786 "unique_ptr constructed with null function pointer deleter");
2787 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002788 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002789 : __ptr_(pointer())
2790 {
2791 static_assert(!is_pointer<deleter_type>::value,
2792 "unique_ptr constructed with null function pointer deleter");
2793 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002794#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00002795 template <class _Pp>
2796 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p,
2797 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002798 : __ptr_(__p)
2799 {
2800 static_assert(!is_pointer<deleter_type>::value,
2801 "unique_ptr constructed with null function pointer deleter");
2802 }
2803
Logan Chiene1678a12014-01-31 09:30:46 +00002804 template <class _Pp>
Howard Hinnant99968442011-11-29 18:15:50 +00002805 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002806 is_reference<deleter_type>::value,
2807 deleter_type,
Logan Chiene1678a12014-01-31 09:30:46 +00002808 typename add_lvalue_reference<const deleter_type>::type>::type __d,
2809 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant1694d232011-05-28 14:41:13 +00002810 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002811 : __ptr_(__p, __d) {}
2812
2813 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2814 is_reference<deleter_type>::value,
2815 deleter_type,
2816 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002817 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002818 : __ptr_(pointer(), __d) {}
2819
Logan Chiene1678a12014-01-31 09:30:46 +00002820 template <class _Pp>
2821 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p,
2822 typename remove_reference<deleter_type>::type&& __d,
2823 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant1694d232011-05-28 14:41:13 +00002824 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002825 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002826 {
2827 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2828 }
2829
2830 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002831 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002832 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002833 {
2834 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2835 }
2836
Howard Hinnant1694d232011-05-28 14:41:13 +00002837 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002838 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002839
Howard Hinnant1694d232011-05-28 14:41:13 +00002840 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002841 {
2842 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002843 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002844 return *this;
2845 }
Howard Hinnant8e843502011-12-18 21:19:44 +00002846
2847 template <class _Up, class _Ep>
2848 _LIBCPP_INLINE_VISIBILITY
2849 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2850 typename enable_if
2851 <
2852 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002853 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant8e843502011-12-18 21:19:44 +00002854 && is_convertible<_Ep, deleter_type>::value &&
2855 (
2856 !is_reference<deleter_type>::value ||
2857 is_same<deleter_type, _Ep>::value
2858 ),
2859 __nat
2860 >::type = __nat()
2861 ) _NOEXCEPT
2862 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2863
2864
2865 template <class _Up, class _Ep>
2866 _LIBCPP_INLINE_VISIBILITY
2867 typename enable_if
2868 <
2869 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002870 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2871 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant8e843502011-12-18 21:19:44 +00002872 unique_ptr&
2873 >::type
2874 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2875 {
2876 reset(__u.release());
2877 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2878 return *this;
2879 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002880#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002881
2882 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2883 : __ptr_(__p)
2884 {
2885 static_assert(!is_pointer<deleter_type>::value,
2886 "unique_ptr constructed with null function pointer deleter");
2887 }
2888
2889 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002890 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002891
2892 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002893 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002894
2895 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2896 {
2897 return __rv<unique_ptr>(*this);
2898 }
2899
2900 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002901 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002902
2903 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2904 {
2905 reset(__u->release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002906 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002907 return *this;
2908 }
2909
Howard Hinnant73d21a42010-09-04 23:28:19 +00002910#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002911 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2912
Howard Hinnant1694d232011-05-28 14:41:13 +00002913 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002914 {
2915 reset();
2916 return *this;
2917 }
2918
2919 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2920 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002921 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2922 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2923 {return __ptr_.second();}
2924 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2925 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002926 _LIBCPP_INLINE_VISIBILITY
2927 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2928 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002929
Howard Hinnant1694d232011-05-28 14:41:13 +00002930 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002931 {
2932 pointer __t = __ptr_.first();
2933 __ptr_.first() = pointer();
2934 return __t;
2935 }
2936
Logan Chiene1678a12014-01-31 09:30:46 +00002937 template <class _Pp>
2938 _LIBCPP_INLINE_VISIBILITY
2939 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, void>::type
2940 reset(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002941 {
2942 pointer __tmp = __ptr_.first();
2943 __ptr_.first() = __p;
2944 if (__tmp)
2945 __ptr_.second()(__tmp);
2946 }
Marshall Clow61d4dd02016-02-25 16:50:51 +00002947 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t = nullptr) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002948 {
2949 pointer __tmp = __ptr_.first();
2950 __ptr_.first() = nullptr;
2951 if (__tmp)
2952 __ptr_.second()(__tmp);
2953 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002954
2955 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2956private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002957
Howard Hinnant73d21a42010-09-04 23:28:19 +00002958#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002959 template <class _Up>
2960 explicit unique_ptr(_Up);
2961 template <class _Up>
2962 unique_ptr(_Up __u,
2963 typename conditional<
2964 is_reference<deleter_type>::value,
2965 deleter_type,
2966 typename add_lvalue_reference<const deleter_type>::type>::type,
2967 typename enable_if
2968 <
2969 is_convertible<_Up, pointer>::value,
2970 __nat
2971 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002972#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002973};
2974
2975template <class _Tp, class _Dp>
2976inline _LIBCPP_INLINE_VISIBILITY
2977void
Howard Hinnant1694d232011-05-28 14:41:13 +00002978swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002979
2980template <class _T1, class _D1, class _T2, class _D2>
2981inline _LIBCPP_INLINE_VISIBILITY
2982bool
2983operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2984
2985template <class _T1, class _D1, class _T2, class _D2>
2986inline _LIBCPP_INLINE_VISIBILITY
2987bool
2988operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2989
2990template <class _T1, class _D1, class _T2, class _D2>
2991inline _LIBCPP_INLINE_VISIBILITY
2992bool
Howard Hinnant3fadda32012-02-21 21:02:58 +00002993operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2994{
2995 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2996 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselier8492cd82015-02-05 23:01:40 +00002997 typedef typename common_type<_P1, _P2>::type _Vp;
2998 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnant3fadda32012-02-21 21:02:58 +00002999}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003000
3001template <class _T1, class _D1, class _T2, class _D2>
3002inline _LIBCPP_INLINE_VISIBILITY
3003bool
3004operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
3005
3006template <class _T1, class _D1, class _T2, class _D2>
3007inline _LIBCPP_INLINE_VISIBILITY
3008bool
3009operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
3010
3011template <class _T1, class _D1, class _T2, class _D2>
3012inline _LIBCPP_INLINE_VISIBILITY
3013bool
3014operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
3015
Howard Hinnant3fadda32012-02-21 21:02:58 +00003016template <class _T1, class _D1>
3017inline _LIBCPP_INLINE_VISIBILITY
3018bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003019operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003020{
3021 return !__x;
3022}
3023
3024template <class _T1, class _D1>
3025inline _LIBCPP_INLINE_VISIBILITY
3026bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003027operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003028{
3029 return !__x;
3030}
3031
3032template <class _T1, class _D1>
3033inline _LIBCPP_INLINE_VISIBILITY
3034bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003035operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003036{
3037 return static_cast<bool>(__x);
3038}
3039
3040template <class _T1, class _D1>
3041inline _LIBCPP_INLINE_VISIBILITY
3042bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003043operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003044{
3045 return static_cast<bool>(__x);
3046}
3047
3048template <class _T1, class _D1>
3049inline _LIBCPP_INLINE_VISIBILITY
3050bool
3051operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3052{
3053 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3054 return less<_P1>()(__x.get(), nullptr);
3055}
3056
3057template <class _T1, class _D1>
3058inline _LIBCPP_INLINE_VISIBILITY
3059bool
3060operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3061{
3062 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3063 return less<_P1>()(nullptr, __x.get());
3064}
3065
3066template <class _T1, class _D1>
3067inline _LIBCPP_INLINE_VISIBILITY
3068bool
3069operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3070{
3071 return nullptr < __x;
3072}
3073
3074template <class _T1, class _D1>
3075inline _LIBCPP_INLINE_VISIBILITY
3076bool
3077operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3078{
3079 return __x < nullptr;
3080}
3081
3082template <class _T1, class _D1>
3083inline _LIBCPP_INLINE_VISIBILITY
3084bool
3085operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3086{
3087 return !(nullptr < __x);
3088}
3089
3090template <class _T1, class _D1>
3091inline _LIBCPP_INLINE_VISIBILITY
3092bool
3093operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3094{
3095 return !(__x < nullptr);
3096}
3097
3098template <class _T1, class _D1>
3099inline _LIBCPP_INLINE_VISIBILITY
3100bool
3101operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3102{
3103 return !(__x < nullptr);
3104}
3105
3106template <class _T1, class _D1>
3107inline _LIBCPP_INLINE_VISIBILITY
3108bool
3109operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3110{
3111 return !(nullptr < __x);
3112}
3113
Howard Hinnant87073e42012-05-01 15:37:54 +00003114#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3115
3116template <class _Tp, class _Dp>
3117inline _LIBCPP_INLINE_VISIBILITY
3118unique_ptr<_Tp, _Dp>
3119move(unique_ptr<_Tp, _Dp>& __t)
3120{
3121 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3122}
3123
3124#endif
3125
Marshall Clowfd7481e2013-07-01 18:16:03 +00003126#if _LIBCPP_STD_VER > 11
3127
3128template<class _Tp>
3129struct __unique_if
3130{
3131 typedef unique_ptr<_Tp> __unique_single;
3132};
3133
3134template<class _Tp>
3135struct __unique_if<_Tp[]>
3136{
3137 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3138};
3139
3140template<class _Tp, size_t _Np>
3141struct __unique_if<_Tp[_Np]>
3142{
3143 typedef void __unique_array_known_bound;
3144};
3145
3146template<class _Tp, class... _Args>
Marshall Clowfb551102013-07-02 20:06:09 +00003147inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003148typename __unique_if<_Tp>::__unique_single
3149make_unique(_Args&&... __args)
3150{
3151 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3152}
3153
3154template<class _Tp>
Marshall Clowfb551102013-07-02 20:06:09 +00003155inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003156typename __unique_if<_Tp>::__unique_array_unknown_bound
3157make_unique(size_t __n)
3158{
3159 typedef typename remove_extent<_Tp>::type _Up;
3160 return unique_ptr<_Tp>(new _Up[__n]());
3161}
3162
3163template<class _Tp, class... _Args>
3164 typename __unique_if<_Tp>::__unique_array_known_bound
3165 make_unique(_Args&&...) = delete;
3166
3167#endif // _LIBCPP_STD_VER > 11
3168
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003169template <class _Size>
3170inline _LIBCPP_INLINE_VISIBILITY
3171_Size
3172__loadword(const void* __p)
3173{
3174 _Size __r;
3175 std::memcpy(&__r, __p, sizeof(__r));
3176 return __r;
3177}
3178
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003179// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
3180// is 64 bits. This is because cityhash64 uses 64bit x 64bit
3181// multiplication, which can be very slow on 32-bit systems.
Howard Hinnant40c13d32011-12-05 00:08:45 +00003182template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003183struct __murmur2_or_cityhash;
Howard Hinnant40c13d32011-12-05 00:08:45 +00003184
3185template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003186struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003187{
3188 _Size operator()(const void* __key, _Size __len);
3189};
3190
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003191// murmur2
Howard Hinnant40c13d32011-12-05 00:08:45 +00003192template <class _Size>
3193_Size
Marshall Clow7a3731f2016-01-11 19:27:10 +00003194__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnant40c13d32011-12-05 00:08:45 +00003195{
3196 const _Size __m = 0x5bd1e995;
3197 const _Size __r = 24;
3198 _Size __h = __len;
3199 const unsigned char* __data = static_cast<const unsigned char*>(__key);
3200 for (; __len >= 4; __data += 4, __len -= 4)
3201 {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003202 _Size __k = __loadword<_Size>(__data);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003203 __k *= __m;
3204 __k ^= __k >> __r;
3205 __k *= __m;
3206 __h *= __m;
3207 __h ^= __k;
3208 }
3209 switch (__len)
3210 {
3211 case 3:
3212 __h ^= __data[2] << 16;
3213 case 2:
3214 __h ^= __data[1] << 8;
3215 case 1:
3216 __h ^= __data[0];
3217 __h *= __m;
3218 }
3219 __h ^= __h >> 13;
3220 __h *= __m;
3221 __h ^= __h >> 15;
3222 return __h;
3223}
3224
3225template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003226struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003227{
3228 _Size operator()(const void* __key, _Size __len);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003229
3230 private:
3231 // Some primes between 2^63 and 2^64.
3232 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
3233 static const _Size __k1 = 0xb492b66fbe98f273ULL;
3234 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
3235 static const _Size __k3 = 0xc949d7c7509e6557ULL;
3236
3237 static _Size __rotate(_Size __val, int __shift) {
3238 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
3239 }
3240
3241 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
3242 return (__val >> __shift) | (__val << (64 - __shift));
3243 }
3244
3245 static _Size __shift_mix(_Size __val) {
3246 return __val ^ (__val >> 47);
3247 }
3248
3249 static _Size __hash_len_16(_Size __u, _Size __v) {
3250 const _Size __mul = 0x9ddfea08eb382d69ULL;
3251 _Size __a = (__u ^ __v) * __mul;
3252 __a ^= (__a >> 47);
3253 _Size __b = (__v ^ __a) * __mul;
3254 __b ^= (__b >> 47);
3255 __b *= __mul;
3256 return __b;
3257 }
3258
3259 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
3260 if (__len > 8) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003261 const _Size __a = __loadword<_Size>(__s);
3262 const _Size __b = __loadword<_Size>(__s + __len - 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003263 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
3264 }
3265 if (__len >= 4) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003266 const uint32_t __a = __loadword<uint32_t>(__s);
3267 const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003268 return __hash_len_16(__len + (__a << 3), __b);
3269 }
3270 if (__len > 0) {
3271 const unsigned char __a = __s[0];
3272 const unsigned char __b = __s[__len >> 1];
3273 const unsigned char __c = __s[__len - 1];
3274 const uint32_t __y = static_cast<uint32_t>(__a) +
3275 (static_cast<uint32_t>(__b) << 8);
3276 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3277 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3278 }
3279 return __k2;
3280 }
3281
3282 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003283 const _Size __a = __loadword<_Size>(__s) * __k1;
3284 const _Size __b = __loadword<_Size>(__s + 8);
3285 const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;
3286 const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003287 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3288 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3289 }
3290
3291 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3292 // Callers do best to use "random-looking" values for a and b.
3293 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3294 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3295 __a += __w;
3296 __b = __rotate(__b + __a + __z, 21);
3297 const _Size __c = __a;
3298 __a += __x;
3299 __a += __y;
3300 __b += __rotate(__a, 44);
3301 return pair<_Size, _Size>(__a + __z, __b + __c);
3302 }
3303
3304 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3305 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3306 const char* __s, _Size __a, _Size __b) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003307 return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),
3308 __loadword<_Size>(__s + 8),
3309 __loadword<_Size>(__s + 16),
3310 __loadword<_Size>(__s + 24),
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003311 __a,
3312 __b);
3313 }
3314
3315 // Return an 8-byte hash for 33 to 64 bytes.
3316 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003317 _Size __z = __loadword<_Size>(__s + 24);
3318 _Size __a = __loadword<_Size>(__s) +
3319 (__len + __loadword<_Size>(__s + __len - 16)) * __k0;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003320 _Size __b = __rotate(__a + __z, 52);
3321 _Size __c = __rotate(__a, 37);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003322 __a += __loadword<_Size>(__s + 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003323 __c += __rotate(__a, 7);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003324 __a += __loadword<_Size>(__s + 16);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003325 _Size __vf = __a + __z;
3326 _Size __vs = __b + __rotate(__a, 31) + __c;
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003327 __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);
3328 __z += __loadword<_Size>(__s + __len - 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003329 __b = __rotate(__a + __z, 52);
3330 __c = __rotate(__a, 37);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003331 __a += __loadword<_Size>(__s + __len - 24);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003332 __c += __rotate(__a, 7);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003333 __a += __loadword<_Size>(__s + __len - 16);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003334 _Size __wf = __a + __z;
3335 _Size __ws = __b + __rotate(__a, 31) + __c;
3336 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3337 return __shift_mix(__r * __k0 + __vs) * __k2;
3338 }
Howard Hinnant40c13d32011-12-05 00:08:45 +00003339};
3340
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003341// cityhash64
Howard Hinnant40c13d32011-12-05 00:08:45 +00003342template <class _Size>
3343_Size
Marshall Clow7a3731f2016-01-11 19:27:10 +00003344__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnant40c13d32011-12-05 00:08:45 +00003345{
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003346 const char* __s = static_cast<const char*>(__key);
3347 if (__len <= 32) {
3348 if (__len <= 16) {
3349 return __hash_len_0_to_16(__s, __len);
3350 } else {
3351 return __hash_len_17_to_32(__s, __len);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003352 }
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003353 } else if (__len <= 64) {
3354 return __hash_len_33_to_64(__s, __len);
3355 }
3356
3357 // For strings over 64 bytes we hash the end first, and then as we
3358 // loop we keep 56 bytes of state: v, w, x, y, and z.
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003359 _Size __x = __loadword<_Size>(__s + __len - 40);
3360 _Size __y = __loadword<_Size>(__s + __len - 16) +
3361 __loadword<_Size>(__s + __len - 56);
3362 _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,
3363 __loadword<_Size>(__s + __len - 24));
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003364 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3365 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003366 __x = __x * __k1 + __loadword<_Size>(__s);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003367
3368 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3369 __len = (__len - 1) & ~static_cast<_Size>(63);
3370 do {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003371 __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;
3372 __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003373 __x ^= __w.second;
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003374 __y += __v.first + __loadword<_Size>(__s + 40);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003375 __z = __rotate(__z + __w.first, 33) * __k1;
3376 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3377 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003378 __y + __loadword<_Size>(__s + 16));
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003379 std::swap(__z, __x);
3380 __s += 64;
3381 __len -= 64;
3382 } while (__len != 0);
3383 return __hash_len_16(
3384 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3385 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003386}
3387
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003388template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3389struct __scalar_hash;
3390
3391template <class _Tp>
3392struct __scalar_hash<_Tp, 0>
3393 : public unary_function<_Tp, size_t>
Howard Hinnant21aefc32010-06-03 16:42:57 +00003394{
Howard Hinnant82894812010-09-22 16:48:34 +00003395 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003396 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003397 {
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003398 union
3399 {
3400 _Tp __t;
3401 size_t __a;
3402 } __u;
3403 __u.__a = 0;
3404 __u.__t = __v;
3405 return __u.__a;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003406 }
3407};
3408
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003409template <class _Tp>
3410struct __scalar_hash<_Tp, 1>
3411 : public unary_function<_Tp, size_t>
3412{
3413 _LIBCPP_INLINE_VISIBILITY
3414 size_t operator()(_Tp __v) const _NOEXCEPT
3415 {
3416 union
3417 {
3418 _Tp __t;
3419 size_t __a;
3420 } __u;
3421 __u.__t = __v;
3422 return __u.__a;
3423 }
3424};
3425
3426template <class _Tp>
3427struct __scalar_hash<_Tp, 2>
3428 : public unary_function<_Tp, size_t>
3429{
3430 _LIBCPP_INLINE_VISIBILITY
3431 size_t operator()(_Tp __v) const _NOEXCEPT
3432 {
3433 union
3434 {
3435 _Tp __t;
3436 struct
3437 {
3438 size_t __a;
3439 size_t __b;
Eric Fiselier692177d2015-07-18 20:40:46 +00003440 } __s;
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003441 } __u;
3442 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003443 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003444 }
3445};
3446
3447template <class _Tp>
3448struct __scalar_hash<_Tp, 3>
3449 : public unary_function<_Tp, size_t>
3450{
3451 _LIBCPP_INLINE_VISIBILITY
3452 size_t operator()(_Tp __v) const _NOEXCEPT
3453 {
3454 union
3455 {
3456 _Tp __t;
3457 struct
3458 {
3459 size_t __a;
3460 size_t __b;
3461 size_t __c;
Eric Fiselier692177d2015-07-18 20:40:46 +00003462 } __s;
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003463 } __u;
3464 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003465 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003466 }
3467};
3468
3469template <class _Tp>
3470struct __scalar_hash<_Tp, 4>
3471 : public unary_function<_Tp, size_t>
3472{
3473 _LIBCPP_INLINE_VISIBILITY
3474 size_t operator()(_Tp __v) const _NOEXCEPT
3475 {
3476 union
3477 {
3478 _Tp __t;
3479 struct
3480 {
3481 size_t __a;
3482 size_t __b;
3483 size_t __c;
3484 size_t __d;
Eric Fiselier692177d2015-07-18 20:40:46 +00003485 } __s;
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003486 } __u;
3487 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003488 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003489 }
3490};
3491
3492template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003493struct _LIBCPP_TYPE_VIS_ONLY hash<_Tp*>
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003494 : public unary_function<_Tp*, size_t>
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003495{
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003496 _LIBCPP_INLINE_VISIBILITY
3497 size_t operator()(_Tp* __v) const _NOEXCEPT
3498 {
3499 union
3500 {
3501 _Tp* __t;
3502 size_t __a;
3503 } __u;
3504 __u.__t = __v;
3505 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
3506 }
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003507};
3508
Howard Hinnant21aefc32010-06-03 16:42:57 +00003509template <class _Tp, class _Dp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003510struct _LIBCPP_TYPE_VIS_ONLY hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003511{
3512 typedef unique_ptr<_Tp, _Dp> argument_type;
3513 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003514 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003515 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003516 {
3517 typedef typename argument_type::pointer pointer;
3518 return hash<pointer>()(__ptr.get());
3519 }
3520};
3521
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003522struct __destruct_n
3523{
3524private:
3525 size_t size;
3526
3527 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003528 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003529 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3530
3531 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003532 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003533 {}
3534
Howard Hinnant1694d232011-05-28 14:41:13 +00003535 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003536 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003537 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003538 {}
3539
Howard Hinnant1694d232011-05-28 14:41:13 +00003540 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003541 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003542 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003543 {}
3544public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003545 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3546 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003547
3548 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003549 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003550 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003551
3552 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003553 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003554 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003555
3556 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003557 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003558 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003559};
3560
3561template <class _Alloc>
3562class __allocator_destructor
3563{
3564 typedef allocator_traits<_Alloc> __alloc_traits;
3565public:
3566 typedef typename __alloc_traits::pointer pointer;
3567 typedef typename __alloc_traits::size_type size_type;
3568private:
3569 _Alloc& __alloc_;
3570 size_type __s_;
3571public:
3572 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00003573 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003574 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003575 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003576 void operator()(pointer __p) _NOEXCEPT
3577 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003578};
3579
3580template <class _InputIterator, class _ForwardIterator>
3581_ForwardIterator
3582uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3583{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003584 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003585#ifndef _LIBCPP_NO_EXCEPTIONS
3586 _ForwardIterator __s = __r;
3587 try
3588 {
3589#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003590 for (; __f != __l; ++__f, (void) ++__r)
3591 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant8292d742011-12-29 17:45:35 +00003592#ifndef _LIBCPP_NO_EXCEPTIONS
3593 }
3594 catch (...)
3595 {
3596 for (; __s != __r; ++__s)
3597 __s->~value_type();
3598 throw;
3599 }
3600#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003601 return __r;
3602}
3603
3604template <class _InputIterator, class _Size, class _ForwardIterator>
3605_ForwardIterator
3606uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3607{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003608 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003609#ifndef _LIBCPP_NO_EXCEPTIONS
3610 _ForwardIterator __s = __r;
3611 try
3612 {
3613#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003614 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3615 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant8292d742011-12-29 17:45:35 +00003616#ifndef _LIBCPP_NO_EXCEPTIONS
3617 }
3618 catch (...)
3619 {
3620 for (; __s != __r; ++__s)
3621 __s->~value_type();
3622 throw;
3623 }
3624#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003625 return __r;
3626}
3627
3628template <class _ForwardIterator, class _Tp>
3629void
3630uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3631{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003632 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003633#ifndef _LIBCPP_NO_EXCEPTIONS
3634 _ForwardIterator __s = __f;
3635 try
3636 {
3637#endif
3638 for (; __f != __l; ++__f)
Marshall Clow5dce73d2015-05-19 15:01:48 +00003639 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant8292d742011-12-29 17:45:35 +00003640#ifndef _LIBCPP_NO_EXCEPTIONS
3641 }
3642 catch (...)
3643 {
3644 for (; __s != __f; ++__s)
3645 __s->~value_type();
3646 throw;
3647 }
3648#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003649}
3650
3651template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003652_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003653uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3654{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003655 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003656#ifndef _LIBCPP_NO_EXCEPTIONS
3657 _ForwardIterator __s = __f;
3658 try
3659 {
3660#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003661 for (; __n > 0; ++__f, (void) --__n)
3662 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant8292d742011-12-29 17:45:35 +00003663#ifndef _LIBCPP_NO_EXCEPTIONS
3664 }
3665 catch (...)
3666 {
3667 for (; __s != __f; ++__s)
3668 __s->~value_type();
3669 throw;
3670 }
3671#endif
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003672 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003673}
3674
Howard Hinnant82894812010-09-22 16:48:34 +00003675class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003676 : public std::exception
3677{
3678public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003679 virtual ~bad_weak_ptr() _NOEXCEPT;
3680 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003681};
3682
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003683template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003684
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003685class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003686{
3687 __shared_count(const __shared_count&);
3688 __shared_count& operator=(const __shared_count&);
3689
3690protected:
3691 long __shared_owners_;
3692 virtual ~__shared_count();
3693private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003694 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003695
3696public:
Howard Hinnant82894812010-09-22 16:48:34 +00003697 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003698 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003699 : __shared_owners_(__refs) {}
3700
Howard Hinnant1694d232011-05-28 14:41:13 +00003701 void __add_shared() _NOEXCEPT;
3702 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003703 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc6e46692015-07-07 00:27:16 +00003704 long use_count() const _NOEXCEPT {
3705 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3706 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003707};
3708
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003709class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003710 : private __shared_count
3711{
3712 long __shared_weak_owners_;
3713
3714public:
Howard Hinnant82894812010-09-22 16:48:34 +00003715 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003716 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003717 : __shared_count(__refs),
3718 __shared_weak_owners_(__refs) {}
3719protected:
3720 virtual ~__shared_weak_count();
3721
3722public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003723 void __add_shared() _NOEXCEPT;
3724 void __add_weak() _NOEXCEPT;
3725 void __release_shared() _NOEXCEPT;
3726 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003728 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3729 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003730
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003731 // Define the function out only if we build static libc++ without RTTI.
3732 // Otherwise we may break clients who need to compile their projects with
3733 // -fno-rtti and yet link against a libc++.dylib compiled
3734 // without -fno-rtti.
3735#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant1694d232011-05-28 14:41:13 +00003736 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003737#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003738private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003739 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003740};
3741
3742template <class _Tp, class _Dp, class _Alloc>
3743class __shared_ptr_pointer
3744 : public __shared_weak_count
3745{
3746 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3747public:
Howard Hinnant82894812010-09-22 16:48:34 +00003748 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003749 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003750 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003751
Howard Hinnantd4444702010-08-11 17:04:31 +00003752#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00003753 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00003754#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003755
3756private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003757 virtual void __on_zero_shared() _NOEXCEPT;
3758 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003759};
3760
Howard Hinnantd4444702010-08-11 17:04:31 +00003761#ifndef _LIBCPP_NO_RTTI
3762
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003763template <class _Tp, class _Dp, class _Alloc>
3764const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00003765__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003766{
Marshall Clow4b3ca8c2014-11-17 19:05:50 +00003767 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003768}
3769
Howard Hinnant324bb032010-08-22 00:02:43 +00003770#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003771
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003772template <class _Tp, class _Dp, class _Alloc>
3773void
Howard Hinnant1694d232011-05-28 14:41:13 +00003774__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003775{
3776 __data_.first().second()(__data_.first().first());
3777 __data_.first().second().~_Dp();
3778}
3779
3780template <class _Tp, class _Dp, class _Alloc>
3781void
Howard Hinnant1694d232011-05-28 14:41:13 +00003782__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003783{
Eric Fiselier8492cd82015-02-05 23:01:40 +00003784 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3785 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003786 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3787
Eric Fiselier8492cd82015-02-05 23:01:40 +00003788 _Al __a(__data_.second());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003789 __data_.second().~_Alloc();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003790 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003791}
3792
3793template <class _Tp, class _Alloc>
3794class __shared_ptr_emplace
3795 : public __shared_weak_count
3796{
3797 __compressed_pair<_Alloc, _Tp> __data_;
3798public:
3799#ifndef _LIBCPP_HAS_NO_VARIADICS
3800
Howard Hinnant82894812010-09-22 16:48:34 +00003801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003802 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003803 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003804
3805 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00003806 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003807 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00003808 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3809 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003810
3811#else // _LIBCPP_HAS_NO_VARIADICS
3812
Howard Hinnant82894812010-09-22 16:48:34 +00003813 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003814 __shared_ptr_emplace(_Alloc __a)
3815 : __data_(__a) {}
3816
3817 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00003818 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003819 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3820 : __data_(__a, _Tp(__a0)) {}
3821
3822 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00003823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003824 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3825 : __data_(__a, _Tp(__a0, __a1)) {}
3826
3827 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00003828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003829 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3830 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3831
3832#endif // _LIBCPP_HAS_NO_VARIADICS
3833
3834private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003835 virtual void __on_zero_shared() _NOEXCEPT;
3836 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003837public:
Howard Hinnant82894812010-09-22 16:48:34 +00003838 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003839 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003840};
3841
3842template <class _Tp, class _Alloc>
3843void
Howard Hinnant1694d232011-05-28 14:41:13 +00003844__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003845{
3846 __data_.second().~_Tp();
3847}
3848
3849template <class _Tp, class _Alloc>
3850void
Howard Hinnant1694d232011-05-28 14:41:13 +00003851__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003852{
Eric Fiselier8492cd82015-02-05 23:01:40 +00003853 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3854 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003855 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40 +00003856 _Al __a(__data_.first());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003857 __data_.first().~_Alloc();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003858 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003859}
3860
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003861template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003862
3863template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003864class _LIBCPP_TYPE_VIS_ONLY shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003865{
Howard Hinnant324bb032010-08-22 00:02:43 +00003866public:
3867 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003868private:
3869 element_type* __ptr_;
3870 __shared_weak_count* __cntrl_;
3871
3872 struct __nat {int __for_bool_;};
3873public:
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00003875 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003876 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00003877 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiene1678a12014-01-31 09:30:46 +00003878 template<class _Yp>
3879 explicit shared_ptr(_Yp* __p,
3880 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3881 template<class _Yp, class _Dp>
3882 shared_ptr(_Yp* __p, _Dp __d,
3883 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3884 template<class _Yp, class _Dp, class _Alloc>
3885 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3886 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003887 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3888 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003889 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3890 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003891 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003892 template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003894 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003895 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3896 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003897#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003899 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003900 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003901 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3902 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003903#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003904 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003905 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003906#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003907 template<class _Yp>
3908 shared_ptr(auto_ptr<_Yp>&& __r,
3909 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant324bb032010-08-22 00:02:43 +00003910#else
Logan Chiene1678a12014-01-31 09:30:46 +00003911 template<class _Yp>
3912 shared_ptr(auto_ptr<_Yp> __r,
3913 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003914#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003915#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003916 template <class _Yp, class _Dp>
3917 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3918 typename enable_if
3919 <
3920 !is_lvalue_reference<_Dp>::value &&
3921 !is_array<_Yp>::value &&
3922 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3923 __nat
3924 >::type = __nat());
3925 template <class _Yp, class _Dp>
3926 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3927 typename enable_if
3928 <
3929 is_lvalue_reference<_Dp>::value &&
3930 !is_array<_Yp>::value &&
3931 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3932 __nat
3933 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003934#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003935 template <class _Yp, class _Dp>
3936 shared_ptr(unique_ptr<_Yp, _Dp>,
3937 typename enable_if
3938 <
3939 !is_lvalue_reference<_Dp>::value &&
3940 !is_array<_Yp>::value &&
3941 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3942 __nat
3943 >::type = __nat());
3944 template <class _Yp, class _Dp>
3945 shared_ptr(unique_ptr<_Yp, _Dp>,
3946 typename enable_if
3947 <
3948 is_lvalue_reference<_Dp>::value &&
3949 !is_array<_Yp>::value &&
3950 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3951 __nat
3952 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003953#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003954
3955 ~shared_ptr();
3956
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003958 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003959 template<class _Yp>
3960 typename enable_if
3961 <
3962 is_convertible<_Yp*, element_type*>::value,
3963 shared_ptr&
3964 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003965 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00003966 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003967#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003968 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003969 shared_ptr& operator=(shared_ptr&& __r) _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 shared_ptr<_Tp>&
3975 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00003976 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00003977 operator=(shared_ptr<_Yp>&& __r);
3978 template<class _Yp>
Eric Fiselier566bcb42016-04-21 22:54:21 +00003979 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00003980 typename enable_if
3981 <
3982 !is_array<_Yp>::value &&
3983 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant37c4acf2013-09-13 23:56:00 +00003984 shared_ptr
3985 >::type&
Howard Hinnant57199402012-01-02 17:56:02 +00003986 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003987#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003988 template<class _Yp>
Eric Fiselier566bcb42016-04-21 22:54:21 +00003989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00003990 typename enable_if
3991 <
3992 !is_array<_Yp>::value &&
3993 is_convertible<_Yp*, element_type*>::value,
3994 shared_ptr&
3995 >::type
3996 operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003997#endif
Howard Hinnant57199402012-01-02 17:56:02 +00003998 template <class _Yp, class _Dp>
3999 typename enable_if
4000 <
4001 !is_array<_Yp>::value &&
4002 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4003 shared_ptr&
4004 >::type
Howard Hinnant73d21a42010-09-04 23:28:19 +00004005#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004006 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004007 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00004008#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov28c02db2015-12-09 22:32:36 +00004009 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004010 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004011#endif
4012
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004013 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004014 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004015 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004016 void reset() _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00004017 template<class _Yp>
4018 typename enable_if
4019 <
4020 is_convertible<_Yp*, element_type*>::value,
4021 void
4022 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004023 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004024 reset(_Yp* __p);
4025 template<class _Yp, class _Dp>
4026 typename enable_if
4027 <
4028 is_convertible<_Yp*, element_type*>::value,
4029 void
4030 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004031 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004032 reset(_Yp* __p, _Dp __d);
4033 template<class _Yp, class _Dp, class _Alloc>
4034 typename enable_if
4035 <
4036 is_convertible<_Yp*, element_type*>::value,
4037 void
4038 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004039 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004040 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004041
Howard Hinnant82894812010-09-22 16:48:34 +00004042 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004043 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004044 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004045 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
4046 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004047 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004048 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004049 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004050 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00004051 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004052 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00004053 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant77861882012-02-21 21:46:43 +00004054 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant99968442011-11-29 18:15:50 +00004055 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00004056 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00004057 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004058 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant99968442011-11-29 18:15:50 +00004059 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00004060 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00004061 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004062 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant5fec82d2012-07-30 01:40:57 +00004063 _LIBCPP_INLINE_VISIBILITY
4064 bool
4065 __owner_equivalent(const shared_ptr& __p) const
4066 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004067
Howard Hinnantd4444702010-08-11 17:04:31 +00004068#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004069 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00004070 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004071 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004072 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00004073#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004074
4075#ifndef _LIBCPP_HAS_NO_VARIADICS
4076
4077 template<class ..._Args>
4078 static
4079 shared_ptr<_Tp>
4080 make_shared(_Args&& ...__args);
4081
4082 template<class _Alloc, class ..._Args>
4083 static
4084 shared_ptr<_Tp>
4085 allocate_shared(const _Alloc& __a, _Args&& ...__args);
4086
4087#else // _LIBCPP_HAS_NO_VARIADICS
4088
4089 static shared_ptr<_Tp> make_shared();
4090
4091 template<class _A0>
4092 static shared_ptr<_Tp> make_shared(_A0&);
4093
4094 template<class _A0, class _A1>
4095 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
4096
4097 template<class _A0, class _A1, class _A2>
4098 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
4099
4100 template<class _Alloc>
4101 static shared_ptr<_Tp>
4102 allocate_shared(const _Alloc& __a);
4103
4104 template<class _Alloc, class _A0>
4105 static shared_ptr<_Tp>
4106 allocate_shared(const _Alloc& __a, _A0& __a0);
4107
4108 template<class _Alloc, class _A0, class _A1>
4109 static shared_ptr<_Tp>
4110 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
4111
4112 template<class _Alloc, class _A0, class _A1, class _A2>
4113 static shared_ptr<_Tp>
4114 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
4115
4116#endif // _LIBCPP_HAS_NO_VARIADICS
4117
4118private:
4119
4120 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00004121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004122 void
Marshall Clowfc3a3ff2015-05-27 20:36:14 +00004123 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004124 {
4125 if (__e)
Marshall Clowc4113372015-06-19 15:54:13 +00004126 {
4127 __e->__weak_this_.__ptr_ = const_cast<_Yp*>(static_cast<const _Yp*>(__e));
4128 __e->__weak_this_.__cntrl_ = __cntrl_;
Marshall Clow46d06b92015-06-19 19:32:06 +00004129 __cntrl_->__add_weak();
Marshall Clowc4113372015-06-19 15:54:13 +00004130 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004131 }
4132
Howard Hinnant82894812010-09-22 16:48:34 +00004133 _LIBCPP_INLINE_VISIBILITY
Marshall Clow60784f62015-05-27 22:44:47 +00004134 void __enable_weak_this(const volatile void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004135
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004136 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
4137 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004138};
4139
4140template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004141inline
Howard Hinnant46e94932012-07-07 20:56:04 +00004142_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004143shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004144 : __ptr_(0),
4145 __cntrl_(0)
4146{
4147}
4148
4149template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004150inline
Howard Hinnant46e94932012-07-07 20:56:04 +00004151_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004152shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004153 : __ptr_(0),
4154 __cntrl_(0)
4155{
4156}
4157
4158template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004159template<class _Yp>
4160shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4161 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004162 : __ptr_(__p)
4163{
4164 unique_ptr<_Yp> __hold(__p);
4165 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4166 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
4167 __hold.release();
4168 __enable_weak_this(__p);
4169}
4170
4171template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004172template<class _Yp, class _Dp>
4173shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4174 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004175 : __ptr_(__p)
4176{
4177#ifndef _LIBCPP_NO_EXCEPTIONS
4178 try
4179 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004180#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004181 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4182 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
4183 __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>
4196shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
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, allocator<_Tp> > _CntrlBlk;
4204 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4205#ifndef _LIBCPP_NO_EXCEPTIONS
4206 }
4207 catch (...)
4208 {
4209 __d(__p);
4210 throw;
4211 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004212#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004213}
4214
4215template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004216template<class _Yp, class _Dp, class _Alloc>
4217shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4218 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004219 : __ptr_(__p)
4220{
4221#ifndef _LIBCPP_NO_EXCEPTIONS
4222 try
4223 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004224#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004225 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004226 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004227 typedef __allocator_destructor<_A2> _D2;
4228 _A2 __a2(__a);
4229 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004230 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4231 _CntrlBlk(__p, __d, __a);
4232 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004233 __enable_weak_this(__p);
4234#ifndef _LIBCPP_NO_EXCEPTIONS
4235 }
4236 catch (...)
4237 {
4238 __d(__p);
4239 throw;
4240 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004241#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004242}
4243
4244template<class _Tp>
4245template<class _Dp, class _Alloc>
4246shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4247 : __ptr_(0)
4248{
4249#ifndef _LIBCPP_NO_EXCEPTIONS
4250 try
4251 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004252#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004253 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004254 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004255 typedef __allocator_destructor<_A2> _D2;
4256 _A2 __a2(__a);
4257 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004258 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4259 _CntrlBlk(__p, __d, __a);
4260 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004261#ifndef _LIBCPP_NO_EXCEPTIONS
4262 }
4263 catch (...)
4264 {
4265 __d(__p);
4266 throw;
4267 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004268#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004269}
4270
4271template<class _Tp>
4272template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004273inline
Howard Hinnant1694d232011-05-28 14:41:13 +00004274shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004275 : __ptr_(__p),
4276 __cntrl_(__r.__cntrl_)
4277{
4278 if (__cntrl_)
4279 __cntrl_->__add_shared();
4280}
4281
4282template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004283inline
Howard Hinnant1694d232011-05-28 14:41:13 +00004284shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004285 : __ptr_(__r.__ptr_),
4286 __cntrl_(__r.__cntrl_)
4287{
4288 if (__cntrl_)
4289 __cntrl_->__add_shared();
4290}
4291
4292template<class _Tp>
4293template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004294inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004295shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4296 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004297 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004298 : __ptr_(__r.__ptr_),
4299 __cntrl_(__r.__cntrl_)
4300{
4301 if (__cntrl_)
4302 __cntrl_->__add_shared();
4303}
4304
Howard Hinnant73d21a42010-09-04 23:28:19 +00004305#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004306
4307template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004308inline
Howard Hinnant1694d232011-05-28 14:41:13 +00004309shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004310 : __ptr_(__r.__ptr_),
4311 __cntrl_(__r.__cntrl_)
4312{
4313 __r.__ptr_ = 0;
4314 __r.__cntrl_ = 0;
4315}
4316
4317template<class _Tp>
4318template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004319inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004320shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4321 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004322 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004323 : __ptr_(__r.__ptr_),
4324 __cntrl_(__r.__cntrl_)
4325{
4326 __r.__ptr_ = 0;
4327 __r.__cntrl_ = 0;
4328}
4329
Howard Hinnant73d21a42010-09-04 23:28:19 +00004330#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004331
4332template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004333template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004334#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00004335shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004336#else
Logan Chiene1678a12014-01-31 09:30:46 +00004337shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004338#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004339 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004340 : __ptr_(__r.get())
4341{
4342 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4343 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
4344 __enable_weak_this(__r.get());
4345 __r.release();
4346}
4347
4348template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004349template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004350#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004351shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4352#else
4353shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4354#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004355 typename enable_if
4356 <
4357 !is_lvalue_reference<_Dp>::value &&
4358 !is_array<_Yp>::value &&
4359 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4360 __nat
4361 >::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004362 : __ptr_(__r.get())
4363{
Marshall Clow0ad232a2015-05-10 13:59:45 +00004364#if _LIBCPP_STD_VER > 11
4365 if (__ptr_ == nullptr)
4366 __cntrl_ = nullptr;
4367 else
4368#endif
4369 {
4370 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4371 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
4372 __enable_weak_this(__r.get());
4373 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004374 __r.release();
4375}
4376
4377template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004378template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004379#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004380shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4381#else
4382shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4383#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004384 typename enable_if
4385 <
4386 is_lvalue_reference<_Dp>::value &&
4387 !is_array<_Yp>::value &&
4388 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4389 __nat
4390 >::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004391 : __ptr_(__r.get())
4392{
Marshall Clow0ad232a2015-05-10 13:59:45 +00004393#if _LIBCPP_STD_VER > 11
4394 if (__ptr_ == nullptr)
4395 __cntrl_ = nullptr;
4396 else
4397#endif
4398 {
4399 typedef __shared_ptr_pointer<_Yp*,
4400 reference_wrapper<typename remove_reference<_Dp>::type>,
4401 allocator<_Yp> > _CntrlBlk;
4402 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
4403 __enable_weak_this(__r.get());
4404 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004405 __r.release();
4406}
4407
4408#ifndef _LIBCPP_HAS_NO_VARIADICS
4409
4410template<class _Tp>
4411template<class ..._Args>
4412shared_ptr<_Tp>
4413shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4414{
4415 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4416 typedef allocator<_CntrlBlk> _A2;
4417 typedef __allocator_destructor<_A2> _D2;
4418 _A2 __a2;
4419 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004420 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004421 shared_ptr<_Tp> __r;
4422 __r.__ptr_ = __hold2.get()->get();
4423 __r.__cntrl_ = __hold2.release();
4424 __r.__enable_weak_this(__r.__ptr_);
4425 return __r;
4426}
4427
4428template<class _Tp>
4429template<class _Alloc, class ..._Args>
4430shared_ptr<_Tp>
4431shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4432{
4433 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004434 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004435 typedef __allocator_destructor<_A2> _D2;
4436 _A2 __a2(__a);
4437 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004438 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4439 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004440 shared_ptr<_Tp> __r;
4441 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004442 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004443 __r.__enable_weak_this(__r.__ptr_);
4444 return __r;
4445}
4446
4447#else // _LIBCPP_HAS_NO_VARIADICS
4448
4449template<class _Tp>
4450shared_ptr<_Tp>
4451shared_ptr<_Tp>::make_shared()
4452{
4453 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4454 typedef allocator<_CntrlBlk> _Alloc2;
4455 typedef __allocator_destructor<_Alloc2> _D2;
4456 _Alloc2 __alloc2;
4457 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4458 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4459 shared_ptr<_Tp> __r;
4460 __r.__ptr_ = __hold2.get()->get();
4461 __r.__cntrl_ = __hold2.release();
4462 __r.__enable_weak_this(__r.__ptr_);
4463 return __r;
4464}
4465
4466template<class _Tp>
4467template<class _A0>
4468shared_ptr<_Tp>
4469shared_ptr<_Tp>::make_shared(_A0& __a0)
4470{
4471 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4472 typedef allocator<_CntrlBlk> _Alloc2;
4473 typedef __allocator_destructor<_Alloc2> _D2;
4474 _Alloc2 __alloc2;
4475 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4476 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4477 shared_ptr<_Tp> __r;
4478 __r.__ptr_ = __hold2.get()->get();
4479 __r.__cntrl_ = __hold2.release();
4480 __r.__enable_weak_this(__r.__ptr_);
4481 return __r;
4482}
4483
4484template<class _Tp>
4485template<class _A0, class _A1>
4486shared_ptr<_Tp>
4487shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4488{
4489 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4490 typedef allocator<_CntrlBlk> _Alloc2;
4491 typedef __allocator_destructor<_Alloc2> _D2;
4492 _Alloc2 __alloc2;
4493 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4494 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4495 shared_ptr<_Tp> __r;
4496 __r.__ptr_ = __hold2.get()->get();
4497 __r.__cntrl_ = __hold2.release();
4498 __r.__enable_weak_this(__r.__ptr_);
4499 return __r;
4500}
4501
4502template<class _Tp>
4503template<class _A0, class _A1, class _A2>
4504shared_ptr<_Tp>
4505shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4506{
4507 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4508 typedef allocator<_CntrlBlk> _Alloc2;
4509 typedef __allocator_destructor<_Alloc2> _D2;
4510 _Alloc2 __alloc2;
4511 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4512 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4513 shared_ptr<_Tp> __r;
4514 __r.__ptr_ = __hold2.get()->get();
4515 __r.__cntrl_ = __hold2.release();
4516 __r.__enable_weak_this(__r.__ptr_);
4517 return __r;
4518}
4519
4520template<class _Tp>
4521template<class _Alloc>
4522shared_ptr<_Tp>
4523shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4524{
4525 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004526 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004527 typedef __allocator_destructor<_Alloc2> _D2;
4528 _Alloc2 __alloc2(__a);
4529 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004530 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4531 _CntrlBlk(__a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004532 shared_ptr<_Tp> __r;
4533 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004534 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004535 __r.__enable_weak_this(__r.__ptr_);
4536 return __r;
4537}
4538
4539template<class _Tp>
4540template<class _Alloc, class _A0>
4541shared_ptr<_Tp>
4542shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4543{
4544 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004545 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004546 typedef __allocator_destructor<_Alloc2> _D2;
4547 _Alloc2 __alloc2(__a);
4548 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004549 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4550 _CntrlBlk(__a, __a0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004551 shared_ptr<_Tp> __r;
4552 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004553 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004554 __r.__enable_weak_this(__r.__ptr_);
4555 return __r;
4556}
4557
4558template<class _Tp>
4559template<class _Alloc, class _A0, class _A1>
4560shared_ptr<_Tp>
4561shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4562{
4563 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004564 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004565 typedef __allocator_destructor<_Alloc2> _D2;
4566 _Alloc2 __alloc2(__a);
4567 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004568 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4569 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004570 shared_ptr<_Tp> __r;
4571 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004572 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004573 __r.__enable_weak_this(__r.__ptr_);
4574 return __r;
4575}
4576
4577template<class _Tp>
4578template<class _Alloc, class _A0, class _A1, class _A2>
4579shared_ptr<_Tp>
4580shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4581{
4582 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004583 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004584 typedef __allocator_destructor<_Alloc2> _D2;
4585 _Alloc2 __alloc2(__a);
4586 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004587 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4588 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004589 shared_ptr<_Tp> __r;
4590 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004591 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004592 __r.__enable_weak_this(__r.__ptr_);
4593 return __r;
4594}
4595
4596#endif // _LIBCPP_HAS_NO_VARIADICS
4597
4598template<class _Tp>
4599shared_ptr<_Tp>::~shared_ptr()
4600{
4601 if (__cntrl_)
4602 __cntrl_->__release_shared();
4603}
4604
4605template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004606inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004607shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004608shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004609{
4610 shared_ptr(__r).swap(*this);
4611 return *this;
4612}
4613
4614template<class _Tp>
4615template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004616inline
Howard Hinnant57199402012-01-02 17:56:02 +00004617typename enable_if
4618<
4619 is_convertible<_Yp*, _Tp*>::value,
4620 shared_ptr<_Tp>&
4621>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004622shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004623{
4624 shared_ptr(__r).swap(*this);
4625 return *this;
4626}
4627
Howard Hinnant73d21a42010-09-04 23:28:19 +00004628#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004629
4630template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004631inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004632shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004633shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004634{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004635 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004636 return *this;
4637}
4638
4639template<class _Tp>
4640template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004641inline
Howard Hinnant57199402012-01-02 17:56:02 +00004642typename enable_if
4643<
4644 is_convertible<_Yp*, _Tp*>::value,
4645 shared_ptr<_Tp>&
4646>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004647shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4648{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004649 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004650 return *this;
4651}
4652
4653template<class _Tp>
4654template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004655inline
Howard Hinnant57199402012-01-02 17:56:02 +00004656typename enable_if
4657<
4658 !is_array<_Yp>::value &&
4659 is_convertible<_Yp*, _Tp*>::value,
Howard Hinnant37c4acf2013-09-13 23:56:00 +00004660 shared_ptr<_Tp>
4661>::type&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004662shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4663{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004664 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004665 return *this;
4666}
4667
4668template<class _Tp>
4669template <class _Yp, class _Dp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004670inline
Howard Hinnant57199402012-01-02 17:56:02 +00004671typename enable_if
4672<
4673 !is_array<_Yp>::value &&
4674 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4675 shared_ptr<_Tp>&
4676>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004677shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4678{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004679 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004680 return *this;
4681}
4682
Howard Hinnant73d21a42010-09-04 23:28:19 +00004683#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004684
4685template<class _Tp>
4686template<class _Yp>
4687inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004688typename enable_if
4689<
4690 !is_array<_Yp>::value &&
4691 is_convertible<_Yp*, _Tp*>::value,
4692 shared_ptr<_Tp>&
4693>::type
Howard Hinnant324bb032010-08-22 00:02:43 +00004694shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004695{
4696 shared_ptr(__r).swap(*this);
4697 return *this;
4698}
4699
4700template<class _Tp>
4701template <class _Yp, class _Dp>
4702inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004703typename enable_if
4704<
4705 !is_array<_Yp>::value &&
4706 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4707 shared_ptr<_Tp>&
4708>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004709shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4710{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004711 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004712 return *this;
4713}
4714
Howard Hinnant73d21a42010-09-04 23:28:19 +00004715#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004716
4717template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004718inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004719void
Howard Hinnant1694d232011-05-28 14:41:13 +00004720shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004721{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004722 _VSTD::swap(__ptr_, __r.__ptr_);
4723 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004724}
4725
4726template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004727inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004728void
Howard Hinnant1694d232011-05-28 14:41:13 +00004729shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004730{
4731 shared_ptr().swap(*this);
4732}
4733
4734template<class _Tp>
4735template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004736inline
Howard Hinnant57199402012-01-02 17:56:02 +00004737typename enable_if
4738<
4739 is_convertible<_Yp*, _Tp*>::value,
4740 void
4741>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004742shared_ptr<_Tp>::reset(_Yp* __p)
4743{
4744 shared_ptr(__p).swap(*this);
4745}
4746
4747template<class _Tp>
4748template<class _Yp, class _Dp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004749inline
Howard Hinnant57199402012-01-02 17:56:02 +00004750typename enable_if
4751<
4752 is_convertible<_Yp*, _Tp*>::value,
4753 void
4754>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004755shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4756{
4757 shared_ptr(__p, __d).swap(*this);
4758}
4759
4760template<class _Tp>
4761template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00004762inline
Howard Hinnant57199402012-01-02 17:56:02 +00004763typename enable_if
4764<
4765 is_convertible<_Yp*, _Tp*>::value,
4766 void
4767>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004768shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4769{
4770 shared_ptr(__p, __d, __a).swap(*this);
4771}
4772
4773#ifndef _LIBCPP_HAS_NO_VARIADICS
4774
Howard Hinnant324bb032010-08-22 00:02:43 +00004775template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004776inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004777typename enable_if
4778<
4779 !is_array<_Tp>::value,
4780 shared_ptr<_Tp>
4781>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004782make_shared(_Args&& ...__args)
4783{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004784 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004785}
4786
Howard Hinnant324bb032010-08-22 00:02:43 +00004787template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004788inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004789typename enable_if
4790<
4791 !is_array<_Tp>::value,
4792 shared_ptr<_Tp>
4793>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004794allocate_shared(const _Alloc& __a, _Args&& ...__args)
4795{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004796 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004797}
4798
4799#else // _LIBCPP_HAS_NO_VARIADICS
4800
4801template<class _Tp>
4802inline _LIBCPP_INLINE_VISIBILITY
4803shared_ptr<_Tp>
4804make_shared()
4805{
4806 return shared_ptr<_Tp>::make_shared();
4807}
4808
4809template<class _Tp, class _A0>
4810inline _LIBCPP_INLINE_VISIBILITY
4811shared_ptr<_Tp>
4812make_shared(_A0& __a0)
4813{
4814 return shared_ptr<_Tp>::make_shared(__a0);
4815}
4816
4817template<class _Tp, class _A0, class _A1>
4818inline _LIBCPP_INLINE_VISIBILITY
4819shared_ptr<_Tp>
4820make_shared(_A0& __a0, _A1& __a1)
4821{
4822 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4823}
4824
Howard Hinnant324bb032010-08-22 00:02:43 +00004825template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004826inline _LIBCPP_INLINE_VISIBILITY
4827shared_ptr<_Tp>
4828make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4829{
4830 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4831}
4832
4833template<class _Tp, class _Alloc>
4834inline _LIBCPP_INLINE_VISIBILITY
4835shared_ptr<_Tp>
4836allocate_shared(const _Alloc& __a)
4837{
4838 return shared_ptr<_Tp>::allocate_shared(__a);
4839}
4840
4841template<class _Tp, class _Alloc, class _A0>
4842inline _LIBCPP_INLINE_VISIBILITY
4843shared_ptr<_Tp>
4844allocate_shared(const _Alloc& __a, _A0& __a0)
4845{
4846 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4847}
4848
4849template<class _Tp, class _Alloc, class _A0, class _A1>
4850inline _LIBCPP_INLINE_VISIBILITY
4851shared_ptr<_Tp>
4852allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4853{
4854 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4855}
4856
4857template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4858inline _LIBCPP_INLINE_VISIBILITY
4859shared_ptr<_Tp>
4860allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4861{
4862 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4863}
4864
4865#endif // _LIBCPP_HAS_NO_VARIADICS
4866
4867template<class _Tp, class _Up>
4868inline _LIBCPP_INLINE_VISIBILITY
4869bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004870operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004871{
4872 return __x.get() == __y.get();
4873}
4874
4875template<class _Tp, class _Up>
4876inline _LIBCPP_INLINE_VISIBILITY
4877bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004878operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004879{
4880 return !(__x == __y);
4881}
4882
4883template<class _Tp, class _Up>
4884inline _LIBCPP_INLINE_VISIBILITY
4885bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004886operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004887{
Eric Fiselier8492cd82015-02-05 23:01:40 +00004888 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4889 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnant3fadda32012-02-21 21:02:58 +00004890}
4891
4892template<class _Tp, class _Up>
4893inline _LIBCPP_INLINE_VISIBILITY
4894bool
4895operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4896{
4897 return __y < __x;
4898}
4899
4900template<class _Tp, class _Up>
4901inline _LIBCPP_INLINE_VISIBILITY
4902bool
4903operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4904{
4905 return !(__y < __x);
4906}
4907
4908template<class _Tp, class _Up>
4909inline _LIBCPP_INLINE_VISIBILITY
4910bool
4911operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4912{
4913 return !(__x < __y);
4914}
4915
4916template<class _Tp>
4917inline _LIBCPP_INLINE_VISIBILITY
4918bool
4919operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4920{
4921 return !__x;
4922}
4923
4924template<class _Tp>
4925inline _LIBCPP_INLINE_VISIBILITY
4926bool
4927operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4928{
4929 return !__x;
4930}
4931
4932template<class _Tp>
4933inline _LIBCPP_INLINE_VISIBILITY
4934bool
4935operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4936{
4937 return static_cast<bool>(__x);
4938}
4939
4940template<class _Tp>
4941inline _LIBCPP_INLINE_VISIBILITY
4942bool
4943operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4944{
4945 return static_cast<bool>(__x);
4946}
4947
4948template<class _Tp>
4949inline _LIBCPP_INLINE_VISIBILITY
4950bool
4951operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4952{
4953 return less<_Tp*>()(__x.get(), nullptr);
4954}
4955
4956template<class _Tp>
4957inline _LIBCPP_INLINE_VISIBILITY
4958bool
4959operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4960{
4961 return less<_Tp*>()(nullptr, __x.get());
4962}
4963
4964template<class _Tp>
4965inline _LIBCPP_INLINE_VISIBILITY
4966bool
4967operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4968{
4969 return nullptr < __x;
4970}
4971
4972template<class _Tp>
4973inline _LIBCPP_INLINE_VISIBILITY
4974bool
4975operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4976{
4977 return __x < nullptr;
4978}
4979
4980template<class _Tp>
4981inline _LIBCPP_INLINE_VISIBILITY
4982bool
4983operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4984{
4985 return !(nullptr < __x);
4986}
4987
4988template<class _Tp>
4989inline _LIBCPP_INLINE_VISIBILITY
4990bool
4991operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4992{
4993 return !(__x < nullptr);
4994}
4995
4996template<class _Tp>
4997inline _LIBCPP_INLINE_VISIBILITY
4998bool
4999operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5000{
5001 return !(__x < nullptr);
5002}
5003
5004template<class _Tp>
5005inline _LIBCPP_INLINE_VISIBILITY
5006bool
5007operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5008{
5009 return !(nullptr < __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005010}
5011
5012template<class _Tp>
5013inline _LIBCPP_INLINE_VISIBILITY
5014void
Howard Hinnant1694d232011-05-28 14:41:13 +00005015swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005016{
5017 __x.swap(__y);
5018}
5019
5020template<class _Tp, class _Up>
5021inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005022typename enable_if
5023<
5024 !is_array<_Tp>::value && !is_array<_Up>::value,
5025 shared_ptr<_Tp>
5026>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005027static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005028{
5029 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
5030}
5031
5032template<class _Tp, class _Up>
5033inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005034typename enable_if
5035<
5036 !is_array<_Tp>::value && !is_array<_Up>::value,
5037 shared_ptr<_Tp>
5038>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005039dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005040{
5041 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
5042 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
5043}
5044
5045template<class _Tp, class _Up>
Howard Hinnant57199402012-01-02 17:56:02 +00005046typename enable_if
5047<
5048 is_array<_Tp>::value == is_array<_Up>::value,
5049 shared_ptr<_Tp>
5050>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005051const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005052{
Howard Hinnant57199402012-01-02 17:56:02 +00005053 typedef typename remove_extent<_Tp>::type _RTp;
5054 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005055}
5056
Howard Hinnantd4444702010-08-11 17:04:31 +00005057#ifndef _LIBCPP_NO_RTTI
5058
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005059template<class _Dp, class _Tp>
5060inline _LIBCPP_INLINE_VISIBILITY
5061_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00005062get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005063{
5064 return __p.template __get_deleter<_Dp>();
5065}
5066
Howard Hinnant324bb032010-08-22 00:02:43 +00005067#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00005068
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005069template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005070class _LIBCPP_TYPE_VIS_ONLY weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005071{
Howard Hinnant324bb032010-08-22 00:02:43 +00005072public:
5073 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005074private:
5075 element_type* __ptr_;
5076 __shared_weak_count* __cntrl_;
5077
Howard Hinnant324bb032010-08-22 00:02:43 +00005078public:
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005079 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00005080 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005081 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00005082 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5083 _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005084 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005085 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005086 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00005087 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5088 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005089
Howard Hinnant57199402012-01-02 17:56:02 +00005090#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005091 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005092 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005093 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant57199402012-01-02 17:56:02 +00005094 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5095 _NOEXCEPT;
5096#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00005097 ~weak_ptr();
5098
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005099 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005100 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00005101 template<class _Yp>
5102 typename enable_if
5103 <
5104 is_convertible<_Yp*, element_type*>::value,
5105 weak_ptr&
5106 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005107 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005108 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
5109
5110#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5111
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005112 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005113 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
5114 template<class _Yp>
5115 typename enable_if
5116 <
5117 is_convertible<_Yp*, element_type*>::value,
5118 weak_ptr&
5119 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005120 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005121 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
5122
5123#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5124
5125 template<class _Yp>
5126 typename enable_if
5127 <
5128 is_convertible<_Yp*, element_type*>::value,
5129 weak_ptr&
5130 >::type
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005132 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005133
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005135 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005137 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005138
Howard Hinnant82894812010-09-22 16:48:34 +00005139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005140 long use_count() const _NOEXCEPT
5141 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00005142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005143 bool expired() const _NOEXCEPT
5144 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5145 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00005146 template<class _Up>
5147 _LIBCPP_INLINE_VISIBILITY
5148 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005149 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00005150 template<class _Up>
5151 _LIBCPP_INLINE_VISIBILITY
5152 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005153 {return __cntrl_ < __r.__cntrl_;}
5154
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005155 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
5156 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005157};
5158
5159template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005160inline
Howard Hinnant46e94932012-07-07 20:56:04 +00005161_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005162weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005163 : __ptr_(0),
5164 __cntrl_(0)
5165{
5166}
5167
5168template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005169inline
Howard Hinnant1694d232011-05-28 14:41:13 +00005170weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005171 : __ptr_(__r.__ptr_),
5172 __cntrl_(__r.__cntrl_)
5173{
5174 if (__cntrl_)
5175 __cntrl_->__add_weak();
5176}
5177
5178template<class _Tp>
5179template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005180inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005181weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005182 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005183 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005184 : __ptr_(__r.__ptr_),
5185 __cntrl_(__r.__cntrl_)
5186{
5187 if (__cntrl_)
5188 __cntrl_->__add_weak();
5189}
5190
5191template<class _Tp>
5192template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005193inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005194weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005195 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005196 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005197 : __ptr_(__r.__ptr_),
5198 __cntrl_(__r.__cntrl_)
5199{
5200 if (__cntrl_)
5201 __cntrl_->__add_weak();
5202}
5203
Howard Hinnant57199402012-01-02 17:56:02 +00005204#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5205
5206template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005207inline
Howard Hinnant57199402012-01-02 17:56:02 +00005208weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5209 : __ptr_(__r.__ptr_),
5210 __cntrl_(__r.__cntrl_)
5211{
5212 __r.__ptr_ = 0;
5213 __r.__cntrl_ = 0;
5214}
5215
5216template<class _Tp>
5217template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005218inline
Howard Hinnant57199402012-01-02 17:56:02 +00005219weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5220 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5221 _NOEXCEPT
5222 : __ptr_(__r.__ptr_),
5223 __cntrl_(__r.__cntrl_)
5224{
5225 __r.__ptr_ = 0;
5226 __r.__cntrl_ = 0;
5227}
5228
5229#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5230
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005231template<class _Tp>
5232weak_ptr<_Tp>::~weak_ptr()
5233{
5234 if (__cntrl_)
5235 __cntrl_->__release_weak();
5236}
5237
5238template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005239inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005240weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00005241weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005242{
5243 weak_ptr(__r).swap(*this);
5244 return *this;
5245}
5246
5247template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005248template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005249inline
Howard Hinnant57199402012-01-02 17:56:02 +00005250typename enable_if
5251<
5252 is_convertible<_Yp*, _Tp*>::value,
5253 weak_ptr<_Tp>&
5254>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005255weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005256{
5257 weak_ptr(__r).swap(*this);
5258 return *this;
5259}
5260
Howard Hinnant57199402012-01-02 17:56:02 +00005261#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5262
5263template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005264inline
Howard Hinnant57199402012-01-02 17:56:02 +00005265weak_ptr<_Tp>&
5266weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5267{
5268 weak_ptr(_VSTD::move(__r)).swap(*this);
5269 return *this;
5270}
5271
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005272template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005273template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005274inline
Howard Hinnant57199402012-01-02 17:56:02 +00005275typename enable_if
5276<
5277 is_convertible<_Yp*, _Tp*>::value,
5278 weak_ptr<_Tp>&
5279>::type
5280weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5281{
5282 weak_ptr(_VSTD::move(__r)).swap(*this);
5283 return *this;
5284}
5285
5286#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5287
5288template<class _Tp>
5289template<class _Yp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005290inline
Howard Hinnant57199402012-01-02 17:56:02 +00005291typename enable_if
5292<
5293 is_convertible<_Yp*, _Tp*>::value,
5294 weak_ptr<_Tp>&
5295>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005296weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005297{
5298 weak_ptr(__r).swap(*this);
5299 return *this;
5300}
5301
5302template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005303inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005304void
Howard Hinnant1694d232011-05-28 14:41:13 +00005305weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005306{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005307 _VSTD::swap(__ptr_, __r.__ptr_);
5308 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005309}
5310
5311template<class _Tp>
5312inline _LIBCPP_INLINE_VISIBILITY
5313void
Howard Hinnant1694d232011-05-28 14:41:13 +00005314swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005315{
5316 __x.swap(__y);
5317}
5318
5319template<class _Tp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13 +00005320inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005321void
Howard Hinnant1694d232011-05-28 14:41:13 +00005322weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005323{
5324 weak_ptr().swap(*this);
5325}
5326
5327template<class _Tp>
5328template<class _Yp>
5329shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5330 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
5331 : __ptr_(__r.__ptr_),
5332 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5333{
5334 if (__cntrl_ == 0)
5335#ifndef _LIBCPP_NO_EXCEPTIONS
5336 throw bad_weak_ptr();
5337#else
5338 assert(!"bad_weak_ptr");
5339#endif
5340}
5341
5342template<class _Tp>
5343shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00005344weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005345{
5346 shared_ptr<_Tp> __r;
5347 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5348 if (__r.__cntrl_)
5349 __r.__ptr_ = __ptr_;
5350 return __r;
5351}
5352
Marshall Clow3f159e82015-11-12 15:56:44 +00005353#if _LIBCPP_STD_VER > 14
5354template <class _Tp = void> struct owner_less;
5355#else
Howard Hinnant324bb032010-08-22 00:02:43 +00005356template <class _Tp> struct owner_less;
Marshall Clow3f159e82015-11-12 15:56:44 +00005357#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005358
5359template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005360struct _LIBCPP_TYPE_VIS_ONLY owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005361 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00005362{
5363 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005364 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005365 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5366 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005367 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005368 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5369 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005371 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5372 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00005373};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005374
5375template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005376struct _LIBCPP_TYPE_VIS_ONLY owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005377 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5378{
Howard Hinnant324bb032010-08-22 00:02:43 +00005379 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005381 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5382 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005383 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005384 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5385 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005387 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5388 {return __x.owner_before(__y);}
5389};
5390
Marshall Clow3f159e82015-11-12 15:56:44 +00005391#if _LIBCPP_STD_VER > 14
5392template <>
5393struct _LIBCPP_TYPE_VIS_ONLY owner_less<void>
5394{
5395 template <class _Tp, class _Up>
5396 _LIBCPP_INLINE_VISIBILITY
5397 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const
5398 {return __x.owner_before(__y);}
5399 template <class _Tp, class _Up>
5400 _LIBCPP_INLINE_VISIBILITY
5401 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const
5402 {return __x.owner_before(__y);}
5403 template <class _Tp, class _Up>
5404 _LIBCPP_INLINE_VISIBILITY
5405 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const
5406 {return __x.owner_before(__y);}
5407 template <class _Tp, class _Up>
5408 _LIBCPP_INLINE_VISIBILITY
5409 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const
5410 {return __x.owner_before(__y);}
5411 typedef void is_transparent;
5412};
5413#endif
5414
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005415template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005416class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005417{
5418 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00005419protected:
Howard Hinnant46e94932012-07-07 20:56:04 +00005420 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005421 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005422 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005423 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005425 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5426 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00005427 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005428 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00005429public:
Howard Hinnant82894812010-09-22 16:48:34 +00005430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005431 shared_ptr<_Tp> shared_from_this()
5432 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00005433 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005434 shared_ptr<_Tp const> shared_from_this() const
5435 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005436
5437 template <class _Up> friend class shared_ptr;
5438};
5439
Howard Hinnant21aefc32010-06-03 16:42:57 +00005440template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005441struct _LIBCPP_TYPE_VIS_ONLY hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00005442{
5443 typedef shared_ptr<_Tp> argument_type;
5444 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005445 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005446 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00005447 {
5448 return hash<_Tp*>()(__ptr.get());
5449 }
5450};
5451
Howard Hinnant99968442011-11-29 18:15:50 +00005452template<class _CharT, class _Traits, class _Yp>
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005453inline _LIBCPP_INLINE_VISIBILITY
5454basic_ostream<_CharT, _Traits>&
Howard Hinnant99968442011-11-29 18:15:50 +00005455operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005456
Eric Fiselier00f4a492015-08-19 17:21:46 +00005457// TODO(EricWF): Enable this for both Clang and GCC. Currently it is only
5458// enabled with clang.
5459#if defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005460
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005461class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005462{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005463 void* __lx;
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005464public:
5465 void lock() _NOEXCEPT;
5466 void unlock() _NOEXCEPT;
5467
5468private:
5469 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5470 __sp_mut(const __sp_mut&);
5471 __sp_mut& operator=(const __sp_mut&);
5472
Howard Hinnant83eade62013-03-06 23:30:19 +00005473 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005474};
5475
Howard Hinnant83eade62013-03-06 23:30:19 +00005476_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005477
5478template <class _Tp>
5479inline _LIBCPP_INLINE_VISIBILITY
5480bool
5481atomic_is_lock_free(const shared_ptr<_Tp>*)
5482{
5483 return false;
5484}
5485
5486template <class _Tp>
5487shared_ptr<_Tp>
5488atomic_load(const shared_ptr<_Tp>* __p)
5489{
5490 __sp_mut& __m = __get_sp_mut(__p);
5491 __m.lock();
5492 shared_ptr<_Tp> __q = *__p;
5493 __m.unlock();
5494 return __q;
5495}
5496
5497template <class _Tp>
5498inline _LIBCPP_INLINE_VISIBILITY
5499shared_ptr<_Tp>
5500atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5501{
5502 return atomic_load(__p);
5503}
5504
5505template <class _Tp>
5506void
5507atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5508{
5509 __sp_mut& __m = __get_sp_mut(__p);
5510 __m.lock();
5511 __p->swap(__r);
5512 __m.unlock();
5513}
5514
5515template <class _Tp>
5516inline _LIBCPP_INLINE_VISIBILITY
5517void
5518atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5519{
5520 atomic_store(__p, __r);
5521}
5522
5523template <class _Tp>
5524shared_ptr<_Tp>
5525atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5526{
5527 __sp_mut& __m = __get_sp_mut(__p);
5528 __m.lock();
5529 __p->swap(__r);
5530 __m.unlock();
5531 return __r;
5532}
5533
5534template <class _Tp>
5535inline _LIBCPP_INLINE_VISIBILITY
5536shared_ptr<_Tp>
5537atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5538{
5539 return atomic_exchange(__p, __r);
5540}
5541
5542template <class _Tp>
5543bool
5544atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5545{
5546 __sp_mut& __m = __get_sp_mut(__p);
5547 __m.lock();
5548 if (__p->__owner_equivalent(*__v))
5549 {
5550 *__p = __w;
5551 __m.unlock();
5552 return true;
5553 }
5554 *__v = *__p;
5555 __m.unlock();
5556 return false;
5557}
5558
5559template <class _Tp>
5560inline _LIBCPP_INLINE_VISIBILITY
5561bool
5562atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5563{
5564 return atomic_compare_exchange_strong(__p, __v, __w);
5565}
5566
5567template <class _Tp>
5568inline _LIBCPP_INLINE_VISIBILITY
5569bool
5570atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5571 shared_ptr<_Tp> __w, memory_order, memory_order)
5572{
5573 return atomic_compare_exchange_strong(__p, __v, __w);
5574}
5575
5576template <class _Tp>
5577inline _LIBCPP_INLINE_VISIBILITY
5578bool
5579atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5580 shared_ptr<_Tp> __w, memory_order, memory_order)
5581{
5582 return atomic_compare_exchange_weak(__p, __v, __w);
5583}
5584
Eric Fiselier00f4a492015-08-19 17:21:46 +00005585#endif // defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005586
Howard Hinnant324bb032010-08-22 00:02:43 +00005587//enum class
Howard Hinnant83eade62013-03-06 23:30:19 +00005588struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005589{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005590 enum __lx
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005591 {
5592 relaxed,
5593 preferred,
5594 strict
5595 };
5596
Howard Hinnant9c0df142012-10-30 19:06:59 +00005597 __lx __v_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005598
Howard Hinnant82894812010-09-22 16:48:34 +00005599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9c0df142012-10-30 19:06:59 +00005600 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00005601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005602 operator int() const {return __v_;}
5603};
5604
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005605_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5606_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5607_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
5608_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5609_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005610
5611template <class _Tp>
5612inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005613_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005614undeclare_reachable(_Tp* __p)
5615{
5616 return static_cast<_Tp*>(__undeclare_reachable(__p));
5617}
5618
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005619_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005620
Marshall Clow7d914d12015-07-13 20:04:56 +00005621// --- Helper for container swap --
5622template <typename _Alloc>
Eric Fiselier566bcb42016-04-21 22:54:21 +00005623inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow7d914d12015-07-13 20:04:56 +00005624void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5625#if _LIBCPP_STD_VER >= 14
5626 _NOEXCEPT
5627#else
5628 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5629#endif
5630{
5631 __swap_allocator(__a1, __a2,
5632 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5633}
5634
5635template <typename _Alloc>
5636_LIBCPP_INLINE_VISIBILITY
5637void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5638#if _LIBCPP_STD_VER >= 14
5639 _NOEXCEPT
5640#else
5641 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5642#endif
5643{
5644 using _VSTD::swap;
5645 swap(__a1, __a2);
5646}
5647
5648template <typename _Alloc>
Eric Fiselier566bcb42016-04-21 22:54:21 +00005649inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow7d914d12015-07-13 20:04:56 +00005650void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5651
Marshall Clowd434e2a2015-08-18 19:51:37 +00005652template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Marshall Clowaf961ed2015-08-18 18:57:00 +00005653struct __noexcept_move_assign_container : public integral_constant<bool,
5654 _Traits::propagate_on_container_move_assignment::value
5655#if _LIBCPP_STD_VER > 14
5656 || _Traits::is_always_equal::value
5657#else
5658 && is_nothrow_move_assignable<_Alloc>::value
5659#endif
5660 > {};
Marshall Clow7d914d12015-07-13 20:04:56 +00005661
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005662_LIBCPP_END_NAMESPACE_STD
5663
5664#endif // _LIBCPP_MEMORY