blob: 9d214beb68f6dd341c9d5f10546d588e4ef3dc53 [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&)
1681 {return numeric_limits<size_type>::max();}
1682
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;}
1912 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1913 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1914 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowdbaf7a02015-05-10 13:14:08 +00001915#if _LIBCPP_STD_VER >= 14
1916 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1917#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001918};
1919
1920template <class _Tp>
1921pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00001922get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001923{
1924 pair<_Tp*, ptrdiff_t> __r(0, 0);
1925 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1926 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1927 / sizeof(_Tp);
1928 if (__n > __m)
1929 __n = __m;
1930 while (__n > 0)
1931 {
1932 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1933 if (__r.first)
1934 {
1935 __r.second = __n;
1936 break;
1937 }
1938 __n /= 2;
1939 }
1940 return __r;
1941}
1942
1943template <class _Tp>
1944inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001945void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001946
1947template <class _Tp>
1948struct auto_ptr_ref
1949{
1950 _Tp* __ptr_;
1951};
1952
1953template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001954class _LIBCPP_TYPE_VIS_ONLY auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001955{
1956private:
1957 _Tp* __ptr_;
1958public:
1959 typedef _Tp element_type;
1960
1961 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1962 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1963 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1964 : __ptr_(__p.release()) {}
1965 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1966 {reset(__p.release()); return *this;}
1967 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1968 {reset(__p.release()); return *this;}
1969 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1970 {reset(__p.__ptr_); return *this;}
1971 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1972
1973 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1974 {return *__ptr_;}
1975 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1976 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1977 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1978 {
1979 _Tp* __t = __ptr_;
1980 __ptr_ = 0;
1981 return __t;
1982 }
1983 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1984 {
1985 if (__ptr_ != __p)
1986 delete __ptr_;
1987 __ptr_ = __p;
1988 }
1989
1990 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1991 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1992 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1993 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1994 {return auto_ptr<_Up>(release());}
1995};
1996
1997template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001998class _LIBCPP_TYPE_VIS_ONLY auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001999{
2000public:
2001 typedef void element_type;
2002};
2003
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002004template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
2005 typename remove_cv<_T2>::type>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00002006 bool = is_empty<_T1>::value
Eric Fiselier3a0e4302015-06-13 07:08:02 +00002007 && !__libcpp_is_final<_T1>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00002008 bool = is_empty<_T2>::value
Eric Fiselier3a0e4302015-06-13 07:08:02 +00002009 && !__libcpp_is_final<_T2>::value
Howard Hinnantd4cf2152011-12-11 20:31:33 +00002010 >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002011struct __libcpp_compressed_pair_switch;
2012
2013template <class _T1, class _T2, bool IsSame>
2014struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
2015
2016template <class _T1, class _T2, bool IsSame>
2017struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
2018
2019template <class _T1, class _T2, bool IsSame>
2020struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
2021
2022template <class _T1, class _T2>
2023struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
2024
2025template <class _T1, class _T2>
2026struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
2027
2028template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
2029class __libcpp_compressed_pair_imp;
2030
2031template <class _T1, class _T2>
2032class __libcpp_compressed_pair_imp<_T1, _T2, 0>
2033{
2034private:
2035 _T1 __first_;
2036 _T2 __second_;
2037public:
2038 typedef _T1 _T1_param;
2039 typedef _T2 _T2_param;
2040
2041 typedef typename remove_reference<_T1>::type& _T1_reference;
2042 typedef typename remove_reference<_T2>::type& _T2_reference;
2043
2044 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2045 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2046
Marshall Clowcd6ed542015-07-16 03:05:06 +00002047 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_(), __second_() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002048 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowcd6ed542015-07-16 03:05:06 +00002049 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002050 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowcd6ed542015-07-16 03:05:06 +00002051 : __first_(), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002052 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002053 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002054
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002055#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002056
2057 _LIBCPP_INLINE_VISIBILITY
2058 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2059 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2060 is_nothrow_copy_constructible<_T2>::value)
2061 : __first_(__p.first()),
2062 __second_(__p.second()) {}
2063
2064 _LIBCPP_INLINE_VISIBILITY
2065 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2066 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2067 is_nothrow_copy_assignable<_T2>::value)
2068 {
2069 __first_ = __p.first();
2070 __second_ = __p.second();
2071 return *this;
2072 }
2073
Howard Hinnant61aa6012011-07-01 19:24:36 +00002074 _LIBCPP_INLINE_VISIBILITY
2075 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002076 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2077 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002078 : __first_(_VSTD::forward<_T1>(__p.first())),
2079 __second_(_VSTD::forward<_T2>(__p.second())) {}
2080
2081 _LIBCPP_INLINE_VISIBILITY
2082 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2083 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2084 is_nothrow_move_assignable<_T2>::value)
2085 {
2086 __first_ = _VSTD::forward<_T1>(__p.first());
2087 __second_ = _VSTD::forward<_T2>(__p.second());
2088 return *this;
2089 }
2090
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002091#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002092
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002093#ifndef _LIBCPP_HAS_NO_VARIADICS
2094
2095 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2096 _LIBCPP_INLINE_VISIBILITY
2097 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2098 tuple<_Args1...> __first_args,
2099 tuple<_Args2...> __second_args,
2100 __tuple_indices<_I1...>,
2101 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002102 : __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2103 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002104 {}
2105
2106#endif // _LIBCPP_HAS_NO_VARIADICS
2107
Howard Hinnant1694d232011-05-28 14:41:13 +00002108 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2109 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002110
Howard Hinnant1694d232011-05-28 14:41:13 +00002111 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2112 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002113
2114 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002115 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002116 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002117 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002118 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002119 swap(__first_, __x.__first_);
2120 swap(__second_, __x.__second_);
2121 }
2122};
2123
2124template <class _T1, class _T2>
2125class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2126 : private _T1
2127{
2128private:
2129 _T2 __second_;
2130public:
2131 typedef _T1 _T1_param;
2132 typedef _T2 _T2_param;
2133
2134 typedef _T1& _T1_reference;
2135 typedef typename remove_reference<_T2>::type& _T2_reference;
2136
2137 typedef const _T1& _T1_const_reference;
2138 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2139
Marshall Clowcd6ed542015-07-16 03:05:06 +00002140 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __second_() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002141 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowcd6ed542015-07-16 03:05:06 +00002142 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002143 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002144 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002145 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002146 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002147
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002148#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002149
2150 _LIBCPP_INLINE_VISIBILITY
2151 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2152 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2153 is_nothrow_copy_constructible<_T2>::value)
2154 : _T1(__p.first()), __second_(__p.second()) {}
2155
2156 _LIBCPP_INLINE_VISIBILITY
2157 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2158 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2159 is_nothrow_copy_assignable<_T2>::value)
2160 {
2161 _T1::operator=(__p.first());
2162 __second_ = __p.second();
2163 return *this;
2164 }
2165
Howard Hinnant61aa6012011-07-01 19:24:36 +00002166 _LIBCPP_INLINE_VISIBILITY
2167 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002168 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2169 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002170 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002171
2172 _LIBCPP_INLINE_VISIBILITY
2173 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2174 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2175 is_nothrow_move_assignable<_T2>::value)
2176 {
2177 _T1::operator=(_VSTD::move(__p.first()));
2178 __second_ = _VSTD::forward<_T2>(__p.second());
2179 return *this;
2180 }
2181
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002182#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002183
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002184#ifndef _LIBCPP_HAS_NO_VARIADICS
2185
2186 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2187 _LIBCPP_INLINE_VISIBILITY
2188 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2189 tuple<_Args1...> __first_args,
2190 tuple<_Args2...> __second_args,
2191 __tuple_indices<_I1...>,
2192 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002193 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2194 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002195 {}
2196
2197#endif // _LIBCPP_HAS_NO_VARIADICS
2198
Howard Hinnant1694d232011-05-28 14:41:13 +00002199 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2200 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002201
Howard Hinnant1694d232011-05-28 14:41:13 +00002202 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2203 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002204
2205 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002206 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002207 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002208 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002209 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002210 swap(__second_, __x.__second_);
2211 }
2212};
2213
2214template <class _T1, class _T2>
2215class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2216 : private _T2
2217{
2218private:
2219 _T1 __first_;
2220public:
2221 typedef _T1 _T1_param;
2222 typedef _T2 _T2_param;
2223
2224 typedef typename remove_reference<_T1>::type& _T1_reference;
2225 typedef _T2& _T2_reference;
2226
2227 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2228 typedef const _T2& _T2_const_reference;
2229
Marshall Clowcd6ed542015-07-16 03:05:06 +00002230 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002231 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002232 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002233 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowcd6ed542015-07-16 03:05:06 +00002234 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002235 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00002236 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2237 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002238 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002239
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002240#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002241
2242 _LIBCPP_INLINE_VISIBILITY
2243 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2244 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2245 is_nothrow_copy_constructible<_T2>::value)
2246 : _T2(__p.second()), __first_(__p.first()) {}
2247
2248 _LIBCPP_INLINE_VISIBILITY
2249 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2250 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2251 is_nothrow_copy_assignable<_T2>::value)
2252 {
2253 _T2::operator=(__p.second());
2254 __first_ = __p.first();
2255 return *this;
2256 }
2257
Howard Hinnant61aa6012011-07-01 19:24:36 +00002258 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002259 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002260 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2261 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002262 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002263
2264 _LIBCPP_INLINE_VISIBILITY
2265 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2266 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2267 is_nothrow_move_assignable<_T2>::value)
2268 {
2269 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2270 __first_ = _VSTD::move(__p.first());
2271 return *this;
2272 }
2273
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002274#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002275
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002276#ifndef _LIBCPP_HAS_NO_VARIADICS
2277
2278 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2279 _LIBCPP_INLINE_VISIBILITY
2280 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2281 tuple<_Args1...> __first_args,
2282 tuple<_Args2...> __second_args,
2283 __tuple_indices<_I1...>,
2284 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002285 : _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...),
2286 __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002287
2288 {}
2289
2290#endif // _LIBCPP_HAS_NO_VARIADICS
2291
Howard Hinnant1694d232011-05-28 14:41:13 +00002292 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2293 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002294
Howard Hinnant1694d232011-05-28 14:41:13 +00002295 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2296 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002297
2298 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002299 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002300 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002301 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002302 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002303 swap(__first_, __x.__first_);
2304 }
2305};
2306
2307template <class _T1, class _T2>
2308class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2309 : private _T1,
2310 private _T2
2311{
2312public:
2313 typedef _T1 _T1_param;
2314 typedef _T2 _T2_param;
2315
2316 typedef _T1& _T1_reference;
2317 typedef _T2& _T2_reference;
2318
2319 typedef const _T1& _T1_const_reference;
2320 typedef const _T2& _T2_const_reference;
2321
2322 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2323 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002324 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002325 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002326 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002327 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002328 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002329
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002330#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002331
2332 _LIBCPP_INLINE_VISIBILITY
2333 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2334 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2335 is_nothrow_copy_constructible<_T2>::value)
2336 : _T1(__p.first()), _T2(__p.second()) {}
2337
2338 _LIBCPP_INLINE_VISIBILITY
2339 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2340 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2341 is_nothrow_copy_assignable<_T2>::value)
2342 {
2343 _T1::operator=(__p.first());
2344 _T2::operator=(__p.second());
2345 return *this;
2346 }
2347
Howard Hinnant61aa6012011-07-01 19:24:36 +00002348 _LIBCPP_INLINE_VISIBILITY
2349 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002350 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2351 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002352 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002353
2354 _LIBCPP_INLINE_VISIBILITY
2355 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2356 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2357 is_nothrow_move_assignable<_T2>::value)
2358 {
2359 _T1::operator=(_VSTD::move(__p.first()));
2360 _T2::operator=(_VSTD::move(__p.second()));
2361 return *this;
2362 }
2363
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002364#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002365
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002366#ifndef _LIBCPP_HAS_NO_VARIADICS
2367
2368 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2369 _LIBCPP_INLINE_VISIBILITY
2370 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2371 tuple<_Args1...> __first_args,
2372 tuple<_Args2...> __second_args,
2373 __tuple_indices<_I1...>,
2374 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002375 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2376 _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002377 {}
2378
2379#endif // _LIBCPP_HAS_NO_VARIADICS
2380
Howard Hinnant1694d232011-05-28 14:41:13 +00002381 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2382 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002383
Howard Hinnant1694d232011-05-28 14:41:13 +00002384 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2385 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002386
Howard Hinnantec3773c2011-12-01 20:21:04 +00002387 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant1694d232011-05-28 14:41:13 +00002388 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002389 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002390 {
2391 }
2392};
2393
2394template <class _T1, class _T2>
2395class __compressed_pair
2396 : private __libcpp_compressed_pair_imp<_T1, _T2>
2397{
2398 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2399public:
2400 typedef typename base::_T1_param _T1_param;
2401 typedef typename base::_T2_param _T2_param;
2402
2403 typedef typename base::_T1_reference _T1_reference;
2404 typedef typename base::_T2_reference _T2_reference;
2405
2406 typedef typename base::_T1_const_reference _T1_const_reference;
2407 typedef typename base::_T2_const_reference _T2_const_reference;
2408
2409 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002410 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002411 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002412 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002413 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002414 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002415 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002416
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002417#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002418
2419 _LIBCPP_INLINE_VISIBILITY
2420 __compressed_pair(const __compressed_pair& __p)
2421 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2422 is_nothrow_copy_constructible<_T2>::value)
2423 : base(__p) {}
2424
2425 _LIBCPP_INLINE_VISIBILITY
2426 __compressed_pair& operator=(const __compressed_pair& __p)
2427 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2428 is_nothrow_copy_assignable<_T2>::value)
2429 {
2430 base::operator=(__p);
2431 return *this;
2432 }
2433
Howard Hinnant61aa6012011-07-01 19:24:36 +00002434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002435 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002436 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2437 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002438 : base(_VSTD::move(__p)) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002439
2440 _LIBCPP_INLINE_VISIBILITY
2441 __compressed_pair& operator=(__compressed_pair&& __p)
2442 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2443 is_nothrow_move_assignable<_T2>::value)
2444 {
2445 base::operator=(_VSTD::move(__p));
2446 return *this;
2447 }
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002448
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002449#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002450
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002451#ifndef _LIBCPP_HAS_NO_VARIADICS
2452
2453 template <class... _Args1, class... _Args2>
2454 _LIBCPP_INLINE_VISIBILITY
2455 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2456 tuple<_Args2...> __second_args)
Howard Hinnant57199402012-01-02 17:56:02 +00002457 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002458 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2459 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2460 {}
2461
2462#endif // _LIBCPP_HAS_NO_VARIADICS
2463
Howard Hinnant1694d232011-05-28 14:41:13 +00002464 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2465 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002466
Howard Hinnant1694d232011-05-28 14:41:13 +00002467 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2468 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002469
Howard Hinnant1694d232011-05-28 14:41:13 +00002470 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2471 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002472 __is_nothrow_swappable<_T2>::value)
Howard Hinnant1694d232011-05-28 14:41:13 +00002473 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002474};
2475
2476template <class _T1, class _T2>
2477inline _LIBCPP_INLINE_VISIBILITY
2478void
2479swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002480 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002481 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002482 {__x.swap(__y);}
2483
Howard Hinnant57199402012-01-02 17:56:02 +00002484// __same_or_less_cv_qualified
2485
2486template <class _Ptr1, class _Ptr2,
2487 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2488 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2489 >::value
2490 >
2491struct __same_or_less_cv_qualified_imp
2492 : is_convertible<_Ptr1, _Ptr2> {};
2493
2494template <class _Ptr1, class _Ptr2>
2495struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2496 : false_type {};
2497
Marshall Clow5f64a2b2014-04-26 05:19:48 +00002498template <class _Ptr1, class _Ptr2, bool = is_pointer<_Ptr1>::value ||
2499 is_same<_Ptr1, _Ptr2>::value ||
2500 __has_element_type<_Ptr1>::value>
Howard Hinnant57199402012-01-02 17:56:02 +00002501struct __same_or_less_cv_qualified
2502 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2503
2504template <class _Ptr1, class _Ptr2>
Marshall Clow5f64a2b2014-04-26 05:19:48 +00002505struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, false>
Howard Hinnant57199402012-01-02 17:56:02 +00002506 : false_type {};
2507
2508// default_delete
2509
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002510template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002511struct _LIBCPP_TYPE_VIS_ONLY default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002512{
Howard Hinnant46e94932012-07-07 20:56:04 +00002513#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2514 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2515#else
2516 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2517#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002518 template <class _Up>
2519 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002520 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2521 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002522 {
2523 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant05e7d242013-04-24 19:44:26 +00002524 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002525 delete __ptr;
2526 }
2527};
2528
2529template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002530struct _LIBCPP_TYPE_VIS_ONLY default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002531{
Howard Hinnant8e843502011-12-18 21:19:44 +00002532public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002533#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2534 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2535#else
2536 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2537#endif
Howard Hinnant8e843502011-12-18 21:19:44 +00002538 template <class _Up>
2539 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant57199402012-01-02 17:56:02 +00002540 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant8e843502011-12-18 21:19:44 +00002541 template <class _Up>
2542 _LIBCPP_INLINE_VISIBILITY
2543 void operator() (_Up* __ptr,
Howard Hinnant57199402012-01-02 17:56:02 +00002544 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002545 {
2546 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant05e7d242013-04-24 19:44:26 +00002547 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002548 delete [] __ptr;
2549 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002550};
2551
2552template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002553class _LIBCPP_TYPE_VIS_ONLY unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002554{
2555public:
2556 typedef _Tp element_type;
2557 typedef _Dp deleter_type;
2558 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2559private:
2560 __compressed_pair<pointer, deleter_type> __ptr_;
2561
Howard Hinnant57199402012-01-02 17:56:02 +00002562#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002563 unique_ptr(unique_ptr&);
2564 template <class _Up, class _Ep>
2565 unique_ptr(unique_ptr<_Up, _Ep>&);
2566 unique_ptr& operator=(unique_ptr&);
2567 template <class _Up, class _Ep>
2568 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002569#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002570
2571 struct __nat {int __for_bool_;};
2572
2573 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2574 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2575public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002576 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002577 : __ptr_(pointer())
2578 {
2579 static_assert(!is_pointer<deleter_type>::value,
2580 "unique_ptr constructed with null function pointer deleter");
2581 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002582 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002583 : __ptr_(pointer())
2584 {
2585 static_assert(!is_pointer<deleter_type>::value,
2586 "unique_ptr constructed with null function pointer deleter");
2587 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002588 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002589 : __ptr_(_VSTD::move(__p))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002590 {
2591 static_assert(!is_pointer<deleter_type>::value,
2592 "unique_ptr constructed with null function pointer deleter");
2593 }
2594
Howard Hinnant73d21a42010-09-04 23:28:19 +00002595#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002596 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2597 is_reference<deleter_type>::value,
2598 deleter_type,
2599 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002600 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002601 : __ptr_(__p, __d) {}
2602
2603 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002604 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002605 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002606 {
2607 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2608 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002609 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002610 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002611 template <class _Up, class _Ep>
2612 _LIBCPP_INLINE_VISIBILITY
2613 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2614 typename enable_if
2615 <
2616 !is_array<_Up>::value &&
2617 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2618 is_convertible<_Ep, deleter_type>::value &&
2619 (
2620 !is_reference<deleter_type>::value ||
2621 is_same<deleter_type, _Ep>::value
2622 ),
2623 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002624 >::type = __nat()) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002625 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002626
2627 template <class _Up>
2628 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2629 typename enable_if<
2630 is_convertible<_Up*, _Tp*>::value &&
2631 is_same<_Dp, default_delete<_Tp> >::value,
2632 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002633 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002634 : __ptr_(__p.release())
2635 {
2636 }
2637
Howard Hinnant1694d232011-05-28 14:41:13 +00002638 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002639 {
2640 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002641 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002642 return *this;
2643 }
2644
2645 template <class _Up, class _Ep>
2646 _LIBCPP_INLINE_VISIBILITY
2647 typename enable_if
2648 <
Howard Hinnant57199402012-01-02 17:56:02 +00002649 !is_array<_Up>::value &&
2650 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2651 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002652 unique_ptr&
2653 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002654 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002655 {
2656 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002657 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002658 return *this;
2659 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002660#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002661
2662 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2663 {
2664 return __rv<unique_ptr>(*this);
2665 }
2666
2667 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002668 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002669
2670 template <class _Up, class _Ep>
2671 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2672 {
2673 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002674 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002675 return *this;
2676 }
2677
2678 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002679 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002680
2681 template <class _Up>
2682 _LIBCPP_INLINE_VISIBILITY
2683 typename enable_if<
2684 is_convertible<_Up*, _Tp*>::value &&
2685 is_same<_Dp, default_delete<_Tp> >::value,
2686 unique_ptr&
2687 >::type
2688 operator=(auto_ptr<_Up> __p)
2689 {reset(__p.release()); return *this;}
2690
Howard Hinnant73d21a42010-09-04 23:28:19 +00002691#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002692 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2693
Howard Hinnant1694d232011-05-28 14:41:13 +00002694 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002695 {
2696 reset();
2697 return *this;
2698 }
2699
2700 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2701 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002702 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2703 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2704 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2705 {return __ptr_.second();}
2706 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2707 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002708 _LIBCPP_INLINE_VISIBILITY
2709 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2710 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002711
Howard Hinnant1694d232011-05-28 14:41:13 +00002712 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002713 {
2714 pointer __t = __ptr_.first();
2715 __ptr_.first() = pointer();
2716 return __t;
2717 }
2718
Howard Hinnant1694d232011-05-28 14:41:13 +00002719 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002720 {
2721 pointer __tmp = __ptr_.first();
2722 __ptr_.first() = __p;
2723 if (__tmp)
2724 __ptr_.second()(__tmp);
2725 }
2726
Howard Hinnant1694d232011-05-28 14:41:13 +00002727 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2728 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002729};
2730
2731template <class _Tp, class _Dp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002732class _LIBCPP_TYPE_VIS_ONLY unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002733{
2734public:
2735 typedef _Tp element_type;
2736 typedef _Dp deleter_type;
2737 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2738private:
2739 __compressed_pair<pointer, deleter_type> __ptr_;
2740
Howard Hinnant57199402012-01-02 17:56:02 +00002741#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002742 unique_ptr(unique_ptr&);
2743 template <class _Up>
2744 unique_ptr(unique_ptr<_Up>&);
2745 unique_ptr& operator=(unique_ptr&);
2746 template <class _Up>
2747 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002748#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002749
2750 struct __nat {int __for_bool_;};
2751
2752 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2753 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2754public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002755 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002756 : __ptr_(pointer())
2757 {
2758 static_assert(!is_pointer<deleter_type>::value,
2759 "unique_ptr constructed with null function pointer deleter");
2760 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002761 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002762 : __ptr_(pointer())
2763 {
2764 static_assert(!is_pointer<deleter_type>::value,
2765 "unique_ptr constructed with null function pointer deleter");
2766 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002767#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00002768 template <class _Pp>
2769 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p,
2770 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002771 : __ptr_(__p)
2772 {
2773 static_assert(!is_pointer<deleter_type>::value,
2774 "unique_ptr constructed with null function pointer deleter");
2775 }
2776
Logan Chiene1678a12014-01-31 09:30:46 +00002777 template <class _Pp>
Howard Hinnant99968442011-11-29 18:15:50 +00002778 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002779 is_reference<deleter_type>::value,
2780 deleter_type,
Logan Chiene1678a12014-01-31 09:30:46 +00002781 typename add_lvalue_reference<const deleter_type>::type>::type __d,
2782 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant1694d232011-05-28 14:41:13 +00002783 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002784 : __ptr_(__p, __d) {}
2785
2786 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2787 is_reference<deleter_type>::value,
2788 deleter_type,
2789 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002790 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002791 : __ptr_(pointer(), __d) {}
2792
Logan Chiene1678a12014-01-31 09:30:46 +00002793 template <class _Pp>
2794 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p,
2795 typename remove_reference<deleter_type>::type&& __d,
2796 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant1694d232011-05-28 14:41:13 +00002797 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002798 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002799 {
2800 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2801 }
2802
2803 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002804 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002805 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002806 {
2807 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2808 }
2809
Howard Hinnant1694d232011-05-28 14:41:13 +00002810 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002811 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002812
Howard Hinnant1694d232011-05-28 14:41:13 +00002813 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002814 {
2815 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002816 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002817 return *this;
2818 }
Howard Hinnant8e843502011-12-18 21:19:44 +00002819
2820 template <class _Up, class _Ep>
2821 _LIBCPP_INLINE_VISIBILITY
2822 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2823 typename enable_if
2824 <
2825 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002826 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant8e843502011-12-18 21:19:44 +00002827 && is_convertible<_Ep, deleter_type>::value &&
2828 (
2829 !is_reference<deleter_type>::value ||
2830 is_same<deleter_type, _Ep>::value
2831 ),
2832 __nat
2833 >::type = __nat()
2834 ) _NOEXCEPT
2835 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2836
2837
2838 template <class _Up, class _Ep>
2839 _LIBCPP_INLINE_VISIBILITY
2840 typename enable_if
2841 <
2842 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002843 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2844 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant8e843502011-12-18 21:19:44 +00002845 unique_ptr&
2846 >::type
2847 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2848 {
2849 reset(__u.release());
2850 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2851 return *this;
2852 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002853#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002854
2855 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2856 : __ptr_(__p)
2857 {
2858 static_assert(!is_pointer<deleter_type>::value,
2859 "unique_ptr constructed with null function pointer deleter");
2860 }
2861
2862 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002863 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002864
2865 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002866 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002867
2868 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2869 {
2870 return __rv<unique_ptr>(*this);
2871 }
2872
2873 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002874 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002875
2876 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2877 {
2878 reset(__u->release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002879 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002880 return *this;
2881 }
2882
Howard Hinnant73d21a42010-09-04 23:28:19 +00002883#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002884 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2885
Howard Hinnant1694d232011-05-28 14:41:13 +00002886 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002887 {
2888 reset();
2889 return *this;
2890 }
2891
2892 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2893 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002894 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2895 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2896 {return __ptr_.second();}
2897 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2898 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002899 _LIBCPP_INLINE_VISIBILITY
2900 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2901 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002902
Howard Hinnant1694d232011-05-28 14:41:13 +00002903 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002904 {
2905 pointer __t = __ptr_.first();
2906 __ptr_.first() = pointer();
2907 return __t;
2908 }
2909
Howard Hinnant73d21a42010-09-04 23:28:19 +00002910#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00002911 template <class _Pp>
2912 _LIBCPP_INLINE_VISIBILITY
2913 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, void>::type
2914 reset(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002915 {
2916 pointer __tmp = __ptr_.first();
2917 __ptr_.first() = __p;
2918 if (__tmp)
2919 __ptr_.second()(__tmp);
2920 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002921 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002922 {
2923 pointer __tmp = __ptr_.first();
2924 __ptr_.first() = nullptr;
2925 if (__tmp)
2926 __ptr_.second()(__tmp);
2927 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002928 _LIBCPP_INLINE_VISIBILITY void reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002929 {
2930 pointer __tmp = __ptr_.first();
2931 __ptr_.first() = nullptr;
2932 if (__tmp)
2933 __ptr_.second()(__tmp);
2934 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002935#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002936 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2937 {
2938 pointer __tmp = __ptr_.first();
2939 __ptr_.first() = __p;
2940 if (__tmp)
2941 __ptr_.second()(__tmp);
2942 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002943#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002944
2945 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2946private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002947
Howard Hinnant73d21a42010-09-04 23:28:19 +00002948#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002949 template <class _Up>
2950 explicit unique_ptr(_Up);
2951 template <class _Up>
2952 unique_ptr(_Up __u,
2953 typename conditional<
2954 is_reference<deleter_type>::value,
2955 deleter_type,
2956 typename add_lvalue_reference<const deleter_type>::type>::type,
2957 typename enable_if
2958 <
2959 is_convertible<_Up, pointer>::value,
2960 __nat
2961 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002962#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002963};
2964
2965template <class _Tp, class _Dp>
2966inline _LIBCPP_INLINE_VISIBILITY
2967void
Howard Hinnant1694d232011-05-28 14:41:13 +00002968swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002969
2970template <class _T1, class _D1, class _T2, class _D2>
2971inline _LIBCPP_INLINE_VISIBILITY
2972bool
2973operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2974
2975template <class _T1, class _D1, class _T2, class _D2>
2976inline _LIBCPP_INLINE_VISIBILITY
2977bool
2978operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2979
2980template <class _T1, class _D1, class _T2, class _D2>
2981inline _LIBCPP_INLINE_VISIBILITY
2982bool
Howard Hinnant3fadda32012-02-21 21:02:58 +00002983operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2984{
2985 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2986 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselier8492cd82015-02-05 23:01:40 +00002987 typedef typename common_type<_P1, _P2>::type _Vp;
2988 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnant3fadda32012-02-21 21:02:58 +00002989}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002990
2991template <class _T1, class _D1, class _T2, class _D2>
2992inline _LIBCPP_INLINE_VISIBILITY
2993bool
2994operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2995
2996template <class _T1, class _D1, class _T2, class _D2>
2997inline _LIBCPP_INLINE_VISIBILITY
2998bool
2999operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
3000
3001template <class _T1, class _D1, class _T2, class _D2>
3002inline _LIBCPP_INLINE_VISIBILITY
3003bool
3004operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
3005
Howard Hinnant3fadda32012-02-21 21:02:58 +00003006template <class _T1, class _D1>
3007inline _LIBCPP_INLINE_VISIBILITY
3008bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003009operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003010{
3011 return !__x;
3012}
3013
3014template <class _T1, class _D1>
3015inline _LIBCPP_INLINE_VISIBILITY
3016bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003017operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003018{
3019 return !__x;
3020}
3021
3022template <class _T1, class _D1>
3023inline _LIBCPP_INLINE_VISIBILITY
3024bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003025operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003026{
3027 return static_cast<bool>(__x);
3028}
3029
3030template <class _T1, class _D1>
3031inline _LIBCPP_INLINE_VISIBILITY
3032bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003033operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003034{
3035 return static_cast<bool>(__x);
3036}
3037
3038template <class _T1, class _D1>
3039inline _LIBCPP_INLINE_VISIBILITY
3040bool
3041operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3042{
3043 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3044 return less<_P1>()(__x.get(), nullptr);
3045}
3046
3047template <class _T1, class _D1>
3048inline _LIBCPP_INLINE_VISIBILITY
3049bool
3050operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3051{
3052 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3053 return less<_P1>()(nullptr, __x.get());
3054}
3055
3056template <class _T1, class _D1>
3057inline _LIBCPP_INLINE_VISIBILITY
3058bool
3059operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3060{
3061 return nullptr < __x;
3062}
3063
3064template <class _T1, class _D1>
3065inline _LIBCPP_INLINE_VISIBILITY
3066bool
3067operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3068{
3069 return __x < nullptr;
3070}
3071
3072template <class _T1, class _D1>
3073inline _LIBCPP_INLINE_VISIBILITY
3074bool
3075operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3076{
3077 return !(nullptr < __x);
3078}
3079
3080template <class _T1, class _D1>
3081inline _LIBCPP_INLINE_VISIBILITY
3082bool
3083operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3084{
3085 return !(__x < nullptr);
3086}
3087
3088template <class _T1, class _D1>
3089inline _LIBCPP_INLINE_VISIBILITY
3090bool
3091operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3092{
3093 return !(__x < nullptr);
3094}
3095
3096template <class _T1, class _D1>
3097inline _LIBCPP_INLINE_VISIBILITY
3098bool
3099operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3100{
3101 return !(nullptr < __x);
3102}
3103
Howard Hinnant87073e42012-05-01 15:37:54 +00003104#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3105
3106template <class _Tp, class _Dp>
3107inline _LIBCPP_INLINE_VISIBILITY
3108unique_ptr<_Tp, _Dp>
3109move(unique_ptr<_Tp, _Dp>& __t)
3110{
3111 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3112}
3113
3114#endif
3115
Marshall Clowfd7481e2013-07-01 18:16:03 +00003116#if _LIBCPP_STD_VER > 11
3117
3118template<class _Tp>
3119struct __unique_if
3120{
3121 typedef unique_ptr<_Tp> __unique_single;
3122};
3123
3124template<class _Tp>
3125struct __unique_if<_Tp[]>
3126{
3127 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3128};
3129
3130template<class _Tp, size_t _Np>
3131struct __unique_if<_Tp[_Np]>
3132{
3133 typedef void __unique_array_known_bound;
3134};
3135
3136template<class _Tp, class... _Args>
Marshall Clowfb551102013-07-02 20:06:09 +00003137inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003138typename __unique_if<_Tp>::__unique_single
3139make_unique(_Args&&... __args)
3140{
3141 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3142}
3143
3144template<class _Tp>
Marshall Clowfb551102013-07-02 20:06:09 +00003145inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003146typename __unique_if<_Tp>::__unique_array_unknown_bound
3147make_unique(size_t __n)
3148{
3149 typedef typename remove_extent<_Tp>::type _Up;
3150 return unique_ptr<_Tp>(new _Up[__n]());
3151}
3152
3153template<class _Tp, class... _Args>
3154 typename __unique_if<_Tp>::__unique_array_known_bound
3155 make_unique(_Args&&...) = delete;
3156
3157#endif // _LIBCPP_STD_VER > 11
3158
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00003159template <class _Tp> struct hash;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003160
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003161template <class _Size>
3162inline _LIBCPP_INLINE_VISIBILITY
3163_Size
3164__loadword(const void* __p)
3165{
3166 _Size __r;
3167 std::memcpy(&__r, __p, sizeof(__r));
3168 return __r;
3169}
3170
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003171// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
3172// is 64 bits. This is because cityhash64 uses 64bit x 64bit
3173// multiplication, which can be very slow on 32-bit systems.
Howard Hinnant40c13d32011-12-05 00:08:45 +00003174template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003175struct __murmur2_or_cityhash;
Howard Hinnant40c13d32011-12-05 00:08:45 +00003176
3177template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003178struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003179{
3180 _Size operator()(const void* __key, _Size __len);
3181};
3182
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003183// murmur2
Howard Hinnant40c13d32011-12-05 00:08:45 +00003184template <class _Size>
3185_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003186__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003187{
3188 const _Size __m = 0x5bd1e995;
3189 const _Size __r = 24;
3190 _Size __h = __len;
3191 const unsigned char* __data = static_cast<const unsigned char*>(__key);
3192 for (; __len >= 4; __data += 4, __len -= 4)
3193 {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003194 _Size __k = __loadword<_Size>(__data);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003195 __k *= __m;
3196 __k ^= __k >> __r;
3197 __k *= __m;
3198 __h *= __m;
3199 __h ^= __k;
3200 }
3201 switch (__len)
3202 {
3203 case 3:
3204 __h ^= __data[2] << 16;
3205 case 2:
3206 __h ^= __data[1] << 8;
3207 case 1:
3208 __h ^= __data[0];
3209 __h *= __m;
3210 }
3211 __h ^= __h >> 13;
3212 __h *= __m;
3213 __h ^= __h >> 15;
3214 return __h;
3215}
3216
3217template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003218struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003219{
3220 _Size operator()(const void* __key, _Size __len);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003221
3222 private:
3223 // Some primes between 2^63 and 2^64.
3224 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
3225 static const _Size __k1 = 0xb492b66fbe98f273ULL;
3226 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
3227 static const _Size __k3 = 0xc949d7c7509e6557ULL;
3228
3229 static _Size __rotate(_Size __val, int __shift) {
3230 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
3231 }
3232
3233 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
3234 return (__val >> __shift) | (__val << (64 - __shift));
3235 }
3236
3237 static _Size __shift_mix(_Size __val) {
3238 return __val ^ (__val >> 47);
3239 }
3240
3241 static _Size __hash_len_16(_Size __u, _Size __v) {
3242 const _Size __mul = 0x9ddfea08eb382d69ULL;
3243 _Size __a = (__u ^ __v) * __mul;
3244 __a ^= (__a >> 47);
3245 _Size __b = (__v ^ __a) * __mul;
3246 __b ^= (__b >> 47);
3247 __b *= __mul;
3248 return __b;
3249 }
3250
3251 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
3252 if (__len > 8) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003253 const _Size __a = __loadword<_Size>(__s);
3254 const _Size __b = __loadword<_Size>(__s + __len - 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003255 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
3256 }
3257 if (__len >= 4) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003258 const uint32_t __a = __loadword<uint32_t>(__s);
3259 const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003260 return __hash_len_16(__len + (__a << 3), __b);
3261 }
3262 if (__len > 0) {
3263 const unsigned char __a = __s[0];
3264 const unsigned char __b = __s[__len >> 1];
3265 const unsigned char __c = __s[__len - 1];
3266 const uint32_t __y = static_cast<uint32_t>(__a) +
3267 (static_cast<uint32_t>(__b) << 8);
3268 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3269 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3270 }
3271 return __k2;
3272 }
3273
3274 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003275 const _Size __a = __loadword<_Size>(__s) * __k1;
3276 const _Size __b = __loadword<_Size>(__s + 8);
3277 const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;
3278 const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003279 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3280 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3281 }
3282
3283 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3284 // Callers do best to use "random-looking" values for a and b.
3285 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3286 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3287 __a += __w;
3288 __b = __rotate(__b + __a + __z, 21);
3289 const _Size __c = __a;
3290 __a += __x;
3291 __a += __y;
3292 __b += __rotate(__a, 44);
3293 return pair<_Size, _Size>(__a + __z, __b + __c);
3294 }
3295
3296 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3297 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3298 const char* __s, _Size __a, _Size __b) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003299 return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),
3300 __loadword<_Size>(__s + 8),
3301 __loadword<_Size>(__s + 16),
3302 __loadword<_Size>(__s + 24),
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003303 __a,
3304 __b);
3305 }
3306
3307 // Return an 8-byte hash for 33 to 64 bytes.
3308 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003309 _Size __z = __loadword<_Size>(__s + 24);
3310 _Size __a = __loadword<_Size>(__s) +
3311 (__len + __loadword<_Size>(__s + __len - 16)) * __k0;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003312 _Size __b = __rotate(__a + __z, 52);
3313 _Size __c = __rotate(__a, 37);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003314 __a += __loadword<_Size>(__s + 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003315 __c += __rotate(__a, 7);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003316 __a += __loadword<_Size>(__s + 16);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003317 _Size __vf = __a + __z;
3318 _Size __vs = __b + __rotate(__a, 31) + __c;
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003319 __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);
3320 __z += __loadword<_Size>(__s + __len - 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003321 __b = __rotate(__a + __z, 52);
3322 __c = __rotate(__a, 37);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003323 __a += __loadword<_Size>(__s + __len - 24);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003324 __c += __rotate(__a, 7);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003325 __a += __loadword<_Size>(__s + __len - 16);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003326 _Size __wf = __a + __z;
3327 _Size __ws = __b + __rotate(__a, 31) + __c;
3328 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3329 return __shift_mix(__r * __k0 + __vs) * __k2;
3330 }
Howard Hinnant40c13d32011-12-05 00:08:45 +00003331};
3332
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003333// cityhash64
Howard Hinnant40c13d32011-12-05 00:08:45 +00003334template <class _Size>
3335_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003336__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003337{
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003338 const char* __s = static_cast<const char*>(__key);
3339 if (__len <= 32) {
3340 if (__len <= 16) {
3341 return __hash_len_0_to_16(__s, __len);
3342 } else {
3343 return __hash_len_17_to_32(__s, __len);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003344 }
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003345 } else if (__len <= 64) {
3346 return __hash_len_33_to_64(__s, __len);
3347 }
3348
3349 // For strings over 64 bytes we hash the end first, and then as we
3350 // loop we keep 56 bytes of state: v, w, x, y, and z.
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003351 _Size __x = __loadword<_Size>(__s + __len - 40);
3352 _Size __y = __loadword<_Size>(__s + __len - 16) +
3353 __loadword<_Size>(__s + __len - 56);
3354 _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,
3355 __loadword<_Size>(__s + __len - 24));
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003356 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3357 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003358 __x = __x * __k1 + __loadword<_Size>(__s);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003359
3360 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3361 __len = (__len - 1) & ~static_cast<_Size>(63);
3362 do {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003363 __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;
3364 __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003365 __x ^= __w.second;
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003366 __y += __v.first + __loadword<_Size>(__s + 40);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003367 __z = __rotate(__z + __w.first, 33) * __k1;
3368 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3369 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003370 __y + __loadword<_Size>(__s + 16));
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003371 std::swap(__z, __x);
3372 __s += 64;
3373 __len -= 64;
3374 } while (__len != 0);
3375 return __hash_len_16(
3376 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3377 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003378}
3379
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003380template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3381struct __scalar_hash;
3382
3383template <class _Tp>
3384struct __scalar_hash<_Tp, 0>
3385 : public unary_function<_Tp, size_t>
Howard Hinnant21aefc32010-06-03 16:42:57 +00003386{
Howard Hinnant82894812010-09-22 16:48:34 +00003387 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003388 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003389 {
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003390 union
3391 {
3392 _Tp __t;
3393 size_t __a;
3394 } __u;
3395 __u.__a = 0;
3396 __u.__t = __v;
3397 return __u.__a;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003398 }
3399};
3400
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003401template <class _Tp>
3402struct __scalar_hash<_Tp, 1>
3403 : public unary_function<_Tp, size_t>
3404{
3405 _LIBCPP_INLINE_VISIBILITY
3406 size_t operator()(_Tp __v) const _NOEXCEPT
3407 {
3408 union
3409 {
3410 _Tp __t;
3411 size_t __a;
3412 } __u;
3413 __u.__t = __v;
3414 return __u.__a;
3415 }
3416};
3417
3418template <class _Tp>
3419struct __scalar_hash<_Tp, 2>
3420 : public unary_function<_Tp, size_t>
3421{
3422 _LIBCPP_INLINE_VISIBILITY
3423 size_t operator()(_Tp __v) const _NOEXCEPT
3424 {
3425 union
3426 {
3427 _Tp __t;
3428 struct
3429 {
3430 size_t __a;
3431 size_t __b;
Eric Fiselier692177d2015-07-18 20:40:46 +00003432 } __s;
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003433 } __u;
3434 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003435 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003436 }
3437};
3438
3439template <class _Tp>
3440struct __scalar_hash<_Tp, 3>
3441 : public unary_function<_Tp, size_t>
3442{
3443 _LIBCPP_INLINE_VISIBILITY
3444 size_t operator()(_Tp __v) const _NOEXCEPT
3445 {
3446 union
3447 {
3448 _Tp __t;
3449 struct
3450 {
3451 size_t __a;
3452 size_t __b;
3453 size_t __c;
Eric Fiselier692177d2015-07-18 20:40:46 +00003454 } __s;
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003455 } __u;
3456 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003457 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003458 }
3459};
3460
3461template <class _Tp>
3462struct __scalar_hash<_Tp, 4>
3463 : public unary_function<_Tp, size_t>
3464{
3465 _LIBCPP_INLINE_VISIBILITY
3466 size_t operator()(_Tp __v) const _NOEXCEPT
3467 {
3468 union
3469 {
3470 _Tp __t;
3471 struct
3472 {
3473 size_t __a;
3474 size_t __b;
3475 size_t __c;
3476 size_t __d;
Eric Fiselier692177d2015-07-18 20:40:46 +00003477 } __s;
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003478 } __u;
3479 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003480 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003481 }
3482};
3483
3484template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003485struct _LIBCPP_TYPE_VIS_ONLY hash<_Tp*>
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003486 : public unary_function<_Tp*, size_t>
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003487{
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003488 _LIBCPP_INLINE_VISIBILITY
3489 size_t operator()(_Tp* __v) const _NOEXCEPT
3490 {
3491 union
3492 {
3493 _Tp* __t;
3494 size_t __a;
3495 } __u;
3496 __u.__t = __v;
3497 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
3498 }
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003499};
3500
Howard Hinnant21aefc32010-06-03 16:42:57 +00003501template <class _Tp, class _Dp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003502struct _LIBCPP_TYPE_VIS_ONLY hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003503{
3504 typedef unique_ptr<_Tp, _Dp> argument_type;
3505 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003506 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003507 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003508 {
3509 typedef typename argument_type::pointer pointer;
3510 return hash<pointer>()(__ptr.get());
3511 }
3512};
3513
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003514struct __destruct_n
3515{
3516private:
3517 size_t size;
3518
3519 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003520 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003521 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3522
3523 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003524 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003525 {}
3526
Howard Hinnant1694d232011-05-28 14:41:13 +00003527 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003528 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003529 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003530 {}
3531
Howard Hinnant1694d232011-05-28 14:41:13 +00003532 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003533 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003534 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003535 {}
3536public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003537 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3538 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003539
3540 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003541 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003542 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003543
3544 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003545 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003546 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003547
3548 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003549 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003550 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003551};
3552
3553template <class _Alloc>
3554class __allocator_destructor
3555{
3556 typedef allocator_traits<_Alloc> __alloc_traits;
3557public:
3558 typedef typename __alloc_traits::pointer pointer;
3559 typedef typename __alloc_traits::size_type size_type;
3560private:
3561 _Alloc& __alloc_;
3562 size_type __s_;
3563public:
3564 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00003565 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003566 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003568 void operator()(pointer __p) _NOEXCEPT
3569 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003570};
3571
3572template <class _InputIterator, class _ForwardIterator>
3573_ForwardIterator
3574uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3575{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003576 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003577#ifndef _LIBCPP_NO_EXCEPTIONS
3578 _ForwardIterator __s = __r;
3579 try
3580 {
3581#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003582 for (; __f != __l; ++__f, (void) ++__r)
3583 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant8292d742011-12-29 17:45:35 +00003584#ifndef _LIBCPP_NO_EXCEPTIONS
3585 }
3586 catch (...)
3587 {
3588 for (; __s != __r; ++__s)
3589 __s->~value_type();
3590 throw;
3591 }
3592#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003593 return __r;
3594}
3595
3596template <class _InputIterator, class _Size, class _ForwardIterator>
3597_ForwardIterator
3598uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3599{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003600 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003601#ifndef _LIBCPP_NO_EXCEPTIONS
3602 _ForwardIterator __s = __r;
3603 try
3604 {
3605#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003606 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3607 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant8292d742011-12-29 17:45:35 +00003608#ifndef _LIBCPP_NO_EXCEPTIONS
3609 }
3610 catch (...)
3611 {
3612 for (; __s != __r; ++__s)
3613 __s->~value_type();
3614 throw;
3615 }
3616#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003617 return __r;
3618}
3619
3620template <class _ForwardIterator, class _Tp>
3621void
3622uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3623{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003624 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003625#ifndef _LIBCPP_NO_EXCEPTIONS
3626 _ForwardIterator __s = __f;
3627 try
3628 {
3629#endif
3630 for (; __f != __l; ++__f)
Marshall Clow5dce73d2015-05-19 15:01:48 +00003631 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant8292d742011-12-29 17:45:35 +00003632#ifndef _LIBCPP_NO_EXCEPTIONS
3633 }
3634 catch (...)
3635 {
3636 for (; __s != __f; ++__s)
3637 __s->~value_type();
3638 throw;
3639 }
3640#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003641}
3642
3643template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003644_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003645uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3646{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003647 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003648#ifndef _LIBCPP_NO_EXCEPTIONS
3649 _ForwardIterator __s = __f;
3650 try
3651 {
3652#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003653 for (; __n > 0; ++__f, (void) --__n)
3654 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant8292d742011-12-29 17:45:35 +00003655#ifndef _LIBCPP_NO_EXCEPTIONS
3656 }
3657 catch (...)
3658 {
3659 for (; __s != __f; ++__s)
3660 __s->~value_type();
3661 throw;
3662 }
3663#endif
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003664 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003665}
3666
Howard Hinnant82894812010-09-22 16:48:34 +00003667class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003668 : public std::exception
3669{
3670public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003671 virtual ~bad_weak_ptr() _NOEXCEPT;
3672 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003673};
3674
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003675template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003676
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003677class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003678{
3679 __shared_count(const __shared_count&);
3680 __shared_count& operator=(const __shared_count&);
3681
3682protected:
3683 long __shared_owners_;
3684 virtual ~__shared_count();
3685private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003686 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003687
3688public:
Howard Hinnant82894812010-09-22 16:48:34 +00003689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003690 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003691 : __shared_owners_(__refs) {}
3692
Howard Hinnant1694d232011-05-28 14:41:13 +00003693 void __add_shared() _NOEXCEPT;
3694 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003695 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc6e46692015-07-07 00:27:16 +00003696 long use_count() const _NOEXCEPT {
3697 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3698 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003699};
3700
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003701class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003702 : private __shared_count
3703{
3704 long __shared_weak_owners_;
3705
3706public:
Howard Hinnant82894812010-09-22 16:48:34 +00003707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003708 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003709 : __shared_count(__refs),
3710 __shared_weak_owners_(__refs) {}
3711protected:
3712 virtual ~__shared_weak_count();
3713
3714public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003715 void __add_shared() _NOEXCEPT;
3716 void __add_weak() _NOEXCEPT;
3717 void __release_shared() _NOEXCEPT;
3718 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003720 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3721 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003722
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003723 // Define the function out only if we build static libc++ without RTTI.
3724 // Otherwise we may break clients who need to compile their projects with
3725 // -fno-rtti and yet link against a libc++.dylib compiled
3726 // without -fno-rtti.
3727#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant1694d232011-05-28 14:41:13 +00003728 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003729#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003730private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003731 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003732};
3733
3734template <class _Tp, class _Dp, class _Alloc>
3735class __shared_ptr_pointer
3736 : public __shared_weak_count
3737{
3738 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3739public:
Howard Hinnant82894812010-09-22 16:48:34 +00003740 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003741 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003742 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003743
Howard Hinnantd4444702010-08-11 17:04:31 +00003744#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00003745 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00003746#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003747
3748private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003749 virtual void __on_zero_shared() _NOEXCEPT;
3750 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003751};
3752
Howard Hinnantd4444702010-08-11 17:04:31 +00003753#ifndef _LIBCPP_NO_RTTI
3754
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003755template <class _Tp, class _Dp, class _Alloc>
3756const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00003757__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003758{
Marshall Clow4b3ca8c2014-11-17 19:05:50 +00003759 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003760}
3761
Howard Hinnant324bb032010-08-22 00:02:43 +00003762#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003763
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003764template <class _Tp, class _Dp, class _Alloc>
3765void
Howard Hinnant1694d232011-05-28 14:41:13 +00003766__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003767{
3768 __data_.first().second()(__data_.first().first());
3769 __data_.first().second().~_Dp();
3770}
3771
3772template <class _Tp, class _Dp, class _Alloc>
3773void
Howard Hinnant1694d232011-05-28 14:41:13 +00003774__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003775{
Eric Fiselier8492cd82015-02-05 23:01:40 +00003776 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3777 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003778 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3779
Eric Fiselier8492cd82015-02-05 23:01:40 +00003780 _Al __a(__data_.second());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003781 __data_.second().~_Alloc();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003782 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003783}
3784
3785template <class _Tp, class _Alloc>
3786class __shared_ptr_emplace
3787 : public __shared_weak_count
3788{
3789 __compressed_pair<_Alloc, _Tp> __data_;
3790public:
3791#ifndef _LIBCPP_HAS_NO_VARIADICS
3792
Howard Hinnant82894812010-09-22 16:48:34 +00003793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003794 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003795 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003796
3797 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00003798 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003799 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00003800 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3801 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003802
3803#else // _LIBCPP_HAS_NO_VARIADICS
3804
Howard Hinnant82894812010-09-22 16:48:34 +00003805 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003806 __shared_ptr_emplace(_Alloc __a)
3807 : __data_(__a) {}
3808
3809 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00003810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003811 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3812 : __data_(__a, _Tp(__a0)) {}
3813
3814 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00003815 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003816 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3817 : __data_(__a, _Tp(__a0, __a1)) {}
3818
3819 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00003820 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003821 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3822 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3823
3824#endif // _LIBCPP_HAS_NO_VARIADICS
3825
3826private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003827 virtual void __on_zero_shared() _NOEXCEPT;
3828 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003829public:
Howard Hinnant82894812010-09-22 16:48:34 +00003830 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003831 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003832};
3833
3834template <class _Tp, class _Alloc>
3835void
Howard Hinnant1694d232011-05-28 14:41:13 +00003836__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003837{
3838 __data_.second().~_Tp();
3839}
3840
3841template <class _Tp, class _Alloc>
3842void
Howard Hinnant1694d232011-05-28 14:41:13 +00003843__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003844{
Eric Fiselier8492cd82015-02-05 23:01:40 +00003845 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3846 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003847 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40 +00003848 _Al __a(__data_.first());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003849 __data_.first().~_Alloc();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003850 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003851}
3852
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003853template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003854
3855template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003856class _LIBCPP_TYPE_VIS_ONLY shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003857{
Howard Hinnant324bb032010-08-22 00:02:43 +00003858public:
3859 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003860private:
3861 element_type* __ptr_;
3862 __shared_weak_count* __cntrl_;
3863
3864 struct __nat {int __for_bool_;};
3865public:
Howard Hinnant46e94932012-07-07 20:56:04 +00003866 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
3867 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiene1678a12014-01-31 09:30:46 +00003868 template<class _Yp>
3869 explicit shared_ptr(_Yp* __p,
3870 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3871 template<class _Yp, class _Dp>
3872 shared_ptr(_Yp* __p, _Dp __d,
3873 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3874 template<class _Yp, class _Dp, class _Alloc>
3875 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3876 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003877 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3878 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant1694d232011-05-28 14:41:13 +00003879 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3880 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003881 template<class _Yp>
3882 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003883 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3884 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003885#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003886 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003887 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003888 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3889 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003890#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003891 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003892 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003893#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003894 template<class _Yp>
3895 shared_ptr(auto_ptr<_Yp>&& __r,
3896 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant324bb032010-08-22 00:02:43 +00003897#else
Logan Chiene1678a12014-01-31 09:30:46 +00003898 template<class _Yp>
3899 shared_ptr(auto_ptr<_Yp> __r,
3900 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003901#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003902#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003903 template <class _Yp, class _Dp>
3904 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3905 typename enable_if
3906 <
3907 !is_lvalue_reference<_Dp>::value &&
3908 !is_array<_Yp>::value &&
3909 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3910 __nat
3911 >::type = __nat());
3912 template <class _Yp, class _Dp>
3913 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3914 typename enable_if
3915 <
3916 is_lvalue_reference<_Dp>::value &&
3917 !is_array<_Yp>::value &&
3918 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3919 __nat
3920 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003921#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003922 template <class _Yp, class _Dp>
3923 shared_ptr(unique_ptr<_Yp, _Dp>,
3924 typename enable_if
3925 <
3926 !is_lvalue_reference<_Dp>::value &&
3927 !is_array<_Yp>::value &&
3928 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3929 __nat
3930 >::type = __nat());
3931 template <class _Yp, class _Dp>
3932 shared_ptr(unique_ptr<_Yp, _Dp>,
3933 typename enable_if
3934 <
3935 is_lvalue_reference<_Dp>::value &&
3936 !is_array<_Yp>::value &&
3937 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3938 __nat
3939 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003940#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003941
3942 ~shared_ptr();
3943
Howard Hinnant1694d232011-05-28 14:41:13 +00003944 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003945 template<class _Yp>
3946 typename enable_if
3947 <
3948 is_convertible<_Yp*, element_type*>::value,
3949 shared_ptr&
3950 >::type
3951 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003952#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003953 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003954 template<class _Yp>
3955 typename enable_if
3956 <
3957 is_convertible<_Yp*, element_type*>::value,
3958 shared_ptr<_Tp>&
3959 >::type
3960 operator=(shared_ptr<_Yp>&& __r);
3961 template<class _Yp>
3962 typename enable_if
3963 <
3964 !is_array<_Yp>::value &&
3965 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant37c4acf2013-09-13 23:56:00 +00003966 shared_ptr
3967 >::type&
Howard Hinnant57199402012-01-02 17:56:02 +00003968 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003969#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003970 template<class _Yp>
3971 typename enable_if
3972 <
3973 !is_array<_Yp>::value &&
3974 is_convertible<_Yp*, element_type*>::value,
3975 shared_ptr&
3976 >::type
3977 operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003978#endif
Howard Hinnant57199402012-01-02 17:56:02 +00003979 template <class _Yp, class _Dp>
3980 typename enable_if
3981 <
3982 !is_array<_Yp>::value &&
3983 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3984 shared_ptr&
3985 >::type
Howard Hinnant73d21a42010-09-04 23:28:19 +00003986#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003987 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003988#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003989 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003990#endif
3991
Howard Hinnant1694d232011-05-28 14:41:13 +00003992 void swap(shared_ptr& __r) _NOEXCEPT;
3993 void reset() _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003994 template<class _Yp>
3995 typename enable_if
3996 <
3997 is_convertible<_Yp*, element_type*>::value,
3998 void
3999 >::type
4000 reset(_Yp* __p);
4001 template<class _Yp, class _Dp>
4002 typename enable_if
4003 <
4004 is_convertible<_Yp*, element_type*>::value,
4005 void
4006 >::type
4007 reset(_Yp* __p, _Dp __d);
4008 template<class _Yp, class _Dp, class _Alloc>
4009 typename enable_if
4010 <
4011 is_convertible<_Yp*, element_type*>::value,
4012 void
4013 >::type
4014 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004015
Howard Hinnant82894812010-09-22 16:48:34 +00004016 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004017 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004019 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
4020 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004021 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004022 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004023 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004024 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00004025 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004026 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00004027 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant77861882012-02-21 21:46:43 +00004028 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant99968442011-11-29 18:15:50 +00004029 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00004030 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00004031 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004032 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant99968442011-11-29 18:15:50 +00004033 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00004034 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00004035 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004036 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant5fec82d2012-07-30 01:40:57 +00004037 _LIBCPP_INLINE_VISIBILITY
4038 bool
4039 __owner_equivalent(const shared_ptr& __p) const
4040 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004041
Howard Hinnantd4444702010-08-11 17:04:31 +00004042#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004043 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00004044 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004045 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004046 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00004047#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004048
4049#ifndef _LIBCPP_HAS_NO_VARIADICS
4050
4051 template<class ..._Args>
4052 static
4053 shared_ptr<_Tp>
4054 make_shared(_Args&& ...__args);
4055
4056 template<class _Alloc, class ..._Args>
4057 static
4058 shared_ptr<_Tp>
4059 allocate_shared(const _Alloc& __a, _Args&& ...__args);
4060
4061#else // _LIBCPP_HAS_NO_VARIADICS
4062
4063 static shared_ptr<_Tp> make_shared();
4064
4065 template<class _A0>
4066 static shared_ptr<_Tp> make_shared(_A0&);
4067
4068 template<class _A0, class _A1>
4069 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
4070
4071 template<class _A0, class _A1, class _A2>
4072 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
4073
4074 template<class _Alloc>
4075 static shared_ptr<_Tp>
4076 allocate_shared(const _Alloc& __a);
4077
4078 template<class _Alloc, class _A0>
4079 static shared_ptr<_Tp>
4080 allocate_shared(const _Alloc& __a, _A0& __a0);
4081
4082 template<class _Alloc, class _A0, class _A1>
4083 static shared_ptr<_Tp>
4084 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
4085
4086 template<class _Alloc, class _A0, class _A1, class _A2>
4087 static shared_ptr<_Tp>
4088 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
4089
4090#endif // _LIBCPP_HAS_NO_VARIADICS
4091
4092private:
4093
4094 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00004095 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004096 void
Marshall Clowfc3a3ff2015-05-27 20:36:14 +00004097 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004098 {
4099 if (__e)
Marshall Clowc4113372015-06-19 15:54:13 +00004100 {
4101 __e->__weak_this_.__ptr_ = const_cast<_Yp*>(static_cast<const _Yp*>(__e));
4102 __e->__weak_this_.__cntrl_ = __cntrl_;
Marshall Clow46d06b92015-06-19 19:32:06 +00004103 __cntrl_->__add_weak();
Marshall Clowc4113372015-06-19 15:54:13 +00004104 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004105 }
4106
Howard Hinnant82894812010-09-22 16:48:34 +00004107 _LIBCPP_INLINE_VISIBILITY
Marshall Clow60784f62015-05-27 22:44:47 +00004108 void __enable_weak_this(const volatile void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004109
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004110 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
4111 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004112};
4113
4114template<class _Tp>
4115inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00004116_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004117shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004118 : __ptr_(0),
4119 __cntrl_(0)
4120{
4121}
4122
4123template<class _Tp>
4124inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00004125_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004126shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004127 : __ptr_(0),
4128 __cntrl_(0)
4129{
4130}
4131
4132template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004133template<class _Yp>
4134shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4135 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004136 : __ptr_(__p)
4137{
4138 unique_ptr<_Yp> __hold(__p);
4139 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4140 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
4141 __hold.release();
4142 __enable_weak_this(__p);
4143}
4144
4145template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004146template<class _Yp, class _Dp>
4147shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4148 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004149 : __ptr_(__p)
4150{
4151#ifndef _LIBCPP_NO_EXCEPTIONS
4152 try
4153 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004154#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004155 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4156 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
4157 __enable_weak_this(__p);
4158#ifndef _LIBCPP_NO_EXCEPTIONS
4159 }
4160 catch (...)
4161 {
4162 __d(__p);
4163 throw;
4164 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004165#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004166}
4167
4168template<class _Tp>
4169template<class _Dp>
4170shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4171 : __ptr_(0)
4172{
4173#ifndef _LIBCPP_NO_EXCEPTIONS
4174 try
4175 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004176#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004177 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4178 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4179#ifndef _LIBCPP_NO_EXCEPTIONS
4180 }
4181 catch (...)
4182 {
4183 __d(__p);
4184 throw;
4185 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004186#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004187}
4188
4189template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004190template<class _Yp, class _Dp, class _Alloc>
4191shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4192 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004193 : __ptr_(__p)
4194{
4195#ifndef _LIBCPP_NO_EXCEPTIONS
4196 try
4197 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004198#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004199 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004200 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004201 typedef __allocator_destructor<_A2> _D2;
4202 _A2 __a2(__a);
4203 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004204 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4205 _CntrlBlk(__p, __d, __a);
4206 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004207 __enable_weak_this(__p);
4208#ifndef _LIBCPP_NO_EXCEPTIONS
4209 }
4210 catch (...)
4211 {
4212 __d(__p);
4213 throw;
4214 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004215#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004216}
4217
4218template<class _Tp>
4219template<class _Dp, class _Alloc>
4220shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4221 : __ptr_(0)
4222{
4223#ifndef _LIBCPP_NO_EXCEPTIONS
4224 try
4225 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004226#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004227 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004228 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004229 typedef __allocator_destructor<_A2> _D2;
4230 _A2 __a2(__a);
4231 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004232 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4233 _CntrlBlk(__p, __d, __a);
4234 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004235#ifndef _LIBCPP_NO_EXCEPTIONS
4236 }
4237 catch (...)
4238 {
4239 __d(__p);
4240 throw;
4241 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004242#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004243}
4244
4245template<class _Tp>
4246template<class _Yp>
4247inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004248shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004249 : __ptr_(__p),
4250 __cntrl_(__r.__cntrl_)
4251{
4252 if (__cntrl_)
4253 __cntrl_->__add_shared();
4254}
4255
4256template<class _Tp>
4257inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004258shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004259 : __ptr_(__r.__ptr_),
4260 __cntrl_(__r.__cntrl_)
4261{
4262 if (__cntrl_)
4263 __cntrl_->__add_shared();
4264}
4265
4266template<class _Tp>
4267template<class _Yp>
4268inline _LIBCPP_INLINE_VISIBILITY
4269shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4270 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004271 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004272 : __ptr_(__r.__ptr_),
4273 __cntrl_(__r.__cntrl_)
4274{
4275 if (__cntrl_)
4276 __cntrl_->__add_shared();
4277}
4278
Howard Hinnant73d21a42010-09-04 23:28:19 +00004279#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004280
4281template<class _Tp>
4282inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004283shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004284 : __ptr_(__r.__ptr_),
4285 __cntrl_(__r.__cntrl_)
4286{
4287 __r.__ptr_ = 0;
4288 __r.__cntrl_ = 0;
4289}
4290
4291template<class _Tp>
4292template<class _Yp>
4293inline _LIBCPP_INLINE_VISIBILITY
4294shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4295 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004296 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004297 : __ptr_(__r.__ptr_),
4298 __cntrl_(__r.__cntrl_)
4299{
4300 __r.__ptr_ = 0;
4301 __r.__cntrl_ = 0;
4302}
4303
Howard Hinnant73d21a42010-09-04 23:28:19 +00004304#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004305
4306template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004307template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004308#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00004309shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004310#else
Logan Chiene1678a12014-01-31 09:30:46 +00004311shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004312#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004313 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004314 : __ptr_(__r.get())
4315{
4316 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4317 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
4318 __enable_weak_this(__r.get());
4319 __r.release();
4320}
4321
4322template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004323template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004324#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004325shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4326#else
4327shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4328#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004329 typename enable_if
4330 <
4331 !is_lvalue_reference<_Dp>::value &&
4332 !is_array<_Yp>::value &&
4333 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4334 __nat
4335 >::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004336 : __ptr_(__r.get())
4337{
Marshall Clow0ad232a2015-05-10 13:59:45 +00004338#if _LIBCPP_STD_VER > 11
4339 if (__ptr_ == nullptr)
4340 __cntrl_ = nullptr;
4341 else
4342#endif
4343 {
4344 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4345 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
4346 __enable_weak_this(__r.get());
4347 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004348 __r.release();
4349}
4350
4351template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004352template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004353#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004354shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4355#else
4356shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4357#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004358 typename enable_if
4359 <
4360 is_lvalue_reference<_Dp>::value &&
4361 !is_array<_Yp>::value &&
4362 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4363 __nat
4364 >::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004365 : __ptr_(__r.get())
4366{
Marshall Clow0ad232a2015-05-10 13:59:45 +00004367#if _LIBCPP_STD_VER > 11
4368 if (__ptr_ == nullptr)
4369 __cntrl_ = nullptr;
4370 else
4371#endif
4372 {
4373 typedef __shared_ptr_pointer<_Yp*,
4374 reference_wrapper<typename remove_reference<_Dp>::type>,
4375 allocator<_Yp> > _CntrlBlk;
4376 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
4377 __enable_weak_this(__r.get());
4378 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004379 __r.release();
4380}
4381
4382#ifndef _LIBCPP_HAS_NO_VARIADICS
4383
4384template<class _Tp>
4385template<class ..._Args>
4386shared_ptr<_Tp>
4387shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4388{
4389 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4390 typedef allocator<_CntrlBlk> _A2;
4391 typedef __allocator_destructor<_A2> _D2;
4392 _A2 __a2;
4393 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004394 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004395 shared_ptr<_Tp> __r;
4396 __r.__ptr_ = __hold2.get()->get();
4397 __r.__cntrl_ = __hold2.release();
4398 __r.__enable_weak_this(__r.__ptr_);
4399 return __r;
4400}
4401
4402template<class _Tp>
4403template<class _Alloc, class ..._Args>
4404shared_ptr<_Tp>
4405shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4406{
4407 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004408 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004409 typedef __allocator_destructor<_A2> _D2;
4410 _A2 __a2(__a);
4411 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004412 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4413 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004414 shared_ptr<_Tp> __r;
4415 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004416 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004417 __r.__enable_weak_this(__r.__ptr_);
4418 return __r;
4419}
4420
4421#else // _LIBCPP_HAS_NO_VARIADICS
4422
4423template<class _Tp>
4424shared_ptr<_Tp>
4425shared_ptr<_Tp>::make_shared()
4426{
4427 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4428 typedef allocator<_CntrlBlk> _Alloc2;
4429 typedef __allocator_destructor<_Alloc2> _D2;
4430 _Alloc2 __alloc2;
4431 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4432 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4433 shared_ptr<_Tp> __r;
4434 __r.__ptr_ = __hold2.get()->get();
4435 __r.__cntrl_ = __hold2.release();
4436 __r.__enable_weak_this(__r.__ptr_);
4437 return __r;
4438}
4439
4440template<class _Tp>
4441template<class _A0>
4442shared_ptr<_Tp>
4443shared_ptr<_Tp>::make_shared(_A0& __a0)
4444{
4445 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4446 typedef allocator<_CntrlBlk> _Alloc2;
4447 typedef __allocator_destructor<_Alloc2> _D2;
4448 _Alloc2 __alloc2;
4449 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4450 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4451 shared_ptr<_Tp> __r;
4452 __r.__ptr_ = __hold2.get()->get();
4453 __r.__cntrl_ = __hold2.release();
4454 __r.__enable_weak_this(__r.__ptr_);
4455 return __r;
4456}
4457
4458template<class _Tp>
4459template<class _A0, class _A1>
4460shared_ptr<_Tp>
4461shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4462{
4463 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4464 typedef allocator<_CntrlBlk> _Alloc2;
4465 typedef __allocator_destructor<_Alloc2> _D2;
4466 _Alloc2 __alloc2;
4467 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4468 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4469 shared_ptr<_Tp> __r;
4470 __r.__ptr_ = __hold2.get()->get();
4471 __r.__cntrl_ = __hold2.release();
4472 __r.__enable_weak_this(__r.__ptr_);
4473 return __r;
4474}
4475
4476template<class _Tp>
4477template<class _A0, class _A1, class _A2>
4478shared_ptr<_Tp>
4479shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4480{
4481 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4482 typedef allocator<_CntrlBlk> _Alloc2;
4483 typedef __allocator_destructor<_Alloc2> _D2;
4484 _Alloc2 __alloc2;
4485 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4486 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4487 shared_ptr<_Tp> __r;
4488 __r.__ptr_ = __hold2.get()->get();
4489 __r.__cntrl_ = __hold2.release();
4490 __r.__enable_weak_this(__r.__ptr_);
4491 return __r;
4492}
4493
4494template<class _Tp>
4495template<class _Alloc>
4496shared_ptr<_Tp>
4497shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4498{
4499 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004500 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004501 typedef __allocator_destructor<_Alloc2> _D2;
4502 _Alloc2 __alloc2(__a);
4503 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004504 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4505 _CntrlBlk(__a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004506 shared_ptr<_Tp> __r;
4507 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004508 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004509 __r.__enable_weak_this(__r.__ptr_);
4510 return __r;
4511}
4512
4513template<class _Tp>
4514template<class _Alloc, class _A0>
4515shared_ptr<_Tp>
4516shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4517{
4518 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004519 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004520 typedef __allocator_destructor<_Alloc2> _D2;
4521 _Alloc2 __alloc2(__a);
4522 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004523 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4524 _CntrlBlk(__a, __a0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004525 shared_ptr<_Tp> __r;
4526 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004527 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004528 __r.__enable_weak_this(__r.__ptr_);
4529 return __r;
4530}
4531
4532template<class _Tp>
4533template<class _Alloc, class _A0, class _A1>
4534shared_ptr<_Tp>
4535shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4536{
4537 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004538 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004539 typedef __allocator_destructor<_Alloc2> _D2;
4540 _Alloc2 __alloc2(__a);
4541 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004542 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4543 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004544 shared_ptr<_Tp> __r;
4545 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004546 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004547 __r.__enable_weak_this(__r.__ptr_);
4548 return __r;
4549}
4550
4551template<class _Tp>
4552template<class _Alloc, class _A0, class _A1, class _A2>
4553shared_ptr<_Tp>
4554shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4555{
4556 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004557 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004558 typedef __allocator_destructor<_Alloc2> _D2;
4559 _Alloc2 __alloc2(__a);
4560 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004561 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4562 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004563 shared_ptr<_Tp> __r;
4564 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004565 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004566 __r.__enable_weak_this(__r.__ptr_);
4567 return __r;
4568}
4569
4570#endif // _LIBCPP_HAS_NO_VARIADICS
4571
4572template<class _Tp>
4573shared_ptr<_Tp>::~shared_ptr()
4574{
4575 if (__cntrl_)
4576 __cntrl_->__release_shared();
4577}
4578
4579template<class _Tp>
4580inline _LIBCPP_INLINE_VISIBILITY
4581shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004582shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004583{
4584 shared_ptr(__r).swap(*this);
4585 return *this;
4586}
4587
4588template<class _Tp>
4589template<class _Yp>
4590inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004591typename enable_if
4592<
4593 is_convertible<_Yp*, _Tp*>::value,
4594 shared_ptr<_Tp>&
4595>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004596shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004597{
4598 shared_ptr(__r).swap(*this);
4599 return *this;
4600}
4601
Howard Hinnant73d21a42010-09-04 23:28:19 +00004602#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004603
4604template<class _Tp>
4605inline _LIBCPP_INLINE_VISIBILITY
4606shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004607shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004608{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004609 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004610 return *this;
4611}
4612
4613template<class _Tp>
4614template<class _Yp>
4615inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004616typename enable_if
4617<
4618 is_convertible<_Yp*, _Tp*>::value,
4619 shared_ptr<_Tp>&
4620>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004621shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4622{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004623 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004624 return *this;
4625}
4626
4627template<class _Tp>
4628template<class _Yp>
4629inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004630typename enable_if
4631<
4632 !is_array<_Yp>::value &&
4633 is_convertible<_Yp*, _Tp*>::value,
Howard Hinnant37c4acf2013-09-13 23:56:00 +00004634 shared_ptr<_Tp>
4635>::type&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004636shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4637{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004638 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004639 return *this;
4640}
4641
4642template<class _Tp>
4643template <class _Yp, class _Dp>
4644inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004645typename enable_if
4646<
4647 !is_array<_Yp>::value &&
4648 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4649 shared_ptr<_Tp>&
4650>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004651shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4652{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004653 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004654 return *this;
4655}
4656
Howard Hinnant73d21a42010-09-04 23:28:19 +00004657#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004658
4659template<class _Tp>
4660template<class _Yp>
4661inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004662typename enable_if
4663<
4664 !is_array<_Yp>::value &&
4665 is_convertible<_Yp*, _Tp*>::value,
4666 shared_ptr<_Tp>&
4667>::type
Howard Hinnant324bb032010-08-22 00:02:43 +00004668shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004669{
4670 shared_ptr(__r).swap(*this);
4671 return *this;
4672}
4673
4674template<class _Tp>
4675template <class _Yp, class _Dp>
4676inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004677typename enable_if
4678<
4679 !is_array<_Yp>::value &&
4680 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4681 shared_ptr<_Tp>&
4682>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004683shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4684{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004685 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004686 return *this;
4687}
4688
Howard Hinnant73d21a42010-09-04 23:28:19 +00004689#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004690
4691template<class _Tp>
4692inline _LIBCPP_INLINE_VISIBILITY
4693void
Howard Hinnant1694d232011-05-28 14:41:13 +00004694shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004695{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004696 _VSTD::swap(__ptr_, __r.__ptr_);
4697 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004698}
4699
4700template<class _Tp>
4701inline _LIBCPP_INLINE_VISIBILITY
4702void
Howard Hinnant1694d232011-05-28 14:41:13 +00004703shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004704{
4705 shared_ptr().swap(*this);
4706}
4707
4708template<class _Tp>
4709template<class _Yp>
4710inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004711typename enable_if
4712<
4713 is_convertible<_Yp*, _Tp*>::value,
4714 void
4715>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004716shared_ptr<_Tp>::reset(_Yp* __p)
4717{
4718 shared_ptr(__p).swap(*this);
4719}
4720
4721template<class _Tp>
4722template<class _Yp, class _Dp>
4723inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004724typename enable_if
4725<
4726 is_convertible<_Yp*, _Tp*>::value,
4727 void
4728>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004729shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4730{
4731 shared_ptr(__p, __d).swap(*this);
4732}
4733
4734template<class _Tp>
4735template<class _Yp, class _Dp, class _Alloc>
4736inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004737typename enable_if
4738<
4739 is_convertible<_Yp*, _Tp*>::value,
4740 void
4741>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004742shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4743{
4744 shared_ptr(__p, __d, __a).swap(*this);
4745}
4746
4747#ifndef _LIBCPP_HAS_NO_VARIADICS
4748
Howard Hinnant324bb032010-08-22 00:02:43 +00004749template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004750inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004751typename enable_if
4752<
4753 !is_array<_Tp>::value,
4754 shared_ptr<_Tp>
4755>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004756make_shared(_Args&& ...__args)
4757{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004758 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004759}
4760
Howard Hinnant324bb032010-08-22 00:02:43 +00004761template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004762inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004763typename enable_if
4764<
4765 !is_array<_Tp>::value,
4766 shared_ptr<_Tp>
4767>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004768allocate_shared(const _Alloc& __a, _Args&& ...__args)
4769{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004770 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004771}
4772
4773#else // _LIBCPP_HAS_NO_VARIADICS
4774
4775template<class _Tp>
4776inline _LIBCPP_INLINE_VISIBILITY
4777shared_ptr<_Tp>
4778make_shared()
4779{
4780 return shared_ptr<_Tp>::make_shared();
4781}
4782
4783template<class _Tp, class _A0>
4784inline _LIBCPP_INLINE_VISIBILITY
4785shared_ptr<_Tp>
4786make_shared(_A0& __a0)
4787{
4788 return shared_ptr<_Tp>::make_shared(__a0);
4789}
4790
4791template<class _Tp, class _A0, class _A1>
4792inline _LIBCPP_INLINE_VISIBILITY
4793shared_ptr<_Tp>
4794make_shared(_A0& __a0, _A1& __a1)
4795{
4796 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4797}
4798
Howard Hinnant324bb032010-08-22 00:02:43 +00004799template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004800inline _LIBCPP_INLINE_VISIBILITY
4801shared_ptr<_Tp>
4802make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4803{
4804 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4805}
4806
4807template<class _Tp, class _Alloc>
4808inline _LIBCPP_INLINE_VISIBILITY
4809shared_ptr<_Tp>
4810allocate_shared(const _Alloc& __a)
4811{
4812 return shared_ptr<_Tp>::allocate_shared(__a);
4813}
4814
4815template<class _Tp, class _Alloc, class _A0>
4816inline _LIBCPP_INLINE_VISIBILITY
4817shared_ptr<_Tp>
4818allocate_shared(const _Alloc& __a, _A0& __a0)
4819{
4820 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4821}
4822
4823template<class _Tp, class _Alloc, class _A0, class _A1>
4824inline _LIBCPP_INLINE_VISIBILITY
4825shared_ptr<_Tp>
4826allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4827{
4828 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4829}
4830
4831template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4832inline _LIBCPP_INLINE_VISIBILITY
4833shared_ptr<_Tp>
4834allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4835{
4836 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4837}
4838
4839#endif // _LIBCPP_HAS_NO_VARIADICS
4840
4841template<class _Tp, class _Up>
4842inline _LIBCPP_INLINE_VISIBILITY
4843bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004844operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004845{
4846 return __x.get() == __y.get();
4847}
4848
4849template<class _Tp, class _Up>
4850inline _LIBCPP_INLINE_VISIBILITY
4851bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004852operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004853{
4854 return !(__x == __y);
4855}
4856
4857template<class _Tp, class _Up>
4858inline _LIBCPP_INLINE_VISIBILITY
4859bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004860operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004861{
Eric Fiselier8492cd82015-02-05 23:01:40 +00004862 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4863 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnant3fadda32012-02-21 21:02:58 +00004864}
4865
4866template<class _Tp, class _Up>
4867inline _LIBCPP_INLINE_VISIBILITY
4868bool
4869operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4870{
4871 return __y < __x;
4872}
4873
4874template<class _Tp, class _Up>
4875inline _LIBCPP_INLINE_VISIBILITY
4876bool
4877operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4878{
4879 return !(__y < __x);
4880}
4881
4882template<class _Tp, class _Up>
4883inline _LIBCPP_INLINE_VISIBILITY
4884bool
4885operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4886{
4887 return !(__x < __y);
4888}
4889
4890template<class _Tp>
4891inline _LIBCPP_INLINE_VISIBILITY
4892bool
4893operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4894{
4895 return !__x;
4896}
4897
4898template<class _Tp>
4899inline _LIBCPP_INLINE_VISIBILITY
4900bool
4901operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4902{
4903 return !__x;
4904}
4905
4906template<class _Tp>
4907inline _LIBCPP_INLINE_VISIBILITY
4908bool
4909operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4910{
4911 return static_cast<bool>(__x);
4912}
4913
4914template<class _Tp>
4915inline _LIBCPP_INLINE_VISIBILITY
4916bool
4917operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4918{
4919 return static_cast<bool>(__x);
4920}
4921
4922template<class _Tp>
4923inline _LIBCPP_INLINE_VISIBILITY
4924bool
4925operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4926{
4927 return less<_Tp*>()(__x.get(), nullptr);
4928}
4929
4930template<class _Tp>
4931inline _LIBCPP_INLINE_VISIBILITY
4932bool
4933operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4934{
4935 return less<_Tp*>()(nullptr, __x.get());
4936}
4937
4938template<class _Tp>
4939inline _LIBCPP_INLINE_VISIBILITY
4940bool
4941operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4942{
4943 return nullptr < __x;
4944}
4945
4946template<class _Tp>
4947inline _LIBCPP_INLINE_VISIBILITY
4948bool
4949operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4950{
4951 return __x < nullptr;
4952}
4953
4954template<class _Tp>
4955inline _LIBCPP_INLINE_VISIBILITY
4956bool
4957operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4958{
4959 return !(nullptr < __x);
4960}
4961
4962template<class _Tp>
4963inline _LIBCPP_INLINE_VISIBILITY
4964bool
4965operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4966{
4967 return !(__x < nullptr);
4968}
4969
4970template<class _Tp>
4971inline _LIBCPP_INLINE_VISIBILITY
4972bool
4973operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4974{
4975 return !(__x < nullptr);
4976}
4977
4978template<class _Tp>
4979inline _LIBCPP_INLINE_VISIBILITY
4980bool
4981operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4982{
4983 return !(nullptr < __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004984}
4985
4986template<class _Tp>
4987inline _LIBCPP_INLINE_VISIBILITY
4988void
Howard Hinnant1694d232011-05-28 14:41:13 +00004989swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004990{
4991 __x.swap(__y);
4992}
4993
4994template<class _Tp, class _Up>
4995inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004996typename enable_if
4997<
4998 !is_array<_Tp>::value && !is_array<_Up>::value,
4999 shared_ptr<_Tp>
5000>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005001static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005002{
5003 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
5004}
5005
5006template<class _Tp, class _Up>
5007inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005008typename enable_if
5009<
5010 !is_array<_Tp>::value && !is_array<_Up>::value,
5011 shared_ptr<_Tp>
5012>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005013dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005014{
5015 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
5016 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
5017}
5018
5019template<class _Tp, class _Up>
Howard Hinnant57199402012-01-02 17:56:02 +00005020typename enable_if
5021<
5022 is_array<_Tp>::value == is_array<_Up>::value,
5023 shared_ptr<_Tp>
5024>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005025const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005026{
Howard Hinnant57199402012-01-02 17:56:02 +00005027 typedef typename remove_extent<_Tp>::type _RTp;
5028 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005029}
5030
Howard Hinnantd4444702010-08-11 17:04:31 +00005031#ifndef _LIBCPP_NO_RTTI
5032
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005033template<class _Dp, class _Tp>
5034inline _LIBCPP_INLINE_VISIBILITY
5035_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00005036get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005037{
5038 return __p.template __get_deleter<_Dp>();
5039}
5040
Howard Hinnant324bb032010-08-22 00:02:43 +00005041#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00005042
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005043template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005044class _LIBCPP_TYPE_VIS_ONLY weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005045{
Howard Hinnant324bb032010-08-22 00:02:43 +00005046public:
5047 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005048private:
5049 element_type* __ptr_;
5050 __shared_weak_count* __cntrl_;
5051
Howard Hinnant324bb032010-08-22 00:02:43 +00005052public:
Howard Hinnant46e94932012-07-07 20:56:04 +00005053 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005054 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00005055 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5056 _NOEXCEPT;
5057 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005058 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00005059 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5060 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005061
Howard Hinnant57199402012-01-02 17:56:02 +00005062#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5063 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
5064 template<class _Yp> weak_ptr(weak_ptr<_Yp>&& __r,
5065 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5066 _NOEXCEPT;
5067#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00005068 ~weak_ptr();
5069
Howard Hinnant1694d232011-05-28 14:41:13 +00005070 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00005071 template<class _Yp>
5072 typename enable_if
5073 <
5074 is_convertible<_Yp*, element_type*>::value,
5075 weak_ptr&
5076 >::type
5077 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
5078
5079#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5080
5081 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
5082 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>&& __r) _NOEXCEPT;
5089
5090#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5091
5092 template<class _Yp>
5093 typename enable_if
5094 <
5095 is_convertible<_Yp*, element_type*>::value,
5096 weak_ptr&
5097 >::type
5098 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005099
Howard Hinnant1694d232011-05-28 14:41:13 +00005100 void swap(weak_ptr& __r) _NOEXCEPT;
5101 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005102
Howard Hinnant82894812010-09-22 16:48:34 +00005103 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005104 long use_count() const _NOEXCEPT
5105 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00005106 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005107 bool expired() const _NOEXCEPT
5108 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5109 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00005110 template<class _Up>
5111 _LIBCPP_INLINE_VISIBILITY
5112 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005113 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00005114 template<class _Up>
5115 _LIBCPP_INLINE_VISIBILITY
5116 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005117 {return __cntrl_ < __r.__cntrl_;}
5118
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005119 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
5120 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005121};
5122
5123template<class _Tp>
5124inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00005125_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005126weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005127 : __ptr_(0),
5128 __cntrl_(0)
5129{
5130}
5131
5132template<class _Tp>
5133inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005134weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005135 : __ptr_(__r.__ptr_),
5136 __cntrl_(__r.__cntrl_)
5137{
5138 if (__cntrl_)
5139 __cntrl_->__add_weak();
5140}
5141
5142template<class _Tp>
5143template<class _Yp>
5144inline _LIBCPP_INLINE_VISIBILITY
5145weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005146 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005147 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005148 : __ptr_(__r.__ptr_),
5149 __cntrl_(__r.__cntrl_)
5150{
5151 if (__cntrl_)
5152 __cntrl_->__add_weak();
5153}
5154
5155template<class _Tp>
5156template<class _Yp>
5157inline _LIBCPP_INLINE_VISIBILITY
5158weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005159 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005160 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005161 : __ptr_(__r.__ptr_),
5162 __cntrl_(__r.__cntrl_)
5163{
5164 if (__cntrl_)
5165 __cntrl_->__add_weak();
5166}
5167
Howard Hinnant57199402012-01-02 17:56:02 +00005168#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5169
5170template<class _Tp>
5171inline _LIBCPP_INLINE_VISIBILITY
5172weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5173 : __ptr_(__r.__ptr_),
5174 __cntrl_(__r.__cntrl_)
5175{
5176 __r.__ptr_ = 0;
5177 __r.__cntrl_ = 0;
5178}
5179
5180template<class _Tp>
5181template<class _Yp>
5182inline _LIBCPP_INLINE_VISIBILITY
5183weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5184 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5185 _NOEXCEPT
5186 : __ptr_(__r.__ptr_),
5187 __cntrl_(__r.__cntrl_)
5188{
5189 __r.__ptr_ = 0;
5190 __r.__cntrl_ = 0;
5191}
5192
5193#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5194
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005195template<class _Tp>
5196weak_ptr<_Tp>::~weak_ptr()
5197{
5198 if (__cntrl_)
5199 __cntrl_->__release_weak();
5200}
5201
5202template<class _Tp>
5203inline _LIBCPP_INLINE_VISIBILITY
5204weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00005205weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005206{
5207 weak_ptr(__r).swap(*this);
5208 return *this;
5209}
5210
5211template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005212template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005213inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005214typename enable_if
5215<
5216 is_convertible<_Yp*, _Tp*>::value,
5217 weak_ptr<_Tp>&
5218>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005219weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005220{
5221 weak_ptr(__r).swap(*this);
5222 return *this;
5223}
5224
Howard Hinnant57199402012-01-02 17:56:02 +00005225#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5226
5227template<class _Tp>
5228inline _LIBCPP_INLINE_VISIBILITY
5229weak_ptr<_Tp>&
5230weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5231{
5232 weak_ptr(_VSTD::move(__r)).swap(*this);
5233 return *this;
5234}
5235
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005236template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005237template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005238inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005239typename enable_if
5240<
5241 is_convertible<_Yp*, _Tp*>::value,
5242 weak_ptr<_Tp>&
5243>::type
5244weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5245{
5246 weak_ptr(_VSTD::move(__r)).swap(*this);
5247 return *this;
5248}
5249
5250#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5251
5252template<class _Tp>
5253template<class _Yp>
5254inline _LIBCPP_INLINE_VISIBILITY
5255typename enable_if
5256<
5257 is_convertible<_Yp*, _Tp*>::value,
5258 weak_ptr<_Tp>&
5259>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005260weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005261{
5262 weak_ptr(__r).swap(*this);
5263 return *this;
5264}
5265
5266template<class _Tp>
5267inline _LIBCPP_INLINE_VISIBILITY
5268void
Howard Hinnant1694d232011-05-28 14:41:13 +00005269weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005270{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005271 _VSTD::swap(__ptr_, __r.__ptr_);
5272 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005273}
5274
5275template<class _Tp>
5276inline _LIBCPP_INLINE_VISIBILITY
5277void
Howard Hinnant1694d232011-05-28 14:41:13 +00005278swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005279{
5280 __x.swap(__y);
5281}
5282
5283template<class _Tp>
5284inline _LIBCPP_INLINE_VISIBILITY
5285void
Howard Hinnant1694d232011-05-28 14:41:13 +00005286weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005287{
5288 weak_ptr().swap(*this);
5289}
5290
5291template<class _Tp>
5292template<class _Yp>
5293shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5294 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
5295 : __ptr_(__r.__ptr_),
5296 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5297{
5298 if (__cntrl_ == 0)
5299#ifndef _LIBCPP_NO_EXCEPTIONS
5300 throw bad_weak_ptr();
5301#else
5302 assert(!"bad_weak_ptr");
5303#endif
5304}
5305
5306template<class _Tp>
5307shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00005308weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005309{
5310 shared_ptr<_Tp> __r;
5311 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5312 if (__r.__cntrl_)
5313 __r.__ptr_ = __ptr_;
5314 return __r;
5315}
5316
Howard Hinnant324bb032010-08-22 00:02:43 +00005317template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005318
5319template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005320struct _LIBCPP_TYPE_VIS_ONLY owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005321 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00005322{
5323 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005324 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005325 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5326 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005328 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5329 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005330 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005331 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5332 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00005333};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005334
5335template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005336struct _LIBCPP_TYPE_VIS_ONLY owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005337 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5338{
Howard Hinnant324bb032010-08-22 00:02:43 +00005339 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005341 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5342 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005343 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005344 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5345 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005347 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5348 {return __x.owner_before(__y);}
5349};
5350
5351template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005352class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005353{
5354 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00005355protected:
Howard Hinnant46e94932012-07-07 20:56:04 +00005356 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005357 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005358 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005359 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005361 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5362 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00005363 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005364 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00005365public:
Howard Hinnant82894812010-09-22 16:48:34 +00005366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005367 shared_ptr<_Tp> shared_from_this()
5368 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00005369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005370 shared_ptr<_Tp const> shared_from_this() const
5371 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005372
5373 template <class _Up> friend class shared_ptr;
5374};
5375
Howard Hinnant21aefc32010-06-03 16:42:57 +00005376template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005377struct _LIBCPP_TYPE_VIS_ONLY hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00005378{
5379 typedef shared_ptr<_Tp> argument_type;
5380 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005382 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00005383 {
5384 return hash<_Tp*>()(__ptr.get());
5385 }
5386};
5387
Howard Hinnant99968442011-11-29 18:15:50 +00005388template<class _CharT, class _Traits, class _Yp>
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005389inline _LIBCPP_INLINE_VISIBILITY
5390basic_ostream<_CharT, _Traits>&
Howard Hinnant99968442011-11-29 18:15:50 +00005391operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005392
Eric Fiselier00f4a492015-08-19 17:21:46 +00005393// TODO(EricWF): Enable this for both Clang and GCC. Currently it is only
5394// enabled with clang.
5395#if defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005396
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005397class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005398{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005399 void* __lx;
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005400public:
5401 void lock() _NOEXCEPT;
5402 void unlock() _NOEXCEPT;
5403
5404private:
5405 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5406 __sp_mut(const __sp_mut&);
5407 __sp_mut& operator=(const __sp_mut&);
5408
Howard Hinnant83eade62013-03-06 23:30:19 +00005409 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005410};
5411
Howard Hinnant83eade62013-03-06 23:30:19 +00005412_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005413
5414template <class _Tp>
5415inline _LIBCPP_INLINE_VISIBILITY
5416bool
5417atomic_is_lock_free(const shared_ptr<_Tp>*)
5418{
5419 return false;
5420}
5421
5422template <class _Tp>
5423shared_ptr<_Tp>
5424atomic_load(const shared_ptr<_Tp>* __p)
5425{
5426 __sp_mut& __m = __get_sp_mut(__p);
5427 __m.lock();
5428 shared_ptr<_Tp> __q = *__p;
5429 __m.unlock();
5430 return __q;
5431}
5432
5433template <class _Tp>
5434inline _LIBCPP_INLINE_VISIBILITY
5435shared_ptr<_Tp>
5436atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5437{
5438 return atomic_load(__p);
5439}
5440
5441template <class _Tp>
5442void
5443atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5444{
5445 __sp_mut& __m = __get_sp_mut(__p);
5446 __m.lock();
5447 __p->swap(__r);
5448 __m.unlock();
5449}
5450
5451template <class _Tp>
5452inline _LIBCPP_INLINE_VISIBILITY
5453void
5454atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5455{
5456 atomic_store(__p, __r);
5457}
5458
5459template <class _Tp>
5460shared_ptr<_Tp>
5461atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5462{
5463 __sp_mut& __m = __get_sp_mut(__p);
5464 __m.lock();
5465 __p->swap(__r);
5466 __m.unlock();
5467 return __r;
5468}
5469
5470template <class _Tp>
5471inline _LIBCPP_INLINE_VISIBILITY
5472shared_ptr<_Tp>
5473atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5474{
5475 return atomic_exchange(__p, __r);
5476}
5477
5478template <class _Tp>
5479bool
5480atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5481{
5482 __sp_mut& __m = __get_sp_mut(__p);
5483 __m.lock();
5484 if (__p->__owner_equivalent(*__v))
5485 {
5486 *__p = __w;
5487 __m.unlock();
5488 return true;
5489 }
5490 *__v = *__p;
5491 __m.unlock();
5492 return false;
5493}
5494
5495template <class _Tp>
5496inline _LIBCPP_INLINE_VISIBILITY
5497bool
5498atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5499{
5500 return atomic_compare_exchange_strong(__p, __v, __w);
5501}
5502
5503template <class _Tp>
5504inline _LIBCPP_INLINE_VISIBILITY
5505bool
5506atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5507 shared_ptr<_Tp> __w, memory_order, memory_order)
5508{
5509 return atomic_compare_exchange_strong(__p, __v, __w);
5510}
5511
5512template <class _Tp>
5513inline _LIBCPP_INLINE_VISIBILITY
5514bool
5515atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5516 shared_ptr<_Tp> __w, memory_order, memory_order)
5517{
5518 return atomic_compare_exchange_weak(__p, __v, __w);
5519}
5520
Eric Fiselier00f4a492015-08-19 17:21:46 +00005521#endif // defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005522
Howard Hinnant324bb032010-08-22 00:02:43 +00005523//enum class
Howard Hinnant83eade62013-03-06 23:30:19 +00005524struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005525{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005526 enum __lx
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005527 {
5528 relaxed,
5529 preferred,
5530 strict
5531 };
5532
Howard Hinnant9c0df142012-10-30 19:06:59 +00005533 __lx __v_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005534
Howard Hinnant82894812010-09-22 16:48:34 +00005535 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9c0df142012-10-30 19:06:59 +00005536 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00005537 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005538 operator int() const {return __v_;}
5539};
5540
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005541_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5542_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5543_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
5544_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5545_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005546
5547template <class _Tp>
5548inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005549_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005550undeclare_reachable(_Tp* __p)
5551{
5552 return static_cast<_Tp*>(__undeclare_reachable(__p));
5553}
5554
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005555_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005556
Marshall Clow7d914d12015-07-13 20:04:56 +00005557// --- Helper for container swap --
5558template <typename _Alloc>
5559_LIBCPP_INLINE_VISIBILITY
5560void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5561#if _LIBCPP_STD_VER >= 14
5562 _NOEXCEPT
5563#else
5564 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5565#endif
5566{
5567 __swap_allocator(__a1, __a2,
5568 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5569}
5570
5571template <typename _Alloc>
5572_LIBCPP_INLINE_VISIBILITY
5573void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5574#if _LIBCPP_STD_VER >= 14
5575 _NOEXCEPT
5576#else
5577 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5578#endif
5579{
5580 using _VSTD::swap;
5581 swap(__a1, __a2);
5582}
5583
5584template <typename _Alloc>
5585_LIBCPP_INLINE_VISIBILITY
5586void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5587
Marshall Clowd434e2a2015-08-18 19:51:37 +00005588template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Marshall Clowaf961ed2015-08-18 18:57:00 +00005589struct __noexcept_move_assign_container : public integral_constant<bool,
5590 _Traits::propagate_on_container_move_assignment::value
5591#if _LIBCPP_STD_VER > 14
5592 || _Traits::is_always_equal::value
5593#else
5594 && is_nothrow_move_assignable<_Alloc>::value
5595#endif
5596 > {};
Marshall Clow7d914d12015-07-13 20:04:56 +00005597
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005598_LIBCPP_END_NAMESPACE_STD
5599
5600#endif // _LIBCPP_MEMORY