blob: b382f70b02f02e3f8499cf3847bc5b602c374ce4 [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;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000482
483 // destructor
484 ~weak_ptr();
485
486 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000487 weak_ptr& operator=(weak_ptr const& r) noexcept;
488 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
489 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000490
491 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000492 void swap(weak_ptr& r) noexcept;
493 void reset() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000494
495 // observers
Howard Hinnant1694d232011-05-28 14:41:13 +0000496 long use_count() const noexcept;
497 bool expired() const noexcept;
498 shared_ptr<T> lock() const noexcept;
Marshall Clow1b5f3ad2013-09-03 14:37:50 +0000499 template<class U> bool owner_before(shared_ptr<U> const& b) const;
500 template<class U> bool owner_before(weak_ptr<U> const& b) const;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000501};
502
503// weak_ptr specialized algorithms:
Howard Hinnant1694d232011-05-28 14:41:13 +0000504template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000505
506// class owner_less:
507template<class T> struct owner_less;
508
509template<class T>
510struct owner_less<shared_ptr<T>>
511 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
512{
513 typedef bool result_type;
514 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const;
515 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
516 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
517};
518
519template<class T>
520struct owner_less<weak_ptr<T>>
521 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
522{
523 typedef bool result_type;
524 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const;
525 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
526 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
527};
528
529template<class T>
530class enable_shared_from_this
531{
532protected:
Howard Hinnant1694d232011-05-28 14:41:13 +0000533 constexpr enable_shared_from_this() noexcept;
534 enable_shared_from_this(enable_shared_from_this const&) noexcept;
535 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000536 ~enable_shared_from_this();
537public:
538 shared_ptr<T> shared_from_this();
539 shared_ptr<T const> shared_from_this() const;
540};
541
542template<class T>
543 bool atomic_is_lock_free(const shared_ptr<T>* p);
544template<class T>
545 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
546template<class T>
547 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
548template<class T>
549 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
550template<class T>
551 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
552template<class T>
553 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
554template<class T>
555 shared_ptr<T>
556 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
557template<class T>
558 bool
559 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
560template<class T>
561 bool
562 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
563template<class T>
564 bool
565 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
566 shared_ptr<T> w, memory_order success,
567 memory_order failure);
568template<class T>
569 bool
570 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
571 shared_ptr<T> w, memory_order success,
572 memory_order failure);
573// Hash support
574template <class T> struct hash;
575template <class T, class D> struct hash<unique_ptr<T, D> >;
576template <class T> struct hash<shared_ptr<T> >;
577
578// Pointer safety
579enum class pointer_safety { relaxed, preferred, strict };
580void declare_reachable(void *p);
581template <class T> T *undeclare_reachable(T *p);
582void declare_no_pointers(char *p, size_t n);
583void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant1694d232011-05-28 14:41:13 +0000584pointer_safety get_pointer_safety() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000585
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000586void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
587
588} // std
589
590*/
591
592#include <__config>
593#include <type_traits>
594#include <typeinfo>
595#include <cstddef>
596#include <cstdint>
597#include <new>
598#include <utility>
599#include <limits>
600#include <iterator>
601#include <__functional_base>
Howard Hinnant464aa5c2011-07-18 15:51:59 +0000602#include <iosfwd>
Howard Hinnant6cc99fa2011-12-19 17:58:44 +0000603#include <tuple>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000604#include <cstring>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000605#if defined(_LIBCPP_NO_EXCEPTIONS)
606 #include <cassert>
607#endif
608
Howard Hinnant5fec82d2012-07-30 01:40:57 +0000609#if __has_feature(cxx_atomic)
610# include <atomic>
611#endif
612
Howard Hinnant66c6f972011-11-29 16:45:27 +0000613#include <__undef_min_max>
614
Howard Hinnant08e17472011-10-17 20:05:10 +0000615#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000616#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000617#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000618
619_LIBCPP_BEGIN_NAMESPACE_STD
620
Howard Hinnanta4e87ab2013-08-08 18:38:55 +0000621// addressof moved to <__functional_base>
Douglas Gregor35d2fcf2011-06-22 22:17:44 +0000622
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000623template <class _Tp> class allocator;
624
625template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000626class _LIBCPP_TYPE_VIS_ONLY allocator<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000627{
628public:
629 typedef void* pointer;
630 typedef const void* const_pointer;
631 typedef void value_type;
632
633 template <class _Up> struct rebind {typedef allocator<_Up> other;};
634};
635
Howard Hinnanta1877872012-01-19 23:15:22 +0000636template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000637class _LIBCPP_TYPE_VIS_ONLY allocator<const void>
Howard Hinnanta1877872012-01-19 23:15:22 +0000638{
639public:
640 typedef const void* pointer;
641 typedef const void* const_pointer;
642 typedef const void value_type;
643
644 template <class _Up> struct rebind {typedef allocator<_Up> other;};
645};
646
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000647// pointer_traits
648
649template <class _Tp>
650struct __has_element_type
651{
652private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000653 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000654 template <class _Up> static __two __test(...);
655 template <class _Up> static char __test(typename _Up::element_type* = 0);
656public:
657 static const bool value = sizeof(__test<_Tp>(0)) == 1;
658};
659
660template <class _Ptr, bool = __has_element_type<_Ptr>::value>
661struct __pointer_traits_element_type;
662
663template <class _Ptr>
664struct __pointer_traits_element_type<_Ptr, true>
665{
666 typedef typename _Ptr::element_type type;
667};
668
669#ifndef _LIBCPP_HAS_NO_VARIADICS
670
671template <template <class, class...> class _Sp, class _Tp, class ..._Args>
672struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
673{
674 typedef typename _Sp<_Tp, _Args...>::element_type type;
675};
676
677template <template <class, class...> class _Sp, class _Tp, class ..._Args>
678struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
679{
680 typedef _Tp type;
681};
682
Howard Hinnant324bb032010-08-22 00:02:43 +0000683#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000684
685template <template <class> class _Sp, class _Tp>
686struct __pointer_traits_element_type<_Sp<_Tp>, true>
687{
688 typedef typename _Sp<_Tp>::element_type type;
689};
690
691template <template <class> class _Sp, class _Tp>
692struct __pointer_traits_element_type<_Sp<_Tp>, false>
693{
694 typedef _Tp type;
695};
696
697template <template <class, class> class _Sp, class _Tp, class _A0>
698struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
699{
700 typedef typename _Sp<_Tp, _A0>::element_type type;
701};
702
703template <template <class, class> class _Sp, class _Tp, class _A0>
704struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
705{
706 typedef _Tp type;
707};
708
709template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
710struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
711{
712 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
713};
714
715template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
716struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
717{
718 typedef _Tp type;
719};
720
721template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
722 class _A1, class _A2>
723struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
724{
725 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type 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>, false>
731{
732 typedef _Tp type;
733};
734
Howard Hinnant324bb032010-08-22 00:02:43 +0000735#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000736
737template <class _Tp>
738struct __has_difference_type
739{
740private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000741 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000742 template <class _Up> static __two __test(...);
743 template <class _Up> static char __test(typename _Up::difference_type* = 0);
744public:
745 static const bool value = sizeof(__test<_Tp>(0)) == 1;
746};
747
748template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
749struct __pointer_traits_difference_type
750{
751 typedef ptrdiff_t type;
752};
753
754template <class _Ptr>
755struct __pointer_traits_difference_type<_Ptr, true>
756{
757 typedef typename _Ptr::difference_type type;
758};
759
760template <class _Tp, class _Up>
761struct __has_rebind
762{
763private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000764 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000765 template <class _Xp> static __two __test(...);
766 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
767public:
768 static const bool value = sizeof(__test<_Tp>(0)) == 1;
769};
770
771template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
772struct __pointer_traits_rebind
773{
774#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
775 typedef typename _Tp::template rebind<_Up> type;
776#else
777 typedef typename _Tp::template rebind<_Up>::other type;
778#endif
779};
780
781#ifndef _LIBCPP_HAS_NO_VARIADICS
782
783template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
784struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
785{
786#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
787 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
788#else
789 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
790#endif
791};
792
793template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
794struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
795{
796 typedef _Sp<_Up, _Args...> type;
797};
798
Howard Hinnant324bb032010-08-22 00:02:43 +0000799#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000800
801template <template <class> class _Sp, class _Tp, class _Up>
802struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
803{
804#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
805 typedef typename _Sp<_Tp>::template rebind<_Up> type;
806#else
807 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
808#endif
809};
810
811template <template <class> class _Sp, class _Tp, class _Up>
812struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
813{
814 typedef _Sp<_Up> type;
815};
816
817template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
818struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
819{
820#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
821 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
822#else
823 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
824#endif
825};
826
827template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
828struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
829{
830 typedef _Sp<_Up, _A0> type;
831};
832
833template <template <class, class, class> class _Sp, class _Tp, class _A0,
834 class _A1, class _Up>
835struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
836{
837#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
838 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
839#else
840 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
841#endif
842};
843
844template <template <class, class, class> class _Sp, class _Tp, class _A0,
845 class _A1, class _Up>
846struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
847{
848 typedef _Sp<_Up, _A0, _A1> type;
849};
850
851template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
852 class _A1, class _A2, class _Up>
853struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
854{
855#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
856 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
857#else
858 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
859#endif
860};
861
862template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
863 class _A1, class _A2, class _Up>
864struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
865{
866 typedef _Sp<_Up, _A0, _A1, _A2> type;
867};
868
Howard Hinnant324bb032010-08-22 00:02:43 +0000869#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000870
871template <class _Ptr>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000872struct _LIBCPP_TYPE_VIS_ONLY pointer_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000873{
874 typedef _Ptr pointer;
875 typedef typename __pointer_traits_element_type<pointer>::type element_type;
876 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
877
878#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant6b41c602011-05-11 20:21:19 +0000879 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000880#else
881 template <class _Up> struct rebind
882 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant324bb032010-08-22 00:02:43 +0000883#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000884
885private:
886 struct __nat {};
887public:
Howard Hinnant82894812010-09-22 16:48:34 +0000888 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000889 static pointer pointer_to(typename conditional<is_void<element_type>::value,
890 __nat, element_type>::type& __r)
891 {return pointer::pointer_to(__r);}
892};
893
894template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000895struct _LIBCPP_TYPE_VIS_ONLY pointer_traits<_Tp*>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000896{
897 typedef _Tp* pointer;
898 typedef _Tp element_type;
899 typedef ptrdiff_t difference_type;
900
901#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
902 template <class _Up> using rebind = _Up*;
903#else
904 template <class _Up> struct rebind {typedef _Up* other;};
905#endif
906
907private:
908 struct __nat {};
909public:
Howard Hinnant82894812010-09-22 16:48:34 +0000910 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000911 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant1694d232011-05-28 14:41:13 +0000912 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000913 {return _VSTD::addressof(__r);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000914};
915
916// allocator_traits
917
918namespace __has_pointer_type_imp
919{
Howard Hinnant81277582013-09-21 01:45:05 +0000920 template <class _Up> static __two __test(...);
921 template <class _Up> static char __test(typename _Up::pointer* = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000922}
923
924template <class _Tp>
925struct __has_pointer_type
Howard Hinnant81277582013-09-21 01:45:05 +0000926 : public integral_constant<bool, sizeof(__has_pointer_type_imp::__test<_Tp>(0)) == 1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000927{
928};
929
930namespace __pointer_type_imp
931{
932
933template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
934struct __pointer_type
935{
936 typedef typename _Dp::pointer type;
937};
938
939template <class _Tp, class _Dp>
940struct __pointer_type<_Tp, _Dp, false>
941{
942 typedef _Tp* type;
943};
944
Howard Hinnant47761072010-11-18 01:40:00 +0000945} // __pointer_type_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000946
947template <class _Tp, class _Dp>
948struct __pointer_type
949{
950 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
951};
952
953template <class _Tp>
954struct __has_const_pointer
955{
956private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000957 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000958 template <class _Up> static __two __test(...);
959 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
960public:
961 static const bool value = sizeof(__test<_Tp>(0)) == 1;
962};
963
964template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
965struct __const_pointer
966{
967 typedef typename _Alloc::const_pointer type;
968};
969
970template <class _Tp, class _Ptr, class _Alloc>
971struct __const_pointer<_Tp, _Ptr, _Alloc, false>
972{
973#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
974 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
975#else
976 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
977#endif
978};
979
980template <class _Tp>
981struct __has_void_pointer
982{
983private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000984 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000985 template <class _Up> static __two __test(...);
986 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
987public:
988 static const bool value = sizeof(__test<_Tp>(0)) == 1;
989};
990
991template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
992struct __void_pointer
993{
994 typedef typename _Alloc::void_pointer type;
995};
996
997template <class _Ptr, class _Alloc>
998struct __void_pointer<_Ptr, _Alloc, false>
999{
1000#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1001 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1002#else
1003 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1004#endif
1005};
1006
1007template <class _Tp>
1008struct __has_const_void_pointer
1009{
1010private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001011 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001012 template <class _Up> static __two __test(...);
1013 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1014public:
1015 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1016};
1017
1018template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1019struct __const_void_pointer
1020{
1021 typedef typename _Alloc::const_void_pointer type;
1022};
1023
1024template <class _Ptr, class _Alloc>
1025struct __const_void_pointer<_Ptr, _Alloc, false>
1026{
1027#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1028 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1029#else
1030 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1031#endif
1032};
1033
Howard Hinnant99968442011-11-29 18:15:50 +00001034template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001035inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001036_Tp*
1037__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001038{
1039 return __p;
1040}
1041
1042template <class _Pointer>
1043inline _LIBCPP_INLINE_VISIBILITY
1044typename pointer_traits<_Pointer>::element_type*
Howard Hinnant1694d232011-05-28 14:41:13 +00001045__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001046{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001047 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001048}
1049
1050template <class _Tp>
1051struct __has_size_type
1052{
1053private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001054 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001055 template <class _Up> static __two __test(...);
1056 template <class _Up> static char __test(typename _Up::size_type* = 0);
1057public:
1058 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1059};
1060
Howard Hinnant47761072010-11-18 01:40:00 +00001061template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001062struct __size_type
1063{
Howard Hinnant47761072010-11-18 01:40:00 +00001064 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001065};
1066
Howard Hinnant47761072010-11-18 01:40:00 +00001067template <class _Alloc, class _DiffType>
1068struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001069{
1070 typedef typename _Alloc::size_type type;
1071};
1072
1073template <class _Tp>
1074struct __has_propagate_on_container_copy_assignment
1075{
1076private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001077 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001078 template <class _Up> static __two __test(...);
1079 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1080public:
1081 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1082};
1083
1084template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1085struct __propagate_on_container_copy_assignment
1086{
1087 typedef false_type type;
1088};
1089
1090template <class _Alloc>
1091struct __propagate_on_container_copy_assignment<_Alloc, true>
1092{
1093 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1094};
1095
1096template <class _Tp>
1097struct __has_propagate_on_container_move_assignment
1098{
1099private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001100 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001101 template <class _Up> static __two __test(...);
1102 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1103public:
1104 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1105};
1106
1107template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1108struct __propagate_on_container_move_assignment
1109{
1110 typedef false_type type;
1111};
1112
1113template <class _Alloc>
1114struct __propagate_on_container_move_assignment<_Alloc, true>
1115{
1116 typedef typename _Alloc::propagate_on_container_move_assignment type;
1117};
1118
1119template <class _Tp>
1120struct __has_propagate_on_container_swap
1121{
1122private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001123 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001124 template <class _Up> static __two __test(...);
1125 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1126public:
1127 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1128};
1129
1130template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1131struct __propagate_on_container_swap
1132{
1133 typedef false_type type;
1134};
1135
1136template <class _Alloc>
1137struct __propagate_on_container_swap<_Alloc, true>
1138{
1139 typedef typename _Alloc::propagate_on_container_swap type;
1140};
1141
1142template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1143struct __has_rebind_other
1144{
1145private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001146 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001147 template <class _Xp> static __two __test(...);
1148 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1149public:
1150 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1151};
1152
1153template <class _Tp, class _Up>
1154struct __has_rebind_other<_Tp, _Up, false>
1155{
1156 static const bool value = false;
1157};
1158
1159template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1160struct __allocator_traits_rebind
1161{
1162 typedef typename _Tp::template rebind<_Up>::other type;
1163};
1164
1165#ifndef _LIBCPP_HAS_NO_VARIADICS
1166
1167template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1168struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1169{
1170 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1171};
1172
1173template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1174struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1175{
1176 typedef _Alloc<_Up, _Args...> type;
1177};
1178
Howard Hinnant324bb032010-08-22 00:02:43 +00001179#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001180
1181template <template <class> class _Alloc, class _Tp, class _Up>
1182struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1183{
1184 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1185};
1186
1187template <template <class> class _Alloc, class _Tp, class _Up>
1188struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1189{
1190 typedef _Alloc<_Up> type;
1191};
1192
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001193template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1194struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1195{
1196 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1197};
1198
1199template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1200struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1201{
1202 typedef _Alloc<_Up, _A0> type;
1203};
1204
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001205template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1206 class _A1, class _Up>
1207struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1208{
1209 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1210};
1211
1212template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1213 class _A1, class _Up>
1214struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1215{
1216 typedef _Alloc<_Up, _A0, _A1> type;
1217};
1218
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001219template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1220 class _A1, class _A2, class _Up>
1221struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1222{
1223 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1224};
1225
1226template <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, false>
1229{
1230 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1231};
1232
Howard Hinnant324bb032010-08-22 00:02:43 +00001233#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001234
1235#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1236
1237template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1238auto
1239__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1240 -> decltype(__a.allocate(__sz, __p), true_type());
1241
1242template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1243auto
1244__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1245 -> false_type;
1246
1247template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1248struct __has_allocate_hint
1249 : integral_constant<bool,
1250 is_same<
1251 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1252 declval<_SizeType>(),
1253 declval<_ConstVoidPtr>())),
1254 true_type>::value>
1255{
1256};
1257
Howard Hinnant324bb032010-08-22 00:02:43 +00001258#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001259
1260template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1261struct __has_allocate_hint
1262 : true_type
1263{
1264};
1265
Howard Hinnant324bb032010-08-22 00:02:43 +00001266#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001267
Howard Hinnant23369ee2011-07-29 21:35:53 +00001268#if !defined(_LIBCPP_HAS_NO_ADVANCED_SFINAE) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001269
1270template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnant0949eed2011-06-30 21:18:19 +00001271decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1272 _VSTD::declval<_Args>()...),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001273 true_type())
1274__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1275
1276template <class _Alloc, class _Pointer, class ..._Args>
1277false_type
1278__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1279
1280template <class _Alloc, class _Pointer, class ..._Args>
1281struct __has_construct
1282 : integral_constant<bool,
1283 is_same<
1284 decltype(__has_construct_test(declval<_Alloc>(),
1285 declval<_Pointer>(),
1286 declval<_Args>()...)),
1287 true_type>::value>
1288{
1289};
1290
1291template <class _Alloc, class _Pointer>
1292auto
1293__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1294 -> decltype(__a.destroy(__p), true_type());
1295
1296template <class _Alloc, class _Pointer>
1297auto
1298__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1299 -> false_type;
1300
1301template <class _Alloc, class _Pointer>
1302struct __has_destroy
1303 : integral_constant<bool,
1304 is_same<
1305 decltype(__has_destroy_test(declval<_Alloc>(),
1306 declval<_Pointer>())),
1307 true_type>::value>
1308{
1309};
1310
1311template <class _Alloc>
1312auto
1313__has_max_size_test(_Alloc&& __a)
1314 -> decltype(__a.max_size(), true_type());
1315
1316template <class _Alloc>
1317auto
1318__has_max_size_test(const volatile _Alloc& __a)
1319 -> false_type;
1320
1321template <class _Alloc>
1322struct __has_max_size
1323 : integral_constant<bool,
1324 is_same<
1325 decltype(__has_max_size_test(declval<_Alloc&>())),
1326 true_type>::value>
1327{
1328};
1329
1330template <class _Alloc>
1331auto
1332__has_select_on_container_copy_construction_test(_Alloc&& __a)
1333 -> decltype(__a.select_on_container_copy_construction(), true_type());
1334
1335template <class _Alloc>
1336auto
1337__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1338 -> false_type;
1339
1340template <class _Alloc>
1341struct __has_select_on_container_copy_construction
1342 : integral_constant<bool,
1343 is_same<
1344 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1345 true_type>::value>
1346{
1347};
1348
Howard Hinnant324bb032010-08-22 00:02:43 +00001349#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001350
1351#ifndef _LIBCPP_HAS_NO_VARIADICS
1352
1353template <class _Alloc, class _Pointer, class ..._Args>
1354struct __has_construct
1355 : false_type
1356{
1357};
1358
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001359#else // _LIBCPP_HAS_NO_VARIADICS
1360
1361template <class _Alloc, class _Pointer, class _Args>
1362struct __has_construct
1363 : false_type
1364{
1365};
1366
Howard Hinnant324bb032010-08-22 00:02:43 +00001367#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001368
1369template <class _Alloc, class _Pointer>
1370struct __has_destroy
1371 : false_type
1372{
1373};
1374
1375template <class _Alloc>
1376struct __has_max_size
1377 : true_type
1378{
1379};
1380
1381template <class _Alloc>
1382struct __has_select_on_container_copy_construction
1383 : false_type
1384{
1385};
1386
Howard Hinnant324bb032010-08-22 00:02:43 +00001387#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001388
Howard Hinnant47761072010-11-18 01:40:00 +00001389template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1390struct __alloc_traits_difference_type
1391{
1392 typedef typename pointer_traits<_Ptr>::difference_type type;
1393};
1394
1395template <class _Alloc, class _Ptr>
1396struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1397{
1398 typedef typename _Alloc::difference_type type;
1399};
1400
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001401template <class _Alloc>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001402struct _LIBCPP_TYPE_VIS_ONLY allocator_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001403{
1404 typedef _Alloc allocator_type;
1405 typedef typename allocator_type::value_type value_type;
1406
1407 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1408 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1409 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1410 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1411
Howard Hinnant47761072010-11-18 01:40:00 +00001412 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1413 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001414
1415 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1416 propagate_on_container_copy_assignment;
1417 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1418 propagate_on_container_move_assignment;
1419 typedef typename __propagate_on_container_swap<allocator_type>::type
1420 propagate_on_container_swap;
1421
1422#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1423 template <class _Tp> using rebind_alloc =
Howard Hinnant6b41c602011-05-11 20:21:19 +00001424 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001425 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant324bb032010-08-22 00:02:43 +00001426#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001427 template <class _Tp> struct rebind_alloc
1428 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1429 template <class _Tp> struct rebind_traits
1430 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant324bb032010-08-22 00:02:43 +00001431#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001432
Howard Hinnant82894812010-09-22 16:48:34 +00001433 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001434 static pointer allocate(allocator_type& __a, size_type __n)
1435 {return __a.allocate(__n);}
Howard Hinnant82894812010-09-22 16:48:34 +00001436 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001437 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1438 {return allocate(__a, __n, __hint,
1439 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1440
Howard Hinnant82894812010-09-22 16:48:34 +00001441 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001442 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001443 {__a.deallocate(__p, __n);}
1444
1445#ifndef _LIBCPP_HAS_NO_VARIADICS
1446 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001447 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001448 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
1449 {__construct(__has_construct<allocator_type, pointer, _Args...>(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001450 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001451#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001452 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001453 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001454 static void construct(allocator_type& __a, _Tp* __p)
1455 {
1456 ::new ((void*)__p) _Tp();
1457 }
1458 template <class _Tp, class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00001459 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001460 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1461 {
1462 ::new ((void*)__p) _Tp(__a0);
1463 }
1464 template <class _Tp, class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00001465 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001466 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1467 const _A1& __a1)
1468 {
1469 ::new ((void*)__p) _Tp(__a0, __a1);
1470 }
1471 template <class _Tp, class _A0, class _A1, class _A2>
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, const _A2& __a2)
1475 {
1476 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1477 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001478#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001479
1480 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001481 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001482 static void destroy(allocator_type& __a, _Tp* __p)
1483 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1484
Howard Hinnant82894812010-09-22 16:48:34 +00001485 _LIBCPP_INLINE_VISIBILITY
Marshall Clow08b4f3f2013-08-27 20:22:15 +00001486 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001487 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1488
Howard Hinnant82894812010-09-22 16:48:34 +00001489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001490 static allocator_type
1491 select_on_container_copy_construction(const allocator_type& __a)
1492 {return select_on_container_copy_construction(
1493 __has_select_on_container_copy_construction<const allocator_type>(),
1494 __a);}
1495
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001496 template <class _Ptr>
1497 _LIBCPP_INLINE_VISIBILITY
1498 static
1499 void
1500 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1501 {
1502 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1503 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1504 }
1505
1506 template <class _Tp>
1507 _LIBCPP_INLINE_VISIBILITY
1508 static
1509 typename enable_if
1510 <
1511 (is_same<allocator_type, allocator<_Tp> >::value
1512 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1513 is_trivially_move_constructible<_Tp>::value,
1514 void
1515 >::type
1516 __construct_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1517 {
1518 ptrdiff_t _Np = __end1 - __begin1;
1519 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
1520 __begin2 += _Np;
1521 }
1522
1523 template <class _Ptr>
1524 _LIBCPP_INLINE_VISIBILITY
1525 static
1526 void
1527 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1528 {
1529 while (__end1 != __begin1)
Howard Hinnantf619e232013-01-11 20:36:59 +00001530 {
1531 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1532 --__end2;
1533 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001534 }
1535
1536 template <class _Tp>
1537 _LIBCPP_INLINE_VISIBILITY
1538 static
1539 typename enable_if
1540 <
1541 (is_same<allocator_type, allocator<_Tp> >::value
1542 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1543 is_trivially_move_constructible<_Tp>::value,
1544 void
1545 >::type
1546 __construct_backward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
1547 {
1548 ptrdiff_t _Np = __end1 - __begin1;
1549 __end2 -= _Np;
1550 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
1551 }
1552
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001553private:
1554
Howard Hinnant82894812010-09-22 16:48:34 +00001555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001556 static pointer allocate(allocator_type& __a, size_type __n,
1557 const_void_pointer __hint, true_type)
1558 {return __a.allocate(__n, __hint);}
Howard Hinnant82894812010-09-22 16:48:34 +00001559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001560 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnantec3773c2011-12-01 20:21:04 +00001561 const_void_pointer, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001562 {return __a.allocate(__n);}
1563
1564#ifndef _LIBCPP_HAS_NO_VARIADICS
1565 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001566 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001567 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001568 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001569 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001570 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001571 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1572 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001573 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001574 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001575#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001576
1577 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001579 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1580 {__a.destroy(__p);}
1581 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001583 static void __destroy(false_type, allocator_type&, _Tp* __p)
1584 {
1585 __p->~_Tp();
1586 }
1587
Howard Hinnant82894812010-09-22 16:48:34 +00001588 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001589 static size_type __max_size(true_type, const allocator_type& __a)
1590 {return __a.max_size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001591 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001592 static size_type __max_size(false_type, const allocator_type&)
1593 {return numeric_limits<size_type>::max();}
1594
Howard Hinnant82894812010-09-22 16:48:34 +00001595 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001596 static allocator_type
1597 select_on_container_copy_construction(true_type, const allocator_type& __a)
1598 {return __a.select_on_container_copy_construction();}
Howard Hinnant82894812010-09-22 16:48:34 +00001599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001600 static allocator_type
1601 select_on_container_copy_construction(false_type, const allocator_type& __a)
1602 {return __a;}
1603};
1604
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001605// allocator
1606
1607template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001608class _LIBCPP_TYPE_VIS_ONLY allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001609{
1610public:
1611 typedef size_t size_type;
1612 typedef ptrdiff_t difference_type;
1613 typedef _Tp* pointer;
1614 typedef const _Tp* const_pointer;
1615 typedef _Tp& reference;
1616 typedef const _Tp& const_reference;
1617 typedef _Tp value_type;
1618
Howard Hinnant18884f42011-06-02 21:38:57 +00001619 typedef true_type propagate_on_container_move_assignment;
1620
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001621 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1622
Howard Hinnant1694d232011-05-28 14:41:13 +00001623 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1624 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1625 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001626 {return _VSTD::addressof(__x);}
Howard Hinnant1694d232011-05-28 14:41:13 +00001627 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001628 {return _VSTD::addressof(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001629 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1630 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
Howard Hinnant1694d232011-05-28 14:41:13 +00001631 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
1632 {::operator delete((void*)__p);}
1633 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1634 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001635#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001636 template <class _Up, class... _Args>
1637 _LIBCPP_INLINE_VISIBILITY
1638 void
1639 construct(_Up* __p, _Args&&... __args)
1640 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001641 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001642 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001643#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001644 _LIBCPP_INLINE_VISIBILITY
1645 void
1646 construct(pointer __p)
1647 {
1648 ::new((void*)__p) _Tp();
1649 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001650# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001651
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001652 template <class _A0>
1653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001654 void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001655 construct(pointer __p, _A0& __a0)
1656 {
1657 ::new((void*)__p) _Tp(__a0);
1658 }
1659 template <class _A0>
1660 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001661 void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001662 construct(pointer __p, const _A0& __a0)
1663 {
1664 ::new((void*)__p) _Tp(__a0);
1665 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001666# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001667 template <class _A0, class _A1>
1668 _LIBCPP_INLINE_VISIBILITY
1669 void
1670 construct(pointer __p, _A0& __a0, _A1& __a1)
1671 {
1672 ::new((void*)__p) _Tp(__a0, __a1);
1673 }
1674 template <class _A0, class _A1>
1675 _LIBCPP_INLINE_VISIBILITY
1676 void
1677 construct(pointer __p, const _A0& __a0, _A1& __a1)
1678 {
1679 ::new((void*)__p) _Tp(__a0, __a1);
1680 }
1681 template <class _A0, class _A1>
1682 _LIBCPP_INLINE_VISIBILITY
1683 void
1684 construct(pointer __p, _A0& __a0, const _A1& __a1)
1685 {
1686 ::new((void*)__p) _Tp(__a0, __a1);
1687 }
1688 template <class _A0, class _A1>
1689 _LIBCPP_INLINE_VISIBILITY
1690 void
1691 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1692 {
1693 ::new((void*)__p) _Tp(__a0, __a1);
1694 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001695#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001696 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1697};
1698
Howard Hinnant57199402012-01-02 17:56:02 +00001699template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001700class _LIBCPP_TYPE_VIS_ONLY allocator<const _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00001701{
1702public:
1703 typedef size_t size_type;
1704 typedef ptrdiff_t difference_type;
1705 typedef const _Tp* pointer;
1706 typedef const _Tp* const_pointer;
1707 typedef const _Tp& reference;
1708 typedef const _Tp& const_reference;
Howard Hinnant9360e9f2013-06-07 01:56:37 +00001709 typedef const _Tp value_type;
Howard Hinnant57199402012-01-02 17:56:02 +00001710
1711 typedef true_type propagate_on_container_move_assignment;
1712
1713 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1714
1715 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1716 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1717 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1718 {return _VSTD::addressof(__x);}
1719 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1720 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
1721 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
1722 {::operator delete((void*)__p);}
1723 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1724 {return size_type(~0) / sizeof(_Tp);}
1725#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1726 template <class _Up, class... _Args>
1727 _LIBCPP_INLINE_VISIBILITY
1728 void
1729 construct(_Up* __p, _Args&&... __args)
1730 {
1731 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1732 }
1733#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1734 _LIBCPP_INLINE_VISIBILITY
1735 void
1736 construct(pointer __p)
1737 {
1738 ::new((void*)__p) _Tp();
1739 }
1740# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001741
Howard Hinnant57199402012-01-02 17:56:02 +00001742 template <class _A0>
1743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001744 void
Howard Hinnant57199402012-01-02 17:56:02 +00001745 construct(pointer __p, _A0& __a0)
1746 {
1747 ::new((void*)__p) _Tp(__a0);
1748 }
1749 template <class _A0>
1750 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001751 void
Howard Hinnant57199402012-01-02 17:56:02 +00001752 construct(pointer __p, const _A0& __a0)
1753 {
1754 ::new((void*)__p) _Tp(__a0);
1755 }
Howard Hinnant57199402012-01-02 17:56:02 +00001756# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1757 template <class _A0, class _A1>
1758 _LIBCPP_INLINE_VISIBILITY
1759 void
1760 construct(pointer __p, _A0& __a0, _A1& __a1)
1761 {
1762 ::new((void*)__p) _Tp(__a0, __a1);
1763 }
1764 template <class _A0, class _A1>
1765 _LIBCPP_INLINE_VISIBILITY
1766 void
1767 construct(pointer __p, const _A0& __a0, _A1& __a1)
1768 {
1769 ::new((void*)__p) _Tp(__a0, __a1);
1770 }
1771 template <class _A0, class _A1>
1772 _LIBCPP_INLINE_VISIBILITY
1773 void
1774 construct(pointer __p, _A0& __a0, const _A1& __a1)
1775 {
1776 ::new((void*)__p) _Tp(__a0, __a1);
1777 }
1778 template <class _A0, class _A1>
1779 _LIBCPP_INLINE_VISIBILITY
1780 void
1781 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1782 {
1783 ::new((void*)__p) _Tp(__a0, __a1);
1784 }
1785#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1786 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1787};
1788
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001789template <class _Tp, class _Up>
1790inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001791bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001792
1793template <class _Tp, class _Up>
1794inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001795bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001796
1797template <class _OutputIterator, class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001798class _LIBCPP_TYPE_VIS_ONLY raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001799 : public iterator<output_iterator_tag,
1800 _Tp, // purposefully not C++03
1801 ptrdiff_t, // purposefully not C++03
1802 _Tp*, // purposefully not C++03
1803 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1804{
1805private:
1806 _OutputIterator __x_;
1807public:
1808 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1809 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1810 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1811 {::new(&*__x_) _Tp(__element); return *this;}
1812 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1813 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1814 {raw_storage_iterator __t(*this); ++__x_; return __t;}
1815};
1816
1817template <class _Tp>
1818pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00001819get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001820{
1821 pair<_Tp*, ptrdiff_t> __r(0, 0);
1822 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1823 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1824 / sizeof(_Tp);
1825 if (__n > __m)
1826 __n = __m;
1827 while (__n > 0)
1828 {
1829 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1830 if (__r.first)
1831 {
1832 __r.second = __n;
1833 break;
1834 }
1835 __n /= 2;
1836 }
1837 return __r;
1838}
1839
1840template <class _Tp>
1841inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001842void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001843
1844template <class _Tp>
1845struct auto_ptr_ref
1846{
1847 _Tp* __ptr_;
1848};
1849
1850template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001851class _LIBCPP_TYPE_VIS_ONLY auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001852{
1853private:
1854 _Tp* __ptr_;
1855public:
1856 typedef _Tp element_type;
1857
1858 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1859 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1860 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1861 : __ptr_(__p.release()) {}
1862 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1863 {reset(__p.release()); return *this;}
1864 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1865 {reset(__p.release()); return *this;}
1866 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1867 {reset(__p.__ptr_); return *this;}
1868 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1869
1870 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1871 {return *__ptr_;}
1872 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1873 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1874 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1875 {
1876 _Tp* __t = __ptr_;
1877 __ptr_ = 0;
1878 return __t;
1879 }
1880 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1881 {
1882 if (__ptr_ != __p)
1883 delete __ptr_;
1884 __ptr_ = __p;
1885 }
1886
1887 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1888 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1889 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1890 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1891 {return auto_ptr<_Up>(release());}
1892};
1893
1894template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001895class _LIBCPP_TYPE_VIS_ONLY auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001896{
1897public:
1898 typedef void element_type;
1899};
1900
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001901template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1902 typename remove_cv<_T2>::type>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00001903 bool = is_empty<_T1>::value
1904#if __has_feature(is_final)
1905 && !__is_final(_T1)
1906#endif
1907 ,
1908 bool = is_empty<_T2>::value
1909#if __has_feature(is_final)
1910 && !__is_final(_T2)
1911#endif
1912 >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001913struct __libcpp_compressed_pair_switch;
1914
1915template <class _T1, class _T2, bool IsSame>
1916struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1917
1918template <class _T1, class _T2, bool IsSame>
1919struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1920
1921template <class _T1, class _T2, bool IsSame>
1922struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1923
1924template <class _T1, class _T2>
1925struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
1926
1927template <class _T1, class _T2>
1928struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
1929
1930template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
1931class __libcpp_compressed_pair_imp;
1932
1933template <class _T1, class _T2>
1934class __libcpp_compressed_pair_imp<_T1, _T2, 0>
1935{
1936private:
1937 _T1 __first_;
1938 _T2 __second_;
1939public:
1940 typedef _T1 _T1_param;
1941 typedef _T2 _T2_param;
1942
1943 typedef typename remove_reference<_T1>::type& _T1_reference;
1944 typedef typename remove_reference<_T2>::type& _T2_reference;
1945
1946 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1947 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1948
1949 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant57199402012-01-02 17:56:02 +00001950 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001951 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00001952 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001953 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001954 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001955 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001956
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00001957#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00001958
1959 _LIBCPP_INLINE_VISIBILITY
1960 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
1961 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
1962 is_nothrow_copy_constructible<_T2>::value)
1963 : __first_(__p.first()),
1964 __second_(__p.second()) {}
1965
1966 _LIBCPP_INLINE_VISIBILITY
1967 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
1968 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
1969 is_nothrow_copy_assignable<_T2>::value)
1970 {
1971 __first_ = __p.first();
1972 __second_ = __p.second();
1973 return *this;
1974 }
1975
Howard Hinnant61aa6012011-07-01 19:24:36 +00001976 _LIBCPP_INLINE_VISIBILITY
1977 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00001978 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
1979 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant61aa6012011-07-01 19:24:36 +00001980 : __first_(_VSTD::forward<_T1>(__p.first())),
1981 __second_(_VSTD::forward<_T2>(__p.second())) {}
1982
1983 _LIBCPP_INLINE_VISIBILITY
1984 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
1985 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
1986 is_nothrow_move_assignable<_T2>::value)
1987 {
1988 __first_ = _VSTD::forward<_T1>(__p.first());
1989 __second_ = _VSTD::forward<_T2>(__p.second());
1990 return *this;
1991 }
1992
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00001993#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00001994
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00001995#ifndef _LIBCPP_HAS_NO_VARIADICS
1996
1997 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
1998 _LIBCPP_INLINE_VISIBILITY
1999 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2000 tuple<_Args1...> __first_args,
2001 tuple<_Args2...> __second_args,
2002 __tuple_indices<_I1...>,
2003 __tuple_indices<_I2...>)
2004 : __first_(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
2005 __second_(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
2006 {}
2007
2008#endif // _LIBCPP_HAS_NO_VARIADICS
2009
Howard Hinnant1694d232011-05-28 14:41:13 +00002010 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2011 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002012
Howard Hinnant1694d232011-05-28 14:41:13 +00002013 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2014 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002015
2016 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002017 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2018 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002019 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002020 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002021 swap(__first_, __x.__first_);
2022 swap(__second_, __x.__second_);
2023 }
2024};
2025
2026template <class _T1, class _T2>
2027class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2028 : private _T1
2029{
2030private:
2031 _T2 __second_;
2032public:
2033 typedef _T1 _T1_param;
2034 typedef _T2 _T2_param;
2035
2036 typedef _T1& _T1_reference;
2037 typedef typename remove_reference<_T2>::type& _T2_reference;
2038
2039 typedef const _T1& _T1_const_reference;
2040 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2041
2042 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002043 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002044 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002045 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002046 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002047 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002048 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002049
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002050#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002051
2052 _LIBCPP_INLINE_VISIBILITY
2053 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2054 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2055 is_nothrow_copy_constructible<_T2>::value)
2056 : _T1(__p.first()), __second_(__p.second()) {}
2057
2058 _LIBCPP_INLINE_VISIBILITY
2059 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2060 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2061 is_nothrow_copy_assignable<_T2>::value)
2062 {
2063 _T1::operator=(__p.first());
2064 __second_ = __p.second();
2065 return *this;
2066 }
2067
Howard Hinnant61aa6012011-07-01 19:24:36 +00002068 _LIBCPP_INLINE_VISIBILITY
2069 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002070 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2071 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002072 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002073
2074 _LIBCPP_INLINE_VISIBILITY
2075 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2076 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2077 is_nothrow_move_assignable<_T2>::value)
2078 {
2079 _T1::operator=(_VSTD::move(__p.first()));
2080 __second_ = _VSTD::forward<_T2>(__p.second());
2081 return *this;
2082 }
2083
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002084#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002085
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002086#ifndef _LIBCPP_HAS_NO_VARIADICS
2087
2088 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2089 _LIBCPP_INLINE_VISIBILITY
2090 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2091 tuple<_Args1...> __first_args,
2092 tuple<_Args2...> __second_args,
2093 __tuple_indices<_I1...>,
2094 __tuple_indices<_I2...>)
2095 : _T1(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
2096 __second_(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
2097 {}
2098
2099#endif // _LIBCPP_HAS_NO_VARIADICS
2100
Howard Hinnant1694d232011-05-28 14:41:13 +00002101 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2102 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002103
Howard Hinnant1694d232011-05-28 14:41:13 +00002104 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2105 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002106
2107 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002108 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2109 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002110 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002111 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002112 swap(__second_, __x.__second_);
2113 }
2114};
2115
2116template <class _T1, class _T2>
2117class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2118 : private _T2
2119{
2120private:
2121 _T1 __first_;
2122public:
2123 typedef _T1 _T1_param;
2124 typedef _T2 _T2_param;
2125
2126 typedef typename remove_reference<_T1>::type& _T1_reference;
2127 typedef _T2& _T2_reference;
2128
2129 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2130 typedef const _T2& _T2_const_reference;
2131
2132 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2133 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002134 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002135 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002136 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002137 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00002138 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2139 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002140 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002141
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002142#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002143
2144 _LIBCPP_INLINE_VISIBILITY
2145 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2146 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2147 is_nothrow_copy_constructible<_T2>::value)
2148 : _T2(__p.second()), __first_(__p.first()) {}
2149
2150 _LIBCPP_INLINE_VISIBILITY
2151 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2152 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2153 is_nothrow_copy_assignable<_T2>::value)
2154 {
2155 _T2::operator=(__p.second());
2156 __first_ = __p.first();
2157 return *this;
2158 }
2159
Howard Hinnant61aa6012011-07-01 19:24:36 +00002160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002161 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002162 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2163 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002164 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002165
2166 _LIBCPP_INLINE_VISIBILITY
2167 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2168 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2169 is_nothrow_move_assignable<_T2>::value)
2170 {
2171 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2172 __first_ = _VSTD::move(__p.first());
2173 return *this;
2174 }
2175
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002176#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002177
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002178#ifndef _LIBCPP_HAS_NO_VARIADICS
2179
2180 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2181 _LIBCPP_INLINE_VISIBILITY
2182 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2183 tuple<_Args1...> __first_args,
2184 tuple<_Args2...> __second_args,
2185 __tuple_indices<_I1...>,
2186 __tuple_indices<_I2...>)
2187 : _T2(_VSTD::forward<_Args2>(get<_I2>(__second_args))...),
2188 __first_(_VSTD::forward<_Args1>(get<_I1>(__first_args))...)
2189
2190 {}
2191
2192#endif // _LIBCPP_HAS_NO_VARIADICS
2193
Howard Hinnant1694d232011-05-28 14:41:13 +00002194 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2195 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002196
Howard Hinnant1694d232011-05-28 14:41:13 +00002197 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2198 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002199
2200 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002201 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2202 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002203 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002204 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002205 swap(__first_, __x.__first_);
2206 }
2207};
2208
2209template <class _T1, class _T2>
2210class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2211 : private _T1,
2212 private _T2
2213{
2214public:
2215 typedef _T1 _T1_param;
2216 typedef _T2 _T2_param;
2217
2218 typedef _T1& _T1_reference;
2219 typedef _T2& _T2_reference;
2220
2221 typedef const _T1& _T1_const_reference;
2222 typedef const _T2& _T2_const_reference;
2223
2224 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2225 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002226 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002227 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002228 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002229 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002230 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002231
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002232#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002233
2234 _LIBCPP_INLINE_VISIBILITY
2235 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2236 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2237 is_nothrow_copy_constructible<_T2>::value)
2238 : _T1(__p.first()), _T2(__p.second()) {}
2239
2240 _LIBCPP_INLINE_VISIBILITY
2241 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2242 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2243 is_nothrow_copy_assignable<_T2>::value)
2244 {
2245 _T1::operator=(__p.first());
2246 _T2::operator=(__p.second());
2247 return *this;
2248 }
2249
Howard Hinnant61aa6012011-07-01 19:24:36 +00002250 _LIBCPP_INLINE_VISIBILITY
2251 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002252 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2253 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002254 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002255
2256 _LIBCPP_INLINE_VISIBILITY
2257 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2258 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2259 is_nothrow_move_assignable<_T2>::value)
2260 {
2261 _T1::operator=(_VSTD::move(__p.first()));
2262 _T2::operator=(_VSTD::move(__p.second()));
2263 return *this;
2264 }
2265
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002266#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002267
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002268#ifndef _LIBCPP_HAS_NO_VARIADICS
2269
2270 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2271 _LIBCPP_INLINE_VISIBILITY
2272 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2273 tuple<_Args1...> __first_args,
2274 tuple<_Args2...> __second_args,
2275 __tuple_indices<_I1...>,
2276 __tuple_indices<_I2...>)
2277 : _T1(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
2278 _T2(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
2279 {}
2280
2281#endif // _LIBCPP_HAS_NO_VARIADICS
2282
Howard Hinnant1694d232011-05-28 14:41:13 +00002283 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2284 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002285
Howard Hinnant1694d232011-05-28 14:41:13 +00002286 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2287 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002288
Howard Hinnantec3773c2011-12-01 20:21:04 +00002289 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant1694d232011-05-28 14:41:13 +00002290 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2291 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002292 {
2293 }
2294};
2295
2296template <class _T1, class _T2>
2297class __compressed_pair
2298 : private __libcpp_compressed_pair_imp<_T1, _T2>
2299{
2300 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2301public:
2302 typedef typename base::_T1_param _T1_param;
2303 typedef typename base::_T2_param _T2_param;
2304
2305 typedef typename base::_T1_reference _T1_reference;
2306 typedef typename base::_T2_reference _T2_reference;
2307
2308 typedef typename base::_T1_const_reference _T1_const_reference;
2309 typedef typename base::_T2_const_reference _T2_const_reference;
2310
2311 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002312 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002313 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002314 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002315 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002316 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002317 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002318
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002319#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002320
2321 _LIBCPP_INLINE_VISIBILITY
2322 __compressed_pair(const __compressed_pair& __p)
2323 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2324 is_nothrow_copy_constructible<_T2>::value)
2325 : base(__p) {}
2326
2327 _LIBCPP_INLINE_VISIBILITY
2328 __compressed_pair& operator=(const __compressed_pair& __p)
2329 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2330 is_nothrow_copy_assignable<_T2>::value)
2331 {
2332 base::operator=(__p);
2333 return *this;
2334 }
2335
Howard Hinnant61aa6012011-07-01 19:24:36 +00002336 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002337 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002338 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2339 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002340 : base(_VSTD::move(__p)) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002341
2342 _LIBCPP_INLINE_VISIBILITY
2343 __compressed_pair& operator=(__compressed_pair&& __p)
2344 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2345 is_nothrow_move_assignable<_T2>::value)
2346 {
2347 base::operator=(_VSTD::move(__p));
2348 return *this;
2349 }
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002350
Howard Hinnant3f81e9e2013-11-13 00:39:22 +00002351#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant8c238192013-05-06 16:58:36 +00002352
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002353#ifndef _LIBCPP_HAS_NO_VARIADICS
2354
2355 template <class... _Args1, class... _Args2>
2356 _LIBCPP_INLINE_VISIBILITY
2357 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2358 tuple<_Args2...> __second_args)
Howard Hinnant57199402012-01-02 17:56:02 +00002359 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002360 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2361 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2362 {}
2363
2364#endif // _LIBCPP_HAS_NO_VARIADICS
2365
Howard Hinnant1694d232011-05-28 14:41:13 +00002366 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2367 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002368
Howard Hinnant1694d232011-05-28 14:41:13 +00002369 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2370 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002371
Howard Hinnant1694d232011-05-28 14:41:13 +00002372 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2373 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2374 __is_nothrow_swappable<_T1>::value)
2375 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002376};
2377
2378template <class _T1, class _T2>
2379inline _LIBCPP_INLINE_VISIBILITY
2380void
2381swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002382 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2383 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002384 {__x.swap(__y);}
2385
Howard Hinnant57199402012-01-02 17:56:02 +00002386// __same_or_less_cv_qualified
2387
2388template <class _Ptr1, class _Ptr2,
2389 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2390 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2391 >::value
2392 >
2393struct __same_or_less_cv_qualified_imp
2394 : is_convertible<_Ptr1, _Ptr2> {};
2395
2396template <class _Ptr1, class _Ptr2>
2397struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2398 : false_type {};
2399
2400template <class _Ptr1, class _Ptr2, bool = is_scalar<_Ptr1>::value &&
2401 !is_pointer<_Ptr1>::value>
2402struct __same_or_less_cv_qualified
2403 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2404
2405template <class _Ptr1, class _Ptr2>
2406struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, true>
2407 : false_type {};
2408
2409// default_delete
2410
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002411template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002412struct _LIBCPP_TYPE_VIS_ONLY default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002413{
Howard Hinnant46e94932012-07-07 20:56:04 +00002414#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2415 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2416#else
2417 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2418#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002419 template <class _Up>
2420 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002421 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2422 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002423 {
2424 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant05e7d242013-04-24 19:44:26 +00002425 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002426 delete __ptr;
2427 }
2428};
2429
2430template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002431struct _LIBCPP_TYPE_VIS_ONLY default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002432{
Howard Hinnant8e843502011-12-18 21:19:44 +00002433public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002434#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2435 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2436#else
2437 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2438#endif
Howard Hinnant8e843502011-12-18 21:19:44 +00002439 template <class _Up>
2440 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant57199402012-01-02 17:56:02 +00002441 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant8e843502011-12-18 21:19:44 +00002442 template <class _Up>
2443 _LIBCPP_INLINE_VISIBILITY
2444 void operator() (_Up* __ptr,
Howard Hinnant57199402012-01-02 17:56:02 +00002445 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002446 {
2447 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant05e7d242013-04-24 19:44:26 +00002448 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002449 delete [] __ptr;
2450 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002451};
2452
2453template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002454class _LIBCPP_TYPE_VIS_ONLY unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002455{
2456public:
2457 typedef _Tp element_type;
2458 typedef _Dp deleter_type;
2459 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2460private:
2461 __compressed_pair<pointer, deleter_type> __ptr_;
2462
Howard Hinnant57199402012-01-02 17:56:02 +00002463#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002464 unique_ptr(unique_ptr&);
2465 template <class _Up, class _Ep>
2466 unique_ptr(unique_ptr<_Up, _Ep>&);
2467 unique_ptr& operator=(unique_ptr&);
2468 template <class _Up, class _Ep>
2469 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002470#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002471
2472 struct __nat {int __for_bool_;};
2473
2474 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2475 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2476public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002477 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002478 : __ptr_(pointer())
2479 {
2480 static_assert(!is_pointer<deleter_type>::value,
2481 "unique_ptr constructed with null function pointer deleter");
2482 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002483 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002484 : __ptr_(pointer())
2485 {
2486 static_assert(!is_pointer<deleter_type>::value,
2487 "unique_ptr constructed with null function pointer deleter");
2488 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002489 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002490 : __ptr_(_VSTD::move(__p))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002491 {
2492 static_assert(!is_pointer<deleter_type>::value,
2493 "unique_ptr constructed with null function pointer deleter");
2494 }
2495
Howard Hinnant73d21a42010-09-04 23:28:19 +00002496#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002497 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2498 is_reference<deleter_type>::value,
2499 deleter_type,
2500 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002501 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002502 : __ptr_(__p, __d) {}
2503
2504 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002505 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002506 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002507 {
2508 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2509 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002510 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002511 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002512 template <class _Up, class _Ep>
2513 _LIBCPP_INLINE_VISIBILITY
2514 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2515 typename enable_if
2516 <
2517 !is_array<_Up>::value &&
2518 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2519 is_convertible<_Ep, deleter_type>::value &&
2520 (
2521 !is_reference<deleter_type>::value ||
2522 is_same<deleter_type, _Ep>::value
2523 ),
2524 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002525 >::type = __nat()) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002526 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002527
2528 template <class _Up>
2529 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2530 typename enable_if<
2531 is_convertible<_Up*, _Tp*>::value &&
2532 is_same<_Dp, default_delete<_Tp> >::value,
2533 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002534 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002535 : __ptr_(__p.release())
2536 {
2537 }
2538
Howard Hinnant1694d232011-05-28 14:41:13 +00002539 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002540 {
2541 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002542 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002543 return *this;
2544 }
2545
2546 template <class _Up, class _Ep>
2547 _LIBCPP_INLINE_VISIBILITY
2548 typename enable_if
2549 <
Howard Hinnant57199402012-01-02 17:56:02 +00002550 !is_array<_Up>::value &&
2551 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2552 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002553 unique_ptr&
2554 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002555 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002556 {
2557 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002558 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002559 return *this;
2560 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002561#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002562
2563 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2564 {
2565 return __rv<unique_ptr>(*this);
2566 }
2567
2568 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002569 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002570
2571 template <class _Up, class _Ep>
2572 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2573 {
2574 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002575 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002576 return *this;
2577 }
2578
2579 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002580 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002581
2582 template <class _Up>
2583 _LIBCPP_INLINE_VISIBILITY
2584 typename enable_if<
2585 is_convertible<_Up*, _Tp*>::value &&
2586 is_same<_Dp, default_delete<_Tp> >::value,
2587 unique_ptr&
2588 >::type
2589 operator=(auto_ptr<_Up> __p)
2590 {reset(__p.release()); return *this;}
2591
Howard Hinnant73d21a42010-09-04 23:28:19 +00002592#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002593 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2594
Howard Hinnant1694d232011-05-28 14:41:13 +00002595 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002596 {
2597 reset();
2598 return *this;
2599 }
2600
2601 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2602 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002603 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2604 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2605 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2606 {return __ptr_.second();}
2607 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2608 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002609 _LIBCPP_INLINE_VISIBILITY
2610 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2611 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002612
Howard Hinnant1694d232011-05-28 14:41:13 +00002613 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002614 {
2615 pointer __t = __ptr_.first();
2616 __ptr_.first() = pointer();
2617 return __t;
2618 }
2619
Howard Hinnant1694d232011-05-28 14:41:13 +00002620 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002621 {
2622 pointer __tmp = __ptr_.first();
2623 __ptr_.first() = __p;
2624 if (__tmp)
2625 __ptr_.second()(__tmp);
2626 }
2627
Howard Hinnant1694d232011-05-28 14:41:13 +00002628 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2629 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002630};
2631
2632template <class _Tp, class _Dp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00002633class _LIBCPP_TYPE_VIS_ONLY unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002634{
2635public:
2636 typedef _Tp element_type;
2637 typedef _Dp deleter_type;
2638 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2639private:
2640 __compressed_pair<pointer, deleter_type> __ptr_;
2641
Howard Hinnant57199402012-01-02 17:56:02 +00002642#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002643 unique_ptr(unique_ptr&);
2644 template <class _Up>
2645 unique_ptr(unique_ptr<_Up>&);
2646 unique_ptr& operator=(unique_ptr&);
2647 template <class _Up>
2648 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002649#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002650
2651 struct __nat {int __for_bool_;};
2652
2653 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2654 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2655public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002656 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002657 : __ptr_(pointer())
2658 {
2659 static_assert(!is_pointer<deleter_type>::value,
2660 "unique_ptr constructed with null function pointer deleter");
2661 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002662 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002663 : __ptr_(pointer())
2664 {
2665 static_assert(!is_pointer<deleter_type>::value,
2666 "unique_ptr constructed with null function pointer deleter");
2667 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002668#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002669 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002670 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002671 >
Howard Hinnant99968442011-11-29 18:15:50 +00002672 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002673 : __ptr_(__p)
2674 {
2675 static_assert(!is_pointer<deleter_type>::value,
2676 "unique_ptr constructed with null function pointer deleter");
2677 }
2678
Howard Hinnant99968442011-11-29 18:15:50 +00002679 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002680 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002681 >
Howard Hinnant99968442011-11-29 18:15:50 +00002682 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002683 is_reference<deleter_type>::value,
2684 deleter_type,
2685 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002686 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002687 : __ptr_(__p, __d) {}
2688
2689 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2690 is_reference<deleter_type>::value,
2691 deleter_type,
2692 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002693 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002694 : __ptr_(pointer(), __d) {}
2695
Howard Hinnant99968442011-11-29 18:15:50 +00002696 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002697 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002698 >
Howard Hinnant99968442011-11-29 18:15:50 +00002699 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002700 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002701 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002702 {
2703 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2704 }
2705
2706 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002707 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002708 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002709 {
2710 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2711 }
2712
Howard Hinnant1694d232011-05-28 14:41:13 +00002713 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002714 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002715
Howard Hinnant1694d232011-05-28 14:41:13 +00002716 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002717 {
2718 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002719 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002720 return *this;
2721 }
Howard Hinnant8e843502011-12-18 21:19:44 +00002722
2723 template <class _Up, class _Ep>
2724 _LIBCPP_INLINE_VISIBILITY
2725 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2726 typename enable_if
2727 <
2728 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002729 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant8e843502011-12-18 21:19:44 +00002730 && is_convertible<_Ep, deleter_type>::value &&
2731 (
2732 !is_reference<deleter_type>::value ||
2733 is_same<deleter_type, _Ep>::value
2734 ),
2735 __nat
2736 >::type = __nat()
2737 ) _NOEXCEPT
2738 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2739
2740
2741 template <class _Up, class _Ep>
2742 _LIBCPP_INLINE_VISIBILITY
2743 typename enable_if
2744 <
2745 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002746 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2747 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant8e843502011-12-18 21:19:44 +00002748 unique_ptr&
2749 >::type
2750 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2751 {
2752 reset(__u.release());
2753 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2754 return *this;
2755 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002756#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002757
2758 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2759 : __ptr_(__p)
2760 {
2761 static_assert(!is_pointer<deleter_type>::value,
2762 "unique_ptr constructed with null function pointer deleter");
2763 }
2764
2765 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002766 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002767
2768 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002769 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002770
2771 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2772 {
2773 return __rv<unique_ptr>(*this);
2774 }
2775
2776 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002777 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002778
2779 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2780 {
2781 reset(__u->release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002782 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002783 return *this;
2784 }
2785
Howard Hinnant73d21a42010-09-04 23:28:19 +00002786#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002787 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2788
Howard Hinnant1694d232011-05-28 14:41:13 +00002789 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002790 {
2791 reset();
2792 return *this;
2793 }
2794
2795 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2796 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002797 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2798 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2799 {return __ptr_.second();}
2800 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2801 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002802 _LIBCPP_INLINE_VISIBILITY
2803 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2804 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002805
Howard Hinnant1694d232011-05-28 14:41:13 +00002806 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002807 {
2808 pointer __t = __ptr_.first();
2809 __ptr_.first() = pointer();
2810 return __t;
2811 }
2812
Howard Hinnant73d21a42010-09-04 23:28:19 +00002813#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002814 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002815 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002816 >
Howard Hinnant99968442011-11-29 18:15:50 +00002817 _LIBCPP_INLINE_VISIBILITY void reset(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002818 {
2819 pointer __tmp = __ptr_.first();
2820 __ptr_.first() = __p;
2821 if (__tmp)
2822 __ptr_.second()(__tmp);
2823 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002824 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002825 {
2826 pointer __tmp = __ptr_.first();
2827 __ptr_.first() = nullptr;
2828 if (__tmp)
2829 __ptr_.second()(__tmp);
2830 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002831 _LIBCPP_INLINE_VISIBILITY void reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002832 {
2833 pointer __tmp = __ptr_.first();
2834 __ptr_.first() = nullptr;
2835 if (__tmp)
2836 __ptr_.second()(__tmp);
2837 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002838#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002839 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2840 {
2841 pointer __tmp = __ptr_.first();
2842 __ptr_.first() = __p;
2843 if (__tmp)
2844 __ptr_.second()(__tmp);
2845 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002846#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002847
2848 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2849private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002850
Howard Hinnant73d21a42010-09-04 23:28:19 +00002851#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002852 template <class _Up>
2853 explicit unique_ptr(_Up);
2854 template <class _Up>
2855 unique_ptr(_Up __u,
2856 typename conditional<
2857 is_reference<deleter_type>::value,
2858 deleter_type,
2859 typename add_lvalue_reference<const deleter_type>::type>::type,
2860 typename enable_if
2861 <
2862 is_convertible<_Up, pointer>::value,
2863 __nat
2864 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002865#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002866};
2867
2868template <class _Tp, class _Dp>
2869inline _LIBCPP_INLINE_VISIBILITY
2870void
Howard Hinnant1694d232011-05-28 14:41:13 +00002871swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002872
2873template <class _T1, class _D1, class _T2, class _D2>
2874inline _LIBCPP_INLINE_VISIBILITY
2875bool
2876operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2877
2878template <class _T1, class _D1, class _T2, class _D2>
2879inline _LIBCPP_INLINE_VISIBILITY
2880bool
2881operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2882
2883template <class _T1, class _D1, class _T2, class _D2>
2884inline _LIBCPP_INLINE_VISIBILITY
2885bool
Howard Hinnant3fadda32012-02-21 21:02:58 +00002886operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2887{
2888 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2889 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
2890 typedef typename common_type<_P1, _P2>::type _V;
2891 return less<_V>()(__x.get(), __y.get());
2892}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002893
2894template <class _T1, class _D1, class _T2, class _D2>
2895inline _LIBCPP_INLINE_VISIBILITY
2896bool
2897operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2898
2899template <class _T1, class _D1, class _T2, class _D2>
2900inline _LIBCPP_INLINE_VISIBILITY
2901bool
2902operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2903
2904template <class _T1, class _D1, class _T2, class _D2>
2905inline _LIBCPP_INLINE_VISIBILITY
2906bool
2907operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2908
Howard Hinnant3fadda32012-02-21 21:02:58 +00002909template <class _T1, class _D1>
2910inline _LIBCPP_INLINE_VISIBILITY
2911bool
Howard Hinnant46e94932012-07-07 20:56:04 +00002912operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00002913{
2914 return !__x;
2915}
2916
2917template <class _T1, class _D1>
2918inline _LIBCPP_INLINE_VISIBILITY
2919bool
Howard Hinnant46e94932012-07-07 20:56:04 +00002920operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00002921{
2922 return !__x;
2923}
2924
2925template <class _T1, class _D1>
2926inline _LIBCPP_INLINE_VISIBILITY
2927bool
Howard Hinnant46e94932012-07-07 20:56:04 +00002928operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00002929{
2930 return static_cast<bool>(__x);
2931}
2932
2933template <class _T1, class _D1>
2934inline _LIBCPP_INLINE_VISIBILITY
2935bool
Howard Hinnant46e94932012-07-07 20:56:04 +00002936operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00002937{
2938 return static_cast<bool>(__x);
2939}
2940
2941template <class _T1, class _D1>
2942inline _LIBCPP_INLINE_VISIBILITY
2943bool
2944operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2945{
2946 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2947 return less<_P1>()(__x.get(), nullptr);
2948}
2949
2950template <class _T1, class _D1>
2951inline _LIBCPP_INLINE_VISIBILITY
2952bool
2953operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2954{
2955 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2956 return less<_P1>()(nullptr, __x.get());
2957}
2958
2959template <class _T1, class _D1>
2960inline _LIBCPP_INLINE_VISIBILITY
2961bool
2962operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2963{
2964 return nullptr < __x;
2965}
2966
2967template <class _T1, class _D1>
2968inline _LIBCPP_INLINE_VISIBILITY
2969bool
2970operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2971{
2972 return __x < nullptr;
2973}
2974
2975template <class _T1, class _D1>
2976inline _LIBCPP_INLINE_VISIBILITY
2977bool
2978operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2979{
2980 return !(nullptr < __x);
2981}
2982
2983template <class _T1, class _D1>
2984inline _LIBCPP_INLINE_VISIBILITY
2985bool
2986operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2987{
2988 return !(__x < nullptr);
2989}
2990
2991template <class _T1, class _D1>
2992inline _LIBCPP_INLINE_VISIBILITY
2993bool
2994operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2995{
2996 return !(__x < nullptr);
2997}
2998
2999template <class _T1, class _D1>
3000inline _LIBCPP_INLINE_VISIBILITY
3001bool
3002operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3003{
3004 return !(nullptr < __x);
3005}
3006
Howard Hinnant87073e42012-05-01 15:37:54 +00003007#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3008
3009template <class _Tp, class _Dp>
3010inline _LIBCPP_INLINE_VISIBILITY
3011unique_ptr<_Tp, _Dp>
3012move(unique_ptr<_Tp, _Dp>& __t)
3013{
3014 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3015}
3016
3017#endif
3018
Marshall Clowfd7481e2013-07-01 18:16:03 +00003019#if _LIBCPP_STD_VER > 11
3020
3021template<class _Tp>
3022struct __unique_if
3023{
3024 typedef unique_ptr<_Tp> __unique_single;
3025};
3026
3027template<class _Tp>
3028struct __unique_if<_Tp[]>
3029{
3030 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3031};
3032
3033template<class _Tp, size_t _Np>
3034struct __unique_if<_Tp[_Np]>
3035{
3036 typedef void __unique_array_known_bound;
3037};
3038
3039template<class _Tp, class... _Args>
Marshall Clowfb551102013-07-02 20:06:09 +00003040inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003041typename __unique_if<_Tp>::__unique_single
3042make_unique(_Args&&... __args)
3043{
3044 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3045}
3046
3047template<class _Tp>
Marshall Clowfb551102013-07-02 20:06:09 +00003048inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003049typename __unique_if<_Tp>::__unique_array_unknown_bound
3050make_unique(size_t __n)
3051{
3052 typedef typename remove_extent<_Tp>::type _Up;
3053 return unique_ptr<_Tp>(new _Up[__n]());
3054}
3055
3056template<class _Tp, class... _Args>
3057 typename __unique_if<_Tp>::__unique_array_known_bound
3058 make_unique(_Args&&...) = delete;
3059
3060#endif // _LIBCPP_STD_VER > 11
3061
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00003062template <class _Tp> struct hash;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003063
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003064template <class _Size>
3065inline _LIBCPP_INLINE_VISIBILITY
3066_Size
3067__loadword(const void* __p)
3068{
3069 _Size __r;
3070 std::memcpy(&__r, __p, sizeof(__r));
3071 return __r;
3072}
3073
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003074// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
3075// is 64 bits. This is because cityhash64 uses 64bit x 64bit
3076// multiplication, which can be very slow on 32-bit systems.
Howard Hinnant40c13d32011-12-05 00:08:45 +00003077template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003078struct __murmur2_or_cityhash;
Howard Hinnant40c13d32011-12-05 00:08:45 +00003079
3080template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003081struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003082{
3083 _Size operator()(const void* __key, _Size __len);
3084};
3085
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003086// murmur2
Howard Hinnant40c13d32011-12-05 00:08:45 +00003087template <class _Size>
3088_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003089__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003090{
3091 const _Size __m = 0x5bd1e995;
3092 const _Size __r = 24;
3093 _Size __h = __len;
3094 const unsigned char* __data = static_cast<const unsigned char*>(__key);
3095 for (; __len >= 4; __data += 4, __len -= 4)
3096 {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003097 _Size __k = __loadword<_Size>(__data);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003098 __k *= __m;
3099 __k ^= __k >> __r;
3100 __k *= __m;
3101 __h *= __m;
3102 __h ^= __k;
3103 }
3104 switch (__len)
3105 {
3106 case 3:
3107 __h ^= __data[2] << 16;
3108 case 2:
3109 __h ^= __data[1] << 8;
3110 case 1:
3111 __h ^= __data[0];
3112 __h *= __m;
3113 }
3114 __h ^= __h >> 13;
3115 __h *= __m;
3116 __h ^= __h >> 15;
3117 return __h;
3118}
3119
3120template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003121struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003122{
3123 _Size operator()(const void* __key, _Size __len);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003124
3125 private:
3126 // Some primes between 2^63 and 2^64.
3127 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
3128 static const _Size __k1 = 0xb492b66fbe98f273ULL;
3129 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
3130 static const _Size __k3 = 0xc949d7c7509e6557ULL;
3131
3132 static _Size __rotate(_Size __val, int __shift) {
3133 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
3134 }
3135
3136 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
3137 return (__val >> __shift) | (__val << (64 - __shift));
3138 }
3139
3140 static _Size __shift_mix(_Size __val) {
3141 return __val ^ (__val >> 47);
3142 }
3143
3144 static _Size __hash_len_16(_Size __u, _Size __v) {
3145 const _Size __mul = 0x9ddfea08eb382d69ULL;
3146 _Size __a = (__u ^ __v) * __mul;
3147 __a ^= (__a >> 47);
3148 _Size __b = (__v ^ __a) * __mul;
3149 __b ^= (__b >> 47);
3150 __b *= __mul;
3151 return __b;
3152 }
3153
3154 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
3155 if (__len > 8) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003156 const _Size __a = __loadword<_Size>(__s);
3157 const _Size __b = __loadword<_Size>(__s + __len - 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003158 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
3159 }
3160 if (__len >= 4) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003161 const uint32_t __a = __loadword<uint32_t>(__s);
3162 const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003163 return __hash_len_16(__len + (__a << 3), __b);
3164 }
3165 if (__len > 0) {
3166 const unsigned char __a = __s[0];
3167 const unsigned char __b = __s[__len >> 1];
3168 const unsigned char __c = __s[__len - 1];
3169 const uint32_t __y = static_cast<uint32_t>(__a) +
3170 (static_cast<uint32_t>(__b) << 8);
3171 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3172 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3173 }
3174 return __k2;
3175 }
3176
3177 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003178 const _Size __a = __loadword<_Size>(__s) * __k1;
3179 const _Size __b = __loadword<_Size>(__s + 8);
3180 const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;
3181 const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003182 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3183 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3184 }
3185
3186 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3187 // Callers do best to use "random-looking" values for a and b.
3188 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3189 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3190 __a += __w;
3191 __b = __rotate(__b + __a + __z, 21);
3192 const _Size __c = __a;
3193 __a += __x;
3194 __a += __y;
3195 __b += __rotate(__a, 44);
3196 return pair<_Size, _Size>(__a + __z, __b + __c);
3197 }
3198
3199 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3200 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3201 const char* __s, _Size __a, _Size __b) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003202 return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),
3203 __loadword<_Size>(__s + 8),
3204 __loadword<_Size>(__s + 16),
3205 __loadword<_Size>(__s + 24),
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003206 __a,
3207 __b);
3208 }
3209
3210 // Return an 8-byte hash for 33 to 64 bytes.
3211 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003212 _Size __z = __loadword<_Size>(__s + 24);
3213 _Size __a = __loadword<_Size>(__s) +
3214 (__len + __loadword<_Size>(__s + __len - 16)) * __k0;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003215 _Size __b = __rotate(__a + __z, 52);
3216 _Size __c = __rotate(__a, 37);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003217 __a += __loadword<_Size>(__s + 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003218 __c += __rotate(__a, 7);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003219 __a += __loadword<_Size>(__s + 16);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003220 _Size __vf = __a + __z;
3221 _Size __vs = __b + __rotate(__a, 31) + __c;
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003222 __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);
3223 __z += __loadword<_Size>(__s + __len - 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003224 __b = __rotate(__a + __z, 52);
3225 __c = __rotate(__a, 37);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003226 __a += __loadword<_Size>(__s + __len - 24);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003227 __c += __rotate(__a, 7);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003228 __a += __loadword<_Size>(__s + __len - 16);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003229 _Size __wf = __a + __z;
3230 _Size __ws = __b + __rotate(__a, 31) + __c;
3231 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3232 return __shift_mix(__r * __k0 + __vs) * __k2;
3233 }
Howard Hinnant40c13d32011-12-05 00:08:45 +00003234};
3235
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003236// cityhash64
Howard Hinnant40c13d32011-12-05 00:08:45 +00003237template <class _Size>
3238_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003239__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003240{
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003241 const char* __s = static_cast<const char*>(__key);
3242 if (__len <= 32) {
3243 if (__len <= 16) {
3244 return __hash_len_0_to_16(__s, __len);
3245 } else {
3246 return __hash_len_17_to_32(__s, __len);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003247 }
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003248 } else if (__len <= 64) {
3249 return __hash_len_33_to_64(__s, __len);
3250 }
3251
3252 // For strings over 64 bytes we hash the end first, and then as we
3253 // loop we keep 56 bytes of state: v, w, x, y, and z.
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003254 _Size __x = __loadword<_Size>(__s + __len - 40);
3255 _Size __y = __loadword<_Size>(__s + __len - 16) +
3256 __loadword<_Size>(__s + __len - 56);
3257 _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,
3258 __loadword<_Size>(__s + __len - 24));
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003259 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3260 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003261 __x = __x * __k1 + __loadword<_Size>(__s);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003262
3263 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3264 __len = (__len - 1) & ~static_cast<_Size>(63);
3265 do {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003266 __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;
3267 __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003268 __x ^= __w.second;
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003269 __y += __v.first + __loadword<_Size>(__s + 40);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003270 __z = __rotate(__z + __w.first, 33) * __k1;
3271 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3272 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003273 __y + __loadword<_Size>(__s + 16));
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003274 std::swap(__z, __x);
3275 __s += 64;
3276 __len -= 64;
3277 } while (__len != 0);
3278 return __hash_len_16(
3279 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3280 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003281}
3282
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003283template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3284struct __scalar_hash;
3285
3286template <class _Tp>
3287struct __scalar_hash<_Tp, 0>
3288 : public unary_function<_Tp, size_t>
Howard Hinnant21aefc32010-06-03 16:42:57 +00003289{
Howard Hinnant82894812010-09-22 16:48:34 +00003290 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003291 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003292 {
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003293 union
3294 {
3295 _Tp __t;
3296 size_t __a;
3297 } __u;
3298 __u.__a = 0;
3299 __u.__t = __v;
3300 return __u.__a;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003301 }
3302};
3303
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003304template <class _Tp>
3305struct __scalar_hash<_Tp, 1>
3306 : public unary_function<_Tp, size_t>
3307{
3308 _LIBCPP_INLINE_VISIBILITY
3309 size_t operator()(_Tp __v) const _NOEXCEPT
3310 {
3311 union
3312 {
3313 _Tp __t;
3314 size_t __a;
3315 } __u;
3316 __u.__t = __v;
3317 return __u.__a;
3318 }
3319};
3320
3321template <class _Tp>
3322struct __scalar_hash<_Tp, 2>
3323 : public unary_function<_Tp, size_t>
3324{
3325 _LIBCPP_INLINE_VISIBILITY
3326 size_t operator()(_Tp __v) const _NOEXCEPT
3327 {
3328 union
3329 {
3330 _Tp __t;
3331 struct
3332 {
3333 size_t __a;
3334 size_t __b;
3335 };
3336 } __u;
3337 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003338 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003339 }
3340};
3341
3342template <class _Tp>
3343struct __scalar_hash<_Tp, 3>
3344 : public unary_function<_Tp, size_t>
3345{
3346 _LIBCPP_INLINE_VISIBILITY
3347 size_t operator()(_Tp __v) const _NOEXCEPT
3348 {
3349 union
3350 {
3351 _Tp __t;
3352 struct
3353 {
3354 size_t __a;
3355 size_t __b;
3356 size_t __c;
3357 };
3358 } __u;
3359 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003360 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003361 }
3362};
3363
3364template <class _Tp>
3365struct __scalar_hash<_Tp, 4>
3366 : public unary_function<_Tp, size_t>
3367{
3368 _LIBCPP_INLINE_VISIBILITY
3369 size_t operator()(_Tp __v) const _NOEXCEPT
3370 {
3371 union
3372 {
3373 _Tp __t;
3374 struct
3375 {
3376 size_t __a;
3377 size_t __b;
3378 size_t __c;
3379 size_t __d;
3380 };
3381 } __u;
3382 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003383 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003384 }
3385};
3386
3387template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003388struct _LIBCPP_TYPE_VIS_ONLY hash<_Tp*>
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003389 : public unary_function<_Tp*, size_t>
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003390{
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003391 _LIBCPP_INLINE_VISIBILITY
3392 size_t operator()(_Tp* __v) const _NOEXCEPT
3393 {
3394 union
3395 {
3396 _Tp* __t;
3397 size_t __a;
3398 } __u;
3399 __u.__t = __v;
3400 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
3401 }
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003402};
3403
Howard Hinnant21aefc32010-06-03 16:42:57 +00003404template <class _Tp, class _Dp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003405struct _LIBCPP_TYPE_VIS_ONLY hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003406{
3407 typedef unique_ptr<_Tp, _Dp> argument_type;
3408 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003409 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003410 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003411 {
3412 typedef typename argument_type::pointer pointer;
3413 return hash<pointer>()(__ptr.get());
3414 }
3415};
3416
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003417struct __destruct_n
3418{
3419private:
3420 size_t size;
3421
3422 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003423 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003424 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3425
3426 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003427 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003428 {}
3429
Howard Hinnant1694d232011-05-28 14:41:13 +00003430 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003431 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003432 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003433 {}
3434
Howard Hinnant1694d232011-05-28 14:41:13 +00003435 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003436 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003437 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003438 {}
3439public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003440 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3441 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003442
3443 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003444 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003445 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003446
3447 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003448 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003449 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003450
3451 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003452 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003453 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003454};
3455
3456template <class _Alloc>
3457class __allocator_destructor
3458{
3459 typedef allocator_traits<_Alloc> __alloc_traits;
3460public:
3461 typedef typename __alloc_traits::pointer pointer;
3462 typedef typename __alloc_traits::size_type size_type;
3463private:
3464 _Alloc& __alloc_;
3465 size_type __s_;
3466public:
3467 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00003468 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003469 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003470 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003471 void operator()(pointer __p) _NOEXCEPT
3472 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003473};
3474
3475template <class _InputIterator, class _ForwardIterator>
3476_ForwardIterator
3477uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3478{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003479 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003480#ifndef _LIBCPP_NO_EXCEPTIONS
3481 _ForwardIterator __s = __r;
3482 try
3483 {
3484#endif
3485 for (; __f != __l; ++__f, ++__r)
3486 ::new(&*__r) value_type(*__f);
3487#ifndef _LIBCPP_NO_EXCEPTIONS
3488 }
3489 catch (...)
3490 {
3491 for (; __s != __r; ++__s)
3492 __s->~value_type();
3493 throw;
3494 }
3495#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003496 return __r;
3497}
3498
3499template <class _InputIterator, class _Size, class _ForwardIterator>
3500_ForwardIterator
3501uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3502{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003503 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003504#ifndef _LIBCPP_NO_EXCEPTIONS
3505 _ForwardIterator __s = __r;
3506 try
3507 {
3508#endif
3509 for (; __n > 0; ++__f, ++__r, --__n)
3510 ::new(&*__r) value_type(*__f);
3511#ifndef _LIBCPP_NO_EXCEPTIONS
3512 }
3513 catch (...)
3514 {
3515 for (; __s != __r; ++__s)
3516 __s->~value_type();
3517 throw;
3518 }
3519#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003520 return __r;
3521}
3522
3523template <class _ForwardIterator, class _Tp>
3524void
3525uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3526{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003527 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003528#ifndef _LIBCPP_NO_EXCEPTIONS
3529 _ForwardIterator __s = __f;
3530 try
3531 {
3532#endif
3533 for (; __f != __l; ++__f)
3534 ::new(&*__f) value_type(__x);
3535#ifndef _LIBCPP_NO_EXCEPTIONS
3536 }
3537 catch (...)
3538 {
3539 for (; __s != __f; ++__s)
3540 __s->~value_type();
3541 throw;
3542 }
3543#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003544}
3545
3546template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003547_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003548uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3549{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003550 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003551#ifndef _LIBCPP_NO_EXCEPTIONS
3552 _ForwardIterator __s = __f;
3553 try
3554 {
3555#endif
3556 for (; __n > 0; ++__f, --__n)
3557 ::new(&*__f) value_type(__x);
3558#ifndef _LIBCPP_NO_EXCEPTIONS
3559 }
3560 catch (...)
3561 {
3562 for (; __s != __f; ++__s)
3563 __s->~value_type();
3564 throw;
3565 }
3566#endif
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003567 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003568}
3569
Howard Hinnant82894812010-09-22 16:48:34 +00003570class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003571 : public std::exception
3572{
3573public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003574 virtual ~bad_weak_ptr() _NOEXCEPT;
3575 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003576};
3577
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003578template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003579
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003580class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003581{
3582 __shared_count(const __shared_count&);
3583 __shared_count& operator=(const __shared_count&);
3584
3585protected:
3586 long __shared_owners_;
3587 virtual ~__shared_count();
3588private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003589 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003590
3591public:
Howard Hinnant82894812010-09-22 16:48:34 +00003592 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003593 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003594 : __shared_owners_(__refs) {}
3595
Howard Hinnant1694d232011-05-28 14:41:13 +00003596 void __add_shared() _NOEXCEPT;
3597 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003598 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003599 long use_count() const _NOEXCEPT {return __shared_owners_ + 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003600};
3601
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003602class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003603 : private __shared_count
3604{
3605 long __shared_weak_owners_;
3606
3607public:
Howard Hinnant82894812010-09-22 16:48:34 +00003608 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003609 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003610 : __shared_count(__refs),
3611 __shared_weak_owners_(__refs) {}
3612protected:
3613 virtual ~__shared_weak_count();
3614
3615public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003616 void __add_shared() _NOEXCEPT;
3617 void __add_weak() _NOEXCEPT;
3618 void __release_shared() _NOEXCEPT;
3619 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003620 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003621 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3622 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003623
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003624 // Define the function out only if we build static libc++ without RTTI.
3625 // Otherwise we may break clients who need to compile their projects with
3626 // -fno-rtti and yet link against a libc++.dylib compiled
3627 // without -fno-rtti.
3628#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant1694d232011-05-28 14:41:13 +00003629 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003630#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003631private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003632 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003633};
3634
3635template <class _Tp, class _Dp, class _Alloc>
3636class __shared_ptr_pointer
3637 : public __shared_weak_count
3638{
3639 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3640public:
Howard Hinnant82894812010-09-22 16:48:34 +00003641 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003642 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003643 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003644
Howard Hinnantd4444702010-08-11 17:04:31 +00003645#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00003646 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00003647#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003648
3649private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003650 virtual void __on_zero_shared() _NOEXCEPT;
3651 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003652};
3653
Howard Hinnantd4444702010-08-11 17:04:31 +00003654#ifndef _LIBCPP_NO_RTTI
3655
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003656template <class _Tp, class _Dp, class _Alloc>
3657const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00003658__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003659{
3660 return __t == typeid(_Dp) ? &__data_.first().second() : 0;
3661}
3662
Howard Hinnant324bb032010-08-22 00:02:43 +00003663#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003664
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003665template <class _Tp, class _Dp, class _Alloc>
3666void
Howard Hinnant1694d232011-05-28 14:41:13 +00003667__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003668{
3669 __data_.first().second()(__data_.first().first());
3670 __data_.first().second().~_Dp();
3671}
3672
3673template <class _Tp, class _Dp, class _Alloc>
3674void
Howard Hinnant1694d232011-05-28 14:41:13 +00003675__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003676{
3677 typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
3678 __data_.second().~_Alloc();
3679 __a.deallocate(this, 1);
3680}
3681
3682template <class _Tp, class _Alloc>
3683class __shared_ptr_emplace
3684 : public __shared_weak_count
3685{
3686 __compressed_pair<_Alloc, _Tp> __data_;
3687public:
3688#ifndef _LIBCPP_HAS_NO_VARIADICS
3689
Howard Hinnant82894812010-09-22 16:48:34 +00003690 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003691 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003692 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003693
3694 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00003695 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003696 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00003697 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3698 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003699
3700#else // _LIBCPP_HAS_NO_VARIADICS
3701
Howard Hinnant82894812010-09-22 16:48:34 +00003702 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003703 __shared_ptr_emplace(_Alloc __a)
3704 : __data_(__a) {}
3705
3706 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00003707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003708 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3709 : __data_(__a, _Tp(__a0)) {}
3710
3711 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00003712 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003713 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3714 : __data_(__a, _Tp(__a0, __a1)) {}
3715
3716 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00003717 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003718 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3719 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3720
3721#endif // _LIBCPP_HAS_NO_VARIADICS
3722
3723private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003724 virtual void __on_zero_shared() _NOEXCEPT;
3725 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003726public:
Howard Hinnant82894812010-09-22 16:48:34 +00003727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003728 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003729};
3730
3731template <class _Tp, class _Alloc>
3732void
Howard Hinnant1694d232011-05-28 14:41:13 +00003733__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003734{
3735 __data_.second().~_Tp();
3736}
3737
3738template <class _Tp, class _Alloc>
3739void
Howard Hinnant1694d232011-05-28 14:41:13 +00003740__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003741{
3742 typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
3743 __data_.first().~_Alloc();
3744 __a.deallocate(this, 1);
3745}
3746
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003747template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003748
3749template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003750class _LIBCPP_TYPE_VIS_ONLY shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003751{
Howard Hinnant324bb032010-08-22 00:02:43 +00003752public:
3753 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003754private:
3755 element_type* __ptr_;
3756 __shared_weak_count* __cntrl_;
3757
3758 struct __nat {int __for_bool_;};
3759public:
Howard Hinnant46e94932012-07-07 20:56:04 +00003760 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
3761 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003762 template<class _Yp,
3763 class = typename enable_if
3764 <
3765 is_convertible<_Yp*, element_type*>::value
3766 >::type
3767 >
3768 explicit shared_ptr(_Yp* __p);
3769 template<class _Yp, class _Dp,
3770 class = typename enable_if
3771 <
3772 is_convertible<_Yp*, element_type*>::value
3773 >::type
3774 >
3775 shared_ptr(_Yp* __p, _Dp __d);
3776 template<class _Yp, class _Dp, class _Alloc,
3777 class = typename enable_if
3778 <
3779 is_convertible<_Yp*, element_type*>::value
3780 >::type
3781 >
3782 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003783 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3784 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant1694d232011-05-28 14:41:13 +00003785 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3786 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003787 template<class _Yp>
3788 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003789 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3790 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003791#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003792 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003793 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003794 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3795 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003796#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003797 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003798 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003799#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003800 template<class _Yp,
3801 class = typename enable_if
3802 <
3803 is_convertible<_Yp*, element_type*>::value
3804 >::type
3805 >
3806 shared_ptr(auto_ptr<_Yp>&& __r);
Howard Hinnant324bb032010-08-22 00:02:43 +00003807#else
Howard Hinnant57199402012-01-02 17:56:02 +00003808 template<class _Yp,
3809 class = typename enable_if
3810 <
3811 is_convertible<_Yp*, element_type*>::value
3812 >::type
3813 >
3814 shared_ptr(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003815#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003816#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003817 template <class _Yp, class _Dp,
3818 class = typename enable_if
3819 <
3820 !is_array<_Yp>::value &&
3821 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3822 >::type
3823 >
3824 shared_ptr(unique_ptr<_Yp, _Dp>&&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003825 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant57199402012-01-02 17:56:02 +00003826 template <class _Yp, class _Dp,
3827 class = typename enable_if
3828 <
3829 !is_array<_Yp>::value &&
3830 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3831 >::type
3832 >
3833 shared_ptr(unique_ptr<_Yp, _Dp>&&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003834 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003835#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003836 template <class _Yp, class _Dp,
3837 class = typename enable_if
3838 <
3839 !is_array<_Yp>::value &&
3840 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3841 >::type
3842 > shared_ptr(unique_ptr<_Yp, _Dp>,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003843 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant57199402012-01-02 17:56:02 +00003844 template <class _Yp, class _Dp,
3845 class = typename enable_if
3846 <
3847 !is_array<_Yp>::value &&
3848 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3849 >::type
3850 >
3851 shared_ptr(unique_ptr<_Yp, _Dp>,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003852 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003853#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003854
3855 ~shared_ptr();
3856
Howard Hinnant1694d232011-05-28 14:41:13 +00003857 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003858 template<class _Yp>
3859 typename enable_if
3860 <
3861 is_convertible<_Yp*, element_type*>::value,
3862 shared_ptr&
3863 >::type
3864 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003865#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003866 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003867 template<class _Yp>
3868 typename enable_if
3869 <
3870 is_convertible<_Yp*, element_type*>::value,
3871 shared_ptr<_Tp>&
3872 >::type
3873 operator=(shared_ptr<_Yp>&& __r);
3874 template<class _Yp>
3875 typename enable_if
3876 <
3877 !is_array<_Yp>::value &&
3878 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant37c4acf2013-09-13 23:56:00 +00003879 shared_ptr
3880 >::type&
Howard Hinnant57199402012-01-02 17:56:02 +00003881 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003882#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003883 template<class _Yp>
3884 typename enable_if
3885 <
3886 !is_array<_Yp>::value &&
3887 is_convertible<_Yp*, element_type*>::value,
3888 shared_ptr&
3889 >::type
3890 operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003891#endif
Howard Hinnant57199402012-01-02 17:56:02 +00003892 template <class _Yp, class _Dp>
3893 typename enable_if
3894 <
3895 !is_array<_Yp>::value &&
3896 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3897 shared_ptr&
3898 >::type
Howard Hinnant73d21a42010-09-04 23:28:19 +00003899#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003900 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003901#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003902 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003903#endif
3904
Howard Hinnant1694d232011-05-28 14:41:13 +00003905 void swap(shared_ptr& __r) _NOEXCEPT;
3906 void reset() _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003907 template<class _Yp>
3908 typename enable_if
3909 <
3910 is_convertible<_Yp*, element_type*>::value,
3911 void
3912 >::type
3913 reset(_Yp* __p);
3914 template<class _Yp, class _Dp>
3915 typename enable_if
3916 <
3917 is_convertible<_Yp*, element_type*>::value,
3918 void
3919 >::type
3920 reset(_Yp* __p, _Dp __d);
3921 template<class _Yp, class _Dp, class _Alloc>
3922 typename enable_if
3923 <
3924 is_convertible<_Yp*, element_type*>::value,
3925 void
3926 >::type
3927 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003928
Howard Hinnant82894812010-09-22 16:48:34 +00003929 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003930 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003931 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003932 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3933 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003934 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003935 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003936 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003937 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003938 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003939 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00003940 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant77861882012-02-21 21:46:43 +00003941 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant99968442011-11-29 18:15:50 +00003942 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00003943 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003944 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003945 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant99968442011-11-29 18:15:50 +00003946 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00003947 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003948 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003949 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003950 _LIBCPP_INLINE_VISIBILITY
3951 bool
3952 __owner_equivalent(const shared_ptr& __p) const
3953 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003954
Howard Hinnantd4444702010-08-11 17:04:31 +00003955#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003956 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00003957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003958 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003959 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00003960#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003961
3962#ifndef _LIBCPP_HAS_NO_VARIADICS
3963
3964 template<class ..._Args>
3965 static
3966 shared_ptr<_Tp>
3967 make_shared(_Args&& ...__args);
3968
3969 template<class _Alloc, class ..._Args>
3970 static
3971 shared_ptr<_Tp>
3972 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3973
3974#else // _LIBCPP_HAS_NO_VARIADICS
3975
3976 static shared_ptr<_Tp> make_shared();
3977
3978 template<class _A0>
3979 static shared_ptr<_Tp> make_shared(_A0&);
3980
3981 template<class _A0, class _A1>
3982 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3983
3984 template<class _A0, class _A1, class _A2>
3985 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3986
3987 template<class _Alloc>
3988 static shared_ptr<_Tp>
3989 allocate_shared(const _Alloc& __a);
3990
3991 template<class _Alloc, class _A0>
3992 static shared_ptr<_Tp>
3993 allocate_shared(const _Alloc& __a, _A0& __a0);
3994
3995 template<class _Alloc, class _A0, class _A1>
3996 static shared_ptr<_Tp>
3997 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3998
3999 template<class _Alloc, class _A0, class _A1, class _A2>
4000 static shared_ptr<_Tp>
4001 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
4002
4003#endif // _LIBCPP_HAS_NO_VARIADICS
4004
4005private:
4006
4007 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00004008 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004009 void
Howard Hinnant1694d232011-05-28 14:41:13 +00004010 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004011 {
4012 if (__e)
4013 __e->__weak_this_ = *this;
4014 }
4015
Howard Hinnant82894812010-09-22 16:48:34 +00004016 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004017 void __enable_weak_this(const void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004018
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004019 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
4020 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004021};
4022
4023template<class _Tp>
4024inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00004025_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004026shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004027 : __ptr_(0),
4028 __cntrl_(0)
4029{
4030}
4031
4032template<class _Tp>
4033inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00004034_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004035shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004036 : __ptr_(0),
4037 __cntrl_(0)
4038{
4039}
4040
4041template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004042template<class _Yp, class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004043shared_ptr<_Tp>::shared_ptr(_Yp* __p)
4044 : __ptr_(__p)
4045{
4046 unique_ptr<_Yp> __hold(__p);
4047 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4048 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
4049 __hold.release();
4050 __enable_weak_this(__p);
4051}
4052
4053template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004054template<class _Yp, class _Dp, class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004055shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
4056 : __ptr_(__p)
4057{
4058#ifndef _LIBCPP_NO_EXCEPTIONS
4059 try
4060 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004061#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004062 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4063 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
4064 __enable_weak_this(__p);
4065#ifndef _LIBCPP_NO_EXCEPTIONS
4066 }
4067 catch (...)
4068 {
4069 __d(__p);
4070 throw;
4071 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004072#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004073}
4074
4075template<class _Tp>
4076template<class _Dp>
4077shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4078 : __ptr_(0)
4079{
4080#ifndef _LIBCPP_NO_EXCEPTIONS
4081 try
4082 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004083#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004084 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4085 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4086#ifndef _LIBCPP_NO_EXCEPTIONS
4087 }
4088 catch (...)
4089 {
4090 __d(__p);
4091 throw;
4092 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004093#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004094}
4095
4096template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004097template<class _Yp, class _Dp, class _Alloc, class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004098shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
4099 : __ptr_(__p)
4100{
4101#ifndef _LIBCPP_NO_EXCEPTIONS
4102 try
4103 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004104#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004105 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
4106 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
4107 typedef __allocator_destructor<_A2> _D2;
4108 _A2 __a2(__a);
4109 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4110 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
4111 __cntrl_ = __hold2.release();
4112 __enable_weak_this(__p);
4113#ifndef _LIBCPP_NO_EXCEPTIONS
4114 }
4115 catch (...)
4116 {
4117 __d(__p);
4118 throw;
4119 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004120#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004121}
4122
4123template<class _Tp>
4124template<class _Dp, class _Alloc>
4125shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4126 : __ptr_(0)
4127{
4128#ifndef _LIBCPP_NO_EXCEPTIONS
4129 try
4130 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004131#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004132 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
4133 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
4134 typedef __allocator_destructor<_A2> _D2;
4135 _A2 __a2(__a);
4136 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4137 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
4138 __cntrl_ = __hold2.release();
4139#ifndef _LIBCPP_NO_EXCEPTIONS
4140 }
4141 catch (...)
4142 {
4143 __d(__p);
4144 throw;
4145 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004146#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004147}
4148
4149template<class _Tp>
4150template<class _Yp>
4151inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004152shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004153 : __ptr_(__p),
4154 __cntrl_(__r.__cntrl_)
4155{
4156 if (__cntrl_)
4157 __cntrl_->__add_shared();
4158}
4159
4160template<class _Tp>
4161inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004162shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004163 : __ptr_(__r.__ptr_),
4164 __cntrl_(__r.__cntrl_)
4165{
4166 if (__cntrl_)
4167 __cntrl_->__add_shared();
4168}
4169
4170template<class _Tp>
4171template<class _Yp>
4172inline _LIBCPP_INLINE_VISIBILITY
4173shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4174 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004175 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004176 : __ptr_(__r.__ptr_),
4177 __cntrl_(__r.__cntrl_)
4178{
4179 if (__cntrl_)
4180 __cntrl_->__add_shared();
4181}
4182
Howard Hinnant73d21a42010-09-04 23:28:19 +00004183#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004184
4185template<class _Tp>
4186inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004187shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004188 : __ptr_(__r.__ptr_),
4189 __cntrl_(__r.__cntrl_)
4190{
4191 __r.__ptr_ = 0;
4192 __r.__cntrl_ = 0;
4193}
4194
4195template<class _Tp>
4196template<class _Yp>
4197inline _LIBCPP_INLINE_VISIBILITY
4198shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4199 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004200 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004201 : __ptr_(__r.__ptr_),
4202 __cntrl_(__r.__cntrl_)
4203{
4204 __r.__ptr_ = 0;
4205 __r.__cntrl_ = 0;
4206}
4207
Howard Hinnant73d21a42010-09-04 23:28:19 +00004208#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004209
4210template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004211template<class _Yp, class>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004212#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004213shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
4214#else
Howard Hinnant92172b82010-08-21 21:14:53 +00004215shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004216#endif
4217 : __ptr_(__r.get())
4218{
4219 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4220 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
4221 __enable_weak_this(__r.get());
4222 __r.release();
4223}
4224
4225template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004226template <class _Yp, class _Dp, class>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004227#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004228shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4229#else
4230shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4231#endif
4232 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
4233 : __ptr_(__r.get())
4234{
4235 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4236 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
4237 __enable_weak_this(__r.get());
4238 __r.release();
4239}
4240
4241template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004242template <class _Yp, class _Dp, class>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004243#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004244shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4245#else
4246shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4247#endif
4248 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
4249 : __ptr_(__r.get())
4250{
4251 typedef __shared_ptr_pointer<_Yp*,
4252 reference_wrapper<typename remove_reference<_Dp>::type>,
4253 allocator<_Yp> > _CntrlBlk;
4254 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
4255 __enable_weak_this(__r.get());
4256 __r.release();
4257}
4258
4259#ifndef _LIBCPP_HAS_NO_VARIADICS
4260
4261template<class _Tp>
4262template<class ..._Args>
4263shared_ptr<_Tp>
4264shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4265{
4266 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4267 typedef allocator<_CntrlBlk> _A2;
4268 typedef __allocator_destructor<_A2> _D2;
4269 _A2 __a2;
4270 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004271 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004272 shared_ptr<_Tp> __r;
4273 __r.__ptr_ = __hold2.get()->get();
4274 __r.__cntrl_ = __hold2.release();
4275 __r.__enable_weak_this(__r.__ptr_);
4276 return __r;
4277}
4278
4279template<class _Tp>
4280template<class _Alloc, class ..._Args>
4281shared_ptr<_Tp>
4282shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4283{
4284 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4285 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
4286 typedef __allocator_destructor<_A2> _D2;
4287 _A2 __a2(__a);
4288 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004289 ::new(__hold2.get()) _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004290 shared_ptr<_Tp> __r;
4291 __r.__ptr_ = __hold2.get()->get();
4292 __r.__cntrl_ = __hold2.release();
4293 __r.__enable_weak_this(__r.__ptr_);
4294 return __r;
4295}
4296
4297#else // _LIBCPP_HAS_NO_VARIADICS
4298
4299template<class _Tp>
4300shared_ptr<_Tp>
4301shared_ptr<_Tp>::make_shared()
4302{
4303 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4304 typedef allocator<_CntrlBlk> _Alloc2;
4305 typedef __allocator_destructor<_Alloc2> _D2;
4306 _Alloc2 __alloc2;
4307 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4308 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4309 shared_ptr<_Tp> __r;
4310 __r.__ptr_ = __hold2.get()->get();
4311 __r.__cntrl_ = __hold2.release();
4312 __r.__enable_weak_this(__r.__ptr_);
4313 return __r;
4314}
4315
4316template<class _Tp>
4317template<class _A0>
4318shared_ptr<_Tp>
4319shared_ptr<_Tp>::make_shared(_A0& __a0)
4320{
4321 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4322 typedef allocator<_CntrlBlk> _Alloc2;
4323 typedef __allocator_destructor<_Alloc2> _D2;
4324 _Alloc2 __alloc2;
4325 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4326 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4327 shared_ptr<_Tp> __r;
4328 __r.__ptr_ = __hold2.get()->get();
4329 __r.__cntrl_ = __hold2.release();
4330 __r.__enable_weak_this(__r.__ptr_);
4331 return __r;
4332}
4333
4334template<class _Tp>
4335template<class _A0, class _A1>
4336shared_ptr<_Tp>
4337shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4338{
4339 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4340 typedef allocator<_CntrlBlk> _Alloc2;
4341 typedef __allocator_destructor<_Alloc2> _D2;
4342 _Alloc2 __alloc2;
4343 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4344 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4345 shared_ptr<_Tp> __r;
4346 __r.__ptr_ = __hold2.get()->get();
4347 __r.__cntrl_ = __hold2.release();
4348 __r.__enable_weak_this(__r.__ptr_);
4349 return __r;
4350}
4351
4352template<class _Tp>
4353template<class _A0, class _A1, class _A2>
4354shared_ptr<_Tp>
4355shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4356{
4357 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4358 typedef allocator<_CntrlBlk> _Alloc2;
4359 typedef __allocator_destructor<_Alloc2> _D2;
4360 _Alloc2 __alloc2;
4361 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4362 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4363 shared_ptr<_Tp> __r;
4364 __r.__ptr_ = __hold2.get()->get();
4365 __r.__cntrl_ = __hold2.release();
4366 __r.__enable_weak_this(__r.__ptr_);
4367 return __r;
4368}
4369
4370template<class _Tp>
4371template<class _Alloc>
4372shared_ptr<_Tp>
4373shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4374{
4375 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4376 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4377 typedef __allocator_destructor<_Alloc2> _D2;
4378 _Alloc2 __alloc2(__a);
4379 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4380 ::new(__hold2.get()) _CntrlBlk(__a);
4381 shared_ptr<_Tp> __r;
4382 __r.__ptr_ = __hold2.get()->get();
4383 __r.__cntrl_ = __hold2.release();
4384 __r.__enable_weak_this(__r.__ptr_);
4385 return __r;
4386}
4387
4388template<class _Tp>
4389template<class _Alloc, class _A0>
4390shared_ptr<_Tp>
4391shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4392{
4393 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4394 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4395 typedef __allocator_destructor<_Alloc2> _D2;
4396 _Alloc2 __alloc2(__a);
4397 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4398 ::new(__hold2.get()) _CntrlBlk(__a, __a0);
4399 shared_ptr<_Tp> __r;
4400 __r.__ptr_ = __hold2.get()->get();
4401 __r.__cntrl_ = __hold2.release();
4402 __r.__enable_weak_this(__r.__ptr_);
4403 return __r;
4404}
4405
4406template<class _Tp>
4407template<class _Alloc, class _A0, class _A1>
4408shared_ptr<_Tp>
4409shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4410{
4411 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4412 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4413 typedef __allocator_destructor<_Alloc2> _D2;
4414 _Alloc2 __alloc2(__a);
4415 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4416 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
4417 shared_ptr<_Tp> __r;
4418 __r.__ptr_ = __hold2.get()->get();
4419 __r.__cntrl_ = __hold2.release();
4420 __r.__enable_weak_this(__r.__ptr_);
4421 return __r;
4422}
4423
4424template<class _Tp>
4425template<class _Alloc, class _A0, class _A1, class _A2>
4426shared_ptr<_Tp>
4427shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4428{
4429 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4430 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4431 typedef __allocator_destructor<_Alloc2> _D2;
4432 _Alloc2 __alloc2(__a);
4433 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4434 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
4435 shared_ptr<_Tp> __r;
4436 __r.__ptr_ = __hold2.get()->get();
4437 __r.__cntrl_ = __hold2.release();
4438 __r.__enable_weak_this(__r.__ptr_);
4439 return __r;
4440}
4441
4442#endif // _LIBCPP_HAS_NO_VARIADICS
4443
4444template<class _Tp>
4445shared_ptr<_Tp>::~shared_ptr()
4446{
4447 if (__cntrl_)
4448 __cntrl_->__release_shared();
4449}
4450
4451template<class _Tp>
4452inline _LIBCPP_INLINE_VISIBILITY
4453shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004454shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004455{
4456 shared_ptr(__r).swap(*this);
4457 return *this;
4458}
4459
4460template<class _Tp>
4461template<class _Yp>
4462inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004463typename enable_if
4464<
4465 is_convertible<_Yp*, _Tp*>::value,
4466 shared_ptr<_Tp>&
4467>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004468shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004469{
4470 shared_ptr(__r).swap(*this);
4471 return *this;
4472}
4473
Howard Hinnant73d21a42010-09-04 23:28:19 +00004474#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004475
4476template<class _Tp>
4477inline _LIBCPP_INLINE_VISIBILITY
4478shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004479shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004480{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004481 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004482 return *this;
4483}
4484
4485template<class _Tp>
4486template<class _Yp>
4487inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004488typename enable_if
4489<
4490 is_convertible<_Yp*, _Tp*>::value,
4491 shared_ptr<_Tp>&
4492>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004493shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4494{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004495 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004496 return *this;
4497}
4498
4499template<class _Tp>
4500template<class _Yp>
4501inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004502typename enable_if
4503<
4504 !is_array<_Yp>::value &&
4505 is_convertible<_Yp*, _Tp*>::value,
Howard Hinnant37c4acf2013-09-13 23:56:00 +00004506 shared_ptr<_Tp>
4507>::type&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004508shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4509{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004510 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004511 return *this;
4512}
4513
4514template<class _Tp>
4515template <class _Yp, class _Dp>
4516inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004517typename enable_if
4518<
4519 !is_array<_Yp>::value &&
4520 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4521 shared_ptr<_Tp>&
4522>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004523shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4524{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004525 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004526 return *this;
4527}
4528
Howard Hinnant73d21a42010-09-04 23:28:19 +00004529#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004530
4531template<class _Tp>
4532template<class _Yp>
4533inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004534typename enable_if
4535<
4536 !is_array<_Yp>::value &&
4537 is_convertible<_Yp*, _Tp*>::value,
4538 shared_ptr<_Tp>&
4539>::type
Howard Hinnant324bb032010-08-22 00:02:43 +00004540shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004541{
4542 shared_ptr(__r).swap(*this);
4543 return *this;
4544}
4545
4546template<class _Tp>
4547template <class _Yp, class _Dp>
4548inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004549typename enable_if
4550<
4551 !is_array<_Yp>::value &&
4552 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4553 shared_ptr<_Tp>&
4554>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004555shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4556{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004557 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004558 return *this;
4559}
4560
Howard Hinnant73d21a42010-09-04 23:28:19 +00004561#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004562
4563template<class _Tp>
4564inline _LIBCPP_INLINE_VISIBILITY
4565void
Howard Hinnant1694d232011-05-28 14:41:13 +00004566shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004567{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004568 _VSTD::swap(__ptr_, __r.__ptr_);
4569 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004570}
4571
4572template<class _Tp>
4573inline _LIBCPP_INLINE_VISIBILITY
4574void
Howard Hinnant1694d232011-05-28 14:41:13 +00004575shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004576{
4577 shared_ptr().swap(*this);
4578}
4579
4580template<class _Tp>
4581template<class _Yp>
4582inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004583typename enable_if
4584<
4585 is_convertible<_Yp*, _Tp*>::value,
4586 void
4587>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004588shared_ptr<_Tp>::reset(_Yp* __p)
4589{
4590 shared_ptr(__p).swap(*this);
4591}
4592
4593template<class _Tp>
4594template<class _Yp, class _Dp>
4595inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004596typename enable_if
4597<
4598 is_convertible<_Yp*, _Tp*>::value,
4599 void
4600>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004601shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4602{
4603 shared_ptr(__p, __d).swap(*this);
4604}
4605
4606template<class _Tp>
4607template<class _Yp, class _Dp, class _Alloc>
4608inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004609typename enable_if
4610<
4611 is_convertible<_Yp*, _Tp*>::value,
4612 void
4613>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004614shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4615{
4616 shared_ptr(__p, __d, __a).swap(*this);
4617}
4618
4619#ifndef _LIBCPP_HAS_NO_VARIADICS
4620
Howard Hinnant324bb032010-08-22 00:02:43 +00004621template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004622inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004623typename enable_if
4624<
4625 !is_array<_Tp>::value,
4626 shared_ptr<_Tp>
4627>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004628make_shared(_Args&& ...__args)
4629{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004630 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004631}
4632
Howard Hinnant324bb032010-08-22 00:02:43 +00004633template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004634inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004635typename enable_if
4636<
4637 !is_array<_Tp>::value,
4638 shared_ptr<_Tp>
4639>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004640allocate_shared(const _Alloc& __a, _Args&& ...__args)
4641{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004642 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004643}
4644
4645#else // _LIBCPP_HAS_NO_VARIADICS
4646
4647template<class _Tp>
4648inline _LIBCPP_INLINE_VISIBILITY
4649shared_ptr<_Tp>
4650make_shared()
4651{
4652 return shared_ptr<_Tp>::make_shared();
4653}
4654
4655template<class _Tp, class _A0>
4656inline _LIBCPP_INLINE_VISIBILITY
4657shared_ptr<_Tp>
4658make_shared(_A0& __a0)
4659{
4660 return shared_ptr<_Tp>::make_shared(__a0);
4661}
4662
4663template<class _Tp, class _A0, class _A1>
4664inline _LIBCPP_INLINE_VISIBILITY
4665shared_ptr<_Tp>
4666make_shared(_A0& __a0, _A1& __a1)
4667{
4668 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4669}
4670
Howard Hinnant324bb032010-08-22 00:02:43 +00004671template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004672inline _LIBCPP_INLINE_VISIBILITY
4673shared_ptr<_Tp>
4674make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4675{
4676 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4677}
4678
4679template<class _Tp, class _Alloc>
4680inline _LIBCPP_INLINE_VISIBILITY
4681shared_ptr<_Tp>
4682allocate_shared(const _Alloc& __a)
4683{
4684 return shared_ptr<_Tp>::allocate_shared(__a);
4685}
4686
4687template<class _Tp, class _Alloc, class _A0>
4688inline _LIBCPP_INLINE_VISIBILITY
4689shared_ptr<_Tp>
4690allocate_shared(const _Alloc& __a, _A0& __a0)
4691{
4692 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4693}
4694
4695template<class _Tp, class _Alloc, class _A0, class _A1>
4696inline _LIBCPP_INLINE_VISIBILITY
4697shared_ptr<_Tp>
4698allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4699{
4700 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4701}
4702
4703template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4704inline _LIBCPP_INLINE_VISIBILITY
4705shared_ptr<_Tp>
4706allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4707{
4708 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4709}
4710
4711#endif // _LIBCPP_HAS_NO_VARIADICS
4712
4713template<class _Tp, class _Up>
4714inline _LIBCPP_INLINE_VISIBILITY
4715bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004716operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004717{
4718 return __x.get() == __y.get();
4719}
4720
4721template<class _Tp, class _Up>
4722inline _LIBCPP_INLINE_VISIBILITY
4723bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004724operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004725{
4726 return !(__x == __y);
4727}
4728
4729template<class _Tp, class _Up>
4730inline _LIBCPP_INLINE_VISIBILITY
4731bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004732operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004733{
Howard Hinnant3fadda32012-02-21 21:02:58 +00004734 typedef typename common_type<_Tp*, _Up*>::type _V;
4735 return less<_V>()(__x.get(), __y.get());
4736}
4737
4738template<class _Tp, class _Up>
4739inline _LIBCPP_INLINE_VISIBILITY
4740bool
4741operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4742{
4743 return __y < __x;
4744}
4745
4746template<class _Tp, class _Up>
4747inline _LIBCPP_INLINE_VISIBILITY
4748bool
4749operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4750{
4751 return !(__y < __x);
4752}
4753
4754template<class _Tp, class _Up>
4755inline _LIBCPP_INLINE_VISIBILITY
4756bool
4757operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4758{
4759 return !(__x < __y);
4760}
4761
4762template<class _Tp>
4763inline _LIBCPP_INLINE_VISIBILITY
4764bool
4765operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4766{
4767 return !__x;
4768}
4769
4770template<class _Tp>
4771inline _LIBCPP_INLINE_VISIBILITY
4772bool
4773operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4774{
4775 return !__x;
4776}
4777
4778template<class _Tp>
4779inline _LIBCPP_INLINE_VISIBILITY
4780bool
4781operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4782{
4783 return static_cast<bool>(__x);
4784}
4785
4786template<class _Tp>
4787inline _LIBCPP_INLINE_VISIBILITY
4788bool
4789operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4790{
4791 return static_cast<bool>(__x);
4792}
4793
4794template<class _Tp>
4795inline _LIBCPP_INLINE_VISIBILITY
4796bool
4797operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4798{
4799 return less<_Tp*>()(__x.get(), nullptr);
4800}
4801
4802template<class _Tp>
4803inline _LIBCPP_INLINE_VISIBILITY
4804bool
4805operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4806{
4807 return less<_Tp*>()(nullptr, __x.get());
4808}
4809
4810template<class _Tp>
4811inline _LIBCPP_INLINE_VISIBILITY
4812bool
4813operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4814{
4815 return nullptr < __x;
4816}
4817
4818template<class _Tp>
4819inline _LIBCPP_INLINE_VISIBILITY
4820bool
4821operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4822{
4823 return __x < nullptr;
4824}
4825
4826template<class _Tp>
4827inline _LIBCPP_INLINE_VISIBILITY
4828bool
4829operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4830{
4831 return !(nullptr < __x);
4832}
4833
4834template<class _Tp>
4835inline _LIBCPP_INLINE_VISIBILITY
4836bool
4837operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4838{
4839 return !(__x < nullptr);
4840}
4841
4842template<class _Tp>
4843inline _LIBCPP_INLINE_VISIBILITY
4844bool
4845operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4846{
4847 return !(__x < nullptr);
4848}
4849
4850template<class _Tp>
4851inline _LIBCPP_INLINE_VISIBILITY
4852bool
4853operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4854{
4855 return !(nullptr < __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004856}
4857
4858template<class _Tp>
4859inline _LIBCPP_INLINE_VISIBILITY
4860void
Howard Hinnant1694d232011-05-28 14:41:13 +00004861swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004862{
4863 __x.swap(__y);
4864}
4865
4866template<class _Tp, class _Up>
4867inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004868typename enable_if
4869<
4870 !is_array<_Tp>::value && !is_array<_Up>::value,
4871 shared_ptr<_Tp>
4872>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004873static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004874{
4875 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4876}
4877
4878template<class _Tp, class _Up>
4879inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004880typename enable_if
4881<
4882 !is_array<_Tp>::value && !is_array<_Up>::value,
4883 shared_ptr<_Tp>
4884>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004885dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004886{
4887 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4888 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4889}
4890
4891template<class _Tp, class _Up>
Howard Hinnant57199402012-01-02 17:56:02 +00004892typename enable_if
4893<
4894 is_array<_Tp>::value == is_array<_Up>::value,
4895 shared_ptr<_Tp>
4896>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004897const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004898{
Howard Hinnant57199402012-01-02 17:56:02 +00004899 typedef typename remove_extent<_Tp>::type _RTp;
4900 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004901}
4902
Howard Hinnantd4444702010-08-11 17:04:31 +00004903#ifndef _LIBCPP_NO_RTTI
4904
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004905template<class _Dp, class _Tp>
4906inline _LIBCPP_INLINE_VISIBILITY
4907_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00004908get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004909{
4910 return __p.template __get_deleter<_Dp>();
4911}
4912
Howard Hinnant324bb032010-08-22 00:02:43 +00004913#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00004914
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004915template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004916class _LIBCPP_TYPE_VIS_ONLY weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004917{
Howard Hinnant324bb032010-08-22 00:02:43 +00004918public:
4919 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004920private:
4921 element_type* __ptr_;
4922 __shared_weak_count* __cntrl_;
4923
Howard Hinnant324bb032010-08-22 00:02:43 +00004924public:
Howard Hinnant46e94932012-07-07 20:56:04 +00004925 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004926 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00004927 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4928 _NOEXCEPT;
4929 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004930 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00004931 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4932 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004933
Howard Hinnant57199402012-01-02 17:56:02 +00004934#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4935 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
4936 template<class _Yp> weak_ptr(weak_ptr<_Yp>&& __r,
4937 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4938 _NOEXCEPT;
4939#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00004940 ~weak_ptr();
4941
Howard Hinnant1694d232011-05-28 14:41:13 +00004942 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00004943 template<class _Yp>
4944 typename enable_if
4945 <
4946 is_convertible<_Yp*, element_type*>::value,
4947 weak_ptr&
4948 >::type
4949 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4950
4951#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4952
4953 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4954 template<class _Yp>
4955 typename enable_if
4956 <
4957 is_convertible<_Yp*, element_type*>::value,
4958 weak_ptr&
4959 >::type
4960 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4961
4962#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4963
4964 template<class _Yp>
4965 typename enable_if
4966 <
4967 is_convertible<_Yp*, element_type*>::value,
4968 weak_ptr&
4969 >::type
4970 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004971
Howard Hinnant1694d232011-05-28 14:41:13 +00004972 void swap(weak_ptr& __r) _NOEXCEPT;
4973 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004974
Howard Hinnant82894812010-09-22 16:48:34 +00004975 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004976 long use_count() const _NOEXCEPT
4977 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00004978 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004979 bool expired() const _NOEXCEPT
4980 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4981 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00004982 template<class _Up>
4983 _LIBCPP_INLINE_VISIBILITY
4984 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004985 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004986 template<class _Up>
4987 _LIBCPP_INLINE_VISIBILITY
4988 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004989 {return __cntrl_ < __r.__cntrl_;}
4990
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004991 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
4992 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004993};
4994
4995template<class _Tp>
4996inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00004997_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004998weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004999 : __ptr_(0),
5000 __cntrl_(0)
5001{
5002}
5003
5004template<class _Tp>
5005inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005006weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005007 : __ptr_(__r.__ptr_),
5008 __cntrl_(__r.__cntrl_)
5009{
5010 if (__cntrl_)
5011 __cntrl_->__add_weak();
5012}
5013
5014template<class _Tp>
5015template<class _Yp>
5016inline _LIBCPP_INLINE_VISIBILITY
5017weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005018 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005019 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005020 : __ptr_(__r.__ptr_),
5021 __cntrl_(__r.__cntrl_)
5022{
5023 if (__cntrl_)
5024 __cntrl_->__add_weak();
5025}
5026
5027template<class _Tp>
5028template<class _Yp>
5029inline _LIBCPP_INLINE_VISIBILITY
5030weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005031 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005032 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005033 : __ptr_(__r.__ptr_),
5034 __cntrl_(__r.__cntrl_)
5035{
5036 if (__cntrl_)
5037 __cntrl_->__add_weak();
5038}
5039
Howard Hinnant57199402012-01-02 17:56:02 +00005040#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5041
5042template<class _Tp>
5043inline _LIBCPP_INLINE_VISIBILITY
5044weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5045 : __ptr_(__r.__ptr_),
5046 __cntrl_(__r.__cntrl_)
5047{
5048 __r.__ptr_ = 0;
5049 __r.__cntrl_ = 0;
5050}
5051
5052template<class _Tp>
5053template<class _Yp>
5054inline _LIBCPP_INLINE_VISIBILITY
5055weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5056 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5057 _NOEXCEPT
5058 : __ptr_(__r.__ptr_),
5059 __cntrl_(__r.__cntrl_)
5060{
5061 __r.__ptr_ = 0;
5062 __r.__cntrl_ = 0;
5063}
5064
5065#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5066
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005067template<class _Tp>
5068weak_ptr<_Tp>::~weak_ptr()
5069{
5070 if (__cntrl_)
5071 __cntrl_->__release_weak();
5072}
5073
5074template<class _Tp>
5075inline _LIBCPP_INLINE_VISIBILITY
5076weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00005077weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005078{
5079 weak_ptr(__r).swap(*this);
5080 return *this;
5081}
5082
5083template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005084template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005085inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005086typename enable_if
5087<
5088 is_convertible<_Yp*, _Tp*>::value,
5089 weak_ptr<_Tp>&
5090>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005091weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005092{
5093 weak_ptr(__r).swap(*this);
5094 return *this;
5095}
5096
Howard Hinnant57199402012-01-02 17:56:02 +00005097#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5098
5099template<class _Tp>
5100inline _LIBCPP_INLINE_VISIBILITY
5101weak_ptr<_Tp>&
5102weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5103{
5104 weak_ptr(_VSTD::move(__r)).swap(*this);
5105 return *this;
5106}
5107
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005108template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005109template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005110inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005111typename enable_if
5112<
5113 is_convertible<_Yp*, _Tp*>::value,
5114 weak_ptr<_Tp>&
5115>::type
5116weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5117{
5118 weak_ptr(_VSTD::move(__r)).swap(*this);
5119 return *this;
5120}
5121
5122#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5123
5124template<class _Tp>
5125template<class _Yp>
5126inline _LIBCPP_INLINE_VISIBILITY
5127typename enable_if
5128<
5129 is_convertible<_Yp*, _Tp*>::value,
5130 weak_ptr<_Tp>&
5131>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005132weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005133{
5134 weak_ptr(__r).swap(*this);
5135 return *this;
5136}
5137
5138template<class _Tp>
5139inline _LIBCPP_INLINE_VISIBILITY
5140void
Howard Hinnant1694d232011-05-28 14:41:13 +00005141weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005142{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005143 _VSTD::swap(__ptr_, __r.__ptr_);
5144 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005145}
5146
5147template<class _Tp>
5148inline _LIBCPP_INLINE_VISIBILITY
5149void
Howard Hinnant1694d232011-05-28 14:41:13 +00005150swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005151{
5152 __x.swap(__y);
5153}
5154
5155template<class _Tp>
5156inline _LIBCPP_INLINE_VISIBILITY
5157void
Howard Hinnant1694d232011-05-28 14:41:13 +00005158weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005159{
5160 weak_ptr().swap(*this);
5161}
5162
5163template<class _Tp>
5164template<class _Yp>
5165shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5166 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
5167 : __ptr_(__r.__ptr_),
5168 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5169{
5170 if (__cntrl_ == 0)
5171#ifndef _LIBCPP_NO_EXCEPTIONS
5172 throw bad_weak_ptr();
5173#else
5174 assert(!"bad_weak_ptr");
5175#endif
5176}
5177
5178template<class _Tp>
5179shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00005180weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005181{
5182 shared_ptr<_Tp> __r;
5183 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5184 if (__r.__cntrl_)
5185 __r.__ptr_ = __ptr_;
5186 return __r;
5187}
5188
Howard Hinnant324bb032010-08-22 00:02:43 +00005189template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005190
5191template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005192struct _LIBCPP_TYPE_VIS_ONLY owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005193 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00005194{
5195 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005197 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5198 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005200 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5201 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005203 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5204 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00005205};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005206
5207template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005208struct _LIBCPP_TYPE_VIS_ONLY owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005209 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5210{
Howard Hinnant324bb032010-08-22 00:02:43 +00005211 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005212 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005213 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5214 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005215 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005216 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5217 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005219 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5220 {return __x.owner_before(__y);}
5221};
5222
5223template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005224class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005225{
5226 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00005227protected:
Howard Hinnant46e94932012-07-07 20:56:04 +00005228 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005229 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005231 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005232 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005233 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5234 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00005235 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005236 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00005237public:
Howard Hinnant82894812010-09-22 16:48:34 +00005238 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005239 shared_ptr<_Tp> shared_from_this()
5240 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00005241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005242 shared_ptr<_Tp const> shared_from_this() const
5243 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005244
5245 template <class _Up> friend class shared_ptr;
5246};
5247
Howard Hinnant21aefc32010-06-03 16:42:57 +00005248template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005249struct _LIBCPP_TYPE_VIS_ONLY hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00005250{
5251 typedef shared_ptr<_Tp> argument_type;
5252 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005254 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00005255 {
5256 return hash<_Tp*>()(__ptr.get());
5257 }
5258};
5259
Howard Hinnant99968442011-11-29 18:15:50 +00005260template<class _CharT, class _Traits, class _Yp>
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005261inline _LIBCPP_INLINE_VISIBILITY
5262basic_ostream<_CharT, _Traits>&
Howard Hinnant99968442011-11-29 18:15:50 +00005263operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005264
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005265#if __has_feature(cxx_atomic)
5266
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005267class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005268{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005269 void* __lx;
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005270public:
5271 void lock() _NOEXCEPT;
5272 void unlock() _NOEXCEPT;
5273
5274private:
5275 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5276 __sp_mut(const __sp_mut&);
5277 __sp_mut& operator=(const __sp_mut&);
5278
Howard Hinnant83eade62013-03-06 23:30:19 +00005279 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005280};
5281
Howard Hinnant83eade62013-03-06 23:30:19 +00005282_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005283
5284template <class _Tp>
5285inline _LIBCPP_INLINE_VISIBILITY
5286bool
5287atomic_is_lock_free(const shared_ptr<_Tp>*)
5288{
5289 return false;
5290}
5291
5292template <class _Tp>
5293shared_ptr<_Tp>
5294atomic_load(const shared_ptr<_Tp>* __p)
5295{
5296 __sp_mut& __m = __get_sp_mut(__p);
5297 __m.lock();
5298 shared_ptr<_Tp> __q = *__p;
5299 __m.unlock();
5300 return __q;
5301}
5302
5303template <class _Tp>
5304inline _LIBCPP_INLINE_VISIBILITY
5305shared_ptr<_Tp>
5306atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5307{
5308 return atomic_load(__p);
5309}
5310
5311template <class _Tp>
5312void
5313atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5314{
5315 __sp_mut& __m = __get_sp_mut(__p);
5316 __m.lock();
5317 __p->swap(__r);
5318 __m.unlock();
5319}
5320
5321template <class _Tp>
5322inline _LIBCPP_INLINE_VISIBILITY
5323void
5324atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5325{
5326 atomic_store(__p, __r);
5327}
5328
5329template <class _Tp>
5330shared_ptr<_Tp>
5331atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5332{
5333 __sp_mut& __m = __get_sp_mut(__p);
5334 __m.lock();
5335 __p->swap(__r);
5336 __m.unlock();
5337 return __r;
5338}
5339
5340template <class _Tp>
5341inline _LIBCPP_INLINE_VISIBILITY
5342shared_ptr<_Tp>
5343atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5344{
5345 return atomic_exchange(__p, __r);
5346}
5347
5348template <class _Tp>
5349bool
5350atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5351{
5352 __sp_mut& __m = __get_sp_mut(__p);
5353 __m.lock();
5354 if (__p->__owner_equivalent(*__v))
5355 {
5356 *__p = __w;
5357 __m.unlock();
5358 return true;
5359 }
5360 *__v = *__p;
5361 __m.unlock();
5362 return false;
5363}
5364
5365template <class _Tp>
5366inline _LIBCPP_INLINE_VISIBILITY
5367bool
5368atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5369{
5370 return atomic_compare_exchange_strong(__p, __v, __w);
5371}
5372
5373template <class _Tp>
5374inline _LIBCPP_INLINE_VISIBILITY
5375bool
5376atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5377 shared_ptr<_Tp> __w, memory_order, memory_order)
5378{
5379 return atomic_compare_exchange_strong(__p, __v, __w);
5380}
5381
5382template <class _Tp>
5383inline _LIBCPP_INLINE_VISIBILITY
5384bool
5385atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5386 shared_ptr<_Tp> __w, memory_order, memory_order)
5387{
5388 return atomic_compare_exchange_weak(__p, __v, __w);
5389}
5390
5391#endif // __has_feature(cxx_atomic)
5392
Howard Hinnant324bb032010-08-22 00:02:43 +00005393//enum class
Howard Hinnant83eade62013-03-06 23:30:19 +00005394struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005395{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005396 enum __lx
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005397 {
5398 relaxed,
5399 preferred,
5400 strict
5401 };
5402
Howard Hinnant9c0df142012-10-30 19:06:59 +00005403 __lx __v_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005404
Howard Hinnant82894812010-09-22 16:48:34 +00005405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9c0df142012-10-30 19:06:59 +00005406 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00005407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005408 operator int() const {return __v_;}
5409};
5410
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005411_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5412_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5413_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
5414_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5415_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005416
5417template <class _Tp>
5418inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005419_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005420undeclare_reachable(_Tp* __p)
5421{
5422 return static_cast<_Tp*>(__undeclare_reachable(__p));
5423}
5424
Howard Hinnant0f678bd2013-08-12 18:38:34 +00005425_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005426
5427_LIBCPP_END_NAMESPACE_STD
5428
5429#endif // _LIBCPP_MEMORY