blob: 04d57d5d7f0d7cef18f1c74c4811ee9de1111dcc [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)
Richard Smith73c1fce2014-06-04 19:54:15 +00001729 {return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));}
Howard Hinnant1694d232011-05-28 14:41:13 +00001730 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith73c1fce2014-06-04 19:54:15 +00001731 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant1694d232011-05-28 14:41:13 +00001732 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1733 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001734#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001735 template <class _Up, class... _Args>
1736 _LIBCPP_INLINE_VISIBILITY
1737 void
1738 construct(_Up* __p, _Args&&... __args)
1739 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001740 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001741 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001742#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001743 _LIBCPP_INLINE_VISIBILITY
1744 void
1745 construct(pointer __p)
1746 {
1747 ::new((void*)__p) _Tp();
1748 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001749# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001750
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001751 template <class _A0>
1752 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001753 void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001754 construct(pointer __p, _A0& __a0)
1755 {
1756 ::new((void*)__p) _Tp(__a0);
1757 }
1758 template <class _A0>
1759 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001760 void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001761 construct(pointer __p, const _A0& __a0)
1762 {
1763 ::new((void*)__p) _Tp(__a0);
1764 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001765# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001766 template <class _A0, class _A1>
1767 _LIBCPP_INLINE_VISIBILITY
1768 void
1769 construct(pointer __p, _A0& __a0, _A1& __a1)
1770 {
1771 ::new((void*)__p) _Tp(__a0, __a1);
1772 }
1773 template <class _A0, class _A1>
1774 _LIBCPP_INLINE_VISIBILITY
1775 void
1776 construct(pointer __p, const _A0& __a0, _A1& __a1)
1777 {
1778 ::new((void*)__p) _Tp(__a0, __a1);
1779 }
1780 template <class _A0, class _A1>
1781 _LIBCPP_INLINE_VISIBILITY
1782 void
1783 construct(pointer __p, _A0& __a0, const _A1& __a1)
1784 {
1785 ::new((void*)__p) _Tp(__a0, __a1);
1786 }
1787 template <class _A0, class _A1>
1788 _LIBCPP_INLINE_VISIBILITY
1789 void
1790 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1791 {
1792 ::new((void*)__p) _Tp(__a0, __a1);
1793 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001794#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001795 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1796};
1797
Howard Hinnant57199402012-01-02 17:56:02 +00001798template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001799class _LIBCPP_TYPE_VIS_ONLY allocator<const _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00001800{
1801public:
1802 typedef size_t size_type;
1803 typedef ptrdiff_t difference_type;
1804 typedef const _Tp* pointer;
1805 typedef const _Tp* const_pointer;
1806 typedef const _Tp& reference;
1807 typedef const _Tp& const_reference;
Howard Hinnant9360e9f2013-06-07 01:56:37 +00001808 typedef const _Tp value_type;
Howard Hinnant57199402012-01-02 17:56:02 +00001809
1810 typedef true_type propagate_on_container_move_assignment;
Marshall Clowb81d6f52015-07-01 21:23:40 +00001811 typedef true_type is_always_equal;
Howard Hinnant57199402012-01-02 17:56:02 +00001812
1813 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1814
1815 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1816 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1817 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1818 {return _VSTD::addressof(__x);}
1819 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Richard Smith73c1fce2014-06-04 19:54:15 +00001820 {return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));}
Howard Hinnant57199402012-01-02 17:56:02 +00001821 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith73c1fce2014-06-04 19:54:15 +00001822 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant57199402012-01-02 17:56:02 +00001823 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1824 {return size_type(~0) / sizeof(_Tp);}
1825#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1826 template <class _Up, class... _Args>
1827 _LIBCPP_INLINE_VISIBILITY
1828 void
1829 construct(_Up* __p, _Args&&... __args)
1830 {
1831 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1832 }
1833#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1834 _LIBCPP_INLINE_VISIBILITY
1835 void
1836 construct(pointer __p)
1837 {
1838 ::new((void*)__p) _Tp();
1839 }
1840# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001841
Howard Hinnant57199402012-01-02 17:56:02 +00001842 template <class _A0>
1843 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001844 void
Howard Hinnant57199402012-01-02 17:56:02 +00001845 construct(pointer __p, _A0& __a0)
1846 {
1847 ::new((void*)__p) _Tp(__a0);
1848 }
1849 template <class _A0>
1850 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001851 void
Howard Hinnant57199402012-01-02 17:56:02 +00001852 construct(pointer __p, const _A0& __a0)
1853 {
1854 ::new((void*)__p) _Tp(__a0);
1855 }
Howard Hinnant57199402012-01-02 17:56:02 +00001856# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1857 template <class _A0, class _A1>
1858 _LIBCPP_INLINE_VISIBILITY
1859 void
1860 construct(pointer __p, _A0& __a0, _A1& __a1)
1861 {
1862 ::new((void*)__p) _Tp(__a0, __a1);
1863 }
1864 template <class _A0, class _A1>
1865 _LIBCPP_INLINE_VISIBILITY
1866 void
1867 construct(pointer __p, const _A0& __a0, _A1& __a1)
1868 {
1869 ::new((void*)__p) _Tp(__a0, __a1);
1870 }
1871 template <class _A0, class _A1>
1872 _LIBCPP_INLINE_VISIBILITY
1873 void
1874 construct(pointer __p, _A0& __a0, const _A1& __a1)
1875 {
1876 ::new((void*)__p) _Tp(__a0, __a1);
1877 }
1878 template <class _A0, class _A1>
1879 _LIBCPP_INLINE_VISIBILITY
1880 void
1881 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1882 {
1883 ::new((void*)__p) _Tp(__a0, __a1);
1884 }
1885#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1886 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1887};
1888
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001889template <class _Tp, class _Up>
1890inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001891bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001892
1893template <class _Tp, class _Up>
1894inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001895bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001896
1897template <class _OutputIterator, class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001898class _LIBCPP_TYPE_VIS_ONLY raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001899 : public iterator<output_iterator_tag,
1900 _Tp, // purposefully not C++03
1901 ptrdiff_t, // purposefully not C++03
1902 _Tp*, // purposefully not C++03
1903 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1904{
1905private:
1906 _OutputIterator __x_;
1907public:
1908 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1909 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1910 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1911 {::new(&*__x_) _Tp(__element); return *this;}
Marshall Clow332ab912015-10-25 18:58:07 +00001912#if _LIBCPP_STD_VER >= 14
1913 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1914 {::new(&*__x_) _Tp(_VSTD::move(__element)); return *this;}
1915#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001916 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1917 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1918 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowdbaf7a02015-05-10 13:14:08 +00001919#if _LIBCPP_STD_VER >= 14
1920 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1921#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001922};
1923
1924template <class _Tp>
1925pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00001926get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001927{
1928 pair<_Tp*, ptrdiff_t> __r(0, 0);
1929 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1930 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1931 / sizeof(_Tp);
1932 if (__n > __m)
1933 __n = __m;
1934 while (__n > 0)
1935 {
1936 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1937 if (__r.first)
1938 {
1939 __r.second = __n;
1940 break;
1941 }
1942 __n /= 2;
1943 }
1944 return __r;
1945}
1946
1947template <class _Tp>
1948inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001949void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001950
1951template <class _Tp>
1952struct auto_ptr_ref
1953{
1954 _Tp* __ptr_;
1955};
1956
1957template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001958class _LIBCPP_TYPE_VIS_ONLY auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001959{
1960private:
1961 _Tp* __ptr_;
1962public:
1963 typedef _Tp element_type;
1964
1965 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1966 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1967 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1968 : __ptr_(__p.release()) {}
1969 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1970 {reset(__p.release()); return *this;}
1971 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1972 {reset(__p.release()); return *this;}
1973 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1974 {reset(__p.__ptr_); return *this;}
1975 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1976
1977 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1978 {return *__ptr_;}
1979 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1980 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1981 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1982 {
1983 _Tp* __t = __ptr_;
1984 __ptr_ = 0;
1985 return __t;
1986 }
1987 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1988 {
1989 if (__ptr_ != __p)
1990 delete __ptr_;
1991 __ptr_ = __p;
1992 }
1993
1994 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1995 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1996 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1997 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1998 {return auto_ptr<_Up>(release());}
1999};
2000
2001template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002002class _LIBCPP_TYPE_VIS_ONLY auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002003{
2004public:
2005 typedef void element_type;
2006};
2007
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002008template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
2009 typename remove_cv<_T2>::type>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00002010 bool = is_empty<_T1>::value
Eric Fiselier3a0e4302015-06-13 07:08:02 +00002011 && !__libcpp_is_final<_T1>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00002012 bool = is_empty<_T2>::value
Eric Fiselier3a0e4302015-06-13 07:08:02 +00002013 && !__libcpp_is_final<_T2>::value
Howard Hinnantd4cf2152011-12-11 20:31:33 +00002014 >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002015struct __libcpp_compressed_pair_switch;
2016
2017template <class _T1, class _T2, bool IsSame>
2018struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
2019
2020template <class _T1, class _T2, bool IsSame>
2021struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
2022
2023template <class _T1, class _T2, bool IsSame>
2024struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
2025
2026template <class _T1, class _T2>
2027struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
2028
2029template <class _T1, class _T2>
2030struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
2031
2032template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
2033class __libcpp_compressed_pair_imp;
2034
2035template <class _T1, class _T2>
2036class __libcpp_compressed_pair_imp<_T1, _T2, 0>
2037{
2038private:
2039 _T1 __first_;
2040 _T2 __second_;
2041public:
2042 typedef _T1 _T1_param;
2043 typedef _T2 _T2_param;
2044
2045 typedef typename remove_reference<_T1>::type& _T1_reference;
2046 typedef typename remove_reference<_T2>::type& _T2_reference;
2047
2048 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2049 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2050
Marshall Clowcd6ed542015-07-16 03:05:06 +00002051 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_(), __second_() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002052 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowcd6ed542015-07-16 03:05:06 +00002053 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002054 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowcd6ed542015-07-16 03:05:06 +00002055 : __first_(), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002056 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002057 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002058
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002059#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002060
2061 _LIBCPP_INLINE_VISIBILITY
2062 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2063 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2064 is_nothrow_copy_constructible<_T2>::value)
2065 : __first_(__p.first()),
2066 __second_(__p.second()) {}
2067
2068 _LIBCPP_INLINE_VISIBILITY
2069 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2070 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2071 is_nothrow_copy_assignable<_T2>::value)
2072 {
2073 __first_ = __p.first();
2074 __second_ = __p.second();
2075 return *this;
2076 }
2077
Howard Hinnant61aa6012011-07-01 19:24:36 +00002078 _LIBCPP_INLINE_VISIBILITY
2079 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002080 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2081 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002082 : __first_(_VSTD::forward<_T1>(__p.first())),
2083 __second_(_VSTD::forward<_T2>(__p.second())) {}
2084
2085 _LIBCPP_INLINE_VISIBILITY
2086 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2087 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2088 is_nothrow_move_assignable<_T2>::value)
2089 {
2090 __first_ = _VSTD::forward<_T1>(__p.first());
2091 __second_ = _VSTD::forward<_T2>(__p.second());
2092 return *this;
2093 }
2094
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002095#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002096
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002097#ifndef _LIBCPP_HAS_NO_VARIADICS
2098
2099 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2100 _LIBCPP_INLINE_VISIBILITY
2101 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2102 tuple<_Args1...> __first_args,
2103 tuple<_Args2...> __second_args,
2104 __tuple_indices<_I1...>,
2105 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002106 : __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2107 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002108 {}
2109
2110#endif // _LIBCPP_HAS_NO_VARIADICS
2111
Howard Hinnant1694d232011-05-28 14:41:13 +00002112 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2113 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002114
Howard Hinnant1694d232011-05-28 14:41:13 +00002115 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2116 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002117
2118 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002119 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002120 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002121 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002122 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002123 swap(__first_, __x.__first_);
2124 swap(__second_, __x.__second_);
2125 }
2126};
2127
2128template <class _T1, class _T2>
2129class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2130 : private _T1
2131{
2132private:
2133 _T2 __second_;
2134public:
2135 typedef _T1 _T1_param;
2136 typedef _T2 _T2_param;
2137
2138 typedef _T1& _T1_reference;
2139 typedef typename remove_reference<_T2>::type& _T2_reference;
2140
2141 typedef const _T1& _T1_const_reference;
2142 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2143
Marshall Clowcd6ed542015-07-16 03:05:06 +00002144 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __second_() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002145 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowcd6ed542015-07-16 03:05:06 +00002146 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002147 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002148 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002149 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002150 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002151
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002152#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002153
2154 _LIBCPP_INLINE_VISIBILITY
2155 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2156 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2157 is_nothrow_copy_constructible<_T2>::value)
2158 : _T1(__p.first()), __second_(__p.second()) {}
2159
2160 _LIBCPP_INLINE_VISIBILITY
2161 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2162 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2163 is_nothrow_copy_assignable<_T2>::value)
2164 {
2165 _T1::operator=(__p.first());
2166 __second_ = __p.second();
2167 return *this;
2168 }
2169
Howard Hinnant61aa6012011-07-01 19:24:36 +00002170 _LIBCPP_INLINE_VISIBILITY
2171 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002172 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2173 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002174 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002175
2176 _LIBCPP_INLINE_VISIBILITY
2177 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2178 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2179 is_nothrow_move_assignable<_T2>::value)
2180 {
2181 _T1::operator=(_VSTD::move(__p.first()));
2182 __second_ = _VSTD::forward<_T2>(__p.second());
2183 return *this;
2184 }
2185
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002186#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002187
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002188#ifndef _LIBCPP_HAS_NO_VARIADICS
2189
2190 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2191 _LIBCPP_INLINE_VISIBILITY
2192 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2193 tuple<_Args1...> __first_args,
2194 tuple<_Args2...> __second_args,
2195 __tuple_indices<_I1...>,
2196 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002197 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2198 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002199 {}
2200
2201#endif // _LIBCPP_HAS_NO_VARIADICS
2202
Howard Hinnant1694d232011-05-28 14:41:13 +00002203 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2204 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002205
Howard Hinnant1694d232011-05-28 14:41:13 +00002206 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2207 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002208
2209 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002210 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002211 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002212 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002213 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002214 swap(__second_, __x.__second_);
2215 }
2216};
2217
2218template <class _T1, class _T2>
2219class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2220 : private _T2
2221{
2222private:
2223 _T1 __first_;
2224public:
2225 typedef _T1 _T1_param;
2226 typedef _T2 _T2_param;
2227
2228 typedef typename remove_reference<_T1>::type& _T1_reference;
2229 typedef _T2& _T2_reference;
2230
2231 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2232 typedef const _T2& _T2_const_reference;
2233
Marshall Clowcd6ed542015-07-16 03:05:06 +00002234 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002235 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002236 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002237 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowcd6ed542015-07-16 03:05:06 +00002238 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002239 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00002240 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2241 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002242 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002243
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002244#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002245
2246 _LIBCPP_INLINE_VISIBILITY
2247 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2248 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2249 is_nothrow_copy_constructible<_T2>::value)
2250 : _T2(__p.second()), __first_(__p.first()) {}
2251
2252 _LIBCPP_INLINE_VISIBILITY
2253 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2254 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2255 is_nothrow_copy_assignable<_T2>::value)
2256 {
2257 _T2::operator=(__p.second());
2258 __first_ = __p.first();
2259 return *this;
2260 }
2261
Howard Hinnant61aa6012011-07-01 19:24:36 +00002262 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002263 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002264 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2265 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002266 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002267
2268 _LIBCPP_INLINE_VISIBILITY
2269 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2270 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2271 is_nothrow_move_assignable<_T2>::value)
2272 {
2273 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2274 __first_ = _VSTD::move(__p.first());
2275 return *this;
2276 }
2277
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002278#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002279
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002280#ifndef _LIBCPP_HAS_NO_VARIADICS
2281
2282 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2283 _LIBCPP_INLINE_VISIBILITY
2284 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2285 tuple<_Args1...> __first_args,
2286 tuple<_Args2...> __second_args,
2287 __tuple_indices<_I1...>,
2288 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002289 : _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...),
2290 __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002291
2292 {}
2293
2294#endif // _LIBCPP_HAS_NO_VARIADICS
2295
Howard Hinnant1694d232011-05-28 14:41:13 +00002296 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2297 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002298
Howard Hinnant1694d232011-05-28 14:41:13 +00002299 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2300 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002301
2302 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002303 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002304 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002305 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002306 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002307 swap(__first_, __x.__first_);
2308 }
2309};
2310
2311template <class _T1, class _T2>
2312class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2313 : private _T1,
2314 private _T2
2315{
2316public:
2317 typedef _T1 _T1_param;
2318 typedef _T2 _T2_param;
2319
2320 typedef _T1& _T1_reference;
2321 typedef _T2& _T2_reference;
2322
2323 typedef const _T1& _T1_const_reference;
2324 typedef const _T2& _T2_const_reference;
2325
2326 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2327 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002328 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002329 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002330 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002331 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002332 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002333
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002334#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002335
2336 _LIBCPP_INLINE_VISIBILITY
2337 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2338 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2339 is_nothrow_copy_constructible<_T2>::value)
2340 : _T1(__p.first()), _T2(__p.second()) {}
2341
2342 _LIBCPP_INLINE_VISIBILITY
2343 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2344 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2345 is_nothrow_copy_assignable<_T2>::value)
2346 {
2347 _T1::operator=(__p.first());
2348 _T2::operator=(__p.second());
2349 return *this;
2350 }
2351
Howard Hinnant61aa6012011-07-01 19:24:36 +00002352 _LIBCPP_INLINE_VISIBILITY
2353 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002354 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2355 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002356 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002357
2358 _LIBCPP_INLINE_VISIBILITY
2359 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2360 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2361 is_nothrow_move_assignable<_T2>::value)
2362 {
2363 _T1::operator=(_VSTD::move(__p.first()));
2364 _T2::operator=(_VSTD::move(__p.second()));
2365 return *this;
2366 }
2367
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002368#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002369
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002370#ifndef _LIBCPP_HAS_NO_VARIADICS
2371
2372 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2373 _LIBCPP_INLINE_VISIBILITY
2374 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2375 tuple<_Args1...> __first_args,
2376 tuple<_Args2...> __second_args,
2377 __tuple_indices<_I1...>,
2378 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002379 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2380 _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002381 {}
2382
2383#endif // _LIBCPP_HAS_NO_VARIADICS
2384
Howard Hinnant1694d232011-05-28 14:41:13 +00002385 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2386 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002387
Howard Hinnant1694d232011-05-28 14:41:13 +00002388 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2389 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002390
Howard Hinnantec3773c2011-12-01 20:21:04 +00002391 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant1694d232011-05-28 14:41:13 +00002392 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002393 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002394 {
2395 }
2396};
2397
2398template <class _T1, class _T2>
2399class __compressed_pair
2400 : private __libcpp_compressed_pair_imp<_T1, _T2>
2401{
2402 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2403public:
2404 typedef typename base::_T1_param _T1_param;
2405 typedef typename base::_T2_param _T2_param;
2406
2407 typedef typename base::_T1_reference _T1_reference;
2408 typedef typename base::_T2_reference _T2_reference;
2409
2410 typedef typename base::_T1_const_reference _T1_const_reference;
2411 typedef typename base::_T2_const_reference _T2_const_reference;
2412
2413 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002414 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002415 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002416 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002417 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002418 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002419 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002420
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002421#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002422
2423 _LIBCPP_INLINE_VISIBILITY
2424 __compressed_pair(const __compressed_pair& __p)
2425 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2426 is_nothrow_copy_constructible<_T2>::value)
2427 : base(__p) {}
2428
2429 _LIBCPP_INLINE_VISIBILITY
2430 __compressed_pair& operator=(const __compressed_pair& __p)
2431 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2432 is_nothrow_copy_assignable<_T2>::value)
2433 {
2434 base::operator=(__p);
2435 return *this;
2436 }
2437
Howard Hinnant61aa6012011-07-01 19:24:36 +00002438 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002439 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002440 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2441 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002442 : base(_VSTD::move(__p)) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002443
2444 _LIBCPP_INLINE_VISIBILITY
2445 __compressed_pair& operator=(__compressed_pair&& __p)
2446 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2447 is_nothrow_move_assignable<_T2>::value)
2448 {
2449 base::operator=(_VSTD::move(__p));
2450 return *this;
2451 }
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002452
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002453#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002454
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002455#ifndef _LIBCPP_HAS_NO_VARIADICS
2456
2457 template <class... _Args1, class... _Args2>
2458 _LIBCPP_INLINE_VISIBILITY
2459 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2460 tuple<_Args2...> __second_args)
Howard Hinnant57199402012-01-02 17:56:02 +00002461 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002462 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2463 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2464 {}
2465
2466#endif // _LIBCPP_HAS_NO_VARIADICS
2467
Howard Hinnant1694d232011-05-28 14:41:13 +00002468 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2469 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002470
Howard Hinnant1694d232011-05-28 14:41:13 +00002471 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2472 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002473
Howard Hinnant1694d232011-05-28 14:41:13 +00002474 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2475 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002476 __is_nothrow_swappable<_T2>::value)
Howard Hinnant1694d232011-05-28 14:41:13 +00002477 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002478};
2479
2480template <class _T1, class _T2>
2481inline _LIBCPP_INLINE_VISIBILITY
2482void
2483swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002484 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002485 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002486 {__x.swap(__y);}
2487
Howard Hinnant57199402012-01-02 17:56:02 +00002488// __same_or_less_cv_qualified
2489
2490template <class _Ptr1, class _Ptr2,
2491 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2492 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2493 >::value
2494 >
2495struct __same_or_less_cv_qualified_imp
2496 : is_convertible<_Ptr1, _Ptr2> {};
2497
2498template <class _Ptr1, class _Ptr2>
2499struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2500 : false_type {};
2501
Marshall Clow5f64a2b2014-04-26 05:19:48 +00002502template <class _Ptr1, class _Ptr2, bool = is_pointer<_Ptr1>::value ||
2503 is_same<_Ptr1, _Ptr2>::value ||
2504 __has_element_type<_Ptr1>::value>
Howard Hinnant57199402012-01-02 17:56:02 +00002505struct __same_or_less_cv_qualified
2506 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2507
2508template <class _Ptr1, class _Ptr2>
Marshall Clow5f64a2b2014-04-26 05:19:48 +00002509struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, false>
Howard Hinnant57199402012-01-02 17:56:02 +00002510 : false_type {};
2511
2512// default_delete
2513
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002514template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002515struct _LIBCPP_TYPE_VIS_ONLY default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002516{
Howard Hinnant46e94932012-07-07 20:56:04 +00002517#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2518 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2519#else
2520 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2521#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002522 template <class _Up>
2523 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002524 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2525 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002526 {
2527 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant05e7d242013-04-24 19:44:26 +00002528 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002529 delete __ptr;
2530 }
2531};
2532
2533template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002534struct _LIBCPP_TYPE_VIS_ONLY default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002535{
Howard Hinnant8e843502011-12-18 21:19:44 +00002536public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002537#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2538 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2539#else
2540 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2541#endif
Howard Hinnant8e843502011-12-18 21:19:44 +00002542 template <class _Up>
2543 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant57199402012-01-02 17:56:02 +00002544 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant8e843502011-12-18 21:19:44 +00002545 template <class _Up>
2546 _LIBCPP_INLINE_VISIBILITY
2547 void operator() (_Up* __ptr,
Howard Hinnant57199402012-01-02 17:56:02 +00002548 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002549 {
2550 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant05e7d242013-04-24 19:44:26 +00002551 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002552 delete [] __ptr;
2553 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002554};
2555
2556template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002557class _LIBCPP_TYPE_VIS_ONLY unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002558{
2559public:
2560 typedef _Tp element_type;
2561 typedef _Dp deleter_type;
2562 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2563private:
2564 __compressed_pair<pointer, deleter_type> __ptr_;
2565
Howard Hinnant57199402012-01-02 17:56:02 +00002566#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002567 unique_ptr(unique_ptr&);
2568 template <class _Up, class _Ep>
2569 unique_ptr(unique_ptr<_Up, _Ep>&);
2570 unique_ptr& operator=(unique_ptr&);
2571 template <class _Up, class _Ep>
2572 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002573#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002574
2575 struct __nat {int __for_bool_;};
2576
2577 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2578 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2579public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002580 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002581 : __ptr_(pointer())
2582 {
2583 static_assert(!is_pointer<deleter_type>::value,
2584 "unique_ptr constructed with null function pointer deleter");
2585 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002586 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002587 : __ptr_(pointer())
2588 {
2589 static_assert(!is_pointer<deleter_type>::value,
2590 "unique_ptr constructed with null function pointer deleter");
2591 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002592 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002593 : __ptr_(_VSTD::move(__p))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002594 {
2595 static_assert(!is_pointer<deleter_type>::value,
2596 "unique_ptr constructed with null function pointer deleter");
2597 }
2598
Howard Hinnant73d21a42010-09-04 23:28:19 +00002599#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002600 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2601 is_reference<deleter_type>::value,
2602 deleter_type,
2603 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002604 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002605 : __ptr_(__p, __d) {}
2606
2607 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002608 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002609 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002610 {
2611 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2612 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002613 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002614 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002615 template <class _Up, class _Ep>
2616 _LIBCPP_INLINE_VISIBILITY
2617 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2618 typename enable_if
2619 <
2620 !is_array<_Up>::value &&
2621 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2622 is_convertible<_Ep, deleter_type>::value &&
2623 (
2624 !is_reference<deleter_type>::value ||
2625 is_same<deleter_type, _Ep>::value
2626 ),
2627 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002628 >::type = __nat()) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002629 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002630
2631 template <class _Up>
2632 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2633 typename enable_if<
2634 is_convertible<_Up*, _Tp*>::value &&
2635 is_same<_Dp, default_delete<_Tp> >::value,
2636 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002637 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002638 : __ptr_(__p.release())
2639 {
2640 }
2641
Howard Hinnant1694d232011-05-28 14:41:13 +00002642 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002643 {
2644 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002645 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002646 return *this;
2647 }
2648
2649 template <class _Up, class _Ep>
2650 _LIBCPP_INLINE_VISIBILITY
2651 typename enable_if
2652 <
Howard Hinnant57199402012-01-02 17:56:02 +00002653 !is_array<_Up>::value &&
2654 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2655 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002656 unique_ptr&
2657 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002658 operator=(unique_ptr<_Up, _Ep>&& __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<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002662 return *this;
2663 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002664#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002665
2666 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2667 {
2668 return __rv<unique_ptr>(*this);
2669 }
2670
2671 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002672 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002673
2674 template <class _Up, class _Ep>
Eric Fiselieraff153a2015-08-28 05:07:06 +00002675 _LIBCPP_INLINE_VISIBILITY
2676 typename enable_if<
2677 !is_array<_Up>::value &&
2678 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2679 is_assignable<deleter_type&, _Ep&>::value,
2680 unique_ptr&
2681 >::type
2682 operator=(unique_ptr<_Up, _Ep> __u)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002683 {
2684 reset(__u.release());
Eric Fiselieraff153a2015-08-28 05:07:06 +00002685 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002686 return *this;
2687 }
2688
2689 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002690 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002691
2692 template <class _Up>
2693 _LIBCPP_INLINE_VISIBILITY
2694 typename enable_if<
2695 is_convertible<_Up*, _Tp*>::value &&
2696 is_same<_Dp, default_delete<_Tp> >::value,
2697 unique_ptr&
2698 >::type
2699 operator=(auto_ptr<_Up> __p)
2700 {reset(__p.release()); return *this;}
2701
Howard Hinnant73d21a42010-09-04 23:28:19 +00002702#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002703 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2704
Howard Hinnant1694d232011-05-28 14:41:13 +00002705 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002706 {
2707 reset();
2708 return *this;
2709 }
2710
2711 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2712 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002713 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2714 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2715 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2716 {return __ptr_.second();}
2717 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2718 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002719 _LIBCPP_INLINE_VISIBILITY
2720 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2721 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002722
Howard Hinnant1694d232011-05-28 14:41:13 +00002723 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002724 {
2725 pointer __t = __ptr_.first();
2726 __ptr_.first() = pointer();
2727 return __t;
2728 }
2729
Howard Hinnant1694d232011-05-28 14:41:13 +00002730 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002731 {
2732 pointer __tmp = __ptr_.first();
2733 __ptr_.first() = __p;
2734 if (__tmp)
2735 __ptr_.second()(__tmp);
2736 }
2737
Howard Hinnant1694d232011-05-28 14:41:13 +00002738 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2739 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002740};
2741
2742template <class _Tp, class _Dp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002743class _LIBCPP_TYPE_VIS_ONLY unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002744{
2745public:
2746 typedef _Tp element_type;
2747 typedef _Dp deleter_type;
2748 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2749private:
2750 __compressed_pair<pointer, deleter_type> __ptr_;
2751
Howard Hinnant57199402012-01-02 17:56:02 +00002752#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002753 unique_ptr(unique_ptr&);
2754 template <class _Up>
2755 unique_ptr(unique_ptr<_Up>&);
2756 unique_ptr& operator=(unique_ptr&);
2757 template <class _Up>
2758 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002759#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002760
2761 struct __nat {int __for_bool_;};
2762
2763 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2764 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2765public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002766 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002767 : __ptr_(pointer())
2768 {
2769 static_assert(!is_pointer<deleter_type>::value,
2770 "unique_ptr constructed with null function pointer deleter");
2771 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002772 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002773 : __ptr_(pointer())
2774 {
2775 static_assert(!is_pointer<deleter_type>::value,
2776 "unique_ptr constructed with null function pointer deleter");
2777 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002778#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00002779 template <class _Pp>
2780 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p,
2781 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002782 : __ptr_(__p)
2783 {
2784 static_assert(!is_pointer<deleter_type>::value,
2785 "unique_ptr constructed with null function pointer deleter");
2786 }
2787
Logan Chiene1678a12014-01-31 09:30:46 +00002788 template <class _Pp>
Howard Hinnant99968442011-11-29 18:15:50 +00002789 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002790 is_reference<deleter_type>::value,
2791 deleter_type,
Logan Chiene1678a12014-01-31 09:30:46 +00002792 typename add_lvalue_reference<const deleter_type>::type>::type __d,
2793 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant1694d232011-05-28 14:41:13 +00002794 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002795 : __ptr_(__p, __d) {}
2796
2797 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2798 is_reference<deleter_type>::value,
2799 deleter_type,
2800 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002801 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002802 : __ptr_(pointer(), __d) {}
2803
Logan Chiene1678a12014-01-31 09:30:46 +00002804 template <class _Pp>
2805 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p,
2806 typename remove_reference<deleter_type>::type&& __d,
2807 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant1694d232011-05-28 14:41:13 +00002808 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002809 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002810 {
2811 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2812 }
2813
2814 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002815 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002816 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002817 {
2818 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2819 }
2820
Howard Hinnant1694d232011-05-28 14:41:13 +00002821 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002822 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002823
Howard Hinnant1694d232011-05-28 14:41:13 +00002824 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002825 {
2826 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002827 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002828 return *this;
2829 }
Howard Hinnant8e843502011-12-18 21:19:44 +00002830
2831 template <class _Up, class _Ep>
2832 _LIBCPP_INLINE_VISIBILITY
2833 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2834 typename enable_if
2835 <
2836 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002837 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant8e843502011-12-18 21:19:44 +00002838 && is_convertible<_Ep, deleter_type>::value &&
2839 (
2840 !is_reference<deleter_type>::value ||
2841 is_same<deleter_type, _Ep>::value
2842 ),
2843 __nat
2844 >::type = __nat()
2845 ) _NOEXCEPT
2846 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2847
2848
2849 template <class _Up, class _Ep>
2850 _LIBCPP_INLINE_VISIBILITY
2851 typename enable_if
2852 <
2853 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002854 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2855 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant8e843502011-12-18 21:19:44 +00002856 unique_ptr&
2857 >::type
2858 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2859 {
2860 reset(__u.release());
2861 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2862 return *this;
2863 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002864#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002865
2866 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2867 : __ptr_(__p)
2868 {
2869 static_assert(!is_pointer<deleter_type>::value,
2870 "unique_ptr constructed with null function pointer deleter");
2871 }
2872
2873 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002874 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002875
2876 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002877 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002878
2879 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2880 {
2881 return __rv<unique_ptr>(*this);
2882 }
2883
2884 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002885 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002886
2887 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2888 {
2889 reset(__u->release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002890 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002891 return *this;
2892 }
2893
Howard Hinnant73d21a42010-09-04 23:28:19 +00002894#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002895 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2896
Howard Hinnant1694d232011-05-28 14:41:13 +00002897 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002898 {
2899 reset();
2900 return *this;
2901 }
2902
2903 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2904 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002905 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2906 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2907 {return __ptr_.second();}
2908 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2909 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002910 _LIBCPP_INLINE_VISIBILITY
2911 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2912 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002913
Howard Hinnant1694d232011-05-28 14:41:13 +00002914 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002915 {
2916 pointer __t = __ptr_.first();
2917 __ptr_.first() = pointer();
2918 return __t;
2919 }
2920
Howard Hinnant73d21a42010-09-04 23:28:19 +00002921#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00002922 template <class _Pp>
2923 _LIBCPP_INLINE_VISIBILITY
2924 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, void>::type
2925 reset(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002926 {
2927 pointer __tmp = __ptr_.first();
2928 __ptr_.first() = __p;
2929 if (__tmp)
2930 __ptr_.second()(__tmp);
2931 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002932 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002933 {
2934 pointer __tmp = __ptr_.first();
2935 __ptr_.first() = nullptr;
2936 if (__tmp)
2937 __ptr_.second()(__tmp);
2938 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002939 _LIBCPP_INLINE_VISIBILITY void reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002940 {
2941 pointer __tmp = __ptr_.first();
2942 __ptr_.first() = nullptr;
2943 if (__tmp)
2944 __ptr_.second()(__tmp);
2945 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002946#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002947 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2948 {
2949 pointer __tmp = __ptr_.first();
2950 __ptr_.first() = __p;
2951 if (__tmp)
2952 __ptr_.second()(__tmp);
2953 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002954#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002955
2956 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2957private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002958
Howard Hinnant73d21a42010-09-04 23:28:19 +00002959#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002960 template <class _Up>
2961 explicit unique_ptr(_Up);
2962 template <class _Up>
2963 unique_ptr(_Up __u,
2964 typename conditional<
2965 is_reference<deleter_type>::value,
2966 deleter_type,
2967 typename add_lvalue_reference<const deleter_type>::type>::type,
2968 typename enable_if
2969 <
2970 is_convertible<_Up, pointer>::value,
2971 __nat
2972 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002973#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002974};
2975
2976template <class _Tp, class _Dp>
2977inline _LIBCPP_INLINE_VISIBILITY
2978void
Howard Hinnant1694d232011-05-28 14:41:13 +00002979swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002980
2981template <class _T1, class _D1, class _T2, class _D2>
2982inline _LIBCPP_INLINE_VISIBILITY
2983bool
2984operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2985
2986template <class _T1, class _D1, class _T2, class _D2>
2987inline _LIBCPP_INLINE_VISIBILITY
2988bool
2989operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2990
2991template <class _T1, class _D1, class _T2, class _D2>
2992inline _LIBCPP_INLINE_VISIBILITY
2993bool
Howard Hinnant3fadda32012-02-21 21:02:58 +00002994operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2995{
2996 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2997 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselier8492cd82015-02-05 23:01:40 +00002998 typedef typename common_type<_P1, _P2>::type _Vp;
2999 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnant3fadda32012-02-21 21:02:58 +00003000}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003001
3002template <class _T1, class _D1, class _T2, class _D2>
3003inline _LIBCPP_INLINE_VISIBILITY
3004bool
3005operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
3006
3007template <class _T1, class _D1, class _T2, class _D2>
3008inline _LIBCPP_INLINE_VISIBILITY
3009bool
3010operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
3011
3012template <class _T1, class _D1, class _T2, class _D2>
3013inline _LIBCPP_INLINE_VISIBILITY
3014bool
3015operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
3016
Howard Hinnant3fadda32012-02-21 21:02:58 +00003017template <class _T1, class _D1>
3018inline _LIBCPP_INLINE_VISIBILITY
3019bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003020operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003021{
3022 return !__x;
3023}
3024
3025template <class _T1, class _D1>
3026inline _LIBCPP_INLINE_VISIBILITY
3027bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003028operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003029{
3030 return !__x;
3031}
3032
3033template <class _T1, class _D1>
3034inline _LIBCPP_INLINE_VISIBILITY
3035bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003036operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003037{
3038 return static_cast<bool>(__x);
3039}
3040
3041template <class _T1, class _D1>
3042inline _LIBCPP_INLINE_VISIBILITY
3043bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003044operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003045{
3046 return static_cast<bool>(__x);
3047}
3048
3049template <class _T1, class _D1>
3050inline _LIBCPP_INLINE_VISIBILITY
3051bool
3052operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3053{
3054 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3055 return less<_P1>()(__x.get(), nullptr);
3056}
3057
3058template <class _T1, class _D1>
3059inline _LIBCPP_INLINE_VISIBILITY
3060bool
3061operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3062{
3063 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3064 return less<_P1>()(nullptr, __x.get());
3065}
3066
3067template <class _T1, class _D1>
3068inline _LIBCPP_INLINE_VISIBILITY
3069bool
3070operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3071{
3072 return nullptr < __x;
3073}
3074
3075template <class _T1, class _D1>
3076inline _LIBCPP_INLINE_VISIBILITY
3077bool
3078operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3079{
3080 return __x < nullptr;
3081}
3082
3083template <class _T1, class _D1>
3084inline _LIBCPP_INLINE_VISIBILITY
3085bool
3086operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3087{
3088 return !(nullptr < __x);
3089}
3090
3091template <class _T1, class _D1>
3092inline _LIBCPP_INLINE_VISIBILITY
3093bool
3094operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3095{
3096 return !(__x < nullptr);
3097}
3098
3099template <class _T1, class _D1>
3100inline _LIBCPP_INLINE_VISIBILITY
3101bool
3102operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3103{
3104 return !(__x < nullptr);
3105}
3106
3107template <class _T1, class _D1>
3108inline _LIBCPP_INLINE_VISIBILITY
3109bool
3110operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3111{
3112 return !(nullptr < __x);
3113}
3114
Howard Hinnant87073e42012-05-01 15:37:54 +00003115#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3116
3117template <class _Tp, class _Dp>
3118inline _LIBCPP_INLINE_VISIBILITY
3119unique_ptr<_Tp, _Dp>
3120move(unique_ptr<_Tp, _Dp>& __t)
3121{
3122 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3123}
3124
3125#endif
3126
Marshall Clowfd7481e2013-07-01 18:16:03 +00003127#if _LIBCPP_STD_VER > 11
3128
3129template<class _Tp>
3130struct __unique_if
3131{
3132 typedef unique_ptr<_Tp> __unique_single;
3133};
3134
3135template<class _Tp>
3136struct __unique_if<_Tp[]>
3137{
3138 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3139};
3140
3141template<class _Tp, size_t _Np>
3142struct __unique_if<_Tp[_Np]>
3143{
3144 typedef void __unique_array_known_bound;
3145};
3146
3147template<class _Tp, class... _Args>
Marshall Clowfb551102013-07-02 20:06:09 +00003148inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003149typename __unique_if<_Tp>::__unique_single
3150make_unique(_Args&&... __args)
3151{
3152 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3153}
3154
3155template<class _Tp>
Marshall Clowfb551102013-07-02 20:06:09 +00003156inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003157typename __unique_if<_Tp>::__unique_array_unknown_bound
3158make_unique(size_t __n)
3159{
3160 typedef typename remove_extent<_Tp>::type _Up;
3161 return unique_ptr<_Tp>(new _Up[__n]());
3162}
3163
3164template<class _Tp, class... _Args>
3165 typename __unique_if<_Tp>::__unique_array_known_bound
3166 make_unique(_Args&&...) = delete;
3167
3168#endif // _LIBCPP_STD_VER > 11
3169
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00003170template <class _Tp> struct hash;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003171
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003172template <class _Size>
3173inline _LIBCPP_INLINE_VISIBILITY
3174_Size
3175__loadword(const void* __p)
3176{
3177 _Size __r;
3178 std::memcpy(&__r, __p, sizeof(__r));
3179 return __r;
3180}
3181
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003182// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
3183// is 64 bits. This is because cityhash64 uses 64bit x 64bit
3184// multiplication, which can be very slow on 32-bit systems.
Howard Hinnant40c13d32011-12-05 00:08:45 +00003185template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003186struct __murmur2_or_cityhash;
Howard Hinnant40c13d32011-12-05 00:08:45 +00003187
3188template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003189struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003190{
3191 _Size operator()(const void* __key, _Size __len);
3192};
3193
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003194// murmur2
Howard Hinnant40c13d32011-12-05 00:08:45 +00003195template <class _Size>
3196_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003197__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003198{
3199 const _Size __m = 0x5bd1e995;
3200 const _Size __r = 24;
3201 _Size __h = __len;
3202 const unsigned char* __data = static_cast<const unsigned char*>(__key);
3203 for (; __len >= 4; __data += 4, __len -= 4)
3204 {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003205 _Size __k = __loadword<_Size>(__data);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003206 __k *= __m;
3207 __k ^= __k >> __r;
3208 __k *= __m;
3209 __h *= __m;
3210 __h ^= __k;
3211 }
3212 switch (__len)
3213 {
3214 case 3:
3215 __h ^= __data[2] << 16;
3216 case 2:
3217 __h ^= __data[1] << 8;
3218 case 1:
3219 __h ^= __data[0];
3220 __h *= __m;
3221 }
3222 __h ^= __h >> 13;
3223 __h *= __m;
3224 __h ^= __h >> 15;
3225 return __h;
3226}
3227
3228template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003229struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003230{
3231 _Size operator()(const void* __key, _Size __len);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003232
3233 private:
3234 // Some primes between 2^63 and 2^64.
3235 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
3236 static const _Size __k1 = 0xb492b66fbe98f273ULL;
3237 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
3238 static const _Size __k3 = 0xc949d7c7509e6557ULL;
3239
3240 static _Size __rotate(_Size __val, int __shift) {
3241 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
3242 }
3243
3244 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
3245 return (__val >> __shift) | (__val << (64 - __shift));
3246 }
3247
3248 static _Size __shift_mix(_Size __val) {
3249 return __val ^ (__val >> 47);
3250 }
3251
3252 static _Size __hash_len_16(_Size __u, _Size __v) {
3253 const _Size __mul = 0x9ddfea08eb382d69ULL;
3254 _Size __a = (__u ^ __v) * __mul;
3255 __a ^= (__a >> 47);
3256 _Size __b = (__v ^ __a) * __mul;
3257 __b ^= (__b >> 47);
3258 __b *= __mul;
3259 return __b;
3260 }
3261
3262 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
3263 if (__len > 8) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003264 const _Size __a = __loadword<_Size>(__s);
3265 const _Size __b = __loadword<_Size>(__s + __len - 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003266 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
3267 }
3268 if (__len >= 4) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003269 const uint32_t __a = __loadword<uint32_t>(__s);
3270 const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003271 return __hash_len_16(__len + (__a << 3), __b);
3272 }
3273 if (__len > 0) {
3274 const unsigned char __a = __s[0];
3275 const unsigned char __b = __s[__len >> 1];
3276 const unsigned char __c = __s[__len - 1];
3277 const uint32_t __y = static_cast<uint32_t>(__a) +
3278 (static_cast<uint32_t>(__b) << 8);
3279 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3280 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3281 }
3282 return __k2;
3283 }
3284
3285 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003286 const _Size __a = __loadword<_Size>(__s) * __k1;
3287 const _Size __b = __loadword<_Size>(__s + 8);
3288 const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;
3289 const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003290 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3291 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3292 }
3293
3294 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3295 // Callers do best to use "random-looking" values for a and b.
3296 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3297 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3298 __a += __w;
3299 __b = __rotate(__b + __a + __z, 21);
3300 const _Size __c = __a;
3301 __a += __x;
3302 __a += __y;
3303 __b += __rotate(__a, 44);
3304 return pair<_Size, _Size>(__a + __z, __b + __c);
3305 }
3306
3307 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3308 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3309 const char* __s, _Size __a, _Size __b) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003310 return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),
3311 __loadword<_Size>(__s + 8),
3312 __loadword<_Size>(__s + 16),
3313 __loadword<_Size>(__s + 24),
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003314 __a,
3315 __b);
3316 }
3317
3318 // Return an 8-byte hash for 33 to 64 bytes.
3319 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003320 _Size __z = __loadword<_Size>(__s + 24);
3321 _Size __a = __loadword<_Size>(__s) +
3322 (__len + __loadword<_Size>(__s + __len - 16)) * __k0;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003323 _Size __b = __rotate(__a + __z, 52);
3324 _Size __c = __rotate(__a, 37);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003325 __a += __loadword<_Size>(__s + 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003326 __c += __rotate(__a, 7);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003327 __a += __loadword<_Size>(__s + 16);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003328 _Size __vf = __a + __z;
3329 _Size __vs = __b + __rotate(__a, 31) + __c;
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003330 __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);
3331 __z += __loadword<_Size>(__s + __len - 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003332 __b = __rotate(__a + __z, 52);
3333 __c = __rotate(__a, 37);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003334 __a += __loadword<_Size>(__s + __len - 24);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003335 __c += __rotate(__a, 7);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003336 __a += __loadword<_Size>(__s + __len - 16);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003337 _Size __wf = __a + __z;
3338 _Size __ws = __b + __rotate(__a, 31) + __c;
3339 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3340 return __shift_mix(__r * __k0 + __vs) * __k2;
3341 }
Howard Hinnant40c13d32011-12-05 00:08:45 +00003342};
3343
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003344// cityhash64
Howard Hinnant40c13d32011-12-05 00:08:45 +00003345template <class _Size>
3346_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003347__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003348{
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003349 const char* __s = static_cast<const char*>(__key);
3350 if (__len <= 32) {
3351 if (__len <= 16) {
3352 return __hash_len_0_to_16(__s, __len);
3353 } else {
3354 return __hash_len_17_to_32(__s, __len);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003355 }
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003356 } else if (__len <= 64) {
3357 return __hash_len_33_to_64(__s, __len);
3358 }
3359
3360 // For strings over 64 bytes we hash the end first, and then as we
3361 // loop we keep 56 bytes of state: v, w, x, y, and z.
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003362 _Size __x = __loadword<_Size>(__s + __len - 40);
3363 _Size __y = __loadword<_Size>(__s + __len - 16) +
3364 __loadword<_Size>(__s + __len - 56);
3365 _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,
3366 __loadword<_Size>(__s + __len - 24));
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003367 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3368 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003369 __x = __x * __k1 + __loadword<_Size>(__s);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003370
3371 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3372 __len = (__len - 1) & ~static_cast<_Size>(63);
3373 do {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003374 __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;
3375 __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003376 __x ^= __w.second;
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003377 __y += __v.first + __loadword<_Size>(__s + 40);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003378 __z = __rotate(__z + __w.first, 33) * __k1;
3379 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3380 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003381 __y + __loadword<_Size>(__s + 16));
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003382 std::swap(__z, __x);
3383 __s += 64;
3384 __len -= 64;
3385 } while (__len != 0);
3386 return __hash_len_16(
3387 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3388 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003389}
3390
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003391template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3392struct __scalar_hash;
3393
3394template <class _Tp>
3395struct __scalar_hash<_Tp, 0>
3396 : public unary_function<_Tp, size_t>
Howard Hinnant21aefc32010-06-03 16:42:57 +00003397{
Howard Hinnant82894812010-09-22 16:48:34 +00003398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003399 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003400 {
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003401 union
3402 {
3403 _Tp __t;
3404 size_t __a;
3405 } __u;
3406 __u.__a = 0;
3407 __u.__t = __v;
3408 return __u.__a;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003409 }
3410};
3411
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003412template <class _Tp>
3413struct __scalar_hash<_Tp, 1>
3414 : public unary_function<_Tp, size_t>
3415{
3416 _LIBCPP_INLINE_VISIBILITY
3417 size_t operator()(_Tp __v) const _NOEXCEPT
3418 {
3419 union
3420 {
3421 _Tp __t;
3422 size_t __a;
3423 } __u;
3424 __u.__t = __v;
3425 return __u.__a;
3426 }
3427};
3428
3429template <class _Tp>
3430struct __scalar_hash<_Tp, 2>
3431 : public unary_function<_Tp, size_t>
3432{
3433 _LIBCPP_INLINE_VISIBILITY
3434 size_t operator()(_Tp __v) const _NOEXCEPT
3435 {
3436 union
3437 {
3438 _Tp __t;
3439 struct
3440 {
3441 size_t __a;
3442 size_t __b;
Eric Fiselier692177d2015-07-18 20:40:46 +00003443 } __s;
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003444 } __u;
3445 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003446 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003447 }
3448};
3449
3450template <class _Tp>
3451struct __scalar_hash<_Tp, 3>
3452 : public unary_function<_Tp, size_t>
3453{
3454 _LIBCPP_INLINE_VISIBILITY
3455 size_t operator()(_Tp __v) const _NOEXCEPT
3456 {
3457 union
3458 {
3459 _Tp __t;
3460 struct
3461 {
3462 size_t __a;
3463 size_t __b;
3464 size_t __c;
Eric Fiselier692177d2015-07-18 20:40:46 +00003465 } __s;
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003466 } __u;
3467 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003468 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003469 }
3470};
3471
3472template <class _Tp>
3473struct __scalar_hash<_Tp, 4>
3474 : public unary_function<_Tp, size_t>
3475{
3476 _LIBCPP_INLINE_VISIBILITY
3477 size_t operator()(_Tp __v) const _NOEXCEPT
3478 {
3479 union
3480 {
3481 _Tp __t;
3482 struct
3483 {
3484 size_t __a;
3485 size_t __b;
3486 size_t __c;
3487 size_t __d;
Eric Fiselier692177d2015-07-18 20:40:46 +00003488 } __s;
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003489 } __u;
3490 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003491 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003492 }
3493};
3494
3495template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003496struct _LIBCPP_TYPE_VIS_ONLY hash<_Tp*>
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003497 : public unary_function<_Tp*, size_t>
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003498{
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003499 _LIBCPP_INLINE_VISIBILITY
3500 size_t operator()(_Tp* __v) const _NOEXCEPT
3501 {
3502 union
3503 {
3504 _Tp* __t;
3505 size_t __a;
3506 } __u;
3507 __u.__t = __v;
3508 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
3509 }
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003510};
3511
Howard Hinnant21aefc32010-06-03 16:42:57 +00003512template <class _Tp, class _Dp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003513struct _LIBCPP_TYPE_VIS_ONLY hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003514{
3515 typedef unique_ptr<_Tp, _Dp> argument_type;
3516 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003517 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003518 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003519 {
3520 typedef typename argument_type::pointer pointer;
3521 return hash<pointer>()(__ptr.get());
3522 }
3523};
3524
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003525struct __destruct_n
3526{
3527private:
3528 size_t size;
3529
3530 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003531 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003532 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3533
3534 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003535 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003536 {}
3537
Howard Hinnant1694d232011-05-28 14:41:13 +00003538 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003539 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003540 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003541 {}
3542
Howard Hinnant1694d232011-05-28 14:41:13 +00003543 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003544 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003545 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003546 {}
3547public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003548 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3549 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003550
3551 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003552 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003553 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003554
3555 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003556 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003557 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003558
3559 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003560 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003561 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003562};
3563
3564template <class _Alloc>
3565class __allocator_destructor
3566{
3567 typedef allocator_traits<_Alloc> __alloc_traits;
3568public:
3569 typedef typename __alloc_traits::pointer pointer;
3570 typedef typename __alloc_traits::size_type size_type;
3571private:
3572 _Alloc& __alloc_;
3573 size_type __s_;
3574public:
3575 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00003576 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003577 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003579 void operator()(pointer __p) _NOEXCEPT
3580 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003581};
3582
3583template <class _InputIterator, class _ForwardIterator>
3584_ForwardIterator
3585uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3586{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003587 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003588#ifndef _LIBCPP_NO_EXCEPTIONS
3589 _ForwardIterator __s = __r;
3590 try
3591 {
3592#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003593 for (; __f != __l; ++__f, (void) ++__r)
3594 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant8292d742011-12-29 17:45:35 +00003595#ifndef _LIBCPP_NO_EXCEPTIONS
3596 }
3597 catch (...)
3598 {
3599 for (; __s != __r; ++__s)
3600 __s->~value_type();
3601 throw;
3602 }
3603#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003604 return __r;
3605}
3606
3607template <class _InputIterator, class _Size, class _ForwardIterator>
3608_ForwardIterator
3609uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3610{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003611 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003612#ifndef _LIBCPP_NO_EXCEPTIONS
3613 _ForwardIterator __s = __r;
3614 try
3615 {
3616#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003617 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3618 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant8292d742011-12-29 17:45:35 +00003619#ifndef _LIBCPP_NO_EXCEPTIONS
3620 }
3621 catch (...)
3622 {
3623 for (; __s != __r; ++__s)
3624 __s->~value_type();
3625 throw;
3626 }
3627#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003628 return __r;
3629}
3630
3631template <class _ForwardIterator, class _Tp>
3632void
3633uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3634{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003635 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003636#ifndef _LIBCPP_NO_EXCEPTIONS
3637 _ForwardIterator __s = __f;
3638 try
3639 {
3640#endif
3641 for (; __f != __l; ++__f)
Marshall Clow5dce73d2015-05-19 15:01:48 +00003642 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant8292d742011-12-29 17:45:35 +00003643#ifndef _LIBCPP_NO_EXCEPTIONS
3644 }
3645 catch (...)
3646 {
3647 for (; __s != __f; ++__s)
3648 __s->~value_type();
3649 throw;
3650 }
3651#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003652}
3653
3654template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003655_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003656uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3657{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003658 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003659#ifndef _LIBCPP_NO_EXCEPTIONS
3660 _ForwardIterator __s = __f;
3661 try
3662 {
3663#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003664 for (; __n > 0; ++__f, (void) --__n)
3665 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant8292d742011-12-29 17:45:35 +00003666#ifndef _LIBCPP_NO_EXCEPTIONS
3667 }
3668 catch (...)
3669 {
3670 for (; __s != __f; ++__s)
3671 __s->~value_type();
3672 throw;
3673 }
3674#endif
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003675 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003676}
3677
Howard Hinnant82894812010-09-22 16:48:34 +00003678class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003679 : public std::exception
3680{
3681public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003682 virtual ~bad_weak_ptr() _NOEXCEPT;
3683 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003684};
3685
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003686template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003687
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003688class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003689{
3690 __shared_count(const __shared_count&);
3691 __shared_count& operator=(const __shared_count&);
3692
3693protected:
3694 long __shared_owners_;
3695 virtual ~__shared_count();
3696private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003697 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003698
3699public:
Howard Hinnant82894812010-09-22 16:48:34 +00003700 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003701 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003702 : __shared_owners_(__refs) {}
3703
Howard Hinnant1694d232011-05-28 14:41:13 +00003704 void __add_shared() _NOEXCEPT;
3705 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003706 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc6e46692015-07-07 00:27:16 +00003707 long use_count() const _NOEXCEPT {
3708 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3709 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003710};
3711
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003712class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003713 : private __shared_count
3714{
3715 long __shared_weak_owners_;
3716
3717public:
Howard Hinnant82894812010-09-22 16:48:34 +00003718 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003719 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003720 : __shared_count(__refs),
3721 __shared_weak_owners_(__refs) {}
3722protected:
3723 virtual ~__shared_weak_count();
3724
3725public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003726 void __add_shared() _NOEXCEPT;
3727 void __add_weak() _NOEXCEPT;
3728 void __release_shared() _NOEXCEPT;
3729 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003730 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003731 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3732 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003733
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003734 // Define the function out only if we build static libc++ without RTTI.
3735 // Otherwise we may break clients who need to compile their projects with
3736 // -fno-rtti and yet link against a libc++.dylib compiled
3737 // without -fno-rtti.
3738#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant1694d232011-05-28 14:41:13 +00003739 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003740#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003741private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003742 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003743};
3744
3745template <class _Tp, class _Dp, class _Alloc>
3746class __shared_ptr_pointer
3747 : public __shared_weak_count
3748{
3749 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3750public:
Howard Hinnant82894812010-09-22 16:48:34 +00003751 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003752 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003753 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003754
Howard Hinnantd4444702010-08-11 17:04:31 +00003755#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00003756 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00003757#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003758
3759private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003760 virtual void __on_zero_shared() _NOEXCEPT;
3761 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003762};
3763
Howard Hinnantd4444702010-08-11 17:04:31 +00003764#ifndef _LIBCPP_NO_RTTI
3765
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003766template <class _Tp, class _Dp, class _Alloc>
3767const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00003768__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003769{
Marshall Clow4b3ca8c2014-11-17 19:05:50 +00003770 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003771}
3772
Howard Hinnant324bb032010-08-22 00:02:43 +00003773#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003774
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003775template <class _Tp, class _Dp, class _Alloc>
3776void
Howard Hinnant1694d232011-05-28 14:41:13 +00003777__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003778{
3779 __data_.first().second()(__data_.first().first());
3780 __data_.first().second().~_Dp();
3781}
3782
3783template <class _Tp, class _Dp, class _Alloc>
3784void
Howard Hinnant1694d232011-05-28 14:41:13 +00003785__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003786{
Eric Fiselier8492cd82015-02-05 23:01:40 +00003787 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3788 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003789 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3790
Eric Fiselier8492cd82015-02-05 23:01:40 +00003791 _Al __a(__data_.second());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003792 __data_.second().~_Alloc();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003793 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003794}
3795
3796template <class _Tp, class _Alloc>
3797class __shared_ptr_emplace
3798 : public __shared_weak_count
3799{
3800 __compressed_pair<_Alloc, _Tp> __data_;
3801public:
3802#ifndef _LIBCPP_HAS_NO_VARIADICS
3803
Howard Hinnant82894812010-09-22 16:48:34 +00003804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003805 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003806 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003807
3808 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00003809 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003810 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00003811 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3812 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003813
3814#else // _LIBCPP_HAS_NO_VARIADICS
3815
Howard Hinnant82894812010-09-22 16:48:34 +00003816 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003817 __shared_ptr_emplace(_Alloc __a)
3818 : __data_(__a) {}
3819
3820 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00003821 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003822 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3823 : __data_(__a, _Tp(__a0)) {}
3824
3825 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00003826 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003827 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3828 : __data_(__a, _Tp(__a0, __a1)) {}
3829
3830 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00003831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003832 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3833 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3834
3835#endif // _LIBCPP_HAS_NO_VARIADICS
3836
3837private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003838 virtual void __on_zero_shared() _NOEXCEPT;
3839 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003840public:
Howard Hinnant82894812010-09-22 16:48:34 +00003841 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003842 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003843};
3844
3845template <class _Tp, class _Alloc>
3846void
Howard Hinnant1694d232011-05-28 14:41:13 +00003847__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003848{
3849 __data_.second().~_Tp();
3850}
3851
3852template <class _Tp, class _Alloc>
3853void
Howard Hinnant1694d232011-05-28 14:41:13 +00003854__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003855{
Eric Fiselier8492cd82015-02-05 23:01:40 +00003856 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3857 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003858 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40 +00003859 _Al __a(__data_.first());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003860 __data_.first().~_Alloc();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003861 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003862}
3863
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003864template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003865
3866template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003867class _LIBCPP_TYPE_VIS_ONLY shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003868{
Howard Hinnant324bb032010-08-22 00:02:43 +00003869public:
3870 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003871private:
3872 element_type* __ptr_;
3873 __shared_weak_count* __cntrl_;
3874
3875 struct __nat {int __for_bool_;};
3876public:
Howard Hinnant46e94932012-07-07 20:56:04 +00003877 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
3878 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiene1678a12014-01-31 09:30:46 +00003879 template<class _Yp>
3880 explicit shared_ptr(_Yp* __p,
3881 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3882 template<class _Yp, class _Dp>
3883 shared_ptr(_Yp* __p, _Dp __d,
3884 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3885 template<class _Yp, class _Dp, class _Alloc>
3886 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3887 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003888 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3889 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant1694d232011-05-28 14:41:13 +00003890 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3891 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003892 template<class _Yp>
3893 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003894 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3895 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003896#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003897 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003898 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003899 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3900 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003901#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003902 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003903 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003904#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003905 template<class _Yp>
3906 shared_ptr(auto_ptr<_Yp>&& __r,
3907 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant324bb032010-08-22 00:02:43 +00003908#else
Logan Chiene1678a12014-01-31 09:30:46 +00003909 template<class _Yp>
3910 shared_ptr(auto_ptr<_Yp> __r,
3911 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003912#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003913#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003914 template <class _Yp, class _Dp>
3915 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3916 typename enable_if
3917 <
3918 !is_lvalue_reference<_Dp>::value &&
3919 !is_array<_Yp>::value &&
3920 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3921 __nat
3922 >::type = __nat());
3923 template <class _Yp, class _Dp>
3924 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3925 typename enable_if
3926 <
3927 is_lvalue_reference<_Dp>::value &&
3928 !is_array<_Yp>::value &&
3929 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3930 __nat
3931 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003932#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003933 template <class _Yp, class _Dp>
3934 shared_ptr(unique_ptr<_Yp, _Dp>,
3935 typename enable_if
3936 <
3937 !is_lvalue_reference<_Dp>::value &&
3938 !is_array<_Yp>::value &&
3939 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3940 __nat
3941 >::type = __nat());
3942 template <class _Yp, class _Dp>
3943 shared_ptr(unique_ptr<_Yp, _Dp>,
3944 typename enable_if
3945 <
3946 is_lvalue_reference<_Dp>::value &&
3947 !is_array<_Yp>::value &&
3948 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3949 __nat
3950 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003951#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003952
3953 ~shared_ptr();
3954
Howard Hinnant1694d232011-05-28 14:41:13 +00003955 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003956 template<class _Yp>
3957 typename enable_if
3958 <
3959 is_convertible<_Yp*, element_type*>::value,
3960 shared_ptr&
3961 >::type
3962 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003963#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003964 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003965 template<class _Yp>
3966 typename enable_if
3967 <
3968 is_convertible<_Yp*, element_type*>::value,
3969 shared_ptr<_Tp>&
3970 >::type
3971 operator=(shared_ptr<_Yp>&& __r);
3972 template<class _Yp>
3973 typename enable_if
3974 <
3975 !is_array<_Yp>::value &&
3976 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant37c4acf2013-09-13 23:56:00 +00003977 shared_ptr
3978 >::type&
Howard Hinnant57199402012-01-02 17:56:02 +00003979 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003980#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003981 template<class _Yp>
3982 typename enable_if
3983 <
3984 !is_array<_Yp>::value &&
3985 is_convertible<_Yp*, element_type*>::value,
3986 shared_ptr&
3987 >::type
3988 operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003989#endif
Howard Hinnant57199402012-01-02 17:56:02 +00003990 template <class _Yp, class _Dp>
3991 typename enable_if
3992 <
3993 !is_array<_Yp>::value &&
3994 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3995 shared_ptr&
3996 >::type
Howard Hinnant73d21a42010-09-04 23:28:19 +00003997#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003998 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003999#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00004000 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004001#endif
4002
Howard Hinnant1694d232011-05-28 14:41:13 +00004003 void swap(shared_ptr& __r) _NOEXCEPT;
4004 void reset() _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00004005 template<class _Yp>
4006 typename enable_if
4007 <
4008 is_convertible<_Yp*, element_type*>::value,
4009 void
4010 >::type
4011 reset(_Yp* __p);
4012 template<class _Yp, class _Dp>
4013 typename enable_if
4014 <
4015 is_convertible<_Yp*, element_type*>::value,
4016 void
4017 >::type
4018 reset(_Yp* __p, _Dp __d);
4019 template<class _Yp, class _Dp, class _Alloc>
4020 typename enable_if
4021 <
4022 is_convertible<_Yp*, element_type*>::value,
4023 void
4024 >::type
4025 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004026
Howard Hinnant82894812010-09-22 16:48:34 +00004027 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004028 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004029 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004030 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
4031 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004032 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004033 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004034 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004035 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00004036 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004037 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00004038 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant77861882012-02-21 21:46:43 +00004039 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant99968442011-11-29 18:15:50 +00004040 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00004041 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00004042 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004043 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant99968442011-11-29 18:15:50 +00004044 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00004045 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00004046 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004047 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant5fec82d2012-07-30 01:40:57 +00004048 _LIBCPP_INLINE_VISIBILITY
4049 bool
4050 __owner_equivalent(const shared_ptr& __p) const
4051 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004052
Howard Hinnantd4444702010-08-11 17:04:31 +00004053#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004054 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00004055 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004056 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004057 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00004058#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004059
4060#ifndef _LIBCPP_HAS_NO_VARIADICS
4061
4062 template<class ..._Args>
4063 static
4064 shared_ptr<_Tp>
4065 make_shared(_Args&& ...__args);
4066
4067 template<class _Alloc, class ..._Args>
4068 static
4069 shared_ptr<_Tp>
4070 allocate_shared(const _Alloc& __a, _Args&& ...__args);
4071
4072#else // _LIBCPP_HAS_NO_VARIADICS
4073
4074 static shared_ptr<_Tp> make_shared();
4075
4076 template<class _A0>
4077 static shared_ptr<_Tp> make_shared(_A0&);
4078
4079 template<class _A0, class _A1>
4080 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
4081
4082 template<class _A0, class _A1, class _A2>
4083 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
4084
4085 template<class _Alloc>
4086 static shared_ptr<_Tp>
4087 allocate_shared(const _Alloc& __a);
4088
4089 template<class _Alloc, class _A0>
4090 static shared_ptr<_Tp>
4091 allocate_shared(const _Alloc& __a, _A0& __a0);
4092
4093 template<class _Alloc, class _A0, class _A1>
4094 static shared_ptr<_Tp>
4095 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
4096
4097 template<class _Alloc, class _A0, class _A1, class _A2>
4098 static shared_ptr<_Tp>
4099 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
4100
4101#endif // _LIBCPP_HAS_NO_VARIADICS
4102
4103private:
4104
4105 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00004106 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004107 void
Marshall Clowfc3a3ff2015-05-27 20:36:14 +00004108 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004109 {
4110 if (__e)
Marshall Clowc4113372015-06-19 15:54:13 +00004111 {
4112 __e->__weak_this_.__ptr_ = const_cast<_Yp*>(static_cast<const _Yp*>(__e));
4113 __e->__weak_this_.__cntrl_ = __cntrl_;
Marshall Clow46d06b92015-06-19 19:32:06 +00004114 __cntrl_->__add_weak();
Marshall Clowc4113372015-06-19 15:54:13 +00004115 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004116 }
4117
Howard Hinnant82894812010-09-22 16:48:34 +00004118 _LIBCPP_INLINE_VISIBILITY
Marshall Clow60784f62015-05-27 22:44:47 +00004119 void __enable_weak_this(const volatile void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004120
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004121 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
4122 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004123};
4124
4125template<class _Tp>
4126inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00004127_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004128shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004129 : __ptr_(0),
4130 __cntrl_(0)
4131{
4132}
4133
4134template<class _Tp>
4135inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00004136_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004137shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004138 : __ptr_(0),
4139 __cntrl_(0)
4140{
4141}
4142
4143template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004144template<class _Yp>
4145shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4146 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004147 : __ptr_(__p)
4148{
4149 unique_ptr<_Yp> __hold(__p);
4150 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4151 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
4152 __hold.release();
4153 __enable_weak_this(__p);
4154}
4155
4156template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004157template<class _Yp, class _Dp>
4158shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4159 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004160 : __ptr_(__p)
4161{
4162#ifndef _LIBCPP_NO_EXCEPTIONS
4163 try
4164 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004165#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004166 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4167 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
4168 __enable_weak_this(__p);
4169#ifndef _LIBCPP_NO_EXCEPTIONS
4170 }
4171 catch (...)
4172 {
4173 __d(__p);
4174 throw;
4175 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004176#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004177}
4178
4179template<class _Tp>
4180template<class _Dp>
4181shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4182 : __ptr_(0)
4183{
4184#ifndef _LIBCPP_NO_EXCEPTIONS
4185 try
4186 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004187#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004188 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4189 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4190#ifndef _LIBCPP_NO_EXCEPTIONS
4191 }
4192 catch (...)
4193 {
4194 __d(__p);
4195 throw;
4196 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004197#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004198}
4199
4200template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004201template<class _Yp, class _Dp, class _Alloc>
4202shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4203 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004204 : __ptr_(__p)
4205{
4206#ifndef _LIBCPP_NO_EXCEPTIONS
4207 try
4208 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004209#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004210 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004211 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004212 typedef __allocator_destructor<_A2> _D2;
4213 _A2 __a2(__a);
4214 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004215 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4216 _CntrlBlk(__p, __d, __a);
4217 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004218 __enable_weak_this(__p);
4219#ifndef _LIBCPP_NO_EXCEPTIONS
4220 }
4221 catch (...)
4222 {
4223 __d(__p);
4224 throw;
4225 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004226#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004227}
4228
4229template<class _Tp>
4230template<class _Dp, class _Alloc>
4231shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4232 : __ptr_(0)
4233{
4234#ifndef _LIBCPP_NO_EXCEPTIONS
4235 try
4236 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004237#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004238 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004239 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004240 typedef __allocator_destructor<_A2> _D2;
4241 _A2 __a2(__a);
4242 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004243 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4244 _CntrlBlk(__p, __d, __a);
4245 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004246#ifndef _LIBCPP_NO_EXCEPTIONS
4247 }
4248 catch (...)
4249 {
4250 __d(__p);
4251 throw;
4252 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004253#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004254}
4255
4256template<class _Tp>
4257template<class _Yp>
4258inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004259shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004260 : __ptr_(__p),
4261 __cntrl_(__r.__cntrl_)
4262{
4263 if (__cntrl_)
4264 __cntrl_->__add_shared();
4265}
4266
4267template<class _Tp>
4268inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004269shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004270 : __ptr_(__r.__ptr_),
4271 __cntrl_(__r.__cntrl_)
4272{
4273 if (__cntrl_)
4274 __cntrl_->__add_shared();
4275}
4276
4277template<class _Tp>
4278template<class _Yp>
4279inline _LIBCPP_INLINE_VISIBILITY
4280shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4281 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004282 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004283 : __ptr_(__r.__ptr_),
4284 __cntrl_(__r.__cntrl_)
4285{
4286 if (__cntrl_)
4287 __cntrl_->__add_shared();
4288}
4289
Howard Hinnant73d21a42010-09-04 23:28:19 +00004290#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004291
4292template<class _Tp>
4293inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004294shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004295 : __ptr_(__r.__ptr_),
4296 __cntrl_(__r.__cntrl_)
4297{
4298 __r.__ptr_ = 0;
4299 __r.__cntrl_ = 0;
4300}
4301
4302template<class _Tp>
4303template<class _Yp>
4304inline _LIBCPP_INLINE_VISIBILITY
4305shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4306 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004307 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004308 : __ptr_(__r.__ptr_),
4309 __cntrl_(__r.__cntrl_)
4310{
4311 __r.__ptr_ = 0;
4312 __r.__cntrl_ = 0;
4313}
4314
Howard Hinnant73d21a42010-09-04 23:28:19 +00004315#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004316
4317template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004318template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004319#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00004320shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004321#else
Logan Chiene1678a12014-01-31 09:30:46 +00004322shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004323#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004324 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004325 : __ptr_(__r.get())
4326{
4327 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4328 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
4329 __enable_weak_this(__r.get());
4330 __r.release();
4331}
4332
4333template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004334template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004335#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004336shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4337#else
4338shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4339#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004340 typename enable_if
4341 <
4342 !is_lvalue_reference<_Dp>::value &&
4343 !is_array<_Yp>::value &&
4344 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4345 __nat
4346 >::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004347 : __ptr_(__r.get())
4348{
Marshall Clow0ad232a2015-05-10 13:59:45 +00004349#if _LIBCPP_STD_VER > 11
4350 if (__ptr_ == nullptr)
4351 __cntrl_ = nullptr;
4352 else
4353#endif
4354 {
4355 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4356 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
4357 __enable_weak_this(__r.get());
4358 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004359 __r.release();
4360}
4361
4362template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004363template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004364#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004365shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4366#else
4367shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4368#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004369 typename enable_if
4370 <
4371 is_lvalue_reference<_Dp>::value &&
4372 !is_array<_Yp>::value &&
4373 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4374 __nat
4375 >::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004376 : __ptr_(__r.get())
4377{
Marshall Clow0ad232a2015-05-10 13:59:45 +00004378#if _LIBCPP_STD_VER > 11
4379 if (__ptr_ == nullptr)
4380 __cntrl_ = nullptr;
4381 else
4382#endif
4383 {
4384 typedef __shared_ptr_pointer<_Yp*,
4385 reference_wrapper<typename remove_reference<_Dp>::type>,
4386 allocator<_Yp> > _CntrlBlk;
4387 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
4388 __enable_weak_this(__r.get());
4389 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004390 __r.release();
4391}
4392
4393#ifndef _LIBCPP_HAS_NO_VARIADICS
4394
4395template<class _Tp>
4396template<class ..._Args>
4397shared_ptr<_Tp>
4398shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4399{
4400 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4401 typedef allocator<_CntrlBlk> _A2;
4402 typedef __allocator_destructor<_A2> _D2;
4403 _A2 __a2;
4404 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004405 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004406 shared_ptr<_Tp> __r;
4407 __r.__ptr_ = __hold2.get()->get();
4408 __r.__cntrl_ = __hold2.release();
4409 __r.__enable_weak_this(__r.__ptr_);
4410 return __r;
4411}
4412
4413template<class _Tp>
4414template<class _Alloc, class ..._Args>
4415shared_ptr<_Tp>
4416shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4417{
4418 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004419 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004420 typedef __allocator_destructor<_A2> _D2;
4421 _A2 __a2(__a);
4422 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004423 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4424 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004425 shared_ptr<_Tp> __r;
4426 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004427 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004428 __r.__enable_weak_this(__r.__ptr_);
4429 return __r;
4430}
4431
4432#else // _LIBCPP_HAS_NO_VARIADICS
4433
4434template<class _Tp>
4435shared_ptr<_Tp>
4436shared_ptr<_Tp>::make_shared()
4437{
4438 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4439 typedef allocator<_CntrlBlk> _Alloc2;
4440 typedef __allocator_destructor<_Alloc2> _D2;
4441 _Alloc2 __alloc2;
4442 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4443 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4444 shared_ptr<_Tp> __r;
4445 __r.__ptr_ = __hold2.get()->get();
4446 __r.__cntrl_ = __hold2.release();
4447 __r.__enable_weak_this(__r.__ptr_);
4448 return __r;
4449}
4450
4451template<class _Tp>
4452template<class _A0>
4453shared_ptr<_Tp>
4454shared_ptr<_Tp>::make_shared(_A0& __a0)
4455{
4456 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4457 typedef allocator<_CntrlBlk> _Alloc2;
4458 typedef __allocator_destructor<_Alloc2> _D2;
4459 _Alloc2 __alloc2;
4460 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4461 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4462 shared_ptr<_Tp> __r;
4463 __r.__ptr_ = __hold2.get()->get();
4464 __r.__cntrl_ = __hold2.release();
4465 __r.__enable_weak_this(__r.__ptr_);
4466 return __r;
4467}
4468
4469template<class _Tp>
4470template<class _A0, class _A1>
4471shared_ptr<_Tp>
4472shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4473{
4474 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4475 typedef allocator<_CntrlBlk> _Alloc2;
4476 typedef __allocator_destructor<_Alloc2> _D2;
4477 _Alloc2 __alloc2;
4478 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4479 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4480 shared_ptr<_Tp> __r;
4481 __r.__ptr_ = __hold2.get()->get();
4482 __r.__cntrl_ = __hold2.release();
4483 __r.__enable_weak_this(__r.__ptr_);
4484 return __r;
4485}
4486
4487template<class _Tp>
4488template<class _A0, class _A1, class _A2>
4489shared_ptr<_Tp>
4490shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4491{
4492 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4493 typedef allocator<_CntrlBlk> _Alloc2;
4494 typedef __allocator_destructor<_Alloc2> _D2;
4495 _Alloc2 __alloc2;
4496 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4497 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4498 shared_ptr<_Tp> __r;
4499 __r.__ptr_ = __hold2.get()->get();
4500 __r.__cntrl_ = __hold2.release();
4501 __r.__enable_weak_this(__r.__ptr_);
4502 return __r;
4503}
4504
4505template<class _Tp>
4506template<class _Alloc>
4507shared_ptr<_Tp>
4508shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4509{
4510 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004511 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004512 typedef __allocator_destructor<_Alloc2> _D2;
4513 _Alloc2 __alloc2(__a);
4514 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004515 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4516 _CntrlBlk(__a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004517 shared_ptr<_Tp> __r;
4518 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004519 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004520 __r.__enable_weak_this(__r.__ptr_);
4521 return __r;
4522}
4523
4524template<class _Tp>
4525template<class _Alloc, class _A0>
4526shared_ptr<_Tp>
4527shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4528{
4529 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004530 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004531 typedef __allocator_destructor<_Alloc2> _D2;
4532 _Alloc2 __alloc2(__a);
4533 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004534 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4535 _CntrlBlk(__a, __a0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004536 shared_ptr<_Tp> __r;
4537 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004538 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004539 __r.__enable_weak_this(__r.__ptr_);
4540 return __r;
4541}
4542
4543template<class _Tp>
4544template<class _Alloc, class _A0, class _A1>
4545shared_ptr<_Tp>
4546shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4547{
4548 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004549 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004550 typedef __allocator_destructor<_Alloc2> _D2;
4551 _Alloc2 __alloc2(__a);
4552 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004553 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4554 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004555 shared_ptr<_Tp> __r;
4556 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004557 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004558 __r.__enable_weak_this(__r.__ptr_);
4559 return __r;
4560}
4561
4562template<class _Tp>
4563template<class _Alloc, class _A0, class _A1, class _A2>
4564shared_ptr<_Tp>
4565shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4566{
4567 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004568 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004569 typedef __allocator_destructor<_Alloc2> _D2;
4570 _Alloc2 __alloc2(__a);
4571 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004572 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4573 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004574 shared_ptr<_Tp> __r;
4575 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004576 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004577 __r.__enable_weak_this(__r.__ptr_);
4578 return __r;
4579}
4580
4581#endif // _LIBCPP_HAS_NO_VARIADICS
4582
4583template<class _Tp>
4584shared_ptr<_Tp>::~shared_ptr()
4585{
4586 if (__cntrl_)
4587 __cntrl_->__release_shared();
4588}
4589
4590template<class _Tp>
4591inline _LIBCPP_INLINE_VISIBILITY
4592shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004593shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004594{
4595 shared_ptr(__r).swap(*this);
4596 return *this;
4597}
4598
4599template<class _Tp>
4600template<class _Yp>
4601inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004602typename enable_if
4603<
4604 is_convertible<_Yp*, _Tp*>::value,
4605 shared_ptr<_Tp>&
4606>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004607shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004608{
4609 shared_ptr(__r).swap(*this);
4610 return *this;
4611}
4612
Howard Hinnant73d21a42010-09-04 23:28:19 +00004613#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004614
4615template<class _Tp>
4616inline _LIBCPP_INLINE_VISIBILITY
4617shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004618shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004619{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004620 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004621 return *this;
4622}
4623
4624template<class _Tp>
4625template<class _Yp>
4626inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004627typename enable_if
4628<
4629 is_convertible<_Yp*, _Tp*>::value,
4630 shared_ptr<_Tp>&
4631>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004632shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4633{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004634 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004635 return *this;
4636}
4637
4638template<class _Tp>
4639template<class _Yp>
4640inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004641typename enable_if
4642<
4643 !is_array<_Yp>::value &&
4644 is_convertible<_Yp*, _Tp*>::value,
Howard Hinnant37c4acf2013-09-13 23:56:00 +00004645 shared_ptr<_Tp>
4646>::type&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004647shared_ptr<_Tp>::operator=(auto_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, class _Dp>
4655inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004656typename enable_if
4657<
4658 !is_array<_Yp>::value &&
4659 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4660 shared_ptr<_Tp>&
4661>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004662shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __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
Howard Hinnant73d21a42010-09-04 23:28:19 +00004668#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004669
4670template<class _Tp>
4671template<class _Yp>
4672inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004673typename enable_if
4674<
4675 !is_array<_Yp>::value &&
4676 is_convertible<_Yp*, _Tp*>::value,
4677 shared_ptr<_Tp>&
4678>::type
Howard Hinnant324bb032010-08-22 00:02:43 +00004679shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004680{
4681 shared_ptr(__r).swap(*this);
4682 return *this;
4683}
4684
4685template<class _Tp>
4686template <class _Yp, class _Dp>
4687inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004688typename enable_if
4689<
4690 !is_array<_Yp>::value &&
4691 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4692 shared_ptr<_Tp>&
4693>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004694shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4695{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004696 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004697 return *this;
4698}
4699
Howard Hinnant73d21a42010-09-04 23:28:19 +00004700#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004701
4702template<class _Tp>
4703inline _LIBCPP_INLINE_VISIBILITY
4704void
Howard Hinnant1694d232011-05-28 14:41:13 +00004705shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004706{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004707 _VSTD::swap(__ptr_, __r.__ptr_);
4708 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004709}
4710
4711template<class _Tp>
4712inline _LIBCPP_INLINE_VISIBILITY
4713void
Howard Hinnant1694d232011-05-28 14:41:13 +00004714shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004715{
4716 shared_ptr().swap(*this);
4717}
4718
4719template<class _Tp>
4720template<class _Yp>
4721inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004722typename enable_if
4723<
4724 is_convertible<_Yp*, _Tp*>::value,
4725 void
4726>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004727shared_ptr<_Tp>::reset(_Yp* __p)
4728{
4729 shared_ptr(__p).swap(*this);
4730}
4731
4732template<class _Tp>
4733template<class _Yp, class _Dp>
4734inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004735typename enable_if
4736<
4737 is_convertible<_Yp*, _Tp*>::value,
4738 void
4739>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004740shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4741{
4742 shared_ptr(__p, __d).swap(*this);
4743}
4744
4745template<class _Tp>
4746template<class _Yp, class _Dp, class _Alloc>
4747inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004748typename enable_if
4749<
4750 is_convertible<_Yp*, _Tp*>::value,
4751 void
4752>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004753shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4754{
4755 shared_ptr(__p, __d, __a).swap(*this);
4756}
4757
4758#ifndef _LIBCPP_HAS_NO_VARIADICS
4759
Howard Hinnant324bb032010-08-22 00:02:43 +00004760template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004761inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004762typename enable_if
4763<
4764 !is_array<_Tp>::value,
4765 shared_ptr<_Tp>
4766>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004767make_shared(_Args&& ...__args)
4768{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004769 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004770}
4771
Howard Hinnant324bb032010-08-22 00:02:43 +00004772template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004773inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004774typename enable_if
4775<
4776 !is_array<_Tp>::value,
4777 shared_ptr<_Tp>
4778>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004779allocate_shared(const _Alloc& __a, _Args&& ...__args)
4780{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004781 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004782}
4783
4784#else // _LIBCPP_HAS_NO_VARIADICS
4785
4786template<class _Tp>
4787inline _LIBCPP_INLINE_VISIBILITY
4788shared_ptr<_Tp>
4789make_shared()
4790{
4791 return shared_ptr<_Tp>::make_shared();
4792}
4793
4794template<class _Tp, class _A0>
4795inline _LIBCPP_INLINE_VISIBILITY
4796shared_ptr<_Tp>
4797make_shared(_A0& __a0)
4798{
4799 return shared_ptr<_Tp>::make_shared(__a0);
4800}
4801
4802template<class _Tp, class _A0, class _A1>
4803inline _LIBCPP_INLINE_VISIBILITY
4804shared_ptr<_Tp>
4805make_shared(_A0& __a0, _A1& __a1)
4806{
4807 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4808}
4809
Howard Hinnant324bb032010-08-22 00:02:43 +00004810template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004811inline _LIBCPP_INLINE_VISIBILITY
4812shared_ptr<_Tp>
4813make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4814{
4815 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4816}
4817
4818template<class _Tp, class _Alloc>
4819inline _LIBCPP_INLINE_VISIBILITY
4820shared_ptr<_Tp>
4821allocate_shared(const _Alloc& __a)
4822{
4823 return shared_ptr<_Tp>::allocate_shared(__a);
4824}
4825
4826template<class _Tp, class _Alloc, class _A0>
4827inline _LIBCPP_INLINE_VISIBILITY
4828shared_ptr<_Tp>
4829allocate_shared(const _Alloc& __a, _A0& __a0)
4830{
4831 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4832}
4833
4834template<class _Tp, class _Alloc, class _A0, class _A1>
4835inline _LIBCPP_INLINE_VISIBILITY
4836shared_ptr<_Tp>
4837allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4838{
4839 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4840}
4841
4842template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4843inline _LIBCPP_INLINE_VISIBILITY
4844shared_ptr<_Tp>
4845allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4846{
4847 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4848}
4849
4850#endif // _LIBCPP_HAS_NO_VARIADICS
4851
4852template<class _Tp, class _Up>
4853inline _LIBCPP_INLINE_VISIBILITY
4854bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004855operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004856{
4857 return __x.get() == __y.get();
4858}
4859
4860template<class _Tp, class _Up>
4861inline _LIBCPP_INLINE_VISIBILITY
4862bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004863operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004864{
4865 return !(__x == __y);
4866}
4867
4868template<class _Tp, class _Up>
4869inline _LIBCPP_INLINE_VISIBILITY
4870bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004871operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004872{
Eric Fiselier8492cd82015-02-05 23:01:40 +00004873 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4874 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnant3fadda32012-02-21 21:02:58 +00004875}
4876
4877template<class _Tp, class _Up>
4878inline _LIBCPP_INLINE_VISIBILITY
4879bool
4880operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4881{
4882 return __y < __x;
4883}
4884
4885template<class _Tp, class _Up>
4886inline _LIBCPP_INLINE_VISIBILITY
4887bool
4888operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4889{
4890 return !(__y < __x);
4891}
4892
4893template<class _Tp, class _Up>
4894inline _LIBCPP_INLINE_VISIBILITY
4895bool
4896operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4897{
4898 return !(__x < __y);
4899}
4900
4901template<class _Tp>
4902inline _LIBCPP_INLINE_VISIBILITY
4903bool
4904operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4905{
4906 return !__x;
4907}
4908
4909template<class _Tp>
4910inline _LIBCPP_INLINE_VISIBILITY
4911bool
4912operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4913{
4914 return !__x;
4915}
4916
4917template<class _Tp>
4918inline _LIBCPP_INLINE_VISIBILITY
4919bool
4920operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4921{
4922 return static_cast<bool>(__x);
4923}
4924
4925template<class _Tp>
4926inline _LIBCPP_INLINE_VISIBILITY
4927bool
4928operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4929{
4930 return static_cast<bool>(__x);
4931}
4932
4933template<class _Tp>
4934inline _LIBCPP_INLINE_VISIBILITY
4935bool
4936operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4937{
4938 return less<_Tp*>()(__x.get(), nullptr);
4939}
4940
4941template<class _Tp>
4942inline _LIBCPP_INLINE_VISIBILITY
4943bool
4944operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4945{
4946 return less<_Tp*>()(nullptr, __x.get());
4947}
4948
4949template<class _Tp>
4950inline _LIBCPP_INLINE_VISIBILITY
4951bool
4952operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4953{
4954 return nullptr < __x;
4955}
4956
4957template<class _Tp>
4958inline _LIBCPP_INLINE_VISIBILITY
4959bool
4960operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4961{
4962 return __x < nullptr;
4963}
4964
4965template<class _Tp>
4966inline _LIBCPP_INLINE_VISIBILITY
4967bool
4968operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4969{
4970 return !(nullptr < __x);
4971}
4972
4973template<class _Tp>
4974inline _LIBCPP_INLINE_VISIBILITY
4975bool
4976operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4977{
4978 return !(__x < nullptr);
4979}
4980
4981template<class _Tp>
4982inline _LIBCPP_INLINE_VISIBILITY
4983bool
4984operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4985{
4986 return !(__x < nullptr);
4987}
4988
4989template<class _Tp>
4990inline _LIBCPP_INLINE_VISIBILITY
4991bool
4992operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4993{
4994 return !(nullptr < __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004995}
4996
4997template<class _Tp>
4998inline _LIBCPP_INLINE_VISIBILITY
4999void
Howard Hinnant1694d232011-05-28 14:41:13 +00005000swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005001{
5002 __x.swap(__y);
5003}
5004
5005template<class _Tp, class _Up>
5006inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005007typename enable_if
5008<
5009 !is_array<_Tp>::value && !is_array<_Up>::value,
5010 shared_ptr<_Tp>
5011>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005012static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005013{
5014 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
5015}
5016
5017template<class _Tp, class _Up>
5018inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005019typename enable_if
5020<
5021 !is_array<_Tp>::value && !is_array<_Up>::value,
5022 shared_ptr<_Tp>
5023>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005024dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005025{
5026 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
5027 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
5028}
5029
5030template<class _Tp, class _Up>
Howard Hinnant57199402012-01-02 17:56:02 +00005031typename enable_if
5032<
5033 is_array<_Tp>::value == is_array<_Up>::value,
5034 shared_ptr<_Tp>
5035>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005036const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005037{
Howard Hinnant57199402012-01-02 17:56:02 +00005038 typedef typename remove_extent<_Tp>::type _RTp;
5039 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005040}
5041
Howard Hinnantd4444702010-08-11 17:04:31 +00005042#ifndef _LIBCPP_NO_RTTI
5043
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005044template<class _Dp, class _Tp>
5045inline _LIBCPP_INLINE_VISIBILITY
5046_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00005047get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005048{
5049 return __p.template __get_deleter<_Dp>();
5050}
5051
Howard Hinnant324bb032010-08-22 00:02:43 +00005052#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00005053
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005054template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005055class _LIBCPP_TYPE_VIS_ONLY weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005056{
Howard Hinnant324bb032010-08-22 00:02:43 +00005057public:
5058 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005059private:
5060 element_type* __ptr_;
5061 __shared_weak_count* __cntrl_;
5062
Howard Hinnant324bb032010-08-22 00:02:43 +00005063public:
Howard Hinnant46e94932012-07-07 20:56:04 +00005064 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005065 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00005066 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5067 _NOEXCEPT;
5068 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005069 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00005070 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5071 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005072
Howard Hinnant57199402012-01-02 17:56:02 +00005073#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5074 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
5075 template<class _Yp> weak_ptr(weak_ptr<_Yp>&& __r,
5076 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5077 _NOEXCEPT;
5078#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00005079 ~weak_ptr();
5080
Howard Hinnant1694d232011-05-28 14:41:13 +00005081 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00005082 template<class _Yp>
5083 typename enable_if
5084 <
5085 is_convertible<_Yp*, element_type*>::value,
5086 weak_ptr&
5087 >::type
5088 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
5089
5090#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5091
5092 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
5093 template<class _Yp>
5094 typename enable_if
5095 <
5096 is_convertible<_Yp*, element_type*>::value,
5097 weak_ptr&
5098 >::type
5099 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
5100
5101#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5102
5103 template<class _Yp>
5104 typename enable_if
5105 <
5106 is_convertible<_Yp*, element_type*>::value,
5107 weak_ptr&
5108 >::type
5109 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005110
Howard Hinnant1694d232011-05-28 14:41:13 +00005111 void swap(weak_ptr& __r) _NOEXCEPT;
5112 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005113
Howard Hinnant82894812010-09-22 16:48:34 +00005114 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005115 long use_count() const _NOEXCEPT
5116 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00005117 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005118 bool expired() const _NOEXCEPT
5119 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5120 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00005121 template<class _Up>
5122 _LIBCPP_INLINE_VISIBILITY
5123 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005124 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00005125 template<class _Up>
5126 _LIBCPP_INLINE_VISIBILITY
5127 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005128 {return __cntrl_ < __r.__cntrl_;}
5129
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005130 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
5131 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005132};
5133
5134template<class _Tp>
5135inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00005136_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005137weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005138 : __ptr_(0),
5139 __cntrl_(0)
5140{
5141}
5142
5143template<class _Tp>
5144inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005145weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005146 : __ptr_(__r.__ptr_),
5147 __cntrl_(__r.__cntrl_)
5148{
5149 if (__cntrl_)
5150 __cntrl_->__add_weak();
5151}
5152
5153template<class _Tp>
5154template<class _Yp>
5155inline _LIBCPP_INLINE_VISIBILITY
5156weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005157 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005158 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005159 : __ptr_(__r.__ptr_),
5160 __cntrl_(__r.__cntrl_)
5161{
5162 if (__cntrl_)
5163 __cntrl_->__add_weak();
5164}
5165
5166template<class _Tp>
5167template<class _Yp>
5168inline _LIBCPP_INLINE_VISIBILITY
5169weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005170 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005171 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005172 : __ptr_(__r.__ptr_),
5173 __cntrl_(__r.__cntrl_)
5174{
5175 if (__cntrl_)
5176 __cntrl_->__add_weak();
5177}
5178
Howard Hinnant57199402012-01-02 17:56:02 +00005179#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5180
5181template<class _Tp>
5182inline _LIBCPP_INLINE_VISIBILITY
5183weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5184 : __ptr_(__r.__ptr_),
5185 __cntrl_(__r.__cntrl_)
5186{
5187 __r.__ptr_ = 0;
5188 __r.__cntrl_ = 0;
5189}
5190
5191template<class _Tp>
5192template<class _Yp>
5193inline _LIBCPP_INLINE_VISIBILITY
5194weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5195 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5196 _NOEXCEPT
5197 : __ptr_(__r.__ptr_),
5198 __cntrl_(__r.__cntrl_)
5199{
5200 __r.__ptr_ = 0;
5201 __r.__cntrl_ = 0;
5202}
5203
5204#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5205
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005206template<class _Tp>
5207weak_ptr<_Tp>::~weak_ptr()
5208{
5209 if (__cntrl_)
5210 __cntrl_->__release_weak();
5211}
5212
5213template<class _Tp>
5214inline _LIBCPP_INLINE_VISIBILITY
5215weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00005216weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005217{
5218 weak_ptr(__r).swap(*this);
5219 return *this;
5220}
5221
5222template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005223template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005224inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005225typename enable_if
5226<
5227 is_convertible<_Yp*, _Tp*>::value,
5228 weak_ptr<_Tp>&
5229>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005230weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005231{
5232 weak_ptr(__r).swap(*this);
5233 return *this;
5234}
5235
Howard Hinnant57199402012-01-02 17:56:02 +00005236#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5237
5238template<class _Tp>
5239inline _LIBCPP_INLINE_VISIBILITY
5240weak_ptr<_Tp>&
5241weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5242{
5243 weak_ptr(_VSTD::move(__r)).swap(*this);
5244 return *this;
5245}
5246
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005247template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005248template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005249inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005250typename enable_if
5251<
5252 is_convertible<_Yp*, _Tp*>::value,
5253 weak_ptr<_Tp>&
5254>::type
5255weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5256{
5257 weak_ptr(_VSTD::move(__r)).swap(*this);
5258 return *this;
5259}
5260
5261#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5262
5263template<class _Tp>
5264template<class _Yp>
5265inline _LIBCPP_INLINE_VISIBILITY
5266typename enable_if
5267<
5268 is_convertible<_Yp*, _Tp*>::value,
5269 weak_ptr<_Tp>&
5270>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005271weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005272{
5273 weak_ptr(__r).swap(*this);
5274 return *this;
5275}
5276
5277template<class _Tp>
5278inline _LIBCPP_INLINE_VISIBILITY
5279void
Howard Hinnant1694d232011-05-28 14:41:13 +00005280weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005281{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005282 _VSTD::swap(__ptr_, __r.__ptr_);
5283 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005284}
5285
5286template<class _Tp>
5287inline _LIBCPP_INLINE_VISIBILITY
5288void
Howard Hinnant1694d232011-05-28 14:41:13 +00005289swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005290{
5291 __x.swap(__y);
5292}
5293
5294template<class _Tp>
5295inline _LIBCPP_INLINE_VISIBILITY
5296void
Howard Hinnant1694d232011-05-28 14:41:13 +00005297weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005298{
5299 weak_ptr().swap(*this);
5300}
5301
5302template<class _Tp>
5303template<class _Yp>
5304shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5305 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
5306 : __ptr_(__r.__ptr_),
5307 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5308{
5309 if (__cntrl_ == 0)
5310#ifndef _LIBCPP_NO_EXCEPTIONS
5311 throw bad_weak_ptr();
5312#else
5313 assert(!"bad_weak_ptr");
5314#endif
5315}
5316
5317template<class _Tp>
5318shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00005319weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005320{
5321 shared_ptr<_Tp> __r;
5322 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5323 if (__r.__cntrl_)
5324 __r.__ptr_ = __ptr_;
5325 return __r;
5326}
5327
Howard Hinnant324bb032010-08-22 00:02:43 +00005328template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005329
5330template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005331struct _LIBCPP_TYPE_VIS_ONLY owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005332 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00005333{
5334 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005336 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5337 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005338 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005339 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5340 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005341 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005342 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5343 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00005344};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005345
5346template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005347struct _LIBCPP_TYPE_VIS_ONLY owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005348 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5349{
Howard Hinnant324bb032010-08-22 00:02:43 +00005350 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005351 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005352 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5353 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005354 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005355 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5356 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005358 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5359 {return __x.owner_before(__y);}
5360};
5361
5362template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005363class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005364{
5365 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00005366protected:
Howard Hinnant46e94932012-07-07 20:56:04 +00005367 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005368 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005370 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005371 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005372 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5373 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00005374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005375 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00005376public:
Howard Hinnant82894812010-09-22 16:48:34 +00005377 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005378 shared_ptr<_Tp> shared_from_this()
5379 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00005380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005381 shared_ptr<_Tp const> shared_from_this() const
5382 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005383
5384 template <class _Up> friend class shared_ptr;
5385};
5386
Howard Hinnant21aefc32010-06-03 16:42:57 +00005387template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005388struct _LIBCPP_TYPE_VIS_ONLY hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00005389{
5390 typedef shared_ptr<_Tp> argument_type;
5391 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005392 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005393 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00005394 {
5395 return hash<_Tp*>()(__ptr.get());
5396 }
5397};
5398
Howard Hinnant99968442011-11-29 18:15:50 +00005399template<class _CharT, class _Traits, class _Yp>
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005400inline _LIBCPP_INLINE_VISIBILITY
5401basic_ostream<_CharT, _Traits>&
Howard Hinnant99968442011-11-29 18:15:50 +00005402operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005403
Eric Fiselier00f4a492015-08-19 17:21:46 +00005404// TODO(EricWF): Enable this for both Clang and GCC. Currently it is only
5405// enabled with clang.
5406#if defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005407
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005408class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005409{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005410 void* __lx;
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005411public:
5412 void lock() _NOEXCEPT;
5413 void unlock() _NOEXCEPT;
5414
5415private:
5416 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5417 __sp_mut(const __sp_mut&);
5418 __sp_mut& operator=(const __sp_mut&);
5419
Howard Hinnant83eade62013-03-06 23:30:19 +00005420 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005421};
5422
Howard Hinnant83eade62013-03-06 23:30:19 +00005423_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005424
5425template <class _Tp>
5426inline _LIBCPP_INLINE_VISIBILITY
5427bool
5428atomic_is_lock_free(const shared_ptr<_Tp>*)
5429{
5430 return false;
5431}
5432
5433template <class _Tp>
5434shared_ptr<_Tp>
5435atomic_load(const shared_ptr<_Tp>* __p)
5436{
5437 __sp_mut& __m = __get_sp_mut(__p);
5438 __m.lock();
5439 shared_ptr<_Tp> __q = *__p;
5440 __m.unlock();
5441 return __q;
5442}
5443
5444template <class _Tp>
5445inline _LIBCPP_INLINE_VISIBILITY
5446shared_ptr<_Tp>
5447atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5448{
5449 return atomic_load(__p);
5450}
5451
5452template <class _Tp>
5453void
5454atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5455{
5456 __sp_mut& __m = __get_sp_mut(__p);
5457 __m.lock();
5458 __p->swap(__r);
5459 __m.unlock();
5460}
5461
5462template <class _Tp>
5463inline _LIBCPP_INLINE_VISIBILITY
5464void
5465atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5466{
5467 atomic_store(__p, __r);
5468}
5469
5470template <class _Tp>
5471shared_ptr<_Tp>
5472atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5473{
5474 __sp_mut& __m = __get_sp_mut(__p);
5475 __m.lock();
5476 __p->swap(__r);
5477 __m.unlock();
5478 return __r;
5479}
5480
5481template <class _Tp>
5482inline _LIBCPP_INLINE_VISIBILITY
5483shared_ptr<_Tp>
5484atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5485{
5486 return atomic_exchange(__p, __r);
5487}
5488
5489template <class _Tp>
5490bool
5491atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5492{
5493 __sp_mut& __m = __get_sp_mut(__p);
5494 __m.lock();
5495 if (__p->__owner_equivalent(*__v))
5496 {
5497 *__p = __w;
5498 __m.unlock();
5499 return true;
5500 }
5501 *__v = *__p;
5502 __m.unlock();
5503 return false;
5504}
5505
5506template <class _Tp>
5507inline _LIBCPP_INLINE_VISIBILITY
5508bool
5509atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5510{
5511 return atomic_compare_exchange_strong(__p, __v, __w);
5512}
5513
5514template <class _Tp>
5515inline _LIBCPP_INLINE_VISIBILITY
5516bool
5517atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5518 shared_ptr<_Tp> __w, memory_order, memory_order)
5519{
5520 return atomic_compare_exchange_strong(__p, __v, __w);
5521}
5522
5523template <class _Tp>
5524inline _LIBCPP_INLINE_VISIBILITY
5525bool
5526atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5527 shared_ptr<_Tp> __w, memory_order, memory_order)
5528{
5529 return atomic_compare_exchange_weak(__p, __v, __w);
5530}
5531
Eric Fiselier00f4a492015-08-19 17:21:46 +00005532#endif // defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005533
Howard Hinnant324bb032010-08-22 00:02:43 +00005534//enum class
Howard Hinnant83eade62013-03-06 23:30:19 +00005535struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005536{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005537 enum __lx
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005538 {
5539 relaxed,
5540 preferred,
5541 strict
5542 };
5543
Howard Hinnant9c0df142012-10-30 19:06:59 +00005544 __lx __v_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005545
Howard Hinnant82894812010-09-22 16:48:34 +00005546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9c0df142012-10-30 19:06:59 +00005547 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00005548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005549 operator int() const {return __v_;}
5550};
5551
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005552_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5553_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5554_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
5555_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5556_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005557
5558template <class _Tp>
5559inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005560_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005561undeclare_reachable(_Tp* __p)
5562{
5563 return static_cast<_Tp*>(__undeclare_reachable(__p));
5564}
5565
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005566_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005567
Marshall Clow7d914d12015-07-13 20:04:56 +00005568// --- Helper for container swap --
5569template <typename _Alloc>
5570_LIBCPP_INLINE_VISIBILITY
5571void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5572#if _LIBCPP_STD_VER >= 14
5573 _NOEXCEPT
5574#else
5575 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5576#endif
5577{
5578 __swap_allocator(__a1, __a2,
5579 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5580}
5581
5582template <typename _Alloc>
5583_LIBCPP_INLINE_VISIBILITY
5584void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5585#if _LIBCPP_STD_VER >= 14
5586 _NOEXCEPT
5587#else
5588 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5589#endif
5590{
5591 using _VSTD::swap;
5592 swap(__a1, __a2);
5593}
5594
5595template <typename _Alloc>
5596_LIBCPP_INLINE_VISIBILITY
5597void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5598
Marshall Clowd434e2a2015-08-18 19:51:37 +00005599template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Marshall Clowaf961ed2015-08-18 18:57:00 +00005600struct __noexcept_move_assign_container : public integral_constant<bool,
5601 _Traits::propagate_on_container_move_assignment::value
5602#if _LIBCPP_STD_VER > 14
5603 || _Traits::is_always_equal::value
5604#else
5605 && is_nothrow_move_assignable<_Alloc>::value
5606#endif
5607 > {};
Marshall Clow7d914d12015-07-13 20:04:56 +00005608
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005609_LIBCPP_END_NAMESPACE_STD
5610
5611#endif // _LIBCPP_MEMORY