blob: a004c89d52a43be6f54b1a15a6aa43b6518f8a85 [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;
78
79 template <class T> using rebind_alloc = Alloc::rebind<U>::other | Alloc<T, Args...>;
80 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
81
82 static pointer allocate(allocator_type& a, size_type n);
83 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint);
84
Howard Hinnant1694d232011-05-28 14:41:13 +000085 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000086
87 template <class T, class... Args>
88 static void construct(allocator_type& a, T* p, Args&&... args);
89
90 template <class T>
91 static void destroy(allocator_type& a, T* p);
92
Marshall Clow08b4f3f2013-08-27 20:22:15 +000093 static size_type max_size(const allocator_type& a); // noexcept in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000094
95 static allocator_type
96 select_on_container_copy_construction(const allocator_type& a);
97};
98
99template <>
100class allocator<void>
101{
102public:
103 typedef void* pointer;
104 typedef const void* const_pointer;
105 typedef void value_type;
106
107 template <class _Up> struct rebind {typedef allocator<_Up> other;};
108};
109
110template <class T>
111class allocator
112{
113public:
114 typedef size_t size_type;
115 typedef ptrdiff_t difference_type;
116 typedef T* pointer;
117 typedef const T* const_pointer;
118 typedef typename add_lvalue_reference<T>::type reference;
119 typedef typename add_lvalue_reference<const T>::type const_reference;
120 typedef T value_type;
121
122 template <class U> struct rebind {typedef allocator<U> other;};
123
Howard Hinnant1694d232011-05-28 14:41:13 +0000124 allocator() noexcept;
125 allocator(const allocator&) noexcept;
126 template <class U> allocator(const allocator<U>&) noexcept;
127 ~allocator();
128 pointer address(reference x) const noexcept;
129 const_pointer address(const_reference x) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000130 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
Howard Hinnant1694d232011-05-28 14:41:13 +0000131 void deallocate(pointer p, size_type n) noexcept;
132 size_type max_size() const noexcept;
133 template<class U, class... Args>
134 void construct(U* p, Args&&... args);
135 template <class U>
136 void destroy(U* p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000137};
138
139template <class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000140bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141
142template <class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000143bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000144
145template <class OutputIterator, class T>
146class raw_storage_iterator
147 : public iterator<output_iterator_tag,
148 T, // purposefully not C++03
149 ptrdiff_t, // purposefully not C++03
150 T*, // purposefully not C++03
151 raw_storage_iterator&> // purposefully not C++03
152{
153public:
154 explicit raw_storage_iterator(OutputIterator x);
155 raw_storage_iterator& operator*();
156 raw_storage_iterator& operator=(const T& element);
157 raw_storage_iterator& operator++();
158 raw_storage_iterator operator++(int);
159};
160
Howard Hinnant1694d232011-05-28 14:41:13 +0000161template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
162template <class T> void return_temporary_buffer(T* p) noexcept;
163
164template <class T> T* addressof(T& r) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000165
166template <class InputIterator, class ForwardIterator>
167ForwardIterator
168uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
169
Howard Hinnant1694d232011-05-28 14:41:13 +0000170template <class InputIterator, class Size, class ForwardIterator>
171ForwardIterator
172uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
173
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000174template <class ForwardIterator, class T>
175void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
176
177template <class ForwardIterator, class Size, class T>
Howard Hinnant2f6a6272010-11-18 16:13:03 +0000178ForwardIterator
179uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000180
181template <class Y> struct auto_ptr_ref {};
182
183template<class X>
184class auto_ptr
185{
186public:
187 typedef X element_type;
188
189 explicit auto_ptr(X* p =0) throw();
190 auto_ptr(auto_ptr&) throw();
191 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
192 auto_ptr& operator=(auto_ptr&) throw();
193 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
194 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
195 ~auto_ptr() throw();
196
197 typename add_lvalue_reference<X>::type operator*() const throw();
198 X* operator->() const throw();
199 X* get() const throw();
200 X* release() throw();
201 void reset(X* p =0) throw();
202
203 auto_ptr(auto_ptr_ref<X>) throw();
204 template<class Y> operator auto_ptr_ref<Y>() throw();
205 template<class Y> operator auto_ptr<Y>() throw();
206};
207
Howard Hinnante92c3d72010-08-19 18:39:17 +0000208template <class T>
209struct default_delete
210{
Howard Hinnant1694d232011-05-28 14:41:13 +0000211 constexpr default_delete() noexcept = default;
212 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000213
Howard Hinnant1694d232011-05-28 14:41:13 +0000214 void operator()(T*) const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000215};
216
217template <class T>
218struct default_delete<T[]>
219{
Howard Hinnant1694d232011-05-28 14:41:13 +0000220 constexpr default_delete() noexcept = default;
221 void operator()(T*) const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000222 template <class U> void operator()(U*) const = delete;
223};
224
Howard Hinnante92c3d72010-08-19 18:39:17 +0000225template <class T, class D = default_delete<T>>
226class unique_ptr
227{
228public:
229 typedef see below pointer;
230 typedef T element_type;
231 typedef D deleter_type;
232
233 // constructors
Howard Hinnant1694d232011-05-28 14:41:13 +0000234 constexpr unique_ptr() noexcept;
235 explicit unique_ptr(pointer p) noexcept;
236 unique_ptr(pointer p, see below d1) noexcept;
237 unique_ptr(pointer p, see below d2) noexcept;
238 unique_ptr(unique_ptr&& u) noexcept;
239 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnante92c3d72010-08-19 18:39:17 +0000240 template <class U, class E>
Howard Hinnant1694d232011-05-28 14:41:13 +0000241 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000242 template <class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000243 unique_ptr(auto_ptr<U>&& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000244
245 // destructor
246 ~unique_ptr();
247
248 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000249 unique_ptr& operator=(unique_ptr&& u) noexcept;
250 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
251 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000252
253 // observers
254 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant1694d232011-05-28 14:41:13 +0000255 pointer operator->() const noexcept;
256 pointer get() const noexcept;
257 deleter_type& get_deleter() noexcept;
258 const deleter_type& get_deleter() const noexcept;
259 explicit operator bool() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000260
261 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000262 pointer release() noexcept;
263 void reset(pointer p = pointer()) noexcept;
264 void swap(unique_ptr& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000265};
266
267template <class T, class D>
268class unique_ptr<T[], D>
269{
270public:
271 typedef implementation-defined pointer;
272 typedef T element_type;
273 typedef D deleter_type;
274
275 // constructors
Howard Hinnant1694d232011-05-28 14:41:13 +0000276 constexpr unique_ptr() noexcept;
277 explicit unique_ptr(pointer p) noexcept;
278 unique_ptr(pointer p, see below d) noexcept;
279 unique_ptr(pointer p, see below d) noexcept;
280 unique_ptr(unique_ptr&& u) noexcept;
281 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnante92c3d72010-08-19 18:39:17 +0000282
283 // destructor
Howard Hinnant324bb032010-08-22 00:02:43 +0000284 ~unique_ptr();
Howard Hinnante92c3d72010-08-19 18:39:17 +0000285
286 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000287 unique_ptr& operator=(unique_ptr&& u) noexcept;
288 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000289
290 // observers
291 T& operator[](size_t i) const;
Howard Hinnant1694d232011-05-28 14:41:13 +0000292 pointer get() const noexcept;
293 deleter_type& get_deleter() noexcept;
294 const deleter_type& get_deleter() const noexcept;
295 explicit operator bool() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000296
297 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000298 pointer release() noexcept;
299 void reset(pointer p = pointer()) noexcept;
300 void reset(nullptr_t) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000301 template <class U> void reset(U) = delete;
Howard Hinnant1694d232011-05-28 14:41:13 +0000302 void swap(unique_ptr& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000303};
304
305template <class T, class D>
Howard Hinnant1694d232011-05-28 14:41:13 +0000306 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000307
308template <class T1, class D1, class T2, class D2>
309 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
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);
320
Howard Hinnant1694d232011-05-28 14:41:13 +0000321template <class T, class D>
322 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
323template <class T, class D>
324 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
325template <class T, class D>
326 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
327template <class T, class D>
328 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
329
330template <class T, class D>
331 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
332template <class T, class D>
333 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
334template <class T, class D>
335 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
336template <class T, class D>
337 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
338template <class T, class D>
339 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
340template <class T, class D>
341 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
342template <class T, class D>
343 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
344template <class T, class D>
345 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
346
Howard Hinnante92c3d72010-08-19 18:39:17 +0000347class bad_weak_ptr
348 : public std::exception
349{
Howard Hinnant1694d232011-05-28 14:41:13 +0000350 bad_weak_ptr() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000351};
352
Marshall Clowfd7481e2013-07-01 18:16:03 +0000353template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
354template<class T> unique_ptr<T> make_unique(size_t n); // C++14
355template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
356
Howard Hinnante92c3d72010-08-19 18:39:17 +0000357template<class T>
358class shared_ptr
359{
360public:
361 typedef T element_type;
362
363 // constructors:
Howard Hinnant1694d232011-05-28 14:41:13 +0000364 constexpr shared_ptr() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000365 template<class Y> explicit shared_ptr(Y* p);
366 template<class Y, class D> shared_ptr(Y* p, D d);
367 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
368 template <class D> shared_ptr(nullptr_t p, D d);
369 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant1694d232011-05-28 14:41:13 +0000370 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
371 shared_ptr(const shared_ptr& r) noexcept;
372 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
373 shared_ptr(shared_ptr&& r) noexcept;
374 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000375 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
376 template<class Y> shared_ptr(auto_ptr<Y>&& r);
377 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
378 shared_ptr(nullptr_t) : shared_ptr() { }
379
380 // destructor:
381 ~shared_ptr();
382
383 // assignment:
Howard Hinnant1694d232011-05-28 14:41:13 +0000384 shared_ptr& operator=(const shared_ptr& r) noexcept;
385 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
386 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000387 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
388 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r);
389 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
390
391 // modifiers:
Howard Hinnant1694d232011-05-28 14:41:13 +0000392 void swap(shared_ptr& r) noexcept;
393 void reset() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000394 template<class Y> void reset(Y* p);
395 template<class Y, class D> void reset(Y* p, D d);
396 template<class Y, class D, class A> void reset(Y* p, D d, A a);
397
Howard Hinnant1694d232011-05-28 14:41:13 +0000398 // observers:
399 T* get() const noexcept;
400 T& operator*() const noexcept;
401 T* operator->() const noexcept;
402 long use_count() const noexcept;
403 bool unique() const noexcept;
404 explicit operator bool() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000405 template<class U> bool owner_before(shared_ptr<U> const& b) const;
406 template<class U> bool owner_before(weak_ptr<U> const& b) const;
407};
408
409// shared_ptr comparisons:
410template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000411 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000412template<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;
422
423template <class T>
424 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
425template <class T>
426 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
427template <class T>
428 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
429template <class T>
430 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
431template <class T>
432 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
433template <class T>
434bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
435template <class T>
436 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
437template <class T>
438 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
439template <class T>
440 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
441template <class T>
442 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
443template <class T>
444 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
445template <class T>
446 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000447
448// shared_ptr specialized algorithms:
Howard Hinnant1694d232011-05-28 14:41:13 +0000449template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000450
451// shared_ptr casts:
452template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000453 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000454template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000455 shared_ptr<T> dynamic_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> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000458
459// shared_ptr I/O:
460template<class E, class T, class Y>
461 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
462
463// shared_ptr get_deleter:
Howard Hinnant1694d232011-05-28 14:41:13 +0000464template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000465
466template<class T, class... Args>
467 shared_ptr<T> make_shared(Args&&... args);
468template<class T, class A, class... Args>
469 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
470
471template<class T>
472class weak_ptr
473{
474public:
475 typedef T element_type;
476
477 // constructors
Howard Hinnant1694d232011-05-28 14:41:13 +0000478 constexpr weak_ptr() noexcept;
479 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
480 weak_ptr(weak_ptr const& r) noexcept;
481 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clow23ef1512014-03-05 03:12:04 +0000482 weak_ptr(weak_ptr&& r) noexcept; // C++14
483 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnante92c3d72010-08-19 18:39:17 +0000484
485 // destructor
486 ~weak_ptr();
487
488 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000489 weak_ptr& operator=(weak_ptr const& r) noexcept;
490 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
491 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clow23ef1512014-03-05 03:12:04 +0000492 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
493 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnante92c3d72010-08-19 18:39:17 +0000494
495 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000496 void swap(weak_ptr& r) noexcept;
497 void reset() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000498
499 // observers
Howard Hinnant1694d232011-05-28 14:41:13 +0000500 long use_count() const noexcept;
501 bool expired() const noexcept;
502 shared_ptr<T> lock() const noexcept;
Marshall Clow1b5f3ad2013-09-03 14:37:50 +0000503 template<class U> bool owner_before(shared_ptr<U> const& b) const;
504 template<class U> bool owner_before(weak_ptr<U> const& b) const;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000505};
506
507// weak_ptr specialized algorithms:
Howard Hinnant1694d232011-05-28 14:41:13 +0000508template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000509
510// class owner_less:
511template<class T> struct owner_less;
512
513template<class T>
514struct owner_less<shared_ptr<T>>
515 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
516{
517 typedef bool result_type;
518 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const;
519 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
520 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
521};
522
523template<class T>
524struct owner_less<weak_ptr<T>>
525 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
526{
527 typedef bool result_type;
528 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const;
529 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
530 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
531};
532
533template<class T>
534class enable_shared_from_this
535{
536protected:
Howard Hinnant1694d232011-05-28 14:41:13 +0000537 constexpr enable_shared_from_this() noexcept;
538 enable_shared_from_this(enable_shared_from_this const&) noexcept;
539 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000540 ~enable_shared_from_this();
541public:
542 shared_ptr<T> shared_from_this();
543 shared_ptr<T const> shared_from_this() const;
544};
545
546template<class T>
547 bool atomic_is_lock_free(const shared_ptr<T>* p);
548template<class T>
549 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
550template<class T>
551 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
552template<class T>
553 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
554template<class T>
555 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
556template<class T>
557 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
558template<class T>
559 shared_ptr<T>
560 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
561template<class T>
562 bool
563 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
564template<class T>
565 bool
566 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
567template<class T>
568 bool
569 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
570 shared_ptr<T> w, memory_order success,
571 memory_order failure);
572template<class T>
573 bool
574 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
575 shared_ptr<T> w, memory_order success,
576 memory_order failure);
577// Hash support
578template <class T> struct hash;
579template <class T, class D> struct hash<unique_ptr<T, D> >;
580template <class T> struct hash<shared_ptr<T> >;
581
582// Pointer safety
583enum class pointer_safety { relaxed, preferred, strict };
584void declare_reachable(void *p);
585template <class T> T *undeclare_reachable(T *p);
586void declare_no_pointers(char *p, size_t n);
587void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant1694d232011-05-28 14:41:13 +0000588pointer_safety get_pointer_safety() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000589
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000590void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
591
592} // std
593
594*/
595
596#include <__config>
597#include <type_traits>
598#include <typeinfo>
599#include <cstddef>
600#include <cstdint>
601#include <new>
602#include <utility>
603#include <limits>
604#include <iterator>
605#include <__functional_base>
Howard Hinnant464aa5c2011-07-18 15:51:59 +0000606#include <iosfwd>
Howard Hinnant6cc99fa2011-12-19 17:58:44 +0000607#include <tuple>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000608#include <cstring>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000609#if defined(_LIBCPP_NO_EXCEPTIONS)
610 #include <cassert>
611#endif
612
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000613#if __has_feature(cxx_atomic) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant5fec82d2012-07-30 01:40:57 +0000614# include <atomic>
615#endif
616
Howard Hinnant66c6f972011-11-29 16:45:27 +0000617#include <__undef_min_max>
Saleem Abdulrasoolf1b30c42015-02-13 22:15:32 +0000618#include <__undef___deallocate>
Howard Hinnant66c6f972011-11-29 16:45:27 +0000619
Howard Hinnant08e17472011-10-17 20:05:10 +0000620#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000621#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000622#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000623
Marshall Clowbf0460e2015-05-31 03:13:31 +0000624extern "C" int printf(const char * __restrict, ...);
625
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000626_LIBCPP_BEGIN_NAMESPACE_STD
627
Howard Hinnanta4e87ab2013-08-08 18:38:55 +0000628// addressof moved to <__functional_base>
Douglas Gregor35d2fcf2011-06-22 22:17:44 +0000629
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000630template <class _Tp> class allocator;
631
632template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000633class _LIBCPP_TYPE_VIS_ONLY allocator<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000634{
635public:
636 typedef void* pointer;
637 typedef const void* const_pointer;
638 typedef void value_type;
639
640 template <class _Up> struct rebind {typedef allocator<_Up> other;};
641};
642
Howard Hinnanta1877872012-01-19 23:15:22 +0000643template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000644class _LIBCPP_TYPE_VIS_ONLY allocator<const void>
Howard Hinnanta1877872012-01-19 23:15:22 +0000645{
646public:
647 typedef const void* pointer;
648 typedef const void* const_pointer;
649 typedef const void value_type;
650
651 template <class _Up> struct rebind {typedef allocator<_Up> other;};
652};
653
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000654// pointer_traits
655
656template <class _Tp>
657struct __has_element_type
658{
659private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000660 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000661 template <class _Up> static __two __test(...);
662 template <class _Up> static char __test(typename _Up::element_type* = 0);
663public:
664 static const bool value = sizeof(__test<_Tp>(0)) == 1;
665};
666
667template <class _Ptr, bool = __has_element_type<_Ptr>::value>
668struct __pointer_traits_element_type;
669
670template <class _Ptr>
671struct __pointer_traits_element_type<_Ptr, true>
672{
673 typedef typename _Ptr::element_type type;
674};
675
676#ifndef _LIBCPP_HAS_NO_VARIADICS
677
678template <template <class, class...> class _Sp, class _Tp, class ..._Args>
679struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
680{
681 typedef typename _Sp<_Tp, _Args...>::element_type type;
682};
683
684template <template <class, class...> class _Sp, class _Tp, class ..._Args>
685struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
686{
687 typedef _Tp type;
688};
689
Howard Hinnant324bb032010-08-22 00:02:43 +0000690#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000691
692template <template <class> class _Sp, class _Tp>
693struct __pointer_traits_element_type<_Sp<_Tp>, true>
694{
695 typedef typename _Sp<_Tp>::element_type type;
696};
697
698template <template <class> class _Sp, class _Tp>
699struct __pointer_traits_element_type<_Sp<_Tp>, false>
700{
701 typedef _Tp type;
702};
703
704template <template <class, class> class _Sp, class _Tp, class _A0>
705struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
706{
707 typedef typename _Sp<_Tp, _A0>::element_type type;
708};
709
710template <template <class, class> class _Sp, class _Tp, class _A0>
711struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
712{
713 typedef _Tp type;
714};
715
716template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
717struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
718{
719 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
720};
721
722template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
723struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
724{
725 typedef _Tp type;
726};
727
728template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
729 class _A1, class _A2>
730struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
731{
732 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
733};
734
735template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
736 class _A1, class _A2>
737struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
738{
739 typedef _Tp type;
740};
741
Howard Hinnant324bb032010-08-22 00:02:43 +0000742#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000743
744template <class _Tp>
745struct __has_difference_type
746{
747private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000748 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000749 template <class _Up> static __two __test(...);
750 template <class _Up> static char __test(typename _Up::difference_type* = 0);
751public:
752 static const bool value = sizeof(__test<_Tp>(0)) == 1;
753};
754
755template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
756struct __pointer_traits_difference_type
757{
758 typedef ptrdiff_t type;
759};
760
761template <class _Ptr>
762struct __pointer_traits_difference_type<_Ptr, true>
763{
764 typedef typename _Ptr::difference_type type;
765};
766
767template <class _Tp, class _Up>
768struct __has_rebind
769{
770private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000771 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000772 template <class _Xp> static __two __test(...);
773 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
774public:
775 static const bool value = sizeof(__test<_Tp>(0)) == 1;
776};
777
778template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
779struct __pointer_traits_rebind
780{
781#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
782 typedef typename _Tp::template rebind<_Up> type;
783#else
784 typedef typename _Tp::template rebind<_Up>::other type;
785#endif
786};
787
788#ifndef _LIBCPP_HAS_NO_VARIADICS
789
790template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
791struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
792{
793#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
794 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
795#else
796 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
797#endif
798};
799
800template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
801struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
802{
803 typedef _Sp<_Up, _Args...> type;
804};
805
Howard Hinnant324bb032010-08-22 00:02:43 +0000806#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000807
808template <template <class> class _Sp, class _Tp, class _Up>
809struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
810{
811#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
812 typedef typename _Sp<_Tp>::template rebind<_Up> type;
813#else
814 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
815#endif
816};
817
818template <template <class> class _Sp, class _Tp, class _Up>
819struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
820{
821 typedef _Sp<_Up> type;
822};
823
824template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
825struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
826{
827#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
828 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
829#else
830 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
831#endif
832};
833
834template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
835struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
836{
837 typedef _Sp<_Up, _A0> type;
838};
839
840template <template <class, class, class> class _Sp, class _Tp, class _A0,
841 class _A1, class _Up>
842struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
843{
844#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
845 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
846#else
847 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
848#endif
849};
850
851template <template <class, class, class> class _Sp, class _Tp, class _A0,
852 class _A1, class _Up>
853struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
854{
855 typedef _Sp<_Up, _A0, _A1> type;
856};
857
858template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
859 class _A1, class _A2, class _Up>
860struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
861{
862#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
863 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
864#else
865 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
866#endif
867};
868
869template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
870 class _A1, class _A2, class _Up>
871struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
872{
873 typedef _Sp<_Up, _A0, _A1, _A2> type;
874};
875
Howard Hinnant324bb032010-08-22 00:02:43 +0000876#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000877
878template <class _Ptr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000879struct _LIBCPP_TYPE_VIS_ONLY pointer_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000880{
881 typedef _Ptr pointer;
882 typedef typename __pointer_traits_element_type<pointer>::type element_type;
883 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
884
885#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant6b41c602011-05-11 20:21:19 +0000886 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000887#else
888 template <class _Up> struct rebind
889 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant324bb032010-08-22 00:02:43 +0000890#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000891
892private:
893 struct __nat {};
894public:
Howard Hinnant82894812010-09-22 16:48:34 +0000895 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000896 static pointer pointer_to(typename conditional<is_void<element_type>::value,
897 __nat, element_type>::type& __r)
898 {return pointer::pointer_to(__r);}
899};
900
901template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000902struct _LIBCPP_TYPE_VIS_ONLY pointer_traits<_Tp*>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000903{
904 typedef _Tp* pointer;
905 typedef _Tp element_type;
906 typedef ptrdiff_t difference_type;
907
908#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
909 template <class _Up> using rebind = _Up*;
910#else
911 template <class _Up> struct rebind {typedef _Up* other;};
912#endif
913
914private:
915 struct __nat {};
916public:
Howard Hinnant82894812010-09-22 16:48:34 +0000917 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000918 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant1694d232011-05-28 14:41:13 +0000919 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000920 {return _VSTD::addressof(__r);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000921};
922
923// allocator_traits
924
925namespace __has_pointer_type_imp
926{
Howard Hinnant81277582013-09-21 01:45:05 +0000927 template <class _Up> static __two __test(...);
928 template <class _Up> static char __test(typename _Up::pointer* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000929}
930
931template <class _Tp>
932struct __has_pointer_type
Howard Hinnant81277582013-09-21 01:45:05 +0000933 : public integral_constant<bool, sizeof(__has_pointer_type_imp::__test<_Tp>(0)) == 1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000934{
935};
936
937namespace __pointer_type_imp
938{
939
940template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
941struct __pointer_type
942{
943 typedef typename _Dp::pointer type;
944};
945
946template <class _Tp, class _Dp>
947struct __pointer_type<_Tp, _Dp, false>
948{
949 typedef _Tp* type;
950};
951
Howard Hinnant47761072010-11-18 01:40:00 +0000952} // __pointer_type_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000953
954template <class _Tp, class _Dp>
955struct __pointer_type
956{
957 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
958};
959
960template <class _Tp>
961struct __has_const_pointer
962{
963private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000964 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000965 template <class _Up> static __two __test(...);
966 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
967public:
968 static const bool value = sizeof(__test<_Tp>(0)) == 1;
969};
970
971template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
972struct __const_pointer
973{
974 typedef typename _Alloc::const_pointer type;
975};
976
977template <class _Tp, class _Ptr, class _Alloc>
978struct __const_pointer<_Tp, _Ptr, _Alloc, false>
979{
980#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
981 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
982#else
983 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
984#endif
985};
986
987template <class _Tp>
988struct __has_void_pointer
989{
990private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000991 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000992 template <class _Up> static __two __test(...);
993 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
994public:
995 static const bool value = sizeof(__test<_Tp>(0)) == 1;
996};
997
998template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
999struct __void_pointer
1000{
1001 typedef typename _Alloc::void_pointer type;
1002};
1003
1004template <class _Ptr, class _Alloc>
1005struct __void_pointer<_Ptr, _Alloc, false>
1006{
1007#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1008 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1009#else
1010 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1011#endif
1012};
1013
1014template <class _Tp>
1015struct __has_const_void_pointer
1016{
1017private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001018 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001019 template <class _Up> static __two __test(...);
1020 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1021public:
1022 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1023};
1024
1025template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1026struct __const_void_pointer
1027{
1028 typedef typename _Alloc::const_void_pointer type;
1029};
1030
1031template <class _Ptr, class _Alloc>
1032struct __const_void_pointer<_Ptr, _Alloc, false>
1033{
1034#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1035 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1036#else
1037 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1038#endif
1039};
1040
Howard Hinnant99968442011-11-29 18:15:50 +00001041template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001042inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001043_Tp*
1044__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001045{
1046 return __p;
1047}
1048
1049template <class _Pointer>
1050inline _LIBCPP_INLINE_VISIBILITY
1051typename pointer_traits<_Pointer>::element_type*
Howard Hinnant1694d232011-05-28 14:41:13 +00001052__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001053{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001054 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001055}
1056
1057template <class _Tp>
1058struct __has_size_type
1059{
1060private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001061 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001062 template <class _Up> static __two __test(...);
1063 template <class _Up> static char __test(typename _Up::size_type* = 0);
1064public:
1065 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1066};
1067
Howard Hinnant47761072010-11-18 01:40:00 +00001068template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001069struct __size_type
1070{
Howard Hinnant47761072010-11-18 01:40:00 +00001071 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001072};
1073
Howard Hinnant47761072010-11-18 01:40:00 +00001074template <class _Alloc, class _DiffType>
1075struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001076{
1077 typedef typename _Alloc::size_type type;
1078};
1079
1080template <class _Tp>
1081struct __has_propagate_on_container_copy_assignment
1082{
1083private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001084 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001085 template <class _Up> static __two __test(...);
1086 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1087public:
1088 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1089};
1090
1091template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1092struct __propagate_on_container_copy_assignment
1093{
1094 typedef false_type type;
1095};
1096
1097template <class _Alloc>
1098struct __propagate_on_container_copy_assignment<_Alloc, true>
1099{
1100 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1101};
1102
1103template <class _Tp>
1104struct __has_propagate_on_container_move_assignment
1105{
1106private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001107 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001108 template <class _Up> static __two __test(...);
1109 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1110public:
1111 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1112};
1113
1114template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1115struct __propagate_on_container_move_assignment
1116{
1117 typedef false_type type;
1118};
1119
1120template <class _Alloc>
1121struct __propagate_on_container_move_assignment<_Alloc, true>
1122{
1123 typedef typename _Alloc::propagate_on_container_move_assignment type;
1124};
1125
1126template <class _Tp>
1127struct __has_propagate_on_container_swap
1128{
1129private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001130 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001131 template <class _Up> static __two __test(...);
1132 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1133public:
1134 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1135};
1136
1137template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1138struct __propagate_on_container_swap
1139{
1140 typedef false_type type;
1141};
1142
1143template <class _Alloc>
1144struct __propagate_on_container_swap<_Alloc, true>
1145{
1146 typedef typename _Alloc::propagate_on_container_swap type;
1147};
1148
1149template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1150struct __has_rebind_other
1151{
1152private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001153 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001154 template <class _Xp> static __two __test(...);
1155 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1156public:
1157 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1158};
1159
1160template <class _Tp, class _Up>
1161struct __has_rebind_other<_Tp, _Up, false>
1162{
1163 static const bool value = false;
1164};
1165
1166template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1167struct __allocator_traits_rebind
1168{
1169 typedef typename _Tp::template rebind<_Up>::other type;
1170};
1171
1172#ifndef _LIBCPP_HAS_NO_VARIADICS
1173
1174template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1175struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1176{
1177 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1178};
1179
1180template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1181struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1182{
1183 typedef _Alloc<_Up, _Args...> type;
1184};
1185
Howard Hinnant324bb032010-08-22 00:02:43 +00001186#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001187
1188template <template <class> class _Alloc, class _Tp, class _Up>
1189struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1190{
1191 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1192};
1193
1194template <template <class> class _Alloc, class _Tp, class _Up>
1195struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1196{
1197 typedef _Alloc<_Up> type;
1198};
1199
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001200template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1201struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1202{
1203 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1204};
1205
1206template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1207struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1208{
1209 typedef _Alloc<_Up, _A0> type;
1210};
1211
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001212template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1213 class _A1, class _Up>
1214struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1215{
1216 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1217};
1218
1219template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1220 class _A1, class _Up>
1221struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1222{
1223 typedef _Alloc<_Up, _A0, _A1> type;
1224};
1225
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001226template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1227 class _A1, class _A2, class _Up>
1228struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1229{
1230 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1231};
1232
1233template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1234 class _A1, class _A2, class _Up>
1235struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1236{
1237 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1238};
1239
Howard Hinnant324bb032010-08-22 00:02:43 +00001240#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001241
1242#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1243
1244template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1245auto
1246__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1247 -> decltype(__a.allocate(__sz, __p), true_type());
1248
1249template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1250auto
1251__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1252 -> false_type;
1253
1254template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1255struct __has_allocate_hint
1256 : integral_constant<bool,
1257 is_same<
1258 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1259 declval<_SizeType>(),
1260 declval<_ConstVoidPtr>())),
1261 true_type>::value>
1262{
1263};
1264
Howard Hinnant324bb032010-08-22 00:02:43 +00001265#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001266
1267template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1268struct __has_allocate_hint
1269 : true_type
1270{
1271};
1272
Howard Hinnant324bb032010-08-22 00:02:43 +00001273#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001274
Howard Hinnant23369ee2011-07-29 21:35:53 +00001275#if !defined(_LIBCPP_HAS_NO_ADVANCED_SFINAE) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001276
1277template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnant0949eed2011-06-30 21:18:19 +00001278decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1279 _VSTD::declval<_Args>()...),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001280 true_type())
1281__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1282
1283template <class _Alloc, class _Pointer, class ..._Args>
1284false_type
1285__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1286
1287template <class _Alloc, class _Pointer, class ..._Args>
1288struct __has_construct
1289 : integral_constant<bool,
1290 is_same<
1291 decltype(__has_construct_test(declval<_Alloc>(),
1292 declval<_Pointer>(),
1293 declval<_Args>()...)),
1294 true_type>::value>
1295{
1296};
1297
1298template <class _Alloc, class _Pointer>
1299auto
1300__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1301 -> decltype(__a.destroy(__p), true_type());
1302
1303template <class _Alloc, class _Pointer>
1304auto
1305__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1306 -> false_type;
1307
1308template <class _Alloc, class _Pointer>
1309struct __has_destroy
1310 : integral_constant<bool,
1311 is_same<
1312 decltype(__has_destroy_test(declval<_Alloc>(),
1313 declval<_Pointer>())),
1314 true_type>::value>
1315{
1316};
1317
1318template <class _Alloc>
1319auto
1320__has_max_size_test(_Alloc&& __a)
1321 -> decltype(__a.max_size(), true_type());
1322
1323template <class _Alloc>
1324auto
1325__has_max_size_test(const volatile _Alloc& __a)
1326 -> false_type;
1327
1328template <class _Alloc>
1329struct __has_max_size
1330 : integral_constant<bool,
1331 is_same<
1332 decltype(__has_max_size_test(declval<_Alloc&>())),
1333 true_type>::value>
1334{
1335};
1336
1337template <class _Alloc>
1338auto
1339__has_select_on_container_copy_construction_test(_Alloc&& __a)
1340 -> decltype(__a.select_on_container_copy_construction(), true_type());
1341
1342template <class _Alloc>
1343auto
1344__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1345 -> false_type;
1346
1347template <class _Alloc>
1348struct __has_select_on_container_copy_construction
1349 : integral_constant<bool,
1350 is_same<
1351 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1352 true_type>::value>
1353{
1354};
1355
Howard Hinnant324bb032010-08-22 00:02:43 +00001356#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001357
1358#ifndef _LIBCPP_HAS_NO_VARIADICS
1359
1360template <class _Alloc, class _Pointer, class ..._Args>
1361struct __has_construct
1362 : false_type
1363{
1364};
1365
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001366#else // _LIBCPP_HAS_NO_VARIADICS
1367
1368template <class _Alloc, class _Pointer, class _Args>
1369struct __has_construct
1370 : false_type
1371{
1372};
1373
Howard Hinnant324bb032010-08-22 00:02:43 +00001374#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001375
1376template <class _Alloc, class _Pointer>
1377struct __has_destroy
1378 : false_type
1379{
1380};
1381
1382template <class _Alloc>
1383struct __has_max_size
1384 : true_type
1385{
1386};
1387
1388template <class _Alloc>
1389struct __has_select_on_container_copy_construction
1390 : false_type
1391{
1392};
1393
Howard Hinnant324bb032010-08-22 00:02:43 +00001394#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001395
Howard Hinnant47761072010-11-18 01:40:00 +00001396template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1397struct __alloc_traits_difference_type
1398{
1399 typedef typename pointer_traits<_Ptr>::difference_type type;
1400};
1401
1402template <class _Alloc, class _Ptr>
1403struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1404{
1405 typedef typename _Alloc::difference_type type;
1406};
1407
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001408template <class _Alloc>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001409struct _LIBCPP_TYPE_VIS_ONLY allocator_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001410{
1411 typedef _Alloc allocator_type;
1412 typedef typename allocator_type::value_type value_type;
1413
1414 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1415 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1416 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1417 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1418
Howard Hinnant47761072010-11-18 01:40:00 +00001419 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1420 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001421
1422 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1423 propagate_on_container_copy_assignment;
1424 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1425 propagate_on_container_move_assignment;
1426 typedef typename __propagate_on_container_swap<allocator_type>::type
1427 propagate_on_container_swap;
1428
1429#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1430 template <class _Tp> using rebind_alloc =
Howard Hinnant6b41c602011-05-11 20:21:19 +00001431 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001432 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant324bb032010-08-22 00:02:43 +00001433#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001434 template <class _Tp> struct rebind_alloc
1435 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1436 template <class _Tp> struct rebind_traits
1437 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant324bb032010-08-22 00:02:43 +00001438#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001439
Howard Hinnant82894812010-09-22 16:48:34 +00001440 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001441 static pointer allocate(allocator_type& __a, size_type __n)
1442 {return __a.allocate(__n);}
Howard Hinnant82894812010-09-22 16:48:34 +00001443 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001444 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1445 {return allocate(__a, __n, __hint,
1446 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1447
Howard Hinnant82894812010-09-22 16:48:34 +00001448 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001449 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001450 {__a.deallocate(__p, __n);}
1451
1452#ifndef _LIBCPP_HAS_NO_VARIADICS
1453 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001454 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001455 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3f5579f2014-11-11 19:22:33 +00001456 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001457 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001458#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001459 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001460 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001461 static void construct(allocator_type& __a, _Tp* __p)
1462 {
1463 ::new ((void*)__p) _Tp();
1464 }
1465 template <class _Tp, class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00001466 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001467 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1468 {
1469 ::new ((void*)__p) _Tp(__a0);
1470 }
1471 template <class _Tp, class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00001472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001473 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1474 const _A1& __a1)
1475 {
1476 ::new ((void*)__p) _Tp(__a0, __a1);
1477 }
1478 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00001479 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001480 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1481 const _A1& __a1, const _A2& __a2)
1482 {
1483 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1484 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001485#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001486
1487 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001488 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001489 static void destroy(allocator_type& __a, _Tp* __p)
1490 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1491
Howard Hinnant82894812010-09-22 16:48:34 +00001492 _LIBCPP_INLINE_VISIBILITY
Marshall Clow08b4f3f2013-08-27 20:22:15 +00001493 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001494 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1495
Howard Hinnant82894812010-09-22 16:48:34 +00001496 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001497 static allocator_type
1498 select_on_container_copy_construction(const allocator_type& __a)
1499 {return select_on_container_copy_construction(
1500 __has_select_on_container_copy_construction<const allocator_type>(),
1501 __a);}
1502
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001503 template <class _Ptr>
1504 _LIBCPP_INLINE_VISIBILITY
1505 static
1506 void
1507 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1508 {
1509 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1510 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1511 }
1512
1513 template <class _Tp>
1514 _LIBCPP_INLINE_VISIBILITY
1515 static
1516 typename enable_if
1517 <
1518 (is_same<allocator_type, allocator<_Tp> >::value
1519 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1520 is_trivially_move_constructible<_Tp>::value,
1521 void
1522 >::type
1523 __construct_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1524 {
1525 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clowbf0460e2015-05-31 03:13:31 +00001526 if (_Np > 0)
1527 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001528 __begin2 += _Np;
1529 }
1530
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001531 template <class _Iter, class _Ptr>
1532 _LIBCPP_INLINE_VISIBILITY
1533 static
1534 void
1535 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1536 {
1537 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1538 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1539 }
1540
1541 template <class _Tp>
1542 _LIBCPP_INLINE_VISIBILITY
1543 static
1544 typename enable_if
1545 <
1546 (is_same<allocator_type, allocator<_Tp> >::value
1547 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1548 is_trivially_move_constructible<_Tp>::value,
1549 void
1550 >::type
1551 __construct_range_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1552 {
1553 typedef typename remove_const<_Tp>::type _Vp;
1554 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clowbf0460e2015-05-31 03:13:31 +00001555 if (_Np > 0)
1556 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
Eric Fiselier088ed9f2015-03-31 16:54:19 +00001557 __begin2 += _Np;
1558 }
1559
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001560 template <class _Ptr>
1561 _LIBCPP_INLINE_VISIBILITY
1562 static
1563 void
1564 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1565 {
1566 while (__end1 != __begin1)
Howard Hinnantf619e232013-01-11 20:36:59 +00001567 {
1568 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1569 --__end2;
1570 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001571 }
1572
1573 template <class _Tp>
1574 _LIBCPP_INLINE_VISIBILITY
1575 static
1576 typename enable_if
1577 <
1578 (is_same<allocator_type, allocator<_Tp> >::value
1579 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1580 is_trivially_move_constructible<_Tp>::value,
1581 void
1582 >::type
1583 __construct_backward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
1584 {
1585 ptrdiff_t _Np = __end1 - __begin1;
1586 __end2 -= _Np;
Marshall Clowbf0460e2015-05-31 03:13:31 +00001587 if (_Np > 0)
1588 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001589 }
1590
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001591private:
1592
Howard Hinnant82894812010-09-22 16:48:34 +00001593 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001594 static pointer allocate(allocator_type& __a, size_type __n,
1595 const_void_pointer __hint, true_type)
1596 {return __a.allocate(__n, __hint);}
Howard Hinnant82894812010-09-22 16:48:34 +00001597 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001598 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnantec3773c2011-12-01 20:21:04 +00001599 const_void_pointer, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001600 {return __a.allocate(__n);}
1601
1602#ifndef _LIBCPP_HAS_NO_VARIADICS
1603 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001604 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001605 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001606 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001607 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001608 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001609 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1610 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001611 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001612 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001613#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001614
1615 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001616 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001617 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1618 {__a.destroy(__p);}
1619 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001620 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001621 static void __destroy(false_type, allocator_type&, _Tp* __p)
1622 {
1623 __p->~_Tp();
1624 }
1625
Howard Hinnant82894812010-09-22 16:48:34 +00001626 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001627 static size_type __max_size(true_type, const allocator_type& __a)
1628 {return __a.max_size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001629 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001630 static size_type __max_size(false_type, const allocator_type&)
1631 {return numeric_limits<size_type>::max();}
1632
Howard Hinnant82894812010-09-22 16:48:34 +00001633 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001634 static allocator_type
1635 select_on_container_copy_construction(true_type, const allocator_type& __a)
1636 {return __a.select_on_container_copy_construction();}
Howard Hinnant82894812010-09-22 16:48:34 +00001637 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001638 static allocator_type
1639 select_on_container_copy_construction(false_type, const allocator_type& __a)
1640 {return __a;}
1641};
1642
Marshall Clow66302c62015-04-07 05:21:38 +00001643template <class _Traits, class _Tp>
1644struct __rebind_alloc_helper
1645{
1646#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Marshall Clow0ad232a2015-05-10 13:59:45 +00001647 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow66302c62015-04-07 05:21:38 +00001648#else
1649 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1650#endif
1651};
1652
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001653// allocator
1654
1655template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001656class _LIBCPP_TYPE_VIS_ONLY allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001657{
1658public:
1659 typedef size_t size_type;
1660 typedef ptrdiff_t difference_type;
1661 typedef _Tp* pointer;
1662 typedef const _Tp* const_pointer;
1663 typedef _Tp& reference;
1664 typedef const _Tp& const_reference;
1665 typedef _Tp value_type;
1666
Howard Hinnant18884f42011-06-02 21:38:57 +00001667 typedef true_type propagate_on_container_move_assignment;
1668
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001669 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1670
Howard Hinnant1694d232011-05-28 14:41:13 +00001671 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1672 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1673 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001674 {return _VSTD::addressof(__x);}
Howard Hinnant1694d232011-05-28 14:41:13 +00001675 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001676 {return _VSTD::addressof(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001677 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Richard Smith73c1fce2014-06-04 19:54:15 +00001678 {return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));}
Howard Hinnant1694d232011-05-28 14:41:13 +00001679 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith73c1fce2014-06-04 19:54:15 +00001680 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant1694d232011-05-28 14:41:13 +00001681 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1682 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001683#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001684 template <class _Up, class... _Args>
1685 _LIBCPP_INLINE_VISIBILITY
1686 void
1687 construct(_Up* __p, _Args&&... __args)
1688 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001689 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001690 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001691#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001692 _LIBCPP_INLINE_VISIBILITY
1693 void
1694 construct(pointer __p)
1695 {
1696 ::new((void*)__p) _Tp();
1697 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001698# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001699
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001700 template <class _A0>
1701 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001702 void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001703 construct(pointer __p, _A0& __a0)
1704 {
1705 ::new((void*)__p) _Tp(__a0);
1706 }
1707 template <class _A0>
1708 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001709 void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001710 construct(pointer __p, const _A0& __a0)
1711 {
1712 ::new((void*)__p) _Tp(__a0);
1713 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001714# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001715 template <class _A0, class _A1>
1716 _LIBCPP_INLINE_VISIBILITY
1717 void
1718 construct(pointer __p, _A0& __a0, _A1& __a1)
1719 {
1720 ::new((void*)__p) _Tp(__a0, __a1);
1721 }
1722 template <class _A0, class _A1>
1723 _LIBCPP_INLINE_VISIBILITY
1724 void
1725 construct(pointer __p, const _A0& __a0, _A1& __a1)
1726 {
1727 ::new((void*)__p) _Tp(__a0, __a1);
1728 }
1729 template <class _A0, class _A1>
1730 _LIBCPP_INLINE_VISIBILITY
1731 void
1732 construct(pointer __p, _A0& __a0, const _A1& __a1)
1733 {
1734 ::new((void*)__p) _Tp(__a0, __a1);
1735 }
1736 template <class _A0, class _A1>
1737 _LIBCPP_INLINE_VISIBILITY
1738 void
1739 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1740 {
1741 ::new((void*)__p) _Tp(__a0, __a1);
1742 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001743#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001744 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1745};
1746
Howard Hinnant57199402012-01-02 17:56:02 +00001747template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001748class _LIBCPP_TYPE_VIS_ONLY allocator<const _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00001749{
1750public:
1751 typedef size_t size_type;
1752 typedef ptrdiff_t difference_type;
1753 typedef const _Tp* pointer;
1754 typedef const _Tp* const_pointer;
1755 typedef const _Tp& reference;
1756 typedef const _Tp& const_reference;
Howard Hinnant9360e9f2013-06-07 01:56:37 +00001757 typedef const _Tp value_type;
Howard Hinnant57199402012-01-02 17:56:02 +00001758
1759 typedef true_type propagate_on_container_move_assignment;
1760
1761 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1762
1763 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1764 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1765 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1766 {return _VSTD::addressof(__x);}
1767 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Richard Smith73c1fce2014-06-04 19:54:15 +00001768 {return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));}
Howard Hinnant57199402012-01-02 17:56:02 +00001769 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith73c1fce2014-06-04 19:54:15 +00001770 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant57199402012-01-02 17:56:02 +00001771 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1772 {return size_type(~0) / sizeof(_Tp);}
1773#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1774 template <class _Up, class... _Args>
1775 _LIBCPP_INLINE_VISIBILITY
1776 void
1777 construct(_Up* __p, _Args&&... __args)
1778 {
1779 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1780 }
1781#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1782 _LIBCPP_INLINE_VISIBILITY
1783 void
1784 construct(pointer __p)
1785 {
1786 ::new((void*)__p) _Tp();
1787 }
1788# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001789
Howard Hinnant57199402012-01-02 17:56:02 +00001790 template <class _A0>
1791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001792 void
Howard Hinnant57199402012-01-02 17:56:02 +00001793 construct(pointer __p, _A0& __a0)
1794 {
1795 ::new((void*)__p) _Tp(__a0);
1796 }
1797 template <class _A0>
1798 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001799 void
Howard Hinnant57199402012-01-02 17:56:02 +00001800 construct(pointer __p, const _A0& __a0)
1801 {
1802 ::new((void*)__p) _Tp(__a0);
1803 }
Howard Hinnant57199402012-01-02 17:56:02 +00001804# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1805 template <class _A0, class _A1>
1806 _LIBCPP_INLINE_VISIBILITY
1807 void
1808 construct(pointer __p, _A0& __a0, _A1& __a1)
1809 {
1810 ::new((void*)__p) _Tp(__a0, __a1);
1811 }
1812 template <class _A0, class _A1>
1813 _LIBCPP_INLINE_VISIBILITY
1814 void
1815 construct(pointer __p, const _A0& __a0, _A1& __a1)
1816 {
1817 ::new((void*)__p) _Tp(__a0, __a1);
1818 }
1819 template <class _A0, class _A1>
1820 _LIBCPP_INLINE_VISIBILITY
1821 void
1822 construct(pointer __p, _A0& __a0, const _A1& __a1)
1823 {
1824 ::new((void*)__p) _Tp(__a0, __a1);
1825 }
1826 template <class _A0, class _A1>
1827 _LIBCPP_INLINE_VISIBILITY
1828 void
1829 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1830 {
1831 ::new((void*)__p) _Tp(__a0, __a1);
1832 }
1833#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1834 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1835};
1836
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001837template <class _Tp, class _Up>
1838inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001839bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001840
1841template <class _Tp, class _Up>
1842inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001843bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001844
1845template <class _OutputIterator, class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001846class _LIBCPP_TYPE_VIS_ONLY raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001847 : public iterator<output_iterator_tag,
1848 _Tp, // purposefully not C++03
1849 ptrdiff_t, // purposefully not C++03
1850 _Tp*, // purposefully not C++03
1851 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1852{
1853private:
1854 _OutputIterator __x_;
1855public:
1856 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1857 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1858 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1859 {::new(&*__x_) _Tp(__element); return *this;}
1860 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1861 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1862 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowdbaf7a02015-05-10 13:14:08 +00001863#if _LIBCPP_STD_VER >= 14
1864 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1865#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001866};
1867
1868template <class _Tp>
1869pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00001870get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001871{
1872 pair<_Tp*, ptrdiff_t> __r(0, 0);
1873 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1874 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1875 / sizeof(_Tp);
1876 if (__n > __m)
1877 __n = __m;
1878 while (__n > 0)
1879 {
1880 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1881 if (__r.first)
1882 {
1883 __r.second = __n;
1884 break;
1885 }
1886 __n /= 2;
1887 }
1888 return __r;
1889}
1890
1891template <class _Tp>
1892inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001893void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001894
1895template <class _Tp>
1896struct auto_ptr_ref
1897{
1898 _Tp* __ptr_;
1899};
1900
1901template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001902class _LIBCPP_TYPE_VIS_ONLY auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001903{
1904private:
1905 _Tp* __ptr_;
1906public:
1907 typedef _Tp element_type;
1908
1909 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1910 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1911 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1912 : __ptr_(__p.release()) {}
1913 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1914 {reset(__p.release()); return *this;}
1915 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1916 {reset(__p.release()); return *this;}
1917 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1918 {reset(__p.__ptr_); return *this;}
1919 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1920
1921 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1922 {return *__ptr_;}
1923 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1924 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1925 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1926 {
1927 _Tp* __t = __ptr_;
1928 __ptr_ = 0;
1929 return __t;
1930 }
1931 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1932 {
1933 if (__ptr_ != __p)
1934 delete __ptr_;
1935 __ptr_ = __p;
1936 }
1937
1938 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1939 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1940 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1941 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1942 {return auto_ptr<_Up>(release());}
1943};
1944
1945template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001946class _LIBCPP_TYPE_VIS_ONLY auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001947{
1948public:
1949 typedef void element_type;
1950};
1951
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001952template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1953 typename remove_cv<_T2>::type>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00001954 bool = is_empty<_T1>::value
1955#if __has_feature(is_final)
1956 && !__is_final(_T1)
1957#endif
1958 ,
1959 bool = is_empty<_T2>::value
1960#if __has_feature(is_final)
1961 && !__is_final(_T2)
1962#endif
1963 >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001964struct __libcpp_compressed_pair_switch;
1965
1966template <class _T1, class _T2, bool IsSame>
1967struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1968
1969template <class _T1, class _T2, bool IsSame>
1970struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1971
1972template <class _T1, class _T2, bool IsSame>
1973struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1974
1975template <class _T1, class _T2>
1976struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
1977
1978template <class _T1, class _T2>
1979struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
1980
1981template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
1982class __libcpp_compressed_pair_imp;
1983
1984template <class _T1, class _T2>
1985class __libcpp_compressed_pair_imp<_T1, _T2, 0>
1986{
1987private:
1988 _T1 __first_;
1989 _T2 __second_;
1990public:
1991 typedef _T1 _T1_param;
1992 typedef _T2 _T2_param;
1993
1994 typedef typename remove_reference<_T1>::type& _T1_reference;
1995 typedef typename remove_reference<_T2>::type& _T2_reference;
1996
1997 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1998 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1999
2000 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002001 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002002 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002003 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002004 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002005 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002006 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002007
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002008#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002009
2010 _LIBCPP_INLINE_VISIBILITY
2011 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2012 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2013 is_nothrow_copy_constructible<_T2>::value)
2014 : __first_(__p.first()),
2015 __second_(__p.second()) {}
2016
2017 _LIBCPP_INLINE_VISIBILITY
2018 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2019 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2020 is_nothrow_copy_assignable<_T2>::value)
2021 {
2022 __first_ = __p.first();
2023 __second_ = __p.second();
2024 return *this;
2025 }
2026
Howard Hinnant61aa6012011-07-01 19:24:36 +00002027 _LIBCPP_INLINE_VISIBILITY
2028 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002029 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2030 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002031 : __first_(_VSTD::forward<_T1>(__p.first())),
2032 __second_(_VSTD::forward<_T2>(__p.second())) {}
2033
2034 _LIBCPP_INLINE_VISIBILITY
2035 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2036 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2037 is_nothrow_move_assignable<_T2>::value)
2038 {
2039 __first_ = _VSTD::forward<_T1>(__p.first());
2040 __second_ = _VSTD::forward<_T2>(__p.second());
2041 return *this;
2042 }
2043
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002044#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002045
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002046#ifndef _LIBCPP_HAS_NO_VARIADICS
2047
2048 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2049 _LIBCPP_INLINE_VISIBILITY
2050 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2051 tuple<_Args1...> __first_args,
2052 tuple<_Args2...> __second_args,
2053 __tuple_indices<_I1...>,
2054 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002055 : __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2056 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002057 {}
2058
2059#endif // _LIBCPP_HAS_NO_VARIADICS
2060
Howard Hinnant1694d232011-05-28 14:41:13 +00002061 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2062 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002063
Howard Hinnant1694d232011-05-28 14:41:13 +00002064 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2065 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002066
2067 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002068 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002069 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002070 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002071 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002072 swap(__first_, __x.__first_);
2073 swap(__second_, __x.__second_);
2074 }
2075};
2076
2077template <class _T1, class _T2>
2078class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2079 : private _T1
2080{
2081private:
2082 _T2 __second_;
2083public:
2084 typedef _T1 _T1_param;
2085 typedef _T2 _T2_param;
2086
2087 typedef _T1& _T1_reference;
2088 typedef typename remove_reference<_T2>::type& _T2_reference;
2089
2090 typedef const _T1& _T1_const_reference;
2091 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2092
2093 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002094 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002095 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002096 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002097 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002098 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002099 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002100
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002101#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002102
2103 _LIBCPP_INLINE_VISIBILITY
2104 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2105 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2106 is_nothrow_copy_constructible<_T2>::value)
2107 : _T1(__p.first()), __second_(__p.second()) {}
2108
2109 _LIBCPP_INLINE_VISIBILITY
2110 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2111 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2112 is_nothrow_copy_assignable<_T2>::value)
2113 {
2114 _T1::operator=(__p.first());
2115 __second_ = __p.second();
2116 return *this;
2117 }
2118
Howard Hinnant61aa6012011-07-01 19:24:36 +00002119 _LIBCPP_INLINE_VISIBILITY
2120 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002121 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2122 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002123 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002124
2125 _LIBCPP_INLINE_VISIBILITY
2126 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2127 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2128 is_nothrow_move_assignable<_T2>::value)
2129 {
2130 _T1::operator=(_VSTD::move(__p.first()));
2131 __second_ = _VSTD::forward<_T2>(__p.second());
2132 return *this;
2133 }
2134
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002135#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002136
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002137#ifndef _LIBCPP_HAS_NO_VARIADICS
2138
2139 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2140 _LIBCPP_INLINE_VISIBILITY
2141 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2142 tuple<_Args1...> __first_args,
2143 tuple<_Args2...> __second_args,
2144 __tuple_indices<_I1...>,
2145 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002146 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2147 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002148 {}
2149
2150#endif // _LIBCPP_HAS_NO_VARIADICS
2151
Howard Hinnant1694d232011-05-28 14:41:13 +00002152 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2153 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002154
Howard Hinnant1694d232011-05-28 14:41:13 +00002155 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2156 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002157
2158 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002159 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002160 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002161 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002162 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002163 swap(__second_, __x.__second_);
2164 }
2165};
2166
2167template <class _T1, class _T2>
2168class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2169 : private _T2
2170{
2171private:
2172 _T1 __first_;
2173public:
2174 typedef _T1 _T1_param;
2175 typedef _T2 _T2_param;
2176
2177 typedef typename remove_reference<_T1>::type& _T1_reference;
2178 typedef _T2& _T2_reference;
2179
2180 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2181 typedef const _T2& _T2_const_reference;
2182
2183 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2184 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002185 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002186 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002187 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002188 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00002189 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2190 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002191 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002192
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002193#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002194
2195 _LIBCPP_INLINE_VISIBILITY
2196 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2197 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2198 is_nothrow_copy_constructible<_T2>::value)
2199 : _T2(__p.second()), __first_(__p.first()) {}
2200
2201 _LIBCPP_INLINE_VISIBILITY
2202 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2203 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2204 is_nothrow_copy_assignable<_T2>::value)
2205 {
2206 _T2::operator=(__p.second());
2207 __first_ = __p.first();
2208 return *this;
2209 }
2210
Howard Hinnant61aa6012011-07-01 19:24:36 +00002211 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002212 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002213 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2214 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002215 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002216
2217 _LIBCPP_INLINE_VISIBILITY
2218 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2219 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2220 is_nothrow_move_assignable<_T2>::value)
2221 {
2222 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2223 __first_ = _VSTD::move(__p.first());
2224 return *this;
2225 }
2226
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002227#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002228
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002229#ifndef _LIBCPP_HAS_NO_VARIADICS
2230
2231 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2232 _LIBCPP_INLINE_VISIBILITY
2233 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2234 tuple<_Args1...> __first_args,
2235 tuple<_Args2...> __second_args,
2236 __tuple_indices<_I1...>,
2237 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002238 : _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...),
2239 __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002240
2241 {}
2242
2243#endif // _LIBCPP_HAS_NO_VARIADICS
2244
Howard Hinnant1694d232011-05-28 14:41:13 +00002245 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2246 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002247
Howard Hinnant1694d232011-05-28 14:41:13 +00002248 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2249 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002250
2251 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002252 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002253 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002254 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002255 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002256 swap(__first_, __x.__first_);
2257 }
2258};
2259
2260template <class _T1, class _T2>
2261class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2262 : private _T1,
2263 private _T2
2264{
2265public:
2266 typedef _T1 _T1_param;
2267 typedef _T2 _T2_param;
2268
2269 typedef _T1& _T1_reference;
2270 typedef _T2& _T2_reference;
2271
2272 typedef const _T1& _T1_const_reference;
2273 typedef const _T2& _T2_const_reference;
2274
2275 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2276 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002277 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002278 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002279 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002280 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002281 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002282
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002283#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002284
2285 _LIBCPP_INLINE_VISIBILITY
2286 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2287 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2288 is_nothrow_copy_constructible<_T2>::value)
2289 : _T1(__p.first()), _T2(__p.second()) {}
2290
2291 _LIBCPP_INLINE_VISIBILITY
2292 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2293 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2294 is_nothrow_copy_assignable<_T2>::value)
2295 {
2296 _T1::operator=(__p.first());
2297 _T2::operator=(__p.second());
2298 return *this;
2299 }
2300
Howard Hinnant61aa6012011-07-01 19:24:36 +00002301 _LIBCPP_INLINE_VISIBILITY
2302 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002303 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2304 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002305 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002306
2307 _LIBCPP_INLINE_VISIBILITY
2308 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2309 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2310 is_nothrow_move_assignable<_T2>::value)
2311 {
2312 _T1::operator=(_VSTD::move(__p.first()));
2313 _T2::operator=(_VSTD::move(__p.second()));
2314 return *this;
2315 }
2316
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002317#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002318
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002319#ifndef _LIBCPP_HAS_NO_VARIADICS
2320
2321 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2322 _LIBCPP_INLINE_VISIBILITY
2323 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2324 tuple<_Args1...> __first_args,
2325 tuple<_Args2...> __second_args,
2326 __tuple_indices<_I1...>,
2327 __tuple_indices<_I2...>)
Marshall Clowba6dbf42014-06-24 00:46:19 +00002328 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2329 _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002330 {}
2331
2332#endif // _LIBCPP_HAS_NO_VARIADICS
2333
Howard Hinnant1694d232011-05-28 14:41:13 +00002334 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2335 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002336
Howard Hinnant1694d232011-05-28 14:41:13 +00002337 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2338 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002339
Howard Hinnantec3773c2011-12-01 20:21:04 +00002340 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant1694d232011-05-28 14:41:13 +00002341 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002342 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002343 {
2344 }
2345};
2346
2347template <class _T1, class _T2>
2348class __compressed_pair
2349 : private __libcpp_compressed_pair_imp<_T1, _T2>
2350{
2351 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2352public:
2353 typedef typename base::_T1_param _T1_param;
2354 typedef typename base::_T2_param _T2_param;
2355
2356 typedef typename base::_T1_reference _T1_reference;
2357 typedef typename base::_T2_reference _T2_reference;
2358
2359 typedef typename base::_T1_const_reference _T1_const_reference;
2360 typedef typename base::_T2_const_reference _T2_const_reference;
2361
2362 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002363 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002364 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002365 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002366 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002367 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002368 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002369
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002370#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002371
2372 _LIBCPP_INLINE_VISIBILITY
2373 __compressed_pair(const __compressed_pair& __p)
2374 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2375 is_nothrow_copy_constructible<_T2>::value)
2376 : base(__p) {}
2377
2378 _LIBCPP_INLINE_VISIBILITY
2379 __compressed_pair& operator=(const __compressed_pair& __p)
2380 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2381 is_nothrow_copy_assignable<_T2>::value)
2382 {
2383 base::operator=(__p);
2384 return *this;
2385 }
2386
Howard Hinnant61aa6012011-07-01 19:24:36 +00002387 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002388 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002389 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2390 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002391 : base(_VSTD::move(__p)) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002392
2393 _LIBCPP_INLINE_VISIBILITY
2394 __compressed_pair& operator=(__compressed_pair&& __p)
2395 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2396 is_nothrow_move_assignable<_T2>::value)
2397 {
2398 base::operator=(_VSTD::move(__p));
2399 return *this;
2400 }
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002401
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002402#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002403
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002404#ifndef _LIBCPP_HAS_NO_VARIADICS
2405
2406 template <class... _Args1, class... _Args2>
2407 _LIBCPP_INLINE_VISIBILITY
2408 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2409 tuple<_Args2...> __second_args)
Howard Hinnant57199402012-01-02 17:56:02 +00002410 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002411 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2412 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2413 {}
2414
2415#endif // _LIBCPP_HAS_NO_VARIADICS
2416
Howard Hinnant1694d232011-05-28 14:41:13 +00002417 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2418 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002419
Howard Hinnant1694d232011-05-28 14:41:13 +00002420 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2421 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002422
Howard Hinnant1694d232011-05-28 14:41:13 +00002423 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2424 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002425 __is_nothrow_swappable<_T2>::value)
Howard Hinnant1694d232011-05-28 14:41:13 +00002426 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002427};
2428
2429template <class _T1, class _T2>
2430inline _LIBCPP_INLINE_VISIBILITY
2431void
2432swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002433 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow394451d2014-06-30 15:35:09 +00002434 __is_nothrow_swappable<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002435 {__x.swap(__y);}
2436
Howard Hinnant57199402012-01-02 17:56:02 +00002437// __same_or_less_cv_qualified
2438
2439template <class _Ptr1, class _Ptr2,
2440 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2441 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2442 >::value
2443 >
2444struct __same_or_less_cv_qualified_imp
2445 : is_convertible<_Ptr1, _Ptr2> {};
2446
2447template <class _Ptr1, class _Ptr2>
2448struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2449 : false_type {};
2450
Marshall Clow5f64a2b2014-04-26 05:19:48 +00002451template <class _Ptr1, class _Ptr2, bool = is_pointer<_Ptr1>::value ||
2452 is_same<_Ptr1, _Ptr2>::value ||
2453 __has_element_type<_Ptr1>::value>
Howard Hinnant57199402012-01-02 17:56:02 +00002454struct __same_or_less_cv_qualified
2455 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2456
2457template <class _Ptr1, class _Ptr2>
Marshall Clow5f64a2b2014-04-26 05:19:48 +00002458struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, false>
Howard Hinnant57199402012-01-02 17:56:02 +00002459 : false_type {};
2460
2461// default_delete
2462
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002463template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002464struct _LIBCPP_TYPE_VIS_ONLY default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002465{
Howard Hinnant46e94932012-07-07 20:56:04 +00002466#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2467 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2468#else
2469 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2470#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002471 template <class _Up>
2472 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002473 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2474 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002475 {
2476 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant05e7d242013-04-24 19:44:26 +00002477 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002478 delete __ptr;
2479 }
2480};
2481
2482template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002483struct _LIBCPP_TYPE_VIS_ONLY default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002484{
Howard Hinnant8e843502011-12-18 21:19:44 +00002485public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002486#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2487 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2488#else
2489 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2490#endif
Howard Hinnant8e843502011-12-18 21:19:44 +00002491 template <class _Up>
2492 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant57199402012-01-02 17:56:02 +00002493 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant8e843502011-12-18 21:19:44 +00002494 template <class _Up>
2495 _LIBCPP_INLINE_VISIBILITY
2496 void operator() (_Up* __ptr,
Howard Hinnant57199402012-01-02 17:56:02 +00002497 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002498 {
2499 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant05e7d242013-04-24 19:44:26 +00002500 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002501 delete [] __ptr;
2502 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002503};
2504
2505template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002506class _LIBCPP_TYPE_VIS_ONLY unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002507{
2508public:
2509 typedef _Tp element_type;
2510 typedef _Dp deleter_type;
2511 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2512private:
2513 __compressed_pair<pointer, deleter_type> __ptr_;
2514
Howard Hinnant57199402012-01-02 17:56:02 +00002515#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002516 unique_ptr(unique_ptr&);
2517 template <class _Up, class _Ep>
2518 unique_ptr(unique_ptr<_Up, _Ep>&);
2519 unique_ptr& operator=(unique_ptr&);
2520 template <class _Up, class _Ep>
2521 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002522#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002523
2524 struct __nat {int __for_bool_;};
2525
2526 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2527 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2528public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002529 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002530 : __ptr_(pointer())
2531 {
2532 static_assert(!is_pointer<deleter_type>::value,
2533 "unique_ptr constructed with null function pointer deleter");
2534 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002535 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002536 : __ptr_(pointer())
2537 {
2538 static_assert(!is_pointer<deleter_type>::value,
2539 "unique_ptr constructed with null function pointer deleter");
2540 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002541 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002542 : __ptr_(_VSTD::move(__p))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002543 {
2544 static_assert(!is_pointer<deleter_type>::value,
2545 "unique_ptr constructed with null function pointer deleter");
2546 }
2547
Howard Hinnant73d21a42010-09-04 23:28:19 +00002548#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002549 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2550 is_reference<deleter_type>::value,
2551 deleter_type,
2552 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002553 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002554 : __ptr_(__p, __d) {}
2555
2556 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002557 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002558 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002559 {
2560 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2561 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002562 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002563 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002564 template <class _Up, class _Ep>
2565 _LIBCPP_INLINE_VISIBILITY
2566 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2567 typename enable_if
2568 <
2569 !is_array<_Up>::value &&
2570 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2571 is_convertible<_Ep, deleter_type>::value &&
2572 (
2573 !is_reference<deleter_type>::value ||
2574 is_same<deleter_type, _Ep>::value
2575 ),
2576 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002577 >::type = __nat()) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002578 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002579
2580 template <class _Up>
2581 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2582 typename enable_if<
2583 is_convertible<_Up*, _Tp*>::value &&
2584 is_same<_Dp, default_delete<_Tp> >::value,
2585 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002586 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002587 : __ptr_(__p.release())
2588 {
2589 }
2590
Howard Hinnant1694d232011-05-28 14:41:13 +00002591 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002592 {
2593 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002594 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002595 return *this;
2596 }
2597
2598 template <class _Up, class _Ep>
2599 _LIBCPP_INLINE_VISIBILITY
2600 typename enable_if
2601 <
Howard Hinnant57199402012-01-02 17:56:02 +00002602 !is_array<_Up>::value &&
2603 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2604 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002605 unique_ptr&
2606 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002607 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002608 {
2609 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002610 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002611 return *this;
2612 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002613#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002614
2615 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2616 {
2617 return __rv<unique_ptr>(*this);
2618 }
2619
2620 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002621 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002622
2623 template <class _Up, class _Ep>
2624 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2625 {
2626 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002627 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002628 return *this;
2629 }
2630
2631 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002632 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002633
2634 template <class _Up>
2635 _LIBCPP_INLINE_VISIBILITY
2636 typename enable_if<
2637 is_convertible<_Up*, _Tp*>::value &&
2638 is_same<_Dp, default_delete<_Tp> >::value,
2639 unique_ptr&
2640 >::type
2641 operator=(auto_ptr<_Up> __p)
2642 {reset(__p.release()); return *this;}
2643
Howard Hinnant73d21a42010-09-04 23:28:19 +00002644#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002645 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2646
Howard Hinnant1694d232011-05-28 14:41:13 +00002647 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002648 {
2649 reset();
2650 return *this;
2651 }
2652
2653 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2654 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002655 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2656 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2657 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2658 {return __ptr_.second();}
2659 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2660 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002661 _LIBCPP_INLINE_VISIBILITY
2662 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2663 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002664
Howard Hinnant1694d232011-05-28 14:41:13 +00002665 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002666 {
2667 pointer __t = __ptr_.first();
2668 __ptr_.first() = pointer();
2669 return __t;
2670 }
2671
Howard Hinnant1694d232011-05-28 14:41:13 +00002672 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002673 {
2674 pointer __tmp = __ptr_.first();
2675 __ptr_.first() = __p;
2676 if (__tmp)
2677 __ptr_.second()(__tmp);
2678 }
2679
Howard Hinnant1694d232011-05-28 14:41:13 +00002680 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2681 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002682};
2683
2684template <class _Tp, class _Dp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002685class _LIBCPP_TYPE_VIS_ONLY unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002686{
2687public:
2688 typedef _Tp element_type;
2689 typedef _Dp deleter_type;
2690 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2691private:
2692 __compressed_pair<pointer, deleter_type> __ptr_;
2693
Howard Hinnant57199402012-01-02 17:56:02 +00002694#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002695 unique_ptr(unique_ptr&);
2696 template <class _Up>
2697 unique_ptr(unique_ptr<_Up>&);
2698 unique_ptr& operator=(unique_ptr&);
2699 template <class _Up>
2700 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002701#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002702
2703 struct __nat {int __for_bool_;};
2704
2705 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2706 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2707public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002708 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002709 : __ptr_(pointer())
2710 {
2711 static_assert(!is_pointer<deleter_type>::value,
2712 "unique_ptr constructed with null function pointer deleter");
2713 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002714 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002715 : __ptr_(pointer())
2716 {
2717 static_assert(!is_pointer<deleter_type>::value,
2718 "unique_ptr constructed with null function pointer deleter");
2719 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002720#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00002721 template <class _Pp>
2722 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p,
2723 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002724 : __ptr_(__p)
2725 {
2726 static_assert(!is_pointer<deleter_type>::value,
2727 "unique_ptr constructed with null function pointer deleter");
2728 }
2729
Logan Chiene1678a12014-01-31 09:30:46 +00002730 template <class _Pp>
Howard Hinnant99968442011-11-29 18:15:50 +00002731 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002732 is_reference<deleter_type>::value,
2733 deleter_type,
Logan Chiene1678a12014-01-31 09:30:46 +00002734 typename add_lvalue_reference<const deleter_type>::type>::type __d,
2735 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant1694d232011-05-28 14:41:13 +00002736 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002737 : __ptr_(__p, __d) {}
2738
2739 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2740 is_reference<deleter_type>::value,
2741 deleter_type,
2742 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002743 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002744 : __ptr_(pointer(), __d) {}
2745
Logan Chiene1678a12014-01-31 09:30:46 +00002746 template <class _Pp>
2747 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p,
2748 typename remove_reference<deleter_type>::type&& __d,
2749 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant1694d232011-05-28 14:41:13 +00002750 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002751 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002752 {
2753 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2754 }
2755
2756 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002757 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002758 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002759 {
2760 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2761 }
2762
Howard Hinnant1694d232011-05-28 14:41:13 +00002763 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002764 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002765
Howard Hinnant1694d232011-05-28 14:41:13 +00002766 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002767 {
2768 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002769 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002770 return *this;
2771 }
Howard Hinnant8e843502011-12-18 21:19:44 +00002772
2773 template <class _Up, class _Ep>
2774 _LIBCPP_INLINE_VISIBILITY
2775 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2776 typename enable_if
2777 <
2778 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002779 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant8e843502011-12-18 21:19:44 +00002780 && is_convertible<_Ep, deleter_type>::value &&
2781 (
2782 !is_reference<deleter_type>::value ||
2783 is_same<deleter_type, _Ep>::value
2784 ),
2785 __nat
2786 >::type = __nat()
2787 ) _NOEXCEPT
2788 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2789
2790
2791 template <class _Up, class _Ep>
2792 _LIBCPP_INLINE_VISIBILITY
2793 typename enable_if
2794 <
2795 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002796 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2797 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant8e843502011-12-18 21:19:44 +00002798 unique_ptr&
2799 >::type
2800 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2801 {
2802 reset(__u.release());
2803 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2804 return *this;
2805 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002806#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002807
2808 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2809 : __ptr_(__p)
2810 {
2811 static_assert(!is_pointer<deleter_type>::value,
2812 "unique_ptr constructed with null function pointer deleter");
2813 }
2814
2815 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002816 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002817
2818 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002819 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002820
2821 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2822 {
2823 return __rv<unique_ptr>(*this);
2824 }
2825
2826 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002827 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002828
2829 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2830 {
2831 reset(__u->release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002832 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002833 return *this;
2834 }
2835
Howard Hinnant73d21a42010-09-04 23:28:19 +00002836#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002837 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2838
Howard Hinnant1694d232011-05-28 14:41:13 +00002839 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002840 {
2841 reset();
2842 return *this;
2843 }
2844
2845 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2846 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002847 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2848 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2849 {return __ptr_.second();}
2850 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2851 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002852 _LIBCPP_INLINE_VISIBILITY
2853 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2854 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002855
Howard Hinnant1694d232011-05-28 14:41:13 +00002856 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002857 {
2858 pointer __t = __ptr_.first();
2859 __ptr_.first() = pointer();
2860 return __t;
2861 }
2862
Howard Hinnant73d21a42010-09-04 23:28:19 +00002863#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00002864 template <class _Pp>
2865 _LIBCPP_INLINE_VISIBILITY
2866 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, void>::type
2867 reset(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002868 {
2869 pointer __tmp = __ptr_.first();
2870 __ptr_.first() = __p;
2871 if (__tmp)
2872 __ptr_.second()(__tmp);
2873 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002874 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002875 {
2876 pointer __tmp = __ptr_.first();
2877 __ptr_.first() = nullptr;
2878 if (__tmp)
2879 __ptr_.second()(__tmp);
2880 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002881 _LIBCPP_INLINE_VISIBILITY void reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002882 {
2883 pointer __tmp = __ptr_.first();
2884 __ptr_.first() = nullptr;
2885 if (__tmp)
2886 __ptr_.second()(__tmp);
2887 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002888#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002889 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2890 {
2891 pointer __tmp = __ptr_.first();
2892 __ptr_.first() = __p;
2893 if (__tmp)
2894 __ptr_.second()(__tmp);
2895 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002896#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002897
2898 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2899private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002900
Howard Hinnant73d21a42010-09-04 23:28:19 +00002901#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002902 template <class _Up>
2903 explicit unique_ptr(_Up);
2904 template <class _Up>
2905 unique_ptr(_Up __u,
2906 typename conditional<
2907 is_reference<deleter_type>::value,
2908 deleter_type,
2909 typename add_lvalue_reference<const deleter_type>::type>::type,
2910 typename enable_if
2911 <
2912 is_convertible<_Up, pointer>::value,
2913 __nat
2914 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002915#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002916};
2917
2918template <class _Tp, class _Dp>
2919inline _LIBCPP_INLINE_VISIBILITY
2920void
Howard Hinnant1694d232011-05-28 14:41:13 +00002921swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002922
2923template <class _T1, class _D1, class _T2, class _D2>
2924inline _LIBCPP_INLINE_VISIBILITY
2925bool
2926operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2927
2928template <class _T1, class _D1, class _T2, class _D2>
2929inline _LIBCPP_INLINE_VISIBILITY
2930bool
2931operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2932
2933template <class _T1, class _D1, class _T2, class _D2>
2934inline _LIBCPP_INLINE_VISIBILITY
2935bool
Howard Hinnant3fadda32012-02-21 21:02:58 +00002936operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2937{
2938 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2939 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselier8492cd82015-02-05 23:01:40 +00002940 typedef typename common_type<_P1, _P2>::type _Vp;
2941 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnant3fadda32012-02-21 21:02:58 +00002942}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002943
2944template <class _T1, class _D1, class _T2, class _D2>
2945inline _LIBCPP_INLINE_VISIBILITY
2946bool
2947operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2948
2949template <class _T1, class _D1, class _T2, class _D2>
2950inline _LIBCPP_INLINE_VISIBILITY
2951bool
2952operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2953
2954template <class _T1, class _D1, class _T2, class _D2>
2955inline _LIBCPP_INLINE_VISIBILITY
2956bool
2957operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2958
Howard Hinnant3fadda32012-02-21 21:02:58 +00002959template <class _T1, class _D1>
2960inline _LIBCPP_INLINE_VISIBILITY
2961bool
Howard Hinnant46e94932012-07-07 20:56:04 +00002962operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00002963{
2964 return !__x;
2965}
2966
2967template <class _T1, class _D1>
2968inline _LIBCPP_INLINE_VISIBILITY
2969bool
Howard Hinnant46e94932012-07-07 20:56:04 +00002970operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00002971{
2972 return !__x;
2973}
2974
2975template <class _T1, class _D1>
2976inline _LIBCPP_INLINE_VISIBILITY
2977bool
Howard Hinnant46e94932012-07-07 20:56:04 +00002978operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00002979{
2980 return static_cast<bool>(__x);
2981}
2982
2983template <class _T1, class _D1>
2984inline _LIBCPP_INLINE_VISIBILITY
2985bool
Howard Hinnant46e94932012-07-07 20:56:04 +00002986operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00002987{
2988 return static_cast<bool>(__x);
2989}
2990
2991template <class _T1, class _D1>
2992inline _LIBCPP_INLINE_VISIBILITY
2993bool
2994operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2995{
2996 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2997 return less<_P1>()(__x.get(), nullptr);
2998}
2999
3000template <class _T1, class _D1>
3001inline _LIBCPP_INLINE_VISIBILITY
3002bool
3003operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3004{
3005 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3006 return less<_P1>()(nullptr, __x.get());
3007}
3008
3009template <class _T1, class _D1>
3010inline _LIBCPP_INLINE_VISIBILITY
3011bool
3012operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3013{
3014 return nullptr < __x;
3015}
3016
3017template <class _T1, class _D1>
3018inline _LIBCPP_INLINE_VISIBILITY
3019bool
3020operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3021{
3022 return __x < nullptr;
3023}
3024
3025template <class _T1, class _D1>
3026inline _LIBCPP_INLINE_VISIBILITY
3027bool
3028operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3029{
3030 return !(nullptr < __x);
3031}
3032
3033template <class _T1, class _D1>
3034inline _LIBCPP_INLINE_VISIBILITY
3035bool
3036operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3037{
3038 return !(__x < nullptr);
3039}
3040
3041template <class _T1, class _D1>
3042inline _LIBCPP_INLINE_VISIBILITY
3043bool
3044operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3045{
3046 return !(__x < nullptr);
3047}
3048
3049template <class _T1, class _D1>
3050inline _LIBCPP_INLINE_VISIBILITY
3051bool
3052operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3053{
3054 return !(nullptr < __x);
3055}
3056
Howard Hinnant87073e42012-05-01 15:37:54 +00003057#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3058
3059template <class _Tp, class _Dp>
3060inline _LIBCPP_INLINE_VISIBILITY
3061unique_ptr<_Tp, _Dp>
3062move(unique_ptr<_Tp, _Dp>& __t)
3063{
3064 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3065}
3066
3067#endif
3068
Marshall Clowfd7481e2013-07-01 18:16:03 +00003069#if _LIBCPP_STD_VER > 11
3070
3071template<class _Tp>
3072struct __unique_if
3073{
3074 typedef unique_ptr<_Tp> __unique_single;
3075};
3076
3077template<class _Tp>
3078struct __unique_if<_Tp[]>
3079{
3080 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3081};
3082
3083template<class _Tp, size_t _Np>
3084struct __unique_if<_Tp[_Np]>
3085{
3086 typedef void __unique_array_known_bound;
3087};
3088
3089template<class _Tp, class... _Args>
Marshall Clowfb551102013-07-02 20:06:09 +00003090inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003091typename __unique_if<_Tp>::__unique_single
3092make_unique(_Args&&... __args)
3093{
3094 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3095}
3096
3097template<class _Tp>
Marshall Clowfb551102013-07-02 20:06:09 +00003098inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003099typename __unique_if<_Tp>::__unique_array_unknown_bound
3100make_unique(size_t __n)
3101{
3102 typedef typename remove_extent<_Tp>::type _Up;
3103 return unique_ptr<_Tp>(new _Up[__n]());
3104}
3105
3106template<class _Tp, class... _Args>
3107 typename __unique_if<_Tp>::__unique_array_known_bound
3108 make_unique(_Args&&...) = delete;
3109
3110#endif // _LIBCPP_STD_VER > 11
3111
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00003112template <class _Tp> struct hash;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003113
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003114template <class _Size>
3115inline _LIBCPP_INLINE_VISIBILITY
3116_Size
3117__loadword(const void* __p)
3118{
3119 _Size __r;
3120 std::memcpy(&__r, __p, sizeof(__r));
3121 return __r;
3122}
3123
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003124// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
3125// is 64 bits. This is because cityhash64 uses 64bit x 64bit
3126// multiplication, which can be very slow on 32-bit systems.
Howard Hinnant40c13d32011-12-05 00:08:45 +00003127template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003128struct __murmur2_or_cityhash;
Howard Hinnant40c13d32011-12-05 00:08:45 +00003129
3130template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003131struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003132{
3133 _Size operator()(const void* __key, _Size __len);
3134};
3135
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003136// murmur2
Howard Hinnant40c13d32011-12-05 00:08:45 +00003137template <class _Size>
3138_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003139__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003140{
3141 const _Size __m = 0x5bd1e995;
3142 const _Size __r = 24;
3143 _Size __h = __len;
3144 const unsigned char* __data = static_cast<const unsigned char*>(__key);
3145 for (; __len >= 4; __data += 4, __len -= 4)
3146 {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003147 _Size __k = __loadword<_Size>(__data);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003148 __k *= __m;
3149 __k ^= __k >> __r;
3150 __k *= __m;
3151 __h *= __m;
3152 __h ^= __k;
3153 }
3154 switch (__len)
3155 {
3156 case 3:
3157 __h ^= __data[2] << 16;
3158 case 2:
3159 __h ^= __data[1] << 8;
3160 case 1:
3161 __h ^= __data[0];
3162 __h *= __m;
3163 }
3164 __h ^= __h >> 13;
3165 __h *= __m;
3166 __h ^= __h >> 15;
3167 return __h;
3168}
3169
3170template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003171struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003172{
3173 _Size operator()(const void* __key, _Size __len);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003174
3175 private:
3176 // Some primes between 2^63 and 2^64.
3177 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
3178 static const _Size __k1 = 0xb492b66fbe98f273ULL;
3179 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
3180 static const _Size __k3 = 0xc949d7c7509e6557ULL;
3181
3182 static _Size __rotate(_Size __val, int __shift) {
3183 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
3184 }
3185
3186 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
3187 return (__val >> __shift) | (__val << (64 - __shift));
3188 }
3189
3190 static _Size __shift_mix(_Size __val) {
3191 return __val ^ (__val >> 47);
3192 }
3193
3194 static _Size __hash_len_16(_Size __u, _Size __v) {
3195 const _Size __mul = 0x9ddfea08eb382d69ULL;
3196 _Size __a = (__u ^ __v) * __mul;
3197 __a ^= (__a >> 47);
3198 _Size __b = (__v ^ __a) * __mul;
3199 __b ^= (__b >> 47);
3200 __b *= __mul;
3201 return __b;
3202 }
3203
3204 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
3205 if (__len > 8) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003206 const _Size __a = __loadword<_Size>(__s);
3207 const _Size __b = __loadword<_Size>(__s + __len - 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003208 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
3209 }
3210 if (__len >= 4) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003211 const uint32_t __a = __loadword<uint32_t>(__s);
3212 const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003213 return __hash_len_16(__len + (__a << 3), __b);
3214 }
3215 if (__len > 0) {
3216 const unsigned char __a = __s[0];
3217 const unsigned char __b = __s[__len >> 1];
3218 const unsigned char __c = __s[__len - 1];
3219 const uint32_t __y = static_cast<uint32_t>(__a) +
3220 (static_cast<uint32_t>(__b) << 8);
3221 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3222 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3223 }
3224 return __k2;
3225 }
3226
3227 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003228 const _Size __a = __loadword<_Size>(__s) * __k1;
3229 const _Size __b = __loadword<_Size>(__s + 8);
3230 const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;
3231 const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003232 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3233 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3234 }
3235
3236 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3237 // Callers do best to use "random-looking" values for a and b.
3238 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3239 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3240 __a += __w;
3241 __b = __rotate(__b + __a + __z, 21);
3242 const _Size __c = __a;
3243 __a += __x;
3244 __a += __y;
3245 __b += __rotate(__a, 44);
3246 return pair<_Size, _Size>(__a + __z, __b + __c);
3247 }
3248
3249 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3250 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3251 const char* __s, _Size __a, _Size __b) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003252 return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),
3253 __loadword<_Size>(__s + 8),
3254 __loadword<_Size>(__s + 16),
3255 __loadword<_Size>(__s + 24),
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003256 __a,
3257 __b);
3258 }
3259
3260 // Return an 8-byte hash for 33 to 64 bytes.
3261 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003262 _Size __z = __loadword<_Size>(__s + 24);
3263 _Size __a = __loadword<_Size>(__s) +
3264 (__len + __loadword<_Size>(__s + __len - 16)) * __k0;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003265 _Size __b = __rotate(__a + __z, 52);
3266 _Size __c = __rotate(__a, 37);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003267 __a += __loadword<_Size>(__s + 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003268 __c += __rotate(__a, 7);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003269 __a += __loadword<_Size>(__s + 16);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003270 _Size __vf = __a + __z;
3271 _Size __vs = __b + __rotate(__a, 31) + __c;
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003272 __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);
3273 __z += __loadword<_Size>(__s + __len - 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003274 __b = __rotate(__a + __z, 52);
3275 __c = __rotate(__a, 37);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003276 __a += __loadword<_Size>(__s + __len - 24);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003277 __c += __rotate(__a, 7);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003278 __a += __loadword<_Size>(__s + __len - 16);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003279 _Size __wf = __a + __z;
3280 _Size __ws = __b + __rotate(__a, 31) + __c;
3281 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3282 return __shift_mix(__r * __k0 + __vs) * __k2;
3283 }
Howard Hinnant40c13d32011-12-05 00:08:45 +00003284};
3285
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003286// cityhash64
Howard Hinnant40c13d32011-12-05 00:08:45 +00003287template <class _Size>
3288_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003289__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003290{
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003291 const char* __s = static_cast<const char*>(__key);
3292 if (__len <= 32) {
3293 if (__len <= 16) {
3294 return __hash_len_0_to_16(__s, __len);
3295 } else {
3296 return __hash_len_17_to_32(__s, __len);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003297 }
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003298 } else if (__len <= 64) {
3299 return __hash_len_33_to_64(__s, __len);
3300 }
3301
3302 // For strings over 64 bytes we hash the end first, and then as we
3303 // loop we keep 56 bytes of state: v, w, x, y, and z.
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003304 _Size __x = __loadword<_Size>(__s + __len - 40);
3305 _Size __y = __loadword<_Size>(__s + __len - 16) +
3306 __loadword<_Size>(__s + __len - 56);
3307 _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,
3308 __loadword<_Size>(__s + __len - 24));
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003309 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3310 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003311 __x = __x * __k1 + __loadword<_Size>(__s);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003312
3313 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3314 __len = (__len - 1) & ~static_cast<_Size>(63);
3315 do {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003316 __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;
3317 __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003318 __x ^= __w.second;
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003319 __y += __v.first + __loadword<_Size>(__s + 40);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003320 __z = __rotate(__z + __w.first, 33) * __k1;
3321 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3322 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003323 __y + __loadword<_Size>(__s + 16));
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003324 std::swap(__z, __x);
3325 __s += 64;
3326 __len -= 64;
3327 } while (__len != 0);
3328 return __hash_len_16(
3329 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3330 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003331}
3332
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003333template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3334struct __scalar_hash;
3335
3336template <class _Tp>
3337struct __scalar_hash<_Tp, 0>
3338 : public unary_function<_Tp, size_t>
Howard Hinnant21aefc32010-06-03 16:42:57 +00003339{
Howard Hinnant82894812010-09-22 16:48:34 +00003340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003341 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003342 {
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003343 union
3344 {
3345 _Tp __t;
3346 size_t __a;
3347 } __u;
3348 __u.__a = 0;
3349 __u.__t = __v;
3350 return __u.__a;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003351 }
3352};
3353
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003354template <class _Tp>
3355struct __scalar_hash<_Tp, 1>
3356 : public unary_function<_Tp, size_t>
3357{
3358 _LIBCPP_INLINE_VISIBILITY
3359 size_t operator()(_Tp __v) const _NOEXCEPT
3360 {
3361 union
3362 {
3363 _Tp __t;
3364 size_t __a;
3365 } __u;
3366 __u.__t = __v;
3367 return __u.__a;
3368 }
3369};
3370
3371template <class _Tp>
3372struct __scalar_hash<_Tp, 2>
3373 : public unary_function<_Tp, size_t>
3374{
3375 _LIBCPP_INLINE_VISIBILITY
3376 size_t operator()(_Tp __v) const _NOEXCEPT
3377 {
3378 union
3379 {
3380 _Tp __t;
3381 struct
3382 {
3383 size_t __a;
3384 size_t __b;
3385 };
3386 } __u;
3387 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003388 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003389 }
3390};
3391
3392template <class _Tp>
3393struct __scalar_hash<_Tp, 3>
3394 : public unary_function<_Tp, size_t>
3395{
3396 _LIBCPP_INLINE_VISIBILITY
3397 size_t operator()(_Tp __v) const _NOEXCEPT
3398 {
3399 union
3400 {
3401 _Tp __t;
3402 struct
3403 {
3404 size_t __a;
3405 size_t __b;
3406 size_t __c;
3407 };
3408 } __u;
3409 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003410 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003411 }
3412};
3413
3414template <class _Tp>
3415struct __scalar_hash<_Tp, 4>
3416 : public unary_function<_Tp, size_t>
3417{
3418 _LIBCPP_INLINE_VISIBILITY
3419 size_t operator()(_Tp __v) const _NOEXCEPT
3420 {
3421 union
3422 {
3423 _Tp __t;
3424 struct
3425 {
3426 size_t __a;
3427 size_t __b;
3428 size_t __c;
3429 size_t __d;
3430 };
3431 } __u;
3432 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003433 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003434 }
3435};
3436
3437template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003438struct _LIBCPP_TYPE_VIS_ONLY hash<_Tp*>
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003439 : public unary_function<_Tp*, size_t>
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003440{
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003441 _LIBCPP_INLINE_VISIBILITY
3442 size_t operator()(_Tp* __v) const _NOEXCEPT
3443 {
3444 union
3445 {
3446 _Tp* __t;
3447 size_t __a;
3448 } __u;
3449 __u.__t = __v;
3450 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
3451 }
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003452};
3453
Howard Hinnant21aefc32010-06-03 16:42:57 +00003454template <class _Tp, class _Dp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003455struct _LIBCPP_TYPE_VIS_ONLY hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003456{
3457 typedef unique_ptr<_Tp, _Dp> argument_type;
3458 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003459 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003460 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003461 {
3462 typedef typename argument_type::pointer pointer;
3463 return hash<pointer>()(__ptr.get());
3464 }
3465};
3466
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003467struct __destruct_n
3468{
3469private:
3470 size_t size;
3471
3472 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003473 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003474 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3475
3476 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003477 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003478 {}
3479
Howard Hinnant1694d232011-05-28 14:41:13 +00003480 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003481 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003482 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003483 {}
3484
Howard Hinnant1694d232011-05-28 14:41:13 +00003485 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003486 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003487 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003488 {}
3489public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003490 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3491 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003492
3493 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003494 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003495 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003496
3497 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003498 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003499 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003500
3501 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003502 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003503 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003504};
3505
3506template <class _Alloc>
3507class __allocator_destructor
3508{
3509 typedef allocator_traits<_Alloc> __alloc_traits;
3510public:
3511 typedef typename __alloc_traits::pointer pointer;
3512 typedef typename __alloc_traits::size_type size_type;
3513private:
3514 _Alloc& __alloc_;
3515 size_type __s_;
3516public:
3517 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00003518 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003519 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003520 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003521 void operator()(pointer __p) _NOEXCEPT
3522 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003523};
3524
3525template <class _InputIterator, class _ForwardIterator>
3526_ForwardIterator
3527uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3528{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003529 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003530#ifndef _LIBCPP_NO_EXCEPTIONS
3531 _ForwardIterator __s = __r;
3532 try
3533 {
3534#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003535 for (; __f != __l; ++__f, (void) ++__r)
3536 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant8292d742011-12-29 17:45:35 +00003537#ifndef _LIBCPP_NO_EXCEPTIONS
3538 }
3539 catch (...)
3540 {
3541 for (; __s != __r; ++__s)
3542 __s->~value_type();
3543 throw;
3544 }
3545#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003546 return __r;
3547}
3548
3549template <class _InputIterator, class _Size, class _ForwardIterator>
3550_ForwardIterator
3551uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3552{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003553 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003554#ifndef _LIBCPP_NO_EXCEPTIONS
3555 _ForwardIterator __s = __r;
3556 try
3557 {
3558#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003559 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3560 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant8292d742011-12-29 17:45:35 +00003561#ifndef _LIBCPP_NO_EXCEPTIONS
3562 }
3563 catch (...)
3564 {
3565 for (; __s != __r; ++__s)
3566 __s->~value_type();
3567 throw;
3568 }
3569#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003570 return __r;
3571}
3572
3573template <class _ForwardIterator, class _Tp>
3574void
3575uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3576{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003577 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003578#ifndef _LIBCPP_NO_EXCEPTIONS
3579 _ForwardIterator __s = __f;
3580 try
3581 {
3582#endif
3583 for (; __f != __l; ++__f)
Marshall Clow5dce73d2015-05-19 15:01:48 +00003584 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant8292d742011-12-29 17:45:35 +00003585#ifndef _LIBCPP_NO_EXCEPTIONS
3586 }
3587 catch (...)
3588 {
3589 for (; __s != __f; ++__s)
3590 __s->~value_type();
3591 throw;
3592 }
3593#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003594}
3595
3596template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003597_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003598uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
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 = __f;
3603 try
3604 {
3605#endif
Marshall Clow5dce73d2015-05-19 15:01:48 +00003606 for (; __n > 0; ++__f, (void) --__n)
3607 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant8292d742011-12-29 17:45:35 +00003608#ifndef _LIBCPP_NO_EXCEPTIONS
3609 }
3610 catch (...)
3611 {
3612 for (; __s != __f; ++__s)
3613 __s->~value_type();
3614 throw;
3615 }
3616#endif
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003617 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003618}
3619
Howard Hinnant82894812010-09-22 16:48:34 +00003620class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003621 : public std::exception
3622{
3623public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003624 virtual ~bad_weak_ptr() _NOEXCEPT;
3625 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003626};
3627
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003628template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003629
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003630class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003631{
3632 __shared_count(const __shared_count&);
3633 __shared_count& operator=(const __shared_count&);
3634
3635protected:
3636 long __shared_owners_;
3637 virtual ~__shared_count();
3638private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003639 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003640
3641public:
Howard Hinnant82894812010-09-22 16:48:34 +00003642 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003643 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003644 : __shared_owners_(__refs) {}
3645
Howard Hinnant1694d232011-05-28 14:41:13 +00003646 void __add_shared() _NOEXCEPT;
3647 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003648 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003649 long use_count() const _NOEXCEPT {return __shared_owners_ + 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003650};
3651
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003652class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003653 : private __shared_count
3654{
3655 long __shared_weak_owners_;
3656
3657public:
Howard Hinnant82894812010-09-22 16:48:34 +00003658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003659 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003660 : __shared_count(__refs),
3661 __shared_weak_owners_(__refs) {}
3662protected:
3663 virtual ~__shared_weak_count();
3664
3665public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003666 void __add_shared() _NOEXCEPT;
3667 void __add_weak() _NOEXCEPT;
3668 void __release_shared() _NOEXCEPT;
3669 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003670 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003671 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3672 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003673
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003674 // Define the function out only if we build static libc++ without RTTI.
3675 // Otherwise we may break clients who need to compile their projects with
3676 // -fno-rtti and yet link against a libc++.dylib compiled
3677 // without -fno-rtti.
3678#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant1694d232011-05-28 14:41:13 +00003679 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003680#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003681private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003682 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003683};
3684
3685template <class _Tp, class _Dp, class _Alloc>
3686class __shared_ptr_pointer
3687 : public __shared_weak_count
3688{
3689 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3690public:
Howard Hinnant82894812010-09-22 16:48:34 +00003691 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003692 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003693 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003694
Howard Hinnantd4444702010-08-11 17:04:31 +00003695#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00003696 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00003697#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003698
3699private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003700 virtual void __on_zero_shared() _NOEXCEPT;
3701 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003702};
3703
Howard Hinnantd4444702010-08-11 17:04:31 +00003704#ifndef _LIBCPP_NO_RTTI
3705
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003706template <class _Tp, class _Dp, class _Alloc>
3707const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00003708__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003709{
Marshall Clow4b3ca8c2014-11-17 19:05:50 +00003710 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003711}
3712
Howard Hinnant324bb032010-08-22 00:02:43 +00003713#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003714
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003715template <class _Tp, class _Dp, class _Alloc>
3716void
Howard Hinnant1694d232011-05-28 14:41:13 +00003717__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003718{
3719 __data_.first().second()(__data_.first().first());
3720 __data_.first().second().~_Dp();
3721}
3722
3723template <class _Tp, class _Dp, class _Alloc>
3724void
Howard Hinnant1694d232011-05-28 14:41:13 +00003725__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003726{
Eric Fiselier8492cd82015-02-05 23:01:40 +00003727 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3728 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003729 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3730
Eric Fiselier8492cd82015-02-05 23:01:40 +00003731 _Al __a(__data_.second());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003732 __data_.second().~_Alloc();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003733 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003734}
3735
3736template <class _Tp, class _Alloc>
3737class __shared_ptr_emplace
3738 : public __shared_weak_count
3739{
3740 __compressed_pair<_Alloc, _Tp> __data_;
3741public:
3742#ifndef _LIBCPP_HAS_NO_VARIADICS
3743
Howard Hinnant82894812010-09-22 16:48:34 +00003744 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003745 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003746 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003747
3748 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00003749 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003750 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00003751 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3752 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003753
3754#else // _LIBCPP_HAS_NO_VARIADICS
3755
Howard Hinnant82894812010-09-22 16:48:34 +00003756 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003757 __shared_ptr_emplace(_Alloc __a)
3758 : __data_(__a) {}
3759
3760 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00003761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003762 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3763 : __data_(__a, _Tp(__a0)) {}
3764
3765 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00003766 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003767 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3768 : __data_(__a, _Tp(__a0, __a1)) {}
3769
3770 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00003771 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003772 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3773 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3774
3775#endif // _LIBCPP_HAS_NO_VARIADICS
3776
3777private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003778 virtual void __on_zero_shared() _NOEXCEPT;
3779 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003780public:
Howard Hinnant82894812010-09-22 16:48:34 +00003781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003782 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003783};
3784
3785template <class _Tp, class _Alloc>
3786void
Howard Hinnant1694d232011-05-28 14:41:13 +00003787__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003788{
3789 __data_.second().~_Tp();
3790}
3791
3792template <class _Tp, class _Alloc>
3793void
Howard Hinnant1694d232011-05-28 14:41:13 +00003794__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003795{
Eric Fiselier8492cd82015-02-05 23:01:40 +00003796 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3797 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003798 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40 +00003799 _Al __a(__data_.first());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003800 __data_.first().~_Alloc();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00003801 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003802}
3803
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003804template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003805
3806template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003807class _LIBCPP_TYPE_VIS_ONLY shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003808{
Howard Hinnant324bb032010-08-22 00:02:43 +00003809public:
3810 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003811private:
3812 element_type* __ptr_;
3813 __shared_weak_count* __cntrl_;
3814
3815 struct __nat {int __for_bool_;};
3816public:
Howard Hinnant46e94932012-07-07 20:56:04 +00003817 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
3818 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiene1678a12014-01-31 09:30:46 +00003819 template<class _Yp>
3820 explicit shared_ptr(_Yp* __p,
3821 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3822 template<class _Yp, class _Dp>
3823 shared_ptr(_Yp* __p, _Dp __d,
3824 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3825 template<class _Yp, class _Dp, class _Alloc>
3826 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3827 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003828 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3829 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant1694d232011-05-28 14:41:13 +00003830 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3831 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003832 template<class _Yp>
3833 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003834 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3835 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003836#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003837 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003838 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003839 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3840 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003841#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003842 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003843 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003844#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003845 template<class _Yp>
3846 shared_ptr(auto_ptr<_Yp>&& __r,
3847 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant324bb032010-08-22 00:02:43 +00003848#else
Logan Chiene1678a12014-01-31 09:30:46 +00003849 template<class _Yp>
3850 shared_ptr(auto_ptr<_Yp> __r,
3851 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003852#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003853#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003854 template <class _Yp, class _Dp>
3855 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3856 typename enable_if
3857 <
3858 !is_lvalue_reference<_Dp>::value &&
3859 !is_array<_Yp>::value &&
3860 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3861 __nat
3862 >::type = __nat());
3863 template <class _Yp, class _Dp>
3864 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3865 typename enable_if
3866 <
3867 is_lvalue_reference<_Dp>::value &&
3868 !is_array<_Yp>::value &&
3869 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3870 __nat
3871 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003872#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00003873 template <class _Yp, class _Dp>
3874 shared_ptr(unique_ptr<_Yp, _Dp>,
3875 typename enable_if
3876 <
3877 !is_lvalue_reference<_Dp>::value &&
3878 !is_array<_Yp>::value &&
3879 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3880 __nat
3881 >::type = __nat());
3882 template <class _Yp, class _Dp>
3883 shared_ptr(unique_ptr<_Yp, _Dp>,
3884 typename enable_if
3885 <
3886 is_lvalue_reference<_Dp>::value &&
3887 !is_array<_Yp>::value &&
3888 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3889 __nat
3890 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003891#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003892
3893 ~shared_ptr();
3894
Howard Hinnant1694d232011-05-28 14:41:13 +00003895 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003896 template<class _Yp>
3897 typename enable_if
3898 <
3899 is_convertible<_Yp*, element_type*>::value,
3900 shared_ptr&
3901 >::type
3902 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003903#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003904 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003905 template<class _Yp>
3906 typename enable_if
3907 <
3908 is_convertible<_Yp*, element_type*>::value,
3909 shared_ptr<_Tp>&
3910 >::type
3911 operator=(shared_ptr<_Yp>&& __r);
3912 template<class _Yp>
3913 typename enable_if
3914 <
3915 !is_array<_Yp>::value &&
3916 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant37c4acf2013-09-13 23:56:00 +00003917 shared_ptr
3918 >::type&
Howard Hinnant57199402012-01-02 17:56:02 +00003919 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003920#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003921 template<class _Yp>
3922 typename enable_if
3923 <
3924 !is_array<_Yp>::value &&
3925 is_convertible<_Yp*, element_type*>::value,
3926 shared_ptr&
3927 >::type
3928 operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003929#endif
Howard Hinnant57199402012-01-02 17:56:02 +00003930 template <class _Yp, class _Dp>
3931 typename enable_if
3932 <
3933 !is_array<_Yp>::value &&
3934 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3935 shared_ptr&
3936 >::type
Howard Hinnant73d21a42010-09-04 23:28:19 +00003937#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003938 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003939#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003940 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003941#endif
3942
Howard Hinnant1694d232011-05-28 14:41:13 +00003943 void swap(shared_ptr& __r) _NOEXCEPT;
3944 void reset() _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 void
3950 >::type
3951 reset(_Yp* __p);
3952 template<class _Yp, class _Dp>
3953 typename enable_if
3954 <
3955 is_convertible<_Yp*, element_type*>::value,
3956 void
3957 >::type
3958 reset(_Yp* __p, _Dp __d);
3959 template<class _Yp, class _Dp, class _Alloc>
3960 typename enable_if
3961 <
3962 is_convertible<_Yp*, element_type*>::value,
3963 void
3964 >::type
3965 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003966
Howard Hinnant82894812010-09-22 16:48:34 +00003967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003968 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003969 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003970 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3971 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003972 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003973 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003974 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003975 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003976 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003977 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00003978 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant77861882012-02-21 21:46:43 +00003979 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant99968442011-11-29 18:15:50 +00003980 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00003981 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003982 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003983 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant99968442011-11-29 18:15:50 +00003984 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00003985 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003986 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003987 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003988 _LIBCPP_INLINE_VISIBILITY
3989 bool
3990 __owner_equivalent(const shared_ptr& __p) const
3991 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003992
Howard Hinnantd4444702010-08-11 17:04:31 +00003993#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003994 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00003995 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003996 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003997 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00003998#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003999
4000#ifndef _LIBCPP_HAS_NO_VARIADICS
4001
4002 template<class ..._Args>
4003 static
4004 shared_ptr<_Tp>
4005 make_shared(_Args&& ...__args);
4006
4007 template<class _Alloc, class ..._Args>
4008 static
4009 shared_ptr<_Tp>
4010 allocate_shared(const _Alloc& __a, _Args&& ...__args);
4011
4012#else // _LIBCPP_HAS_NO_VARIADICS
4013
4014 static shared_ptr<_Tp> make_shared();
4015
4016 template<class _A0>
4017 static shared_ptr<_Tp> make_shared(_A0&);
4018
4019 template<class _A0, class _A1>
4020 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
4021
4022 template<class _A0, class _A1, class _A2>
4023 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
4024
4025 template<class _Alloc>
4026 static shared_ptr<_Tp>
4027 allocate_shared(const _Alloc& __a);
4028
4029 template<class _Alloc, class _A0>
4030 static shared_ptr<_Tp>
4031 allocate_shared(const _Alloc& __a, _A0& __a0);
4032
4033 template<class _Alloc, class _A0, class _A1>
4034 static shared_ptr<_Tp>
4035 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
4036
4037 template<class _Alloc, class _A0, class _A1, class _A2>
4038 static shared_ptr<_Tp>
4039 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
4040
4041#endif // _LIBCPP_HAS_NO_VARIADICS
4042
4043private:
4044
4045 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00004046 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004047 void
Marshall Clowfc3a3ff2015-05-27 20:36:14 +00004048 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004049 {
4050 if (__e)
4051 __e->__weak_this_ = *this;
4052 }
4053
Howard Hinnant82894812010-09-22 16:48:34 +00004054 _LIBCPP_INLINE_VISIBILITY
Marshall Clow60784f62015-05-27 22:44:47 +00004055 void __enable_weak_this(const volatile void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004056
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004057 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
4058 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004059};
4060
4061template<class _Tp>
4062inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00004063_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004064shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004065 : __ptr_(0),
4066 __cntrl_(0)
4067{
4068}
4069
4070template<class _Tp>
4071inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00004072_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004073shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004074 : __ptr_(0),
4075 __cntrl_(0)
4076{
4077}
4078
4079template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004080template<class _Yp>
4081shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4082 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004083 : __ptr_(__p)
4084{
4085 unique_ptr<_Yp> __hold(__p);
4086 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4087 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
4088 __hold.release();
4089 __enable_weak_this(__p);
4090}
4091
4092template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004093template<class _Yp, class _Dp>
4094shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4095 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004096 : __ptr_(__p)
4097{
4098#ifndef _LIBCPP_NO_EXCEPTIONS
4099 try
4100 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004101#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004102 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4103 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
4104 __enable_weak_this(__p);
4105#ifndef _LIBCPP_NO_EXCEPTIONS
4106 }
4107 catch (...)
4108 {
4109 __d(__p);
4110 throw;
4111 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004112#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004113}
4114
4115template<class _Tp>
4116template<class _Dp>
4117shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4118 : __ptr_(0)
4119{
4120#ifndef _LIBCPP_NO_EXCEPTIONS
4121 try
4122 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004123#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004124 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4125 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4126#ifndef _LIBCPP_NO_EXCEPTIONS
4127 }
4128 catch (...)
4129 {
4130 __d(__p);
4131 throw;
4132 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004133#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004134}
4135
4136template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004137template<class _Yp, class _Dp, class _Alloc>
4138shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4139 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004140 : __ptr_(__p)
4141{
4142#ifndef _LIBCPP_NO_EXCEPTIONS
4143 try
4144 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004145#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004146 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004147 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004148 typedef __allocator_destructor<_A2> _D2;
4149 _A2 __a2(__a);
4150 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004151 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4152 _CntrlBlk(__p, __d, __a);
4153 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004154 __enable_weak_this(__p);
4155#ifndef _LIBCPP_NO_EXCEPTIONS
4156 }
4157 catch (...)
4158 {
4159 __d(__p);
4160 throw;
4161 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004162#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004163}
4164
4165template<class _Tp>
4166template<class _Dp, class _Alloc>
4167shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4168 : __ptr_(0)
4169{
4170#ifndef _LIBCPP_NO_EXCEPTIONS
4171 try
4172 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004173#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004174 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004175 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004176 typedef __allocator_destructor<_A2> _D2;
4177 _A2 __a2(__a);
4178 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004179 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4180 _CntrlBlk(__p, __d, __a);
4181 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004182#ifndef _LIBCPP_NO_EXCEPTIONS
4183 }
4184 catch (...)
4185 {
4186 __d(__p);
4187 throw;
4188 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004189#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004190}
4191
4192template<class _Tp>
4193template<class _Yp>
4194inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004195shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004196 : __ptr_(__p),
4197 __cntrl_(__r.__cntrl_)
4198{
4199 if (__cntrl_)
4200 __cntrl_->__add_shared();
4201}
4202
4203template<class _Tp>
4204inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004205shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004206 : __ptr_(__r.__ptr_),
4207 __cntrl_(__r.__cntrl_)
4208{
4209 if (__cntrl_)
4210 __cntrl_->__add_shared();
4211}
4212
4213template<class _Tp>
4214template<class _Yp>
4215inline _LIBCPP_INLINE_VISIBILITY
4216shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4217 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004218 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004219 : __ptr_(__r.__ptr_),
4220 __cntrl_(__r.__cntrl_)
4221{
4222 if (__cntrl_)
4223 __cntrl_->__add_shared();
4224}
4225
Howard Hinnant73d21a42010-09-04 23:28:19 +00004226#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004227
4228template<class _Tp>
4229inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004230shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004231 : __ptr_(__r.__ptr_),
4232 __cntrl_(__r.__cntrl_)
4233{
4234 __r.__ptr_ = 0;
4235 __r.__cntrl_ = 0;
4236}
4237
4238template<class _Tp>
4239template<class _Yp>
4240inline _LIBCPP_INLINE_VISIBILITY
4241shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4242 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004243 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004244 : __ptr_(__r.__ptr_),
4245 __cntrl_(__r.__cntrl_)
4246{
4247 __r.__ptr_ = 0;
4248 __r.__cntrl_ = 0;
4249}
4250
Howard Hinnant73d21a42010-09-04 23:28:19 +00004251#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004252
4253template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004254template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004255#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiene1678a12014-01-31 09:30:46 +00004256shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004257#else
Logan Chiene1678a12014-01-31 09:30:46 +00004258shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004259#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004260 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004261 : __ptr_(__r.get())
4262{
4263 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4264 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
4265 __enable_weak_this(__r.get());
4266 __r.release();
4267}
4268
4269template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004270template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004271#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004272shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4273#else
4274shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4275#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004276 typename enable_if
4277 <
4278 !is_lvalue_reference<_Dp>::value &&
4279 !is_array<_Yp>::value &&
4280 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4281 __nat
4282 >::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004283 : __ptr_(__r.get())
4284{
Marshall Clow0ad232a2015-05-10 13:59:45 +00004285#if _LIBCPP_STD_VER > 11
4286 if (__ptr_ == nullptr)
4287 __cntrl_ = nullptr;
4288 else
4289#endif
4290 {
4291 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4292 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
4293 __enable_weak_this(__r.get());
4294 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004295 __r.release();
4296}
4297
4298template<class _Tp>
Logan Chiene1678a12014-01-31 09:30:46 +00004299template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004300#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004301shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4302#else
4303shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4304#endif
Logan Chiene1678a12014-01-31 09:30:46 +00004305 typename enable_if
4306 <
4307 is_lvalue_reference<_Dp>::value &&
4308 !is_array<_Yp>::value &&
4309 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4310 __nat
4311 >::type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004312 : __ptr_(__r.get())
4313{
Marshall Clow0ad232a2015-05-10 13:59:45 +00004314#if _LIBCPP_STD_VER > 11
4315 if (__ptr_ == nullptr)
4316 __cntrl_ = nullptr;
4317 else
4318#endif
4319 {
4320 typedef __shared_ptr_pointer<_Yp*,
4321 reference_wrapper<typename remove_reference<_Dp>::type>,
4322 allocator<_Yp> > _CntrlBlk;
4323 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
4324 __enable_weak_this(__r.get());
4325 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004326 __r.release();
4327}
4328
4329#ifndef _LIBCPP_HAS_NO_VARIADICS
4330
4331template<class _Tp>
4332template<class ..._Args>
4333shared_ptr<_Tp>
4334shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4335{
4336 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4337 typedef allocator<_CntrlBlk> _A2;
4338 typedef __allocator_destructor<_A2> _D2;
4339 _A2 __a2;
4340 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004341 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004342 shared_ptr<_Tp> __r;
4343 __r.__ptr_ = __hold2.get()->get();
4344 __r.__cntrl_ = __hold2.release();
4345 __r.__enable_weak_this(__r.__ptr_);
4346 return __r;
4347}
4348
4349template<class _Tp>
4350template<class _Alloc, class ..._Args>
4351shared_ptr<_Tp>
4352shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4353{
4354 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004355 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004356 typedef __allocator_destructor<_A2> _D2;
4357 _A2 __a2(__a);
4358 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004359 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4360 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004361 shared_ptr<_Tp> __r;
4362 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004363 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004364 __r.__enable_weak_this(__r.__ptr_);
4365 return __r;
4366}
4367
4368#else // _LIBCPP_HAS_NO_VARIADICS
4369
4370template<class _Tp>
4371shared_ptr<_Tp>
4372shared_ptr<_Tp>::make_shared()
4373{
4374 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4375 typedef allocator<_CntrlBlk> _Alloc2;
4376 typedef __allocator_destructor<_Alloc2> _D2;
4377 _Alloc2 __alloc2;
4378 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4379 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4380 shared_ptr<_Tp> __r;
4381 __r.__ptr_ = __hold2.get()->get();
4382 __r.__cntrl_ = __hold2.release();
4383 __r.__enable_weak_this(__r.__ptr_);
4384 return __r;
4385}
4386
4387template<class _Tp>
4388template<class _A0>
4389shared_ptr<_Tp>
4390shared_ptr<_Tp>::make_shared(_A0& __a0)
4391{
4392 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4393 typedef allocator<_CntrlBlk> _Alloc2;
4394 typedef __allocator_destructor<_Alloc2> _D2;
4395 _Alloc2 __alloc2;
4396 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4397 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4398 shared_ptr<_Tp> __r;
4399 __r.__ptr_ = __hold2.get()->get();
4400 __r.__cntrl_ = __hold2.release();
4401 __r.__enable_weak_this(__r.__ptr_);
4402 return __r;
4403}
4404
4405template<class _Tp>
4406template<class _A0, class _A1>
4407shared_ptr<_Tp>
4408shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4409{
4410 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4411 typedef allocator<_CntrlBlk> _Alloc2;
4412 typedef __allocator_destructor<_Alloc2> _D2;
4413 _Alloc2 __alloc2;
4414 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4415 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4416 shared_ptr<_Tp> __r;
4417 __r.__ptr_ = __hold2.get()->get();
4418 __r.__cntrl_ = __hold2.release();
4419 __r.__enable_weak_this(__r.__ptr_);
4420 return __r;
4421}
4422
4423template<class _Tp>
4424template<class _A0, class _A1, class _A2>
4425shared_ptr<_Tp>
4426shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4427{
4428 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4429 typedef allocator<_CntrlBlk> _Alloc2;
4430 typedef __allocator_destructor<_Alloc2> _D2;
4431 _Alloc2 __alloc2;
4432 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4433 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4434 shared_ptr<_Tp> __r;
4435 __r.__ptr_ = __hold2.get()->get();
4436 __r.__cntrl_ = __hold2.release();
4437 __r.__enable_weak_this(__r.__ptr_);
4438 return __r;
4439}
4440
4441template<class _Tp>
4442template<class _Alloc>
4443shared_ptr<_Tp>
4444shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4445{
4446 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004447 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004448 typedef __allocator_destructor<_Alloc2> _D2;
4449 _Alloc2 __alloc2(__a);
4450 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004451 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4452 _CntrlBlk(__a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004453 shared_ptr<_Tp> __r;
4454 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004455 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004456 __r.__enable_weak_this(__r.__ptr_);
4457 return __r;
4458}
4459
4460template<class _Tp>
4461template<class _Alloc, class _A0>
4462shared_ptr<_Tp>
4463shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4464{
4465 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004466 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004467 typedef __allocator_destructor<_Alloc2> _D2;
4468 _Alloc2 __alloc2(__a);
4469 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004470 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4471 _CntrlBlk(__a, __a0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004472 shared_ptr<_Tp> __r;
4473 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004474 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004475 __r.__enable_weak_this(__r.__ptr_);
4476 return __r;
4477}
4478
4479template<class _Tp>
4480template<class _Alloc, class _A0, class _A1>
4481shared_ptr<_Tp>
4482shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4483{
4484 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004485 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004486 typedef __allocator_destructor<_Alloc2> _D2;
4487 _Alloc2 __alloc2(__a);
4488 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004489 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4490 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004491 shared_ptr<_Tp> __r;
4492 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004493 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004494 __r.__enable_weak_this(__r.__ptr_);
4495 return __r;
4496}
4497
4498template<class _Tp>
4499template<class _Alloc, class _A0, class _A1, class _A2>
4500shared_ptr<_Tp>
4501shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4502{
4503 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004504 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004505 typedef __allocator_destructor<_Alloc2> _D2;
4506 _Alloc2 __alloc2(__a);
4507 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004508 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4509 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004510 shared_ptr<_Tp> __r;
4511 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier4e7d5362014-10-23 04:12:28 +00004512 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004513 __r.__enable_weak_this(__r.__ptr_);
4514 return __r;
4515}
4516
4517#endif // _LIBCPP_HAS_NO_VARIADICS
4518
4519template<class _Tp>
4520shared_ptr<_Tp>::~shared_ptr()
4521{
4522 if (__cntrl_)
4523 __cntrl_->__release_shared();
4524}
4525
4526template<class _Tp>
4527inline _LIBCPP_INLINE_VISIBILITY
4528shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004529shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004530{
4531 shared_ptr(__r).swap(*this);
4532 return *this;
4533}
4534
4535template<class _Tp>
4536template<class _Yp>
4537inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004538typename enable_if
4539<
4540 is_convertible<_Yp*, _Tp*>::value,
4541 shared_ptr<_Tp>&
4542>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004543shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004544{
4545 shared_ptr(__r).swap(*this);
4546 return *this;
4547}
4548
Howard Hinnant73d21a42010-09-04 23:28:19 +00004549#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004550
4551template<class _Tp>
4552inline _LIBCPP_INLINE_VISIBILITY
4553shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004554shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004555{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004556 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004557 return *this;
4558}
4559
4560template<class _Tp>
4561template<class _Yp>
4562inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004563typename enable_if
4564<
4565 is_convertible<_Yp*, _Tp*>::value,
4566 shared_ptr<_Tp>&
4567>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004568shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4569{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004570 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004571 return *this;
4572}
4573
4574template<class _Tp>
4575template<class _Yp>
4576inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004577typename enable_if
4578<
4579 !is_array<_Yp>::value &&
4580 is_convertible<_Yp*, _Tp*>::value,
Howard Hinnant37c4acf2013-09-13 23:56:00 +00004581 shared_ptr<_Tp>
4582>::type&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004583shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4584{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004585 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004586 return *this;
4587}
4588
4589template<class _Tp>
4590template <class _Yp, class _Dp>
4591inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004592typename enable_if
4593<
4594 !is_array<_Yp>::value &&
4595 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4596 shared_ptr<_Tp>&
4597>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004598shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4599{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004600 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004601 return *this;
4602}
4603
Howard Hinnant73d21a42010-09-04 23:28:19 +00004604#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004605
4606template<class _Tp>
4607template<class _Yp>
4608inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004609typename enable_if
4610<
4611 !is_array<_Yp>::value &&
4612 is_convertible<_Yp*, _Tp*>::value,
4613 shared_ptr<_Tp>&
4614>::type
Howard Hinnant324bb032010-08-22 00:02:43 +00004615shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004616{
4617 shared_ptr(__r).swap(*this);
4618 return *this;
4619}
4620
4621template<class _Tp>
4622template <class _Yp, class _Dp>
4623inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004624typename enable_if
4625<
4626 !is_array<_Yp>::value &&
4627 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4628 shared_ptr<_Tp>&
4629>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004630shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4631{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004632 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004633 return *this;
4634}
4635
Howard Hinnant73d21a42010-09-04 23:28:19 +00004636#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004637
4638template<class _Tp>
4639inline _LIBCPP_INLINE_VISIBILITY
4640void
Howard Hinnant1694d232011-05-28 14:41:13 +00004641shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004642{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004643 _VSTD::swap(__ptr_, __r.__ptr_);
4644 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004645}
4646
4647template<class _Tp>
4648inline _LIBCPP_INLINE_VISIBILITY
4649void
Howard Hinnant1694d232011-05-28 14:41:13 +00004650shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004651{
4652 shared_ptr().swap(*this);
4653}
4654
4655template<class _Tp>
4656template<class _Yp>
4657inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004658typename enable_if
4659<
4660 is_convertible<_Yp*, _Tp*>::value,
4661 void
4662>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004663shared_ptr<_Tp>::reset(_Yp* __p)
4664{
4665 shared_ptr(__p).swap(*this);
4666}
4667
4668template<class _Tp>
4669template<class _Yp, class _Dp>
4670inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004671typename enable_if
4672<
4673 is_convertible<_Yp*, _Tp*>::value,
4674 void
4675>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004676shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4677{
4678 shared_ptr(__p, __d).swap(*this);
4679}
4680
4681template<class _Tp>
4682template<class _Yp, class _Dp, class _Alloc>
4683inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004684typename enable_if
4685<
4686 is_convertible<_Yp*, _Tp*>::value,
4687 void
4688>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004689shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4690{
4691 shared_ptr(__p, __d, __a).swap(*this);
4692}
4693
4694#ifndef _LIBCPP_HAS_NO_VARIADICS
4695
Howard Hinnant324bb032010-08-22 00:02:43 +00004696template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004697inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004698typename enable_if
4699<
4700 !is_array<_Tp>::value,
4701 shared_ptr<_Tp>
4702>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004703make_shared(_Args&& ...__args)
4704{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004705 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004706}
4707
Howard Hinnant324bb032010-08-22 00:02:43 +00004708template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004709inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004710typename enable_if
4711<
4712 !is_array<_Tp>::value,
4713 shared_ptr<_Tp>
4714>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004715allocate_shared(const _Alloc& __a, _Args&& ...__args)
4716{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004717 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004718}
4719
4720#else // _LIBCPP_HAS_NO_VARIADICS
4721
4722template<class _Tp>
4723inline _LIBCPP_INLINE_VISIBILITY
4724shared_ptr<_Tp>
4725make_shared()
4726{
4727 return shared_ptr<_Tp>::make_shared();
4728}
4729
4730template<class _Tp, class _A0>
4731inline _LIBCPP_INLINE_VISIBILITY
4732shared_ptr<_Tp>
4733make_shared(_A0& __a0)
4734{
4735 return shared_ptr<_Tp>::make_shared(__a0);
4736}
4737
4738template<class _Tp, class _A0, class _A1>
4739inline _LIBCPP_INLINE_VISIBILITY
4740shared_ptr<_Tp>
4741make_shared(_A0& __a0, _A1& __a1)
4742{
4743 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4744}
4745
Howard Hinnant324bb032010-08-22 00:02:43 +00004746template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004747inline _LIBCPP_INLINE_VISIBILITY
4748shared_ptr<_Tp>
4749make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4750{
4751 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4752}
4753
4754template<class _Tp, class _Alloc>
4755inline _LIBCPP_INLINE_VISIBILITY
4756shared_ptr<_Tp>
4757allocate_shared(const _Alloc& __a)
4758{
4759 return shared_ptr<_Tp>::allocate_shared(__a);
4760}
4761
4762template<class _Tp, class _Alloc, class _A0>
4763inline _LIBCPP_INLINE_VISIBILITY
4764shared_ptr<_Tp>
4765allocate_shared(const _Alloc& __a, _A0& __a0)
4766{
4767 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4768}
4769
4770template<class _Tp, class _Alloc, class _A0, class _A1>
4771inline _LIBCPP_INLINE_VISIBILITY
4772shared_ptr<_Tp>
4773allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4774{
4775 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4776}
4777
4778template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4779inline _LIBCPP_INLINE_VISIBILITY
4780shared_ptr<_Tp>
4781allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4782{
4783 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4784}
4785
4786#endif // _LIBCPP_HAS_NO_VARIADICS
4787
4788template<class _Tp, class _Up>
4789inline _LIBCPP_INLINE_VISIBILITY
4790bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004791operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004792{
4793 return __x.get() == __y.get();
4794}
4795
4796template<class _Tp, class _Up>
4797inline _LIBCPP_INLINE_VISIBILITY
4798bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004799operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004800{
4801 return !(__x == __y);
4802}
4803
4804template<class _Tp, class _Up>
4805inline _LIBCPP_INLINE_VISIBILITY
4806bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004807operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004808{
Eric Fiselier8492cd82015-02-05 23:01:40 +00004809 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4810 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnant3fadda32012-02-21 21:02:58 +00004811}
4812
4813template<class _Tp, class _Up>
4814inline _LIBCPP_INLINE_VISIBILITY
4815bool
4816operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4817{
4818 return __y < __x;
4819}
4820
4821template<class _Tp, class _Up>
4822inline _LIBCPP_INLINE_VISIBILITY
4823bool
4824operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4825{
4826 return !(__y < __x);
4827}
4828
4829template<class _Tp, class _Up>
4830inline _LIBCPP_INLINE_VISIBILITY
4831bool
4832operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4833{
4834 return !(__x < __y);
4835}
4836
4837template<class _Tp>
4838inline _LIBCPP_INLINE_VISIBILITY
4839bool
4840operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4841{
4842 return !__x;
4843}
4844
4845template<class _Tp>
4846inline _LIBCPP_INLINE_VISIBILITY
4847bool
4848operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4849{
4850 return !__x;
4851}
4852
4853template<class _Tp>
4854inline _LIBCPP_INLINE_VISIBILITY
4855bool
4856operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4857{
4858 return static_cast<bool>(__x);
4859}
4860
4861template<class _Tp>
4862inline _LIBCPP_INLINE_VISIBILITY
4863bool
4864operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4865{
4866 return static_cast<bool>(__x);
4867}
4868
4869template<class _Tp>
4870inline _LIBCPP_INLINE_VISIBILITY
4871bool
4872operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4873{
4874 return less<_Tp*>()(__x.get(), nullptr);
4875}
4876
4877template<class _Tp>
4878inline _LIBCPP_INLINE_VISIBILITY
4879bool
4880operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4881{
4882 return less<_Tp*>()(nullptr, __x.get());
4883}
4884
4885template<class _Tp>
4886inline _LIBCPP_INLINE_VISIBILITY
4887bool
4888operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4889{
4890 return nullptr < __x;
4891}
4892
4893template<class _Tp>
4894inline _LIBCPP_INLINE_VISIBILITY
4895bool
4896operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4897{
4898 return __x < nullptr;
4899}
4900
4901template<class _Tp>
4902inline _LIBCPP_INLINE_VISIBILITY
4903bool
4904operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4905{
4906 return !(nullptr < __x);
4907}
4908
4909template<class _Tp>
4910inline _LIBCPP_INLINE_VISIBILITY
4911bool
4912operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4913{
4914 return !(__x < nullptr);
4915}
4916
4917template<class _Tp>
4918inline _LIBCPP_INLINE_VISIBILITY
4919bool
4920operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4921{
4922 return !(__x < nullptr);
4923}
4924
4925template<class _Tp>
4926inline _LIBCPP_INLINE_VISIBILITY
4927bool
4928operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4929{
4930 return !(nullptr < __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004931}
4932
4933template<class _Tp>
4934inline _LIBCPP_INLINE_VISIBILITY
4935void
Howard Hinnant1694d232011-05-28 14:41:13 +00004936swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004937{
4938 __x.swap(__y);
4939}
4940
4941template<class _Tp, class _Up>
4942inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004943typename enable_if
4944<
4945 !is_array<_Tp>::value && !is_array<_Up>::value,
4946 shared_ptr<_Tp>
4947>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004948static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004949{
4950 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4951}
4952
4953template<class _Tp, class _Up>
4954inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004955typename enable_if
4956<
4957 !is_array<_Tp>::value && !is_array<_Up>::value,
4958 shared_ptr<_Tp>
4959>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004960dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004961{
4962 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4963 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4964}
4965
4966template<class _Tp, class _Up>
Howard Hinnant57199402012-01-02 17:56:02 +00004967typename enable_if
4968<
4969 is_array<_Tp>::value == is_array<_Up>::value,
4970 shared_ptr<_Tp>
4971>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004972const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004973{
Howard Hinnant57199402012-01-02 17:56:02 +00004974 typedef typename remove_extent<_Tp>::type _RTp;
4975 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004976}
4977
Howard Hinnantd4444702010-08-11 17:04:31 +00004978#ifndef _LIBCPP_NO_RTTI
4979
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004980template<class _Dp, class _Tp>
4981inline _LIBCPP_INLINE_VISIBILITY
4982_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00004983get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004984{
4985 return __p.template __get_deleter<_Dp>();
4986}
4987
Howard Hinnant324bb032010-08-22 00:02:43 +00004988#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00004989
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004990template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004991class _LIBCPP_TYPE_VIS_ONLY weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004992{
Howard Hinnant324bb032010-08-22 00:02:43 +00004993public:
4994 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004995private:
4996 element_type* __ptr_;
4997 __shared_weak_count* __cntrl_;
4998
Howard Hinnant324bb032010-08-22 00:02:43 +00004999public:
Howard Hinnant46e94932012-07-07 20:56:04 +00005000 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005001 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00005002 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5003 _NOEXCEPT;
5004 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005005 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00005006 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5007 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005008
Howard Hinnant57199402012-01-02 17:56:02 +00005009#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5010 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
5011 template<class _Yp> weak_ptr(weak_ptr<_Yp>&& __r,
5012 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5013 _NOEXCEPT;
5014#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00005015 ~weak_ptr();
5016
Howard Hinnant1694d232011-05-28 14:41:13 +00005017 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00005018 template<class _Yp>
5019 typename enable_if
5020 <
5021 is_convertible<_Yp*, element_type*>::value,
5022 weak_ptr&
5023 >::type
5024 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
5025
5026#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5027
5028 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
5029 template<class _Yp>
5030 typename enable_if
5031 <
5032 is_convertible<_Yp*, element_type*>::value,
5033 weak_ptr&
5034 >::type
5035 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
5036
5037#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5038
5039 template<class _Yp>
5040 typename enable_if
5041 <
5042 is_convertible<_Yp*, element_type*>::value,
5043 weak_ptr&
5044 >::type
5045 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005046
Howard Hinnant1694d232011-05-28 14:41:13 +00005047 void swap(weak_ptr& __r) _NOEXCEPT;
5048 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005049
Howard Hinnant82894812010-09-22 16:48:34 +00005050 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005051 long use_count() const _NOEXCEPT
5052 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00005053 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005054 bool expired() const _NOEXCEPT
5055 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5056 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00005057 template<class _Up>
5058 _LIBCPP_INLINE_VISIBILITY
5059 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005060 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00005061 template<class _Up>
5062 _LIBCPP_INLINE_VISIBILITY
5063 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005064 {return __cntrl_ < __r.__cntrl_;}
5065
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005066 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
5067 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005068};
5069
5070template<class _Tp>
5071inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00005072_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005073weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005074 : __ptr_(0),
5075 __cntrl_(0)
5076{
5077}
5078
5079template<class _Tp>
5080inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005081weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005082 : __ptr_(__r.__ptr_),
5083 __cntrl_(__r.__cntrl_)
5084{
5085 if (__cntrl_)
5086 __cntrl_->__add_weak();
5087}
5088
5089template<class _Tp>
5090template<class _Yp>
5091inline _LIBCPP_INLINE_VISIBILITY
5092weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005093 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005094 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005095 : __ptr_(__r.__ptr_),
5096 __cntrl_(__r.__cntrl_)
5097{
5098 if (__cntrl_)
5099 __cntrl_->__add_weak();
5100}
5101
5102template<class _Tp>
5103template<class _Yp>
5104inline _LIBCPP_INLINE_VISIBILITY
5105weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005106 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005107 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005108 : __ptr_(__r.__ptr_),
5109 __cntrl_(__r.__cntrl_)
5110{
5111 if (__cntrl_)
5112 __cntrl_->__add_weak();
5113}
5114
Howard Hinnant57199402012-01-02 17:56:02 +00005115#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5116
5117template<class _Tp>
5118inline _LIBCPP_INLINE_VISIBILITY
5119weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5120 : __ptr_(__r.__ptr_),
5121 __cntrl_(__r.__cntrl_)
5122{
5123 __r.__ptr_ = 0;
5124 __r.__cntrl_ = 0;
5125}
5126
5127template<class _Tp>
5128template<class _Yp>
5129inline _LIBCPP_INLINE_VISIBILITY
5130weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5131 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5132 _NOEXCEPT
5133 : __ptr_(__r.__ptr_),
5134 __cntrl_(__r.__cntrl_)
5135{
5136 __r.__ptr_ = 0;
5137 __r.__cntrl_ = 0;
5138}
5139
5140#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5141
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005142template<class _Tp>
5143weak_ptr<_Tp>::~weak_ptr()
5144{
5145 if (__cntrl_)
5146 __cntrl_->__release_weak();
5147}
5148
5149template<class _Tp>
5150inline _LIBCPP_INLINE_VISIBILITY
5151weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00005152weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005153{
5154 weak_ptr(__r).swap(*this);
5155 return *this;
5156}
5157
5158template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005159template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005160inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005161typename enable_if
5162<
5163 is_convertible<_Yp*, _Tp*>::value,
5164 weak_ptr<_Tp>&
5165>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005166weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005167{
5168 weak_ptr(__r).swap(*this);
5169 return *this;
5170}
5171
Howard Hinnant57199402012-01-02 17:56:02 +00005172#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5173
5174template<class _Tp>
5175inline _LIBCPP_INLINE_VISIBILITY
5176weak_ptr<_Tp>&
5177weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5178{
5179 weak_ptr(_VSTD::move(__r)).swap(*this);
5180 return *this;
5181}
5182
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005183template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005184template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005185inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005186typename enable_if
5187<
5188 is_convertible<_Yp*, _Tp*>::value,
5189 weak_ptr<_Tp>&
5190>::type
5191weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5192{
5193 weak_ptr(_VSTD::move(__r)).swap(*this);
5194 return *this;
5195}
5196
5197#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5198
5199template<class _Tp>
5200template<class _Yp>
5201inline _LIBCPP_INLINE_VISIBILITY
5202typename enable_if
5203<
5204 is_convertible<_Yp*, _Tp*>::value,
5205 weak_ptr<_Tp>&
5206>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005207weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005208{
5209 weak_ptr(__r).swap(*this);
5210 return *this;
5211}
5212
5213template<class _Tp>
5214inline _LIBCPP_INLINE_VISIBILITY
5215void
Howard Hinnant1694d232011-05-28 14:41:13 +00005216weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005217{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005218 _VSTD::swap(__ptr_, __r.__ptr_);
5219 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005220}
5221
5222template<class _Tp>
5223inline _LIBCPP_INLINE_VISIBILITY
5224void
Howard Hinnant1694d232011-05-28 14:41:13 +00005225swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005226{
5227 __x.swap(__y);
5228}
5229
5230template<class _Tp>
5231inline _LIBCPP_INLINE_VISIBILITY
5232void
Howard Hinnant1694d232011-05-28 14:41:13 +00005233weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005234{
5235 weak_ptr().swap(*this);
5236}
5237
5238template<class _Tp>
5239template<class _Yp>
5240shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5241 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
5242 : __ptr_(__r.__ptr_),
5243 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5244{
5245 if (__cntrl_ == 0)
5246#ifndef _LIBCPP_NO_EXCEPTIONS
5247 throw bad_weak_ptr();
5248#else
5249 assert(!"bad_weak_ptr");
5250#endif
5251}
5252
5253template<class _Tp>
5254shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00005255weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005256{
5257 shared_ptr<_Tp> __r;
5258 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5259 if (__r.__cntrl_)
5260 __r.__ptr_ = __ptr_;
5261 return __r;
5262}
5263
Howard Hinnant324bb032010-08-22 00:02:43 +00005264template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005265
5266template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005267struct _LIBCPP_TYPE_VIS_ONLY owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005268 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00005269{
5270 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005271 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005272 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5273 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005274 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005275 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5276 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005277 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005278 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5279 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00005280};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005281
5282template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005283struct _LIBCPP_TYPE_VIS_ONLY owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005284 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5285{
Howard Hinnant324bb032010-08-22 00:02:43 +00005286 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005288 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5289 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005290 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005291 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5292 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005293 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005294 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5295 {return __x.owner_before(__y);}
5296};
5297
5298template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005299class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005300{
5301 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00005302protected:
Howard Hinnant46e94932012-07-07 20:56:04 +00005303 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005304 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005305 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005306 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005307 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005308 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5309 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00005310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005311 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00005312public:
Howard Hinnant82894812010-09-22 16:48:34 +00005313 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005314 shared_ptr<_Tp> shared_from_this()
5315 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00005316 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005317 shared_ptr<_Tp const> shared_from_this() const
5318 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005319
5320 template <class _Up> friend class shared_ptr;
5321};
5322
Howard Hinnant21aefc32010-06-03 16:42:57 +00005323template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005324struct _LIBCPP_TYPE_VIS_ONLY hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00005325{
5326 typedef shared_ptr<_Tp> argument_type;
5327 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005328 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005329 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00005330 {
5331 return hash<_Tp*>()(__ptr.get());
5332 }
5333};
5334
Howard Hinnant99968442011-11-29 18:15:50 +00005335template<class _CharT, class _Traits, class _Yp>
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005336inline _LIBCPP_INLINE_VISIBILITY
5337basic_ostream<_CharT, _Traits>&
Howard Hinnant99968442011-11-29 18:15:50 +00005338operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005339
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +00005340#if __has_feature(cxx_atomic) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005341
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005342class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005343{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005344 void* __lx;
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005345public:
5346 void lock() _NOEXCEPT;
5347 void unlock() _NOEXCEPT;
5348
5349private:
5350 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5351 __sp_mut(const __sp_mut&);
5352 __sp_mut& operator=(const __sp_mut&);
5353
Howard Hinnant83eade62013-03-06 23:30:19 +00005354 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005355};
5356
Howard Hinnant83eade62013-03-06 23:30:19 +00005357_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005358
5359template <class _Tp>
5360inline _LIBCPP_INLINE_VISIBILITY
5361bool
5362atomic_is_lock_free(const shared_ptr<_Tp>*)
5363{
5364 return false;
5365}
5366
5367template <class _Tp>
5368shared_ptr<_Tp>
5369atomic_load(const shared_ptr<_Tp>* __p)
5370{
5371 __sp_mut& __m = __get_sp_mut(__p);
5372 __m.lock();
5373 shared_ptr<_Tp> __q = *__p;
5374 __m.unlock();
5375 return __q;
5376}
5377
5378template <class _Tp>
5379inline _LIBCPP_INLINE_VISIBILITY
5380shared_ptr<_Tp>
5381atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5382{
5383 return atomic_load(__p);
5384}
5385
5386template <class _Tp>
5387void
5388atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5389{
5390 __sp_mut& __m = __get_sp_mut(__p);
5391 __m.lock();
5392 __p->swap(__r);
5393 __m.unlock();
5394}
5395
5396template <class _Tp>
5397inline _LIBCPP_INLINE_VISIBILITY
5398void
5399atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5400{
5401 atomic_store(__p, __r);
5402}
5403
5404template <class _Tp>
5405shared_ptr<_Tp>
5406atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5407{
5408 __sp_mut& __m = __get_sp_mut(__p);
5409 __m.lock();
5410 __p->swap(__r);
5411 __m.unlock();
5412 return __r;
5413}
5414
5415template <class _Tp>
5416inline _LIBCPP_INLINE_VISIBILITY
5417shared_ptr<_Tp>
5418atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5419{
5420 return atomic_exchange(__p, __r);
5421}
5422
5423template <class _Tp>
5424bool
5425atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5426{
5427 __sp_mut& __m = __get_sp_mut(__p);
5428 __m.lock();
5429 if (__p->__owner_equivalent(*__v))
5430 {
5431 *__p = __w;
5432 __m.unlock();
5433 return true;
5434 }
5435 *__v = *__p;
5436 __m.unlock();
5437 return false;
5438}
5439
5440template <class _Tp>
5441inline _LIBCPP_INLINE_VISIBILITY
5442bool
5443atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5444{
5445 return atomic_compare_exchange_strong(__p, __v, __w);
5446}
5447
5448template <class _Tp>
5449inline _LIBCPP_INLINE_VISIBILITY
5450bool
5451atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5452 shared_ptr<_Tp> __w, memory_order, memory_order)
5453{
5454 return atomic_compare_exchange_strong(__p, __v, __w);
5455}
5456
5457template <class _Tp>
5458inline _LIBCPP_INLINE_VISIBILITY
5459bool
5460atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5461 shared_ptr<_Tp> __w, memory_order, memory_order)
5462{
5463 return atomic_compare_exchange_weak(__p, __v, __w);
5464}
5465
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +00005466#endif // __has_feature(cxx_atomic) && !defined(_LIBCPP_HAS_NO_THREADS)
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005467
Howard Hinnant324bb032010-08-22 00:02:43 +00005468//enum class
Howard Hinnant83eade62013-03-06 23:30:19 +00005469struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005470{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005471 enum __lx
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005472 {
5473 relaxed,
5474 preferred,
5475 strict
5476 };
5477
Howard Hinnant9c0df142012-10-30 19:06:59 +00005478 __lx __v_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005479
Howard Hinnant82894812010-09-22 16:48:34 +00005480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9c0df142012-10-30 19:06:59 +00005481 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00005482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005483 operator int() const {return __v_;}
5484};
5485
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005486_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5487_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5488_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
5489_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5490_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005491
5492template <class _Tp>
5493inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005494_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005495undeclare_reachable(_Tp* __p)
5496{
5497 return static_cast<_Tp*>(__undeclare_reachable(__p));
5498}
5499
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005500_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005501
5502_LIBCPP_END_NAMESPACE_STD
5503
5504#endif // _LIBCPP_MEMORY