blob: ffd0cd0ce77d5b19e427c25b33d84145b42079eb [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
93 static size_type max_size(const allocator_type& a);
94
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;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000499 template<class U> bool owner_before(shared_ptr<U> const& b);
500 template<class U> bool owner_before(weak_ptr<U> const& b);
501};
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 Hinnantbc8d3f92010-05-11 19:42:16 +0000621// addressof
622
623template <class _Tp>
624inline _LIBCPP_INLINE_VISIBILITY
625_Tp*
Howard Hinnant1694d232011-05-28 14:41:13 +0000626addressof(_Tp& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000627{
Howard Hinnant4313ec32013-04-16 17:27:56 +0000628 return (_Tp*)&reinterpret_cast<const volatile char&>(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000629}
630
Douglas Gregor35d2fcf2011-06-22 22:17:44 +0000631#if defined(_LIBCPP_HAS_OBJC_ARC) && !defined(_LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF)
632// Objective-C++ Automatic Reference Counting uses qualified pointers
633// that require special addressof() signatures. When
634// _LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF is defined, the compiler
635// itself is providing these definitions. Otherwise, we provide them.
636template <class _Tp>
637inline _LIBCPP_INLINE_VISIBILITY
638__strong _Tp*
639addressof(__strong _Tp& __x) _NOEXCEPT
640{
641 return &__x;
642}
643
644#ifdef _LIBCPP_HAS_OBJC_ARC_WEAK
645template <class _Tp>
646inline _LIBCPP_INLINE_VISIBILITY
647__weak _Tp*
648addressof(__weak _Tp& __x) _NOEXCEPT
649{
650 return &__x;
651}
652#endif
653
654template <class _Tp>
655inline _LIBCPP_INLINE_VISIBILITY
656__autoreleasing _Tp*
657addressof(__autoreleasing _Tp& __x) _NOEXCEPT
658{
659 return &__x;
660}
661
662template <class _Tp>
663inline _LIBCPP_INLINE_VISIBILITY
664__unsafe_unretained _Tp*
665addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT
666{
667 return &__x;
668}
669#endif
670
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000671template <class _Tp> class allocator;
672
673template <>
Howard Hinnant83eade62013-03-06 23:30:19 +0000674class _LIBCPP_TYPE_VIS allocator<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000675{
676public:
677 typedef void* pointer;
678 typedef const void* const_pointer;
679 typedef void value_type;
680
681 template <class _Up> struct rebind {typedef allocator<_Up> other;};
682};
683
Howard Hinnanta1877872012-01-19 23:15:22 +0000684template <>
Howard Hinnant83eade62013-03-06 23:30:19 +0000685class _LIBCPP_TYPE_VIS allocator<const void>
Howard Hinnanta1877872012-01-19 23:15:22 +0000686{
687public:
688 typedef const void* pointer;
689 typedef const void* const_pointer;
690 typedef const void value_type;
691
692 template <class _Up> struct rebind {typedef allocator<_Up> other;};
693};
694
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000695// pointer_traits
696
697template <class _Tp>
698struct __has_element_type
699{
700private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000701 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000702 template <class _Up> static __two __test(...);
703 template <class _Up> static char __test(typename _Up::element_type* = 0);
704public:
705 static const bool value = sizeof(__test<_Tp>(0)) == 1;
706};
707
708template <class _Ptr, bool = __has_element_type<_Ptr>::value>
709struct __pointer_traits_element_type;
710
711template <class _Ptr>
712struct __pointer_traits_element_type<_Ptr, true>
713{
714 typedef typename _Ptr::element_type type;
715};
716
717#ifndef _LIBCPP_HAS_NO_VARIADICS
718
719template <template <class, class...> class _Sp, class _Tp, class ..._Args>
720struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
721{
722 typedef typename _Sp<_Tp, _Args...>::element_type type;
723};
724
725template <template <class, class...> class _Sp, class _Tp, class ..._Args>
726struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
727{
728 typedef _Tp type;
729};
730
Howard Hinnant324bb032010-08-22 00:02:43 +0000731#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000732
733template <template <class> class _Sp, class _Tp>
734struct __pointer_traits_element_type<_Sp<_Tp>, true>
735{
736 typedef typename _Sp<_Tp>::element_type type;
737};
738
739template <template <class> class _Sp, class _Tp>
740struct __pointer_traits_element_type<_Sp<_Tp>, false>
741{
742 typedef _Tp type;
743};
744
745template <template <class, class> class _Sp, class _Tp, class _A0>
746struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
747{
748 typedef typename _Sp<_Tp, _A0>::element_type type;
749};
750
751template <template <class, class> class _Sp, class _Tp, class _A0>
752struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
753{
754 typedef _Tp type;
755};
756
757template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
758struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
759{
760 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
761};
762
763template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
764struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
765{
766 typedef _Tp type;
767};
768
769template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
770 class _A1, class _A2>
771struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
772{
773 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
774};
775
776template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
777 class _A1, class _A2>
778struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
779{
780 typedef _Tp type;
781};
782
Howard Hinnant324bb032010-08-22 00:02:43 +0000783#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000784
785template <class _Tp>
786struct __has_difference_type
787{
788private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000789 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000790 template <class _Up> static __two __test(...);
791 template <class _Up> static char __test(typename _Up::difference_type* = 0);
792public:
793 static const bool value = sizeof(__test<_Tp>(0)) == 1;
794};
795
796template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
797struct __pointer_traits_difference_type
798{
799 typedef ptrdiff_t type;
800};
801
802template <class _Ptr>
803struct __pointer_traits_difference_type<_Ptr, true>
804{
805 typedef typename _Ptr::difference_type type;
806};
807
808template <class _Tp, class _Up>
809struct __has_rebind
810{
811private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000812 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000813 template <class _Xp> static __two __test(...);
814 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
815public:
816 static const bool value = sizeof(__test<_Tp>(0)) == 1;
817};
818
819template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
820struct __pointer_traits_rebind
821{
822#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
823 typedef typename _Tp::template rebind<_Up> type;
824#else
825 typedef typename _Tp::template rebind<_Up>::other type;
826#endif
827};
828
829#ifndef _LIBCPP_HAS_NO_VARIADICS
830
831template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
832struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
833{
834#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
835 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
836#else
837 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
838#endif
839};
840
841template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
842struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
843{
844 typedef _Sp<_Up, _Args...> type;
845};
846
Howard Hinnant324bb032010-08-22 00:02:43 +0000847#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000848
849template <template <class> class _Sp, class _Tp, class _Up>
850struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
851{
852#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
853 typedef typename _Sp<_Tp>::template rebind<_Up> type;
854#else
855 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
856#endif
857};
858
859template <template <class> class _Sp, class _Tp, class _Up>
860struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
861{
862 typedef _Sp<_Up> type;
863};
864
865template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
866struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
867{
868#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
869 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
870#else
871 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
872#endif
873};
874
875template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
876struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
877{
878 typedef _Sp<_Up, _A0> type;
879};
880
881template <template <class, class, class> class _Sp, class _Tp, class _A0,
882 class _A1, class _Up>
883struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
884{
885#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
886 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
887#else
888 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
889#endif
890};
891
892template <template <class, class, class> class _Sp, class _Tp, class _A0,
893 class _A1, class _Up>
894struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
895{
896 typedef _Sp<_Up, _A0, _A1> type;
897};
898
899template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
900 class _A1, class _A2, class _Up>
901struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
902{
903#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
904 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
905#else
906 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
907#endif
908};
909
910template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
911 class _A1, class _A2, class _Up>
912struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
913{
914 typedef _Sp<_Up, _A0, _A1, _A2> type;
915};
916
Howard Hinnant324bb032010-08-22 00:02:43 +0000917#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000918
919template <class _Ptr>
Howard Hinnant83eade62013-03-06 23:30:19 +0000920struct _LIBCPP_TYPE_VIS pointer_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000921{
922 typedef _Ptr pointer;
923 typedef typename __pointer_traits_element_type<pointer>::type element_type;
924 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
925
926#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant6b41c602011-05-11 20:21:19 +0000927 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000928#else
929 template <class _Up> struct rebind
930 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant324bb032010-08-22 00:02:43 +0000931#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000932
933private:
934 struct __nat {};
935public:
Howard Hinnant82894812010-09-22 16:48:34 +0000936 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000937 static pointer pointer_to(typename conditional<is_void<element_type>::value,
938 __nat, element_type>::type& __r)
939 {return pointer::pointer_to(__r);}
940};
941
942template <class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +0000943struct _LIBCPP_TYPE_VIS pointer_traits<_Tp*>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000944{
945 typedef _Tp* pointer;
946 typedef _Tp element_type;
947 typedef ptrdiff_t difference_type;
948
949#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
950 template <class _Up> using rebind = _Up*;
951#else
952 template <class _Up> struct rebind {typedef _Up* other;};
953#endif
954
955private:
956 struct __nat {};
957public:
Howard Hinnant82894812010-09-22 16:48:34 +0000958 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000959 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant1694d232011-05-28 14:41:13 +0000960 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000961 {return _VSTD::addressof(__r);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000962};
963
964// allocator_traits
965
966namespace __has_pointer_type_imp
967{
968 template <class _Up> static __two test(...);
969 template <class _Up> static char test(typename _Up::pointer* = 0);
970}
971
972template <class _Tp>
973struct __has_pointer_type
974 : public integral_constant<bool, sizeof(__has_pointer_type_imp::test<_Tp>(0)) == 1>
975{
976};
977
978namespace __pointer_type_imp
979{
980
981template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
982struct __pointer_type
983{
984 typedef typename _Dp::pointer type;
985};
986
987template <class _Tp, class _Dp>
988struct __pointer_type<_Tp, _Dp, false>
989{
990 typedef _Tp* type;
991};
992
Howard Hinnant47761072010-11-18 01:40:00 +0000993} // __pointer_type_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000994
995template <class _Tp, class _Dp>
996struct __pointer_type
997{
998 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
999};
1000
1001template <class _Tp>
1002struct __has_const_pointer
1003{
1004private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001005 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001006 template <class _Up> static __two __test(...);
1007 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
1008public:
1009 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1010};
1011
1012template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1013struct __const_pointer
1014{
1015 typedef typename _Alloc::const_pointer type;
1016};
1017
1018template <class _Tp, class _Ptr, class _Alloc>
1019struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1020{
1021#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1022 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1023#else
1024 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1025#endif
1026};
1027
1028template <class _Tp>
1029struct __has_void_pointer
1030{
1031private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001032 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001033 template <class _Up> static __two __test(...);
1034 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1035public:
1036 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1037};
1038
1039template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1040struct __void_pointer
1041{
1042 typedef typename _Alloc::void_pointer type;
1043};
1044
1045template <class _Ptr, class _Alloc>
1046struct __void_pointer<_Ptr, _Alloc, false>
1047{
1048#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1049 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1050#else
1051 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1052#endif
1053};
1054
1055template <class _Tp>
1056struct __has_const_void_pointer
1057{
1058private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001059 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001060 template <class _Up> static __two __test(...);
1061 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1062public:
1063 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1064};
1065
1066template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1067struct __const_void_pointer
1068{
1069 typedef typename _Alloc::const_void_pointer type;
1070};
1071
1072template <class _Ptr, class _Alloc>
1073struct __const_void_pointer<_Ptr, _Alloc, false>
1074{
1075#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1076 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1077#else
1078 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1079#endif
1080};
1081
Howard Hinnant99968442011-11-29 18:15:50 +00001082template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001083inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001084_Tp*
1085__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001086{
1087 return __p;
1088}
1089
1090template <class _Pointer>
1091inline _LIBCPP_INLINE_VISIBILITY
1092typename pointer_traits<_Pointer>::element_type*
Howard Hinnant1694d232011-05-28 14:41:13 +00001093__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001094{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001095 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001096}
1097
1098template <class _Tp>
1099struct __has_size_type
1100{
1101private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001102 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001103 template <class _Up> static __two __test(...);
1104 template <class _Up> static char __test(typename _Up::size_type* = 0);
1105public:
1106 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1107};
1108
Howard Hinnant47761072010-11-18 01:40:00 +00001109template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001110struct __size_type
1111{
Howard Hinnant47761072010-11-18 01:40:00 +00001112 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001113};
1114
Howard Hinnant47761072010-11-18 01:40:00 +00001115template <class _Alloc, class _DiffType>
1116struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001117{
1118 typedef typename _Alloc::size_type type;
1119};
1120
1121template <class _Tp>
1122struct __has_propagate_on_container_copy_assignment
1123{
1124private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001125 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001126 template <class _Up> static __two __test(...);
1127 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1128public:
1129 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1130};
1131
1132template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1133struct __propagate_on_container_copy_assignment
1134{
1135 typedef false_type type;
1136};
1137
1138template <class _Alloc>
1139struct __propagate_on_container_copy_assignment<_Alloc, true>
1140{
1141 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1142};
1143
1144template <class _Tp>
1145struct __has_propagate_on_container_move_assignment
1146{
1147private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001148 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001149 template <class _Up> static __two __test(...);
1150 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1151public:
1152 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1153};
1154
1155template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1156struct __propagate_on_container_move_assignment
1157{
1158 typedef false_type type;
1159};
1160
1161template <class _Alloc>
1162struct __propagate_on_container_move_assignment<_Alloc, true>
1163{
1164 typedef typename _Alloc::propagate_on_container_move_assignment type;
1165};
1166
1167template <class _Tp>
1168struct __has_propagate_on_container_swap
1169{
1170private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001171 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001172 template <class _Up> static __two __test(...);
1173 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1174public:
1175 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1176};
1177
1178template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1179struct __propagate_on_container_swap
1180{
1181 typedef false_type type;
1182};
1183
1184template <class _Alloc>
1185struct __propagate_on_container_swap<_Alloc, true>
1186{
1187 typedef typename _Alloc::propagate_on_container_swap type;
1188};
1189
1190template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1191struct __has_rebind_other
1192{
1193private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001194 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001195 template <class _Xp> static __two __test(...);
1196 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1197public:
1198 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1199};
1200
1201template <class _Tp, class _Up>
1202struct __has_rebind_other<_Tp, _Up, false>
1203{
1204 static const bool value = false;
1205};
1206
1207template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1208struct __allocator_traits_rebind
1209{
1210 typedef typename _Tp::template rebind<_Up>::other type;
1211};
1212
1213#ifndef _LIBCPP_HAS_NO_VARIADICS
1214
1215template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1216struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1217{
1218 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1219};
1220
1221template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1222struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1223{
1224 typedef _Alloc<_Up, _Args...> type;
1225};
1226
Howard Hinnant324bb032010-08-22 00:02:43 +00001227#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001228
1229template <template <class> class _Alloc, class _Tp, class _Up>
1230struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1231{
1232 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1233};
1234
1235template <template <class> class _Alloc, class _Tp, class _Up>
1236struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1237{
1238 typedef _Alloc<_Up> type;
1239};
1240
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001241template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1242struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1243{
1244 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1245};
1246
1247template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1248struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1249{
1250 typedef _Alloc<_Up, _A0> type;
1251};
1252
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001253template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1254 class _A1, class _Up>
1255struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1256{
1257 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1258};
1259
1260template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1261 class _A1, class _Up>
1262struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1263{
1264 typedef _Alloc<_Up, _A0, _A1> type;
1265};
1266
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001267template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1268 class _A1, class _A2, class _Up>
1269struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1270{
1271 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1272};
1273
1274template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1275 class _A1, class _A2, class _Up>
1276struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1277{
1278 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1279};
1280
Howard Hinnant324bb032010-08-22 00:02:43 +00001281#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001282
1283#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1284
1285template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1286auto
1287__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1288 -> decltype(__a.allocate(__sz, __p), true_type());
1289
1290template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1291auto
1292__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1293 -> false_type;
1294
1295template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1296struct __has_allocate_hint
1297 : integral_constant<bool,
1298 is_same<
1299 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1300 declval<_SizeType>(),
1301 declval<_ConstVoidPtr>())),
1302 true_type>::value>
1303{
1304};
1305
Howard Hinnant324bb032010-08-22 00:02:43 +00001306#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001307
1308template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1309struct __has_allocate_hint
1310 : true_type
1311{
1312};
1313
Howard Hinnant324bb032010-08-22 00:02:43 +00001314#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001315
Howard Hinnant23369ee2011-07-29 21:35:53 +00001316#if !defined(_LIBCPP_HAS_NO_ADVANCED_SFINAE) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001317
1318template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnant0949eed2011-06-30 21:18:19 +00001319decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1320 _VSTD::declval<_Args>()...),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001321 true_type())
1322__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1323
1324template <class _Alloc, class _Pointer, class ..._Args>
1325false_type
1326__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1327
1328template <class _Alloc, class _Pointer, class ..._Args>
1329struct __has_construct
1330 : integral_constant<bool,
1331 is_same<
1332 decltype(__has_construct_test(declval<_Alloc>(),
1333 declval<_Pointer>(),
1334 declval<_Args>()...)),
1335 true_type>::value>
1336{
1337};
1338
1339template <class _Alloc, class _Pointer>
1340auto
1341__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1342 -> decltype(__a.destroy(__p), true_type());
1343
1344template <class _Alloc, class _Pointer>
1345auto
1346__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1347 -> false_type;
1348
1349template <class _Alloc, class _Pointer>
1350struct __has_destroy
1351 : integral_constant<bool,
1352 is_same<
1353 decltype(__has_destroy_test(declval<_Alloc>(),
1354 declval<_Pointer>())),
1355 true_type>::value>
1356{
1357};
1358
1359template <class _Alloc>
1360auto
1361__has_max_size_test(_Alloc&& __a)
1362 -> decltype(__a.max_size(), true_type());
1363
1364template <class _Alloc>
1365auto
1366__has_max_size_test(const volatile _Alloc& __a)
1367 -> false_type;
1368
1369template <class _Alloc>
1370struct __has_max_size
1371 : integral_constant<bool,
1372 is_same<
1373 decltype(__has_max_size_test(declval<_Alloc&>())),
1374 true_type>::value>
1375{
1376};
1377
1378template <class _Alloc>
1379auto
1380__has_select_on_container_copy_construction_test(_Alloc&& __a)
1381 -> decltype(__a.select_on_container_copy_construction(), true_type());
1382
1383template <class _Alloc>
1384auto
1385__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1386 -> false_type;
1387
1388template <class _Alloc>
1389struct __has_select_on_container_copy_construction
1390 : integral_constant<bool,
1391 is_same<
1392 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1393 true_type>::value>
1394{
1395};
1396
Howard Hinnant324bb032010-08-22 00:02:43 +00001397#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001398
1399#ifndef _LIBCPP_HAS_NO_VARIADICS
1400
1401template <class _Alloc, class _Pointer, class ..._Args>
1402struct __has_construct
1403 : false_type
1404{
1405};
1406
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001407#else // _LIBCPP_HAS_NO_VARIADICS
1408
1409template <class _Alloc, class _Pointer, class _Args>
1410struct __has_construct
1411 : false_type
1412{
1413};
1414
Howard Hinnant324bb032010-08-22 00:02:43 +00001415#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001416
1417template <class _Alloc, class _Pointer>
1418struct __has_destroy
1419 : false_type
1420{
1421};
1422
1423template <class _Alloc>
1424struct __has_max_size
1425 : true_type
1426{
1427};
1428
1429template <class _Alloc>
1430struct __has_select_on_container_copy_construction
1431 : false_type
1432{
1433};
1434
Howard Hinnant324bb032010-08-22 00:02:43 +00001435#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001436
Howard Hinnant47761072010-11-18 01:40:00 +00001437template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1438struct __alloc_traits_difference_type
1439{
1440 typedef typename pointer_traits<_Ptr>::difference_type type;
1441};
1442
1443template <class _Alloc, class _Ptr>
1444struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1445{
1446 typedef typename _Alloc::difference_type type;
1447};
1448
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001449template <class _Alloc>
Howard Hinnant83eade62013-03-06 23:30:19 +00001450struct _LIBCPP_TYPE_VIS allocator_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001451{
1452 typedef _Alloc allocator_type;
1453 typedef typename allocator_type::value_type value_type;
1454
1455 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1456 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1457 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1458 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1459
Howard Hinnant47761072010-11-18 01:40:00 +00001460 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1461 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001462
1463 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1464 propagate_on_container_copy_assignment;
1465 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1466 propagate_on_container_move_assignment;
1467 typedef typename __propagate_on_container_swap<allocator_type>::type
1468 propagate_on_container_swap;
1469
1470#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1471 template <class _Tp> using rebind_alloc =
Howard Hinnant6b41c602011-05-11 20:21:19 +00001472 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001473 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant324bb032010-08-22 00:02:43 +00001474#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001475 template <class _Tp> struct rebind_alloc
1476 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1477 template <class _Tp> struct rebind_traits
1478 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant324bb032010-08-22 00:02:43 +00001479#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001480
Howard Hinnant82894812010-09-22 16:48:34 +00001481 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001482 static pointer allocate(allocator_type& __a, size_type __n)
1483 {return __a.allocate(__n);}
Howard Hinnant82894812010-09-22 16:48:34 +00001484 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001485 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1486 {return allocate(__a, __n, __hint,
1487 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1488
Howard Hinnant82894812010-09-22 16:48:34 +00001489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001490 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001491 {__a.deallocate(__p, __n);}
1492
1493#ifndef _LIBCPP_HAS_NO_VARIADICS
1494 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001495 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001496 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
1497 {__construct(__has_construct<allocator_type, pointer, _Args...>(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001498 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001499#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001500 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001501 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001502 static void construct(allocator_type& __a, _Tp* __p)
1503 {
1504 ::new ((void*)__p) _Tp();
1505 }
1506 template <class _Tp, class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00001507 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001508 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1509 {
1510 ::new ((void*)__p) _Tp(__a0);
1511 }
1512 template <class _Tp, class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00001513 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001514 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1515 const _A1& __a1)
1516 {
1517 ::new ((void*)__p) _Tp(__a0, __a1);
1518 }
1519 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00001520 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001521 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1522 const _A1& __a1, const _A2& __a2)
1523 {
1524 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1525 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001526#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001527
1528 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530 static void destroy(allocator_type& __a, _Tp* __p)
1531 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1532
Howard Hinnant82894812010-09-22 16:48:34 +00001533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001534 static size_type max_size(const allocator_type& __a)
1535 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1536
Howard Hinnant82894812010-09-22 16:48:34 +00001537 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001538 static allocator_type
1539 select_on_container_copy_construction(const allocator_type& __a)
1540 {return select_on_container_copy_construction(
1541 __has_select_on_container_copy_construction<const allocator_type>(),
1542 __a);}
1543
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001544 template <class _Ptr>
1545 _LIBCPP_INLINE_VISIBILITY
1546 static
1547 void
1548 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1549 {
1550 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1551 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1552 }
1553
1554 template <class _Tp>
1555 _LIBCPP_INLINE_VISIBILITY
1556 static
1557 typename enable_if
1558 <
1559 (is_same<allocator_type, allocator<_Tp> >::value
1560 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1561 is_trivially_move_constructible<_Tp>::value,
1562 void
1563 >::type
1564 __construct_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1565 {
1566 ptrdiff_t _Np = __end1 - __begin1;
1567 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
1568 __begin2 += _Np;
1569 }
1570
1571 template <class _Ptr>
1572 _LIBCPP_INLINE_VISIBILITY
1573 static
1574 void
1575 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1576 {
1577 while (__end1 != __begin1)
Howard Hinnantf619e232013-01-11 20:36:59 +00001578 {
1579 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1580 --__end2;
1581 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001582 }
1583
1584 template <class _Tp>
1585 _LIBCPP_INLINE_VISIBILITY
1586 static
1587 typename enable_if
1588 <
1589 (is_same<allocator_type, allocator<_Tp> >::value
1590 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1591 is_trivially_move_constructible<_Tp>::value,
1592 void
1593 >::type
1594 __construct_backward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
1595 {
1596 ptrdiff_t _Np = __end1 - __begin1;
1597 __end2 -= _Np;
1598 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
1599 }
1600
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001601private:
1602
Howard Hinnant82894812010-09-22 16:48:34 +00001603 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001604 static pointer allocate(allocator_type& __a, size_type __n,
1605 const_void_pointer __hint, true_type)
1606 {return __a.allocate(__n, __hint);}
Howard Hinnant82894812010-09-22 16:48:34 +00001607 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001608 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnantec3773c2011-12-01 20:21:04 +00001609 const_void_pointer, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001610 {return __a.allocate(__n);}
1611
1612#ifndef _LIBCPP_HAS_NO_VARIADICS
1613 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001614 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001615 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001616 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001617 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001618 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001619 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1620 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001621 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001622 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001623#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001624
1625 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001626 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001627 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1628 {__a.destroy(__p);}
1629 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001630 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001631 static void __destroy(false_type, allocator_type&, _Tp* __p)
1632 {
1633 __p->~_Tp();
1634 }
1635
Howard Hinnant82894812010-09-22 16:48:34 +00001636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001637 static size_type __max_size(true_type, const allocator_type& __a)
1638 {return __a.max_size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001639 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001640 static size_type __max_size(false_type, const allocator_type&)
1641 {return numeric_limits<size_type>::max();}
1642
Howard Hinnant82894812010-09-22 16:48:34 +00001643 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001644 static allocator_type
1645 select_on_container_copy_construction(true_type, const allocator_type& __a)
1646 {return __a.select_on_container_copy_construction();}
Howard Hinnant82894812010-09-22 16:48:34 +00001647 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001648 static allocator_type
1649 select_on_container_copy_construction(false_type, const allocator_type& __a)
1650 {return __a;}
1651};
1652
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001653// allocator
1654
1655template <class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00001656class _LIBCPP_TYPE_VIS allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001657{
1658public:
1659 typedef size_t size_type;
1660 typedef ptrdiff_t difference_type;
1661 typedef _Tp* pointer;
1662 typedef const _Tp* const_pointer;
1663 typedef _Tp& reference;
1664 typedef const _Tp& const_reference;
1665 typedef _Tp value_type;
1666
Howard Hinnant18884f42011-06-02 21:38:57 +00001667 typedef true_type propagate_on_container_move_assignment;
1668
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001669 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1670
Howard Hinnant1694d232011-05-28 14:41:13 +00001671 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1672 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1673 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001674 {return _VSTD::addressof(__x);}
Howard Hinnant1694d232011-05-28 14:41:13 +00001675 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001676 {return _VSTD::addressof(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001677 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1678 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
Howard Hinnant1694d232011-05-28 14:41:13 +00001679 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
1680 {::operator delete((void*)__p);}
1681 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1682 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001683#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001684 template <class _Up, class... _Args>
1685 _LIBCPP_INLINE_VISIBILITY
1686 void
1687 construct(_Up* __p, _Args&&... __args)
1688 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001689 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001690 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001691#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001692 _LIBCPP_INLINE_VISIBILITY
1693 void
1694 construct(pointer __p)
1695 {
1696 ::new((void*)__p) _Tp();
1697 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001698# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001699
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001700 template <class _A0>
1701 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001702 void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001703 construct(pointer __p, _A0& __a0)
1704 {
1705 ::new((void*)__p) _Tp(__a0);
1706 }
1707 template <class _A0>
1708 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001709 void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001710 construct(pointer __p, const _A0& __a0)
1711 {
1712 ::new((void*)__p) _Tp(__a0);
1713 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001714# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001715 template <class _A0, class _A1>
1716 _LIBCPP_INLINE_VISIBILITY
1717 void
1718 construct(pointer __p, _A0& __a0, _A1& __a1)
1719 {
1720 ::new((void*)__p) _Tp(__a0, __a1);
1721 }
1722 template <class _A0, class _A1>
1723 _LIBCPP_INLINE_VISIBILITY
1724 void
1725 construct(pointer __p, const _A0& __a0, _A1& __a1)
1726 {
1727 ::new((void*)__p) _Tp(__a0, __a1);
1728 }
1729 template <class _A0, class _A1>
1730 _LIBCPP_INLINE_VISIBILITY
1731 void
1732 construct(pointer __p, _A0& __a0, const _A1& __a1)
1733 {
1734 ::new((void*)__p) _Tp(__a0, __a1);
1735 }
1736 template <class _A0, class _A1>
1737 _LIBCPP_INLINE_VISIBILITY
1738 void
1739 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1740 {
1741 ::new((void*)__p) _Tp(__a0, __a1);
1742 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001743#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001744 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1745};
1746
Howard Hinnant57199402012-01-02 17:56:02 +00001747template <class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00001748class _LIBCPP_TYPE_VIS allocator<const _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00001749{
1750public:
1751 typedef size_t size_type;
1752 typedef ptrdiff_t difference_type;
1753 typedef const _Tp* pointer;
1754 typedef const _Tp* const_pointer;
1755 typedef const _Tp& reference;
1756 typedef const _Tp& const_reference;
Howard Hinnant9360e9f2013-06-07 01:56:37 +00001757 typedef const _Tp value_type;
Howard Hinnant57199402012-01-02 17:56:02 +00001758
1759 typedef true_type propagate_on_container_move_assignment;
1760
1761 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1762
1763 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1764 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1765 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1766 {return _VSTD::addressof(__x);}
1767 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1768 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
1769 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
1770 {::operator delete((void*)__p);}
1771 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1772 {return size_type(~0) / sizeof(_Tp);}
1773#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1774 template <class _Up, class... _Args>
1775 _LIBCPP_INLINE_VISIBILITY
1776 void
1777 construct(_Up* __p, _Args&&... __args)
1778 {
1779 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1780 }
1781#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1782 _LIBCPP_INLINE_VISIBILITY
1783 void
1784 construct(pointer __p)
1785 {
1786 ::new((void*)__p) _Tp();
1787 }
1788# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001789
Howard Hinnant57199402012-01-02 17:56:02 +00001790 template <class _A0>
1791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001792 void
Howard Hinnant57199402012-01-02 17:56:02 +00001793 construct(pointer __p, _A0& __a0)
1794 {
1795 ::new((void*)__p) _Tp(__a0);
1796 }
1797 template <class _A0>
1798 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001799 void
Howard Hinnant57199402012-01-02 17:56:02 +00001800 construct(pointer __p, const _A0& __a0)
1801 {
1802 ::new((void*)__p) _Tp(__a0);
1803 }
Howard Hinnant57199402012-01-02 17:56:02 +00001804# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1805 template <class _A0, class _A1>
1806 _LIBCPP_INLINE_VISIBILITY
1807 void
1808 construct(pointer __p, _A0& __a0, _A1& __a1)
1809 {
1810 ::new((void*)__p) _Tp(__a0, __a1);
1811 }
1812 template <class _A0, class _A1>
1813 _LIBCPP_INLINE_VISIBILITY
1814 void
1815 construct(pointer __p, const _A0& __a0, _A1& __a1)
1816 {
1817 ::new((void*)__p) _Tp(__a0, __a1);
1818 }
1819 template <class _A0, class _A1>
1820 _LIBCPP_INLINE_VISIBILITY
1821 void
1822 construct(pointer __p, _A0& __a0, const _A1& __a1)
1823 {
1824 ::new((void*)__p) _Tp(__a0, __a1);
1825 }
1826 template <class _A0, class _A1>
1827 _LIBCPP_INLINE_VISIBILITY
1828 void
1829 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1830 {
1831 ::new((void*)__p) _Tp(__a0, __a1);
1832 }
1833#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1834 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1835};
1836
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001837template <class _Tp, class _Up>
1838inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001839bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001840
1841template <class _Tp, class _Up>
1842inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001843bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001844
1845template <class _OutputIterator, class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00001846class _LIBCPP_TYPE_VIS raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001847 : public iterator<output_iterator_tag,
1848 _Tp, // purposefully not C++03
1849 ptrdiff_t, // purposefully not C++03
1850 _Tp*, // purposefully not C++03
1851 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1852{
1853private:
1854 _OutputIterator __x_;
1855public:
1856 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1857 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1858 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1859 {::new(&*__x_) _Tp(__element); return *this;}
1860 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1861 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1862 {raw_storage_iterator __t(*this); ++__x_; return __t;}
1863};
1864
1865template <class _Tp>
1866pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00001867get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001868{
1869 pair<_Tp*, ptrdiff_t> __r(0, 0);
1870 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1871 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1872 / sizeof(_Tp);
1873 if (__n > __m)
1874 __n = __m;
1875 while (__n > 0)
1876 {
1877 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1878 if (__r.first)
1879 {
1880 __r.second = __n;
1881 break;
1882 }
1883 __n /= 2;
1884 }
1885 return __r;
1886}
1887
1888template <class _Tp>
1889inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001890void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001891
1892template <class _Tp>
1893struct auto_ptr_ref
1894{
1895 _Tp* __ptr_;
1896};
1897
1898template<class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00001899class _LIBCPP_TYPE_VIS auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001900{
1901private:
1902 _Tp* __ptr_;
1903public:
1904 typedef _Tp element_type;
1905
1906 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1907 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1908 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1909 : __ptr_(__p.release()) {}
1910 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1911 {reset(__p.release()); return *this;}
1912 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1913 {reset(__p.release()); return *this;}
1914 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1915 {reset(__p.__ptr_); return *this;}
1916 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1917
1918 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1919 {return *__ptr_;}
1920 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1921 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1922 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1923 {
1924 _Tp* __t = __ptr_;
1925 __ptr_ = 0;
1926 return __t;
1927 }
1928 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1929 {
1930 if (__ptr_ != __p)
1931 delete __ptr_;
1932 __ptr_ = __p;
1933 }
1934
1935 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1936 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1937 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1938 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1939 {return auto_ptr<_Up>(release());}
1940};
1941
1942template <>
Howard Hinnant83eade62013-03-06 23:30:19 +00001943class _LIBCPP_TYPE_VIS auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001944{
1945public:
1946 typedef void element_type;
1947};
1948
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001949template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1950 typename remove_cv<_T2>::type>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00001951 bool = is_empty<_T1>::value
1952#if __has_feature(is_final)
1953 && !__is_final(_T1)
1954#endif
1955 ,
1956 bool = is_empty<_T2>::value
1957#if __has_feature(is_final)
1958 && !__is_final(_T2)
1959#endif
1960 >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001961struct __libcpp_compressed_pair_switch;
1962
1963template <class _T1, class _T2, bool IsSame>
1964struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1965
1966template <class _T1, class _T2, bool IsSame>
1967struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1968
1969template <class _T1, class _T2, bool IsSame>
1970struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1971
1972template <class _T1, class _T2>
1973struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
1974
1975template <class _T1, class _T2>
1976struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
1977
1978template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
1979class __libcpp_compressed_pair_imp;
1980
1981template <class _T1, class _T2>
1982class __libcpp_compressed_pair_imp<_T1, _T2, 0>
1983{
1984private:
1985 _T1 __first_;
1986 _T2 __second_;
1987public:
1988 typedef _T1 _T1_param;
1989 typedef _T2 _T2_param;
1990
1991 typedef typename remove_reference<_T1>::type& _T1_reference;
1992 typedef typename remove_reference<_T2>::type& _T2_reference;
1993
1994 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1995 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1996
1997 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant57199402012-01-02 17:56:02 +00001998 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001999 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002000 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002001 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002002 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002003 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002004
Howard Hinnant61aa6012011-07-01 19:24:36 +00002005#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2006
2007 _LIBCPP_INLINE_VISIBILITY
2008 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2009 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2010 is_nothrow_copy_constructible<_T2>::value)
2011 : __first_(__p.first()),
2012 __second_(__p.second()) {}
2013
2014 _LIBCPP_INLINE_VISIBILITY
2015 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2016 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2017 is_nothrow_copy_assignable<_T2>::value)
2018 {
2019 __first_ = __p.first();
2020 __second_ = __p.second();
2021 return *this;
2022 }
2023
Howard Hinnant73d21a42010-09-04 23:28:19 +00002024#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002025
2026 _LIBCPP_INLINE_VISIBILITY
2027 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002028 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2029 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002030 : __first_(_VSTD::forward<_T1>(__p.first())),
2031 __second_(_VSTD::forward<_T2>(__p.second())) {}
2032
2033 _LIBCPP_INLINE_VISIBILITY
2034 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2035 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2036 is_nothrow_move_assignable<_T2>::value)
2037 {
2038 __first_ = _VSTD::forward<_T1>(__p.first());
2039 __second_ = _VSTD::forward<_T2>(__p.second());
2040 return *this;
2041 }
2042
Howard Hinnant8c238192013-05-06 16:58:36 +00002043#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2044
2045#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2046
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002047#ifndef _LIBCPP_HAS_NO_VARIADICS
2048
2049 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2050 _LIBCPP_INLINE_VISIBILITY
2051 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2052 tuple<_Args1...> __first_args,
2053 tuple<_Args2...> __second_args,
2054 __tuple_indices<_I1...>,
2055 __tuple_indices<_I2...>)
2056 : __first_(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
2057 __second_(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
2058 {}
2059
2060#endif // _LIBCPP_HAS_NO_VARIADICS
2061
Howard Hinnant1694d232011-05-28 14:41:13 +00002062 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2063 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002064
Howard Hinnant1694d232011-05-28 14:41:13 +00002065 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2066 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002067
2068 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002069 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2070 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002071 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002072 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002073 swap(__first_, __x.__first_);
2074 swap(__second_, __x.__second_);
2075 }
2076};
2077
2078template <class _T1, class _T2>
2079class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2080 : private _T1
2081{
2082private:
2083 _T2 __second_;
2084public:
2085 typedef _T1 _T1_param;
2086 typedef _T2 _T2_param;
2087
2088 typedef _T1& _T1_reference;
2089 typedef typename remove_reference<_T2>::type& _T2_reference;
2090
2091 typedef const _T1& _T1_const_reference;
2092 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2093
2094 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002095 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002096 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002097 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002098 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002099 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002100 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002101
Howard Hinnant61aa6012011-07-01 19:24:36 +00002102#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2103
2104 _LIBCPP_INLINE_VISIBILITY
2105 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2106 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2107 is_nothrow_copy_constructible<_T2>::value)
2108 : _T1(__p.first()), __second_(__p.second()) {}
2109
2110 _LIBCPP_INLINE_VISIBILITY
2111 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2112 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2113 is_nothrow_copy_assignable<_T2>::value)
2114 {
2115 _T1::operator=(__p.first());
2116 __second_ = __p.second();
2117 return *this;
2118 }
2119
Howard Hinnant73d21a42010-09-04 23:28:19 +00002120#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002121
2122 _LIBCPP_INLINE_VISIBILITY
2123 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002124 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2125 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002126 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002127
2128 _LIBCPP_INLINE_VISIBILITY
2129 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2130 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2131 is_nothrow_move_assignable<_T2>::value)
2132 {
2133 _T1::operator=(_VSTD::move(__p.first()));
2134 __second_ = _VSTD::forward<_T2>(__p.second());
2135 return *this;
2136 }
2137
Howard Hinnant8c238192013-05-06 16:58:36 +00002138#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2139
2140#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2141
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002142#ifndef _LIBCPP_HAS_NO_VARIADICS
2143
2144 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2145 _LIBCPP_INLINE_VISIBILITY
2146 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2147 tuple<_Args1...> __first_args,
2148 tuple<_Args2...> __second_args,
2149 __tuple_indices<_I1...>,
2150 __tuple_indices<_I2...>)
2151 : _T1(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
2152 __second_(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
2153 {}
2154
2155#endif // _LIBCPP_HAS_NO_VARIADICS
2156
Howard Hinnant1694d232011-05-28 14:41:13 +00002157 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2158 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002159
Howard Hinnant1694d232011-05-28 14:41:13 +00002160 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2161 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002162
2163 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002164 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2165 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002166 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002167 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002168 swap(__second_, __x.__second_);
2169 }
2170};
2171
2172template <class _T1, class _T2>
2173class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2174 : private _T2
2175{
2176private:
2177 _T1 __first_;
2178public:
2179 typedef _T1 _T1_param;
2180 typedef _T2 _T2_param;
2181
2182 typedef typename remove_reference<_T1>::type& _T1_reference;
2183 typedef _T2& _T2_reference;
2184
2185 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2186 typedef const _T2& _T2_const_reference;
2187
2188 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2189 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002190 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002191 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002192 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002193 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00002194 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2195 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002196 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002197
Howard Hinnant61aa6012011-07-01 19:24:36 +00002198#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2199
2200 _LIBCPP_INLINE_VISIBILITY
2201 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2202 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2203 is_nothrow_copy_constructible<_T2>::value)
2204 : _T2(__p.second()), __first_(__p.first()) {}
2205
2206 _LIBCPP_INLINE_VISIBILITY
2207 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2208 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2209 is_nothrow_copy_assignable<_T2>::value)
2210 {
2211 _T2::operator=(__p.second());
2212 __first_ = __p.first();
2213 return *this;
2214 }
2215
Howard Hinnant73d21a42010-09-04 23:28:19 +00002216#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002217
2218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002219 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002220 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2221 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002222 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002223
2224 _LIBCPP_INLINE_VISIBILITY
2225 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2226 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2227 is_nothrow_move_assignable<_T2>::value)
2228 {
2229 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2230 __first_ = _VSTD::move(__p.first());
2231 return *this;
2232 }
2233
Howard Hinnant8c238192013-05-06 16:58:36 +00002234#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2235
2236#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2237
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002238#ifndef _LIBCPP_HAS_NO_VARIADICS
2239
2240 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2241 _LIBCPP_INLINE_VISIBILITY
2242 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2243 tuple<_Args1...> __first_args,
2244 tuple<_Args2...> __second_args,
2245 __tuple_indices<_I1...>,
2246 __tuple_indices<_I2...>)
2247 : _T2(_VSTD::forward<_Args2>(get<_I2>(__second_args))...),
2248 __first_(_VSTD::forward<_Args1>(get<_I1>(__first_args))...)
2249
2250 {}
2251
2252#endif // _LIBCPP_HAS_NO_VARIADICS
2253
Howard Hinnant1694d232011-05-28 14:41:13 +00002254 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2255 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002256
Howard Hinnant1694d232011-05-28 14:41:13 +00002257 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2258 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002259
2260 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002261 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2262 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002263 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002264 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002265 swap(__first_, __x.__first_);
2266 }
2267};
2268
2269template <class _T1, class _T2>
2270class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2271 : private _T1,
2272 private _T2
2273{
2274public:
2275 typedef _T1 _T1_param;
2276 typedef _T2 _T2_param;
2277
2278 typedef _T1& _T1_reference;
2279 typedef _T2& _T2_reference;
2280
2281 typedef const _T1& _T1_const_reference;
2282 typedef const _T2& _T2_const_reference;
2283
2284 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2285 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002286 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002287 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002288 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002289 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002290 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002291
Howard Hinnant61aa6012011-07-01 19:24:36 +00002292#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2293
2294 _LIBCPP_INLINE_VISIBILITY
2295 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2296 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2297 is_nothrow_copy_constructible<_T2>::value)
2298 : _T1(__p.first()), _T2(__p.second()) {}
2299
2300 _LIBCPP_INLINE_VISIBILITY
2301 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2302 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2303 is_nothrow_copy_assignable<_T2>::value)
2304 {
2305 _T1::operator=(__p.first());
2306 _T2::operator=(__p.second());
2307 return *this;
2308 }
2309
Howard Hinnant73d21a42010-09-04 23:28:19 +00002310#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002311
2312 _LIBCPP_INLINE_VISIBILITY
2313 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002314 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2315 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002316 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002317
2318 _LIBCPP_INLINE_VISIBILITY
2319 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2320 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2321 is_nothrow_move_assignable<_T2>::value)
2322 {
2323 _T1::operator=(_VSTD::move(__p.first()));
2324 _T2::operator=(_VSTD::move(__p.second()));
2325 return *this;
2326 }
2327
Howard Hinnant8c238192013-05-06 16:58:36 +00002328#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2329
2330#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2331
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002332#ifndef _LIBCPP_HAS_NO_VARIADICS
2333
2334 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2335 _LIBCPP_INLINE_VISIBILITY
2336 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2337 tuple<_Args1...> __first_args,
2338 tuple<_Args2...> __second_args,
2339 __tuple_indices<_I1...>,
2340 __tuple_indices<_I2...>)
2341 : _T1(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
2342 _T2(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
2343 {}
2344
2345#endif // _LIBCPP_HAS_NO_VARIADICS
2346
Howard Hinnant1694d232011-05-28 14:41:13 +00002347 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2348 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002349
Howard Hinnant1694d232011-05-28 14:41:13 +00002350 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2351 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002352
Howard Hinnantec3773c2011-12-01 20:21:04 +00002353 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant1694d232011-05-28 14:41:13 +00002354 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2355 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002356 {
2357 }
2358};
2359
2360template <class _T1, class _T2>
2361class __compressed_pair
2362 : private __libcpp_compressed_pair_imp<_T1, _T2>
2363{
2364 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2365public:
2366 typedef typename base::_T1_param _T1_param;
2367 typedef typename base::_T2_param _T2_param;
2368
2369 typedef typename base::_T1_reference _T1_reference;
2370 typedef typename base::_T2_reference _T2_reference;
2371
2372 typedef typename base::_T1_const_reference _T1_const_reference;
2373 typedef typename base::_T2_const_reference _T2_const_reference;
2374
2375 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002376 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002377 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002378 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002379 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002380 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002381 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002382
Howard Hinnant61aa6012011-07-01 19:24:36 +00002383#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2384
2385 _LIBCPP_INLINE_VISIBILITY
2386 __compressed_pair(const __compressed_pair& __p)
2387 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2388 is_nothrow_copy_constructible<_T2>::value)
2389 : base(__p) {}
2390
2391 _LIBCPP_INLINE_VISIBILITY
2392 __compressed_pair& operator=(const __compressed_pair& __p)
2393 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2394 is_nothrow_copy_assignable<_T2>::value)
2395 {
2396 base::operator=(__p);
2397 return *this;
2398 }
2399
Howard Hinnant73d21a42010-09-04 23:28:19 +00002400#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002402 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002403 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2404 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002405 : base(_VSTD::move(__p)) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002406
2407 _LIBCPP_INLINE_VISIBILITY
2408 __compressed_pair& operator=(__compressed_pair&& __p)
2409 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2410 is_nothrow_move_assignable<_T2>::value)
2411 {
2412 base::operator=(_VSTD::move(__p));
2413 return *this;
2414 }
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002415
Howard Hinnant8c238192013-05-06 16:58:36 +00002416#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2417
2418#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2419
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002420#ifndef _LIBCPP_HAS_NO_VARIADICS
2421
2422 template <class... _Args1, class... _Args2>
2423 _LIBCPP_INLINE_VISIBILITY
2424 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2425 tuple<_Args2...> __second_args)
Howard Hinnant57199402012-01-02 17:56:02 +00002426 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002427 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2428 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2429 {}
2430
2431#endif // _LIBCPP_HAS_NO_VARIADICS
2432
Howard Hinnant1694d232011-05-28 14:41:13 +00002433 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2434 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002435
Howard Hinnant1694d232011-05-28 14:41:13 +00002436 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2437 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002438
Howard Hinnant1694d232011-05-28 14:41:13 +00002439 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2440 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2441 __is_nothrow_swappable<_T1>::value)
2442 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002443};
2444
2445template <class _T1, class _T2>
2446inline _LIBCPP_INLINE_VISIBILITY
2447void
2448swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002449 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2450 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002451 {__x.swap(__y);}
2452
Howard Hinnant57199402012-01-02 17:56:02 +00002453// __same_or_less_cv_qualified
2454
2455template <class _Ptr1, class _Ptr2,
2456 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2457 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2458 >::value
2459 >
2460struct __same_or_less_cv_qualified_imp
2461 : is_convertible<_Ptr1, _Ptr2> {};
2462
2463template <class _Ptr1, class _Ptr2>
2464struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2465 : false_type {};
2466
2467template <class _Ptr1, class _Ptr2, bool = is_scalar<_Ptr1>::value &&
2468 !is_pointer<_Ptr1>::value>
2469struct __same_or_less_cv_qualified
2470 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2471
2472template <class _Ptr1, class _Ptr2>
2473struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, true>
2474 : false_type {};
2475
2476// default_delete
2477
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002478template <class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00002479struct _LIBCPP_TYPE_VIS default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002480{
Howard Hinnant46e94932012-07-07 20:56:04 +00002481#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2482 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2483#else
2484 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2485#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002486 template <class _Up>
2487 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002488 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2489 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002490 {
2491 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant05e7d242013-04-24 19:44:26 +00002492 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002493 delete __ptr;
2494 }
2495};
2496
2497template <class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00002498struct _LIBCPP_TYPE_VIS default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002499{
Howard Hinnant8e843502011-12-18 21:19:44 +00002500public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002501#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2502 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2503#else
2504 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2505#endif
Howard Hinnant8e843502011-12-18 21:19:44 +00002506 template <class _Up>
2507 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant57199402012-01-02 17:56:02 +00002508 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant8e843502011-12-18 21:19:44 +00002509 template <class _Up>
2510 _LIBCPP_INLINE_VISIBILITY
2511 void operator() (_Up* __ptr,
Howard Hinnant57199402012-01-02 17:56:02 +00002512 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002513 {
2514 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant05e7d242013-04-24 19:44:26 +00002515 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002516 delete [] __ptr;
2517 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002518};
2519
2520template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant83eade62013-03-06 23:30:19 +00002521class _LIBCPP_TYPE_VIS unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002522{
2523public:
2524 typedef _Tp element_type;
2525 typedef _Dp deleter_type;
2526 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2527private:
2528 __compressed_pair<pointer, deleter_type> __ptr_;
2529
Howard Hinnant57199402012-01-02 17:56:02 +00002530#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002531 unique_ptr(unique_ptr&);
2532 template <class _Up, class _Ep>
2533 unique_ptr(unique_ptr<_Up, _Ep>&);
2534 unique_ptr& operator=(unique_ptr&);
2535 template <class _Up, class _Ep>
2536 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002537#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002538
2539 struct __nat {int __for_bool_;};
2540
2541 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2542 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2543public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002544 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002545 : __ptr_(pointer())
2546 {
2547 static_assert(!is_pointer<deleter_type>::value,
2548 "unique_ptr constructed with null function pointer deleter");
2549 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002550 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002551 : __ptr_(pointer())
2552 {
2553 static_assert(!is_pointer<deleter_type>::value,
2554 "unique_ptr constructed with null function pointer deleter");
2555 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002556 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002557 : __ptr_(_VSTD::move(__p))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002558 {
2559 static_assert(!is_pointer<deleter_type>::value,
2560 "unique_ptr constructed with null function pointer deleter");
2561 }
2562
Howard Hinnant73d21a42010-09-04 23:28:19 +00002563#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002564 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2565 is_reference<deleter_type>::value,
2566 deleter_type,
2567 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002568 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002569 : __ptr_(__p, __d) {}
2570
2571 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002572 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002573 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002574 {
2575 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2576 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002577 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002578 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002579 template <class _Up, class _Ep>
2580 _LIBCPP_INLINE_VISIBILITY
2581 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2582 typename enable_if
2583 <
2584 !is_array<_Up>::value &&
2585 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2586 is_convertible<_Ep, deleter_type>::value &&
2587 (
2588 !is_reference<deleter_type>::value ||
2589 is_same<deleter_type, _Ep>::value
2590 ),
2591 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002592 >::type = __nat()) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002593 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002594
2595 template <class _Up>
2596 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2597 typename enable_if<
2598 is_convertible<_Up*, _Tp*>::value &&
2599 is_same<_Dp, default_delete<_Tp> >::value,
2600 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002601 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002602 : __ptr_(__p.release())
2603 {
2604 }
2605
Howard Hinnant1694d232011-05-28 14:41:13 +00002606 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002607 {
2608 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002609 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002610 return *this;
2611 }
2612
2613 template <class _Up, class _Ep>
2614 _LIBCPP_INLINE_VISIBILITY
2615 typename enable_if
2616 <
Howard Hinnant57199402012-01-02 17:56:02 +00002617 !is_array<_Up>::value &&
2618 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2619 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002620 unique_ptr&
2621 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002622 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002623 {
2624 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002625 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002626 return *this;
2627 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002628#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002629
2630 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2631 {
2632 return __rv<unique_ptr>(*this);
2633 }
2634
2635 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002636 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002637
2638 template <class _Up, class _Ep>
2639 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2640 {
2641 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002642 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002643 return *this;
2644 }
2645
2646 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002647 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002648
2649 template <class _Up>
2650 _LIBCPP_INLINE_VISIBILITY
2651 typename enable_if<
2652 is_convertible<_Up*, _Tp*>::value &&
2653 is_same<_Dp, default_delete<_Tp> >::value,
2654 unique_ptr&
2655 >::type
2656 operator=(auto_ptr<_Up> __p)
2657 {reset(__p.release()); return *this;}
2658
Howard Hinnant73d21a42010-09-04 23:28:19 +00002659#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002660 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2661
Howard Hinnant1694d232011-05-28 14:41:13 +00002662 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002663 {
2664 reset();
2665 return *this;
2666 }
2667
2668 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2669 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002670 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2671 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2672 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2673 {return __ptr_.second();}
2674 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2675 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002676 _LIBCPP_INLINE_VISIBILITY
2677 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2678 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002679
Howard Hinnant1694d232011-05-28 14:41:13 +00002680 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002681 {
2682 pointer __t = __ptr_.first();
2683 __ptr_.first() = pointer();
2684 return __t;
2685 }
2686
Howard Hinnant1694d232011-05-28 14:41:13 +00002687 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002688 {
2689 pointer __tmp = __ptr_.first();
2690 __ptr_.first() = __p;
2691 if (__tmp)
2692 __ptr_.second()(__tmp);
2693 }
2694
Howard Hinnant1694d232011-05-28 14:41:13 +00002695 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2696 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002697};
2698
2699template <class _Tp, class _Dp>
Howard Hinnant83eade62013-03-06 23:30:19 +00002700class _LIBCPP_TYPE_VIS unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002701{
2702public:
2703 typedef _Tp element_type;
2704 typedef _Dp deleter_type;
2705 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2706private:
2707 __compressed_pair<pointer, deleter_type> __ptr_;
2708
Howard Hinnant57199402012-01-02 17:56:02 +00002709#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002710 unique_ptr(unique_ptr&);
2711 template <class _Up>
2712 unique_ptr(unique_ptr<_Up>&);
2713 unique_ptr& operator=(unique_ptr&);
2714 template <class _Up>
2715 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002716#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002717
2718 struct __nat {int __for_bool_;};
2719
2720 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2721 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2722public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002723 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002724 : __ptr_(pointer())
2725 {
2726 static_assert(!is_pointer<deleter_type>::value,
2727 "unique_ptr constructed with null function pointer deleter");
2728 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002729 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002730 : __ptr_(pointer())
2731 {
2732 static_assert(!is_pointer<deleter_type>::value,
2733 "unique_ptr constructed with null function pointer deleter");
2734 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002735#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002736 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002737 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002738 >
Howard Hinnant99968442011-11-29 18:15:50 +00002739 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002740 : __ptr_(__p)
2741 {
2742 static_assert(!is_pointer<deleter_type>::value,
2743 "unique_ptr constructed with null function pointer deleter");
2744 }
2745
Howard Hinnant99968442011-11-29 18:15:50 +00002746 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002747 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002748 >
Howard Hinnant99968442011-11-29 18:15:50 +00002749 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002750 is_reference<deleter_type>::value,
2751 deleter_type,
2752 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002753 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002754 : __ptr_(__p, __d) {}
2755
2756 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2757 is_reference<deleter_type>::value,
2758 deleter_type,
2759 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002760 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002761 : __ptr_(pointer(), __d) {}
2762
Howard Hinnant99968442011-11-29 18:15:50 +00002763 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002764 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002765 >
Howard Hinnant99968442011-11-29 18:15:50 +00002766 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002767 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002768 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002769 {
2770 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2771 }
2772
2773 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002774 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002775 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002776 {
2777 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2778 }
2779
Howard Hinnant1694d232011-05-28 14:41:13 +00002780 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002781 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002782
Howard Hinnant1694d232011-05-28 14:41:13 +00002783 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002784 {
2785 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002786 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002787 return *this;
2788 }
Howard Hinnant8e843502011-12-18 21:19:44 +00002789
2790 template <class _Up, class _Ep>
2791 _LIBCPP_INLINE_VISIBILITY
2792 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2793 typename enable_if
2794 <
2795 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002796 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant8e843502011-12-18 21:19:44 +00002797 && is_convertible<_Ep, deleter_type>::value &&
2798 (
2799 !is_reference<deleter_type>::value ||
2800 is_same<deleter_type, _Ep>::value
2801 ),
2802 __nat
2803 >::type = __nat()
2804 ) _NOEXCEPT
2805 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2806
2807
2808 template <class _Up, class _Ep>
2809 _LIBCPP_INLINE_VISIBILITY
2810 typename enable_if
2811 <
2812 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002813 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2814 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant8e843502011-12-18 21:19:44 +00002815 unique_ptr&
2816 >::type
2817 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2818 {
2819 reset(__u.release());
2820 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2821 return *this;
2822 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002823#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002824
2825 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2826 : __ptr_(__p)
2827 {
2828 static_assert(!is_pointer<deleter_type>::value,
2829 "unique_ptr constructed with null function pointer deleter");
2830 }
2831
2832 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002833 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002834
2835 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002836 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002837
2838 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2839 {
2840 return __rv<unique_ptr>(*this);
2841 }
2842
2843 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002844 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002845
2846 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2847 {
2848 reset(__u->release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002849 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002850 return *this;
2851 }
2852
Howard Hinnant73d21a42010-09-04 23:28:19 +00002853#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002854 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2855
Howard Hinnant1694d232011-05-28 14:41:13 +00002856 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002857 {
2858 reset();
2859 return *this;
2860 }
2861
2862 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2863 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002864 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2865 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2866 {return __ptr_.second();}
2867 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2868 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002869 _LIBCPP_INLINE_VISIBILITY
2870 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2871 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002872
Howard Hinnant1694d232011-05-28 14:41:13 +00002873 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002874 {
2875 pointer __t = __ptr_.first();
2876 __ptr_.first() = pointer();
2877 return __t;
2878 }
2879
Howard Hinnant73d21a42010-09-04 23:28:19 +00002880#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002881 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002882 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002883 >
Howard Hinnant99968442011-11-29 18:15:50 +00002884 _LIBCPP_INLINE_VISIBILITY void reset(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002885 {
2886 pointer __tmp = __ptr_.first();
2887 __ptr_.first() = __p;
2888 if (__tmp)
2889 __ptr_.second()(__tmp);
2890 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002891 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002892 {
2893 pointer __tmp = __ptr_.first();
2894 __ptr_.first() = nullptr;
2895 if (__tmp)
2896 __ptr_.second()(__tmp);
2897 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002898 _LIBCPP_INLINE_VISIBILITY void reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002899 {
2900 pointer __tmp = __ptr_.first();
2901 __ptr_.first() = nullptr;
2902 if (__tmp)
2903 __ptr_.second()(__tmp);
2904 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002905#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002906 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2907 {
2908 pointer __tmp = __ptr_.first();
2909 __ptr_.first() = __p;
2910 if (__tmp)
2911 __ptr_.second()(__tmp);
2912 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002913#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002914
2915 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2916private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002917
Howard Hinnant73d21a42010-09-04 23:28:19 +00002918#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002919 template <class _Up>
2920 explicit unique_ptr(_Up);
2921 template <class _Up>
2922 unique_ptr(_Up __u,
2923 typename conditional<
2924 is_reference<deleter_type>::value,
2925 deleter_type,
2926 typename add_lvalue_reference<const deleter_type>::type>::type,
2927 typename enable_if
2928 <
2929 is_convertible<_Up, pointer>::value,
2930 __nat
2931 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002932#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002933};
2934
2935template <class _Tp, class _Dp>
2936inline _LIBCPP_INLINE_VISIBILITY
2937void
Howard Hinnant1694d232011-05-28 14:41:13 +00002938swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002939
2940template <class _T1, class _D1, class _T2, class _D2>
2941inline _LIBCPP_INLINE_VISIBILITY
2942bool
2943operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2944
2945template <class _T1, class _D1, class _T2, class _D2>
2946inline _LIBCPP_INLINE_VISIBILITY
2947bool
2948operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2949
2950template <class _T1, class _D1, class _T2, class _D2>
2951inline _LIBCPP_INLINE_VISIBILITY
2952bool
Howard Hinnant3fadda32012-02-21 21:02:58 +00002953operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2954{
2955 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2956 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
2957 typedef typename common_type<_P1, _P2>::type _V;
2958 return less<_V>()(__x.get(), __y.get());
2959}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002960
2961template <class _T1, class _D1, class _T2, class _D2>
2962inline _LIBCPP_INLINE_VISIBILITY
2963bool
2964operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2965
2966template <class _T1, class _D1, class _T2, class _D2>
2967inline _LIBCPP_INLINE_VISIBILITY
2968bool
2969operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2970
2971template <class _T1, class _D1, class _T2, class _D2>
2972inline _LIBCPP_INLINE_VISIBILITY
2973bool
2974operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2975
Howard Hinnant3fadda32012-02-21 21:02:58 +00002976template <class _T1, class _D1>
2977inline _LIBCPP_INLINE_VISIBILITY
2978bool
Howard Hinnant46e94932012-07-07 20:56:04 +00002979operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00002980{
2981 return !__x;
2982}
2983
2984template <class _T1, class _D1>
2985inline _LIBCPP_INLINE_VISIBILITY
2986bool
Howard Hinnant46e94932012-07-07 20:56:04 +00002987operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00002988{
2989 return !__x;
2990}
2991
2992template <class _T1, class _D1>
2993inline _LIBCPP_INLINE_VISIBILITY
2994bool
Howard Hinnant46e94932012-07-07 20:56:04 +00002995operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00002996{
2997 return static_cast<bool>(__x);
2998}
2999
3000template <class _T1, class _D1>
3001inline _LIBCPP_INLINE_VISIBILITY
3002bool
Howard Hinnant46e94932012-07-07 20:56:04 +00003003operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003004{
3005 return static_cast<bool>(__x);
3006}
3007
3008template <class _T1, class _D1>
3009inline _LIBCPP_INLINE_VISIBILITY
3010bool
3011operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3012{
3013 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3014 return less<_P1>()(__x.get(), nullptr);
3015}
3016
3017template <class _T1, class _D1>
3018inline _LIBCPP_INLINE_VISIBILITY
3019bool
3020operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3021{
3022 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3023 return less<_P1>()(nullptr, __x.get());
3024}
3025
3026template <class _T1, class _D1>
3027inline _LIBCPP_INLINE_VISIBILITY
3028bool
3029operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3030{
3031 return nullptr < __x;
3032}
3033
3034template <class _T1, class _D1>
3035inline _LIBCPP_INLINE_VISIBILITY
3036bool
3037operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3038{
3039 return __x < nullptr;
3040}
3041
3042template <class _T1, class _D1>
3043inline _LIBCPP_INLINE_VISIBILITY
3044bool
3045operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3046{
3047 return !(nullptr < __x);
3048}
3049
3050template <class _T1, class _D1>
3051inline _LIBCPP_INLINE_VISIBILITY
3052bool
3053operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3054{
3055 return !(__x < nullptr);
3056}
3057
3058template <class _T1, class _D1>
3059inline _LIBCPP_INLINE_VISIBILITY
3060bool
3061operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3062{
3063 return !(__x < nullptr);
3064}
3065
3066template <class _T1, class _D1>
3067inline _LIBCPP_INLINE_VISIBILITY
3068bool
3069operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3070{
3071 return !(nullptr < __x);
3072}
3073
Howard Hinnant87073e42012-05-01 15:37:54 +00003074#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3075
3076template <class _Tp, class _Dp>
3077inline _LIBCPP_INLINE_VISIBILITY
3078unique_ptr<_Tp, _Dp>
3079move(unique_ptr<_Tp, _Dp>& __t)
3080{
3081 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3082}
3083
3084#endif
3085
Marshall Clowfd7481e2013-07-01 18:16:03 +00003086#if _LIBCPP_STD_VER > 11
3087
3088template<class _Tp>
3089struct __unique_if
3090{
3091 typedef unique_ptr<_Tp> __unique_single;
3092};
3093
3094template<class _Tp>
3095struct __unique_if<_Tp[]>
3096{
3097 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3098};
3099
3100template<class _Tp, size_t _Np>
3101struct __unique_if<_Tp[_Np]>
3102{
3103 typedef void __unique_array_known_bound;
3104};
3105
3106template<class _Tp, class... _Args>
Marshall Clowfb551102013-07-02 20:06:09 +00003107inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003108typename __unique_if<_Tp>::__unique_single
3109make_unique(_Args&&... __args)
3110{
3111 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3112}
3113
3114template<class _Tp>
Marshall Clowfb551102013-07-02 20:06:09 +00003115inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowfd7481e2013-07-01 18:16:03 +00003116typename __unique_if<_Tp>::__unique_array_unknown_bound
3117make_unique(size_t __n)
3118{
3119 typedef typename remove_extent<_Tp>::type _Up;
3120 return unique_ptr<_Tp>(new _Up[__n]());
3121}
3122
3123template<class _Tp, class... _Args>
3124 typename __unique_if<_Tp>::__unique_array_known_bound
3125 make_unique(_Args&&...) = delete;
3126
3127#endif // _LIBCPP_STD_VER > 11
3128
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00003129template <class _Tp> struct hash;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003130
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003131template <class _Size>
3132inline _LIBCPP_INLINE_VISIBILITY
3133_Size
3134__loadword(const void* __p)
3135{
3136 _Size __r;
3137 std::memcpy(&__r, __p, sizeof(__r));
3138 return __r;
3139}
3140
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003141// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
3142// is 64 bits. This is because cityhash64 uses 64bit x 64bit
3143// multiplication, which can be very slow on 32-bit systems.
Howard Hinnant40c13d32011-12-05 00:08:45 +00003144template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003145struct __murmur2_or_cityhash;
Howard Hinnant40c13d32011-12-05 00:08:45 +00003146
3147template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003148struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003149{
3150 _Size operator()(const void* __key, _Size __len);
3151};
3152
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003153// murmur2
Howard Hinnant40c13d32011-12-05 00:08:45 +00003154template <class _Size>
3155_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003156__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003157{
3158 const _Size __m = 0x5bd1e995;
3159 const _Size __r = 24;
3160 _Size __h = __len;
3161 const unsigned char* __data = static_cast<const unsigned char*>(__key);
3162 for (; __len >= 4; __data += 4, __len -= 4)
3163 {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003164 _Size __k = __loadword<_Size>(__data);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003165 __k *= __m;
3166 __k ^= __k >> __r;
3167 __k *= __m;
3168 __h *= __m;
3169 __h ^= __k;
3170 }
3171 switch (__len)
3172 {
3173 case 3:
3174 __h ^= __data[2] << 16;
3175 case 2:
3176 __h ^= __data[1] << 8;
3177 case 1:
3178 __h ^= __data[0];
3179 __h *= __m;
3180 }
3181 __h ^= __h >> 13;
3182 __h *= __m;
3183 __h ^= __h >> 15;
3184 return __h;
3185}
3186
3187template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003188struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003189{
3190 _Size operator()(const void* __key, _Size __len);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003191
3192 private:
3193 // Some primes between 2^63 and 2^64.
3194 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
3195 static const _Size __k1 = 0xb492b66fbe98f273ULL;
3196 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
3197 static const _Size __k3 = 0xc949d7c7509e6557ULL;
3198
3199 static _Size __rotate(_Size __val, int __shift) {
3200 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
3201 }
3202
3203 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
3204 return (__val >> __shift) | (__val << (64 - __shift));
3205 }
3206
3207 static _Size __shift_mix(_Size __val) {
3208 return __val ^ (__val >> 47);
3209 }
3210
3211 static _Size __hash_len_16(_Size __u, _Size __v) {
3212 const _Size __mul = 0x9ddfea08eb382d69ULL;
3213 _Size __a = (__u ^ __v) * __mul;
3214 __a ^= (__a >> 47);
3215 _Size __b = (__v ^ __a) * __mul;
3216 __b ^= (__b >> 47);
3217 __b *= __mul;
3218 return __b;
3219 }
3220
3221 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
3222 if (__len > 8) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003223 const _Size __a = __loadword<_Size>(__s);
3224 const _Size __b = __loadword<_Size>(__s + __len - 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003225 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
3226 }
3227 if (__len >= 4) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003228 const uint32_t __a = __loadword<uint32_t>(__s);
3229 const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003230 return __hash_len_16(__len + (__a << 3), __b);
3231 }
3232 if (__len > 0) {
3233 const unsigned char __a = __s[0];
3234 const unsigned char __b = __s[__len >> 1];
3235 const unsigned char __c = __s[__len - 1];
3236 const uint32_t __y = static_cast<uint32_t>(__a) +
3237 (static_cast<uint32_t>(__b) << 8);
3238 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3239 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3240 }
3241 return __k2;
3242 }
3243
3244 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003245 const _Size __a = __loadword<_Size>(__s) * __k1;
3246 const _Size __b = __loadword<_Size>(__s + 8);
3247 const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;
3248 const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003249 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3250 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3251 }
3252
3253 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3254 // Callers do best to use "random-looking" values for a and b.
3255 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3256 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3257 __a += __w;
3258 __b = __rotate(__b + __a + __z, 21);
3259 const _Size __c = __a;
3260 __a += __x;
3261 __a += __y;
3262 __b += __rotate(__a, 44);
3263 return pair<_Size, _Size>(__a + __z, __b + __c);
3264 }
3265
3266 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3267 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3268 const char* __s, _Size __a, _Size __b) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003269 return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),
3270 __loadword<_Size>(__s + 8),
3271 __loadword<_Size>(__s + 16),
3272 __loadword<_Size>(__s + 24),
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003273 __a,
3274 __b);
3275 }
3276
3277 // Return an 8-byte hash for 33 to 64 bytes.
3278 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003279 _Size __z = __loadword<_Size>(__s + 24);
3280 _Size __a = __loadword<_Size>(__s) +
3281 (__len + __loadword<_Size>(__s + __len - 16)) * __k0;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003282 _Size __b = __rotate(__a + __z, 52);
3283 _Size __c = __rotate(__a, 37);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003284 __a += __loadword<_Size>(__s + 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003285 __c += __rotate(__a, 7);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003286 __a += __loadword<_Size>(__s + 16);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003287 _Size __vf = __a + __z;
3288 _Size __vs = __b + __rotate(__a, 31) + __c;
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003289 __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);
3290 __z += __loadword<_Size>(__s + __len - 8);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003291 __b = __rotate(__a + __z, 52);
3292 __c = __rotate(__a, 37);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003293 __a += __loadword<_Size>(__s + __len - 24);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003294 __c += __rotate(__a, 7);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003295 __a += __loadword<_Size>(__s + __len - 16);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003296 _Size __wf = __a + __z;
3297 _Size __ws = __b + __rotate(__a, 31) + __c;
3298 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3299 return __shift_mix(__r * __k0 + __vs) * __k2;
3300 }
Howard Hinnant40c13d32011-12-05 00:08:45 +00003301};
3302
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003303// cityhash64
Howard Hinnant40c13d32011-12-05 00:08:45 +00003304template <class _Size>
3305_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003306__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003307{
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003308 const char* __s = static_cast<const char*>(__key);
3309 if (__len <= 32) {
3310 if (__len <= 16) {
3311 return __hash_len_0_to_16(__s, __len);
3312 } else {
3313 return __hash_len_17_to_32(__s, __len);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003314 }
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003315 } else if (__len <= 64) {
3316 return __hash_len_33_to_64(__s, __len);
3317 }
3318
3319 // For strings over 64 bytes we hash the end first, and then as we
3320 // loop we keep 56 bytes of state: v, w, x, y, and z.
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003321 _Size __x = __loadword<_Size>(__s + __len - 40);
3322 _Size __y = __loadword<_Size>(__s + __len - 16) +
3323 __loadword<_Size>(__s + __len - 56);
3324 _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,
3325 __loadword<_Size>(__s + __len - 24));
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003326 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3327 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003328 __x = __x * __k1 + __loadword<_Size>(__s);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003329
3330 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3331 __len = (__len - 1) & ~static_cast<_Size>(63);
3332 do {
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003333 __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;
3334 __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003335 __x ^= __w.second;
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003336 __y += __v.first + __loadword<_Size>(__s + 40);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003337 __z = __rotate(__z + __w.first, 33) * __k1;
3338 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3339 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
Howard Hinnant24ae8f82013-07-03 17:39:28 +00003340 __y + __loadword<_Size>(__s + 16));
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003341 std::swap(__z, __x);
3342 __s += 64;
3343 __len -= 64;
3344 } while (__len != 0);
3345 return __hash_len_16(
3346 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3347 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003348}
3349
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003350template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3351struct __scalar_hash;
3352
3353template <class _Tp>
3354struct __scalar_hash<_Tp, 0>
3355 : public unary_function<_Tp, size_t>
Howard Hinnant21aefc32010-06-03 16:42:57 +00003356{
Howard Hinnant82894812010-09-22 16:48:34 +00003357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003358 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003359 {
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003360 union
3361 {
3362 _Tp __t;
3363 size_t __a;
3364 } __u;
3365 __u.__a = 0;
3366 __u.__t = __v;
3367 return __u.__a;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003368 }
3369};
3370
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003371template <class _Tp>
3372struct __scalar_hash<_Tp, 1>
3373 : public unary_function<_Tp, size_t>
3374{
3375 _LIBCPP_INLINE_VISIBILITY
3376 size_t operator()(_Tp __v) const _NOEXCEPT
3377 {
3378 union
3379 {
3380 _Tp __t;
3381 size_t __a;
3382 } __u;
3383 __u.__t = __v;
3384 return __u.__a;
3385 }
3386};
3387
3388template <class _Tp>
3389struct __scalar_hash<_Tp, 2>
3390 : public unary_function<_Tp, size_t>
3391{
3392 _LIBCPP_INLINE_VISIBILITY
3393 size_t operator()(_Tp __v) const _NOEXCEPT
3394 {
3395 union
3396 {
3397 _Tp __t;
3398 struct
3399 {
3400 size_t __a;
3401 size_t __b;
3402 };
3403 } __u;
3404 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003405 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003406 }
3407};
3408
3409template <class _Tp>
3410struct __scalar_hash<_Tp, 3>
3411 : public unary_function<_Tp, size_t>
3412{
3413 _LIBCPP_INLINE_VISIBILITY
3414 size_t operator()(_Tp __v) const _NOEXCEPT
3415 {
3416 union
3417 {
3418 _Tp __t;
3419 struct
3420 {
3421 size_t __a;
3422 size_t __b;
3423 size_t __c;
3424 };
3425 } __u;
3426 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003427 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003428 }
3429};
3430
3431template <class _Tp>
3432struct __scalar_hash<_Tp, 4>
3433 : public unary_function<_Tp, size_t>
3434{
3435 _LIBCPP_INLINE_VISIBILITY
3436 size_t operator()(_Tp __v) const _NOEXCEPT
3437 {
3438 union
3439 {
3440 _Tp __t;
3441 struct
3442 {
3443 size_t __a;
3444 size_t __b;
3445 size_t __c;
3446 size_t __d;
3447 };
3448 } __u;
3449 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003450 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003451 }
3452};
3453
3454template<class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00003455struct _LIBCPP_TYPE_VIS hash<_Tp*>
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003456 : public unary_function<_Tp*, size_t>
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003457{
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003458 _LIBCPP_INLINE_VISIBILITY
3459 size_t operator()(_Tp* __v) const _NOEXCEPT
3460 {
3461 union
3462 {
3463 _Tp* __t;
3464 size_t __a;
3465 } __u;
3466 __u.__t = __v;
3467 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
3468 }
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003469};
3470
Howard Hinnant21aefc32010-06-03 16:42:57 +00003471template <class _Tp, class _Dp>
Howard Hinnant83eade62013-03-06 23:30:19 +00003472struct _LIBCPP_TYPE_VIS hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003473{
3474 typedef unique_ptr<_Tp, _Dp> argument_type;
3475 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003476 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003477 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003478 {
3479 typedef typename argument_type::pointer pointer;
3480 return hash<pointer>()(__ptr.get());
3481 }
3482};
3483
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003484struct __destruct_n
3485{
3486private:
3487 size_t size;
3488
3489 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003490 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003491 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3492
3493 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003494 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003495 {}
3496
Howard Hinnant1694d232011-05-28 14:41:13 +00003497 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003498 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003499 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003500 {}
3501
Howard Hinnant1694d232011-05-28 14:41:13 +00003502 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003503 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003504 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003505 {}
3506public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003507 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3508 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003509
3510 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003511 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003512 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003513
3514 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003515 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003516 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003517
3518 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003519 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003520 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003521};
3522
3523template <class _Alloc>
3524class __allocator_destructor
3525{
3526 typedef allocator_traits<_Alloc> __alloc_traits;
3527public:
3528 typedef typename __alloc_traits::pointer pointer;
3529 typedef typename __alloc_traits::size_type size_type;
3530private:
3531 _Alloc& __alloc_;
3532 size_type __s_;
3533public:
3534 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00003535 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003536 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003537 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003538 void operator()(pointer __p) _NOEXCEPT
3539 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003540};
3541
3542template <class _InputIterator, class _ForwardIterator>
3543_ForwardIterator
3544uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3545{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003546 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003547#ifndef _LIBCPP_NO_EXCEPTIONS
3548 _ForwardIterator __s = __r;
3549 try
3550 {
3551#endif
3552 for (; __f != __l; ++__f, ++__r)
3553 ::new(&*__r) value_type(*__f);
3554#ifndef _LIBCPP_NO_EXCEPTIONS
3555 }
3556 catch (...)
3557 {
3558 for (; __s != __r; ++__s)
3559 __s->~value_type();
3560 throw;
3561 }
3562#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003563 return __r;
3564}
3565
3566template <class _InputIterator, class _Size, class _ForwardIterator>
3567_ForwardIterator
3568uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3569{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003570 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003571#ifndef _LIBCPP_NO_EXCEPTIONS
3572 _ForwardIterator __s = __r;
3573 try
3574 {
3575#endif
3576 for (; __n > 0; ++__f, ++__r, --__n)
3577 ::new(&*__r) value_type(*__f);
3578#ifndef _LIBCPP_NO_EXCEPTIONS
3579 }
3580 catch (...)
3581 {
3582 for (; __s != __r; ++__s)
3583 __s->~value_type();
3584 throw;
3585 }
3586#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003587 return __r;
3588}
3589
3590template <class _ForwardIterator, class _Tp>
3591void
3592uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3593{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003594 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003595#ifndef _LIBCPP_NO_EXCEPTIONS
3596 _ForwardIterator __s = __f;
3597 try
3598 {
3599#endif
3600 for (; __f != __l; ++__f)
3601 ::new(&*__f) value_type(__x);
3602#ifndef _LIBCPP_NO_EXCEPTIONS
3603 }
3604 catch (...)
3605 {
3606 for (; __s != __f; ++__s)
3607 __s->~value_type();
3608 throw;
3609 }
3610#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003611}
3612
3613template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003614_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003615uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3616{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003617 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003618#ifndef _LIBCPP_NO_EXCEPTIONS
3619 _ForwardIterator __s = __f;
3620 try
3621 {
3622#endif
3623 for (; __n > 0; ++__f, --__n)
3624 ::new(&*__f) value_type(__x);
3625#ifndef _LIBCPP_NO_EXCEPTIONS
3626 }
3627 catch (...)
3628 {
3629 for (; __s != __f; ++__s)
3630 __s->~value_type();
3631 throw;
3632 }
3633#endif
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003634 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003635}
3636
Howard Hinnant82894812010-09-22 16:48:34 +00003637class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003638 : public std::exception
3639{
3640public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003641 virtual ~bad_weak_ptr() _NOEXCEPT;
3642 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003643};
3644
Howard Hinnant83eade62013-03-06 23:30:19 +00003645template<class _Tp> class _LIBCPP_TYPE_VIS weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003646
3647class __shared_count
3648{
3649 __shared_count(const __shared_count&);
3650 __shared_count& operator=(const __shared_count&);
3651
3652protected:
3653 long __shared_owners_;
3654 virtual ~__shared_count();
3655private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003656 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003657
3658public:
Howard Hinnant82894812010-09-22 16:48:34 +00003659 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003660 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003661 : __shared_owners_(__refs) {}
3662
Howard Hinnant1694d232011-05-28 14:41:13 +00003663 void __add_shared() _NOEXCEPT;
3664 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003665 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003666 long use_count() const _NOEXCEPT {return __shared_owners_ + 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003667};
3668
3669class __shared_weak_count
3670 : private __shared_count
3671{
3672 long __shared_weak_owners_;
3673
3674public:
Howard Hinnant82894812010-09-22 16:48:34 +00003675 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003676 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003677 : __shared_count(__refs),
3678 __shared_weak_owners_(__refs) {}
3679protected:
3680 virtual ~__shared_weak_count();
3681
3682public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003683 void __add_shared() _NOEXCEPT;
3684 void __add_weak() _NOEXCEPT;
3685 void __release_shared() _NOEXCEPT;
3686 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003687 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003688 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3689 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003690
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003691 // Define the function out only if we build static libc++ without RTTI.
3692 // Otherwise we may break clients who need to compile their projects with
3693 // -fno-rtti and yet link against a libc++.dylib compiled
3694 // without -fno-rtti.
3695#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant1694d232011-05-28 14:41:13 +00003696 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003697#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003698private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003699 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003700};
3701
3702template <class _Tp, class _Dp, class _Alloc>
3703class __shared_ptr_pointer
3704 : public __shared_weak_count
3705{
3706 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3707public:
Howard Hinnant82894812010-09-22 16:48:34 +00003708 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003709 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003710 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003711
Howard Hinnantd4444702010-08-11 17:04:31 +00003712#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00003713 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00003714#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003715
3716private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003717 virtual void __on_zero_shared() _NOEXCEPT;
3718 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003719};
3720
Howard Hinnantd4444702010-08-11 17:04:31 +00003721#ifndef _LIBCPP_NO_RTTI
3722
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003723template <class _Tp, class _Dp, class _Alloc>
3724const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00003725__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003726{
3727 return __t == typeid(_Dp) ? &__data_.first().second() : 0;
3728}
3729
Howard Hinnant324bb032010-08-22 00:02:43 +00003730#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003731
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003732template <class _Tp, class _Dp, class _Alloc>
3733void
Howard Hinnant1694d232011-05-28 14:41:13 +00003734__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003735{
3736 __data_.first().second()(__data_.first().first());
3737 __data_.first().second().~_Dp();
3738}
3739
3740template <class _Tp, class _Dp, class _Alloc>
3741void
Howard Hinnant1694d232011-05-28 14:41:13 +00003742__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003743{
3744 typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
3745 __data_.second().~_Alloc();
3746 __a.deallocate(this, 1);
3747}
3748
3749template <class _Tp, class _Alloc>
3750class __shared_ptr_emplace
3751 : public __shared_weak_count
3752{
3753 __compressed_pair<_Alloc, _Tp> __data_;
3754public:
3755#ifndef _LIBCPP_HAS_NO_VARIADICS
3756
Howard Hinnant82894812010-09-22 16:48:34 +00003757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003758 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003759 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003760
3761 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00003762 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003763 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00003764 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3765 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003766
3767#else // _LIBCPP_HAS_NO_VARIADICS
3768
Howard Hinnant82894812010-09-22 16:48:34 +00003769 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003770 __shared_ptr_emplace(_Alloc __a)
3771 : __data_(__a) {}
3772
3773 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00003774 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003775 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3776 : __data_(__a, _Tp(__a0)) {}
3777
3778 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00003779 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003780 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3781 : __data_(__a, _Tp(__a0, __a1)) {}
3782
3783 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00003784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003785 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3786 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3787
3788#endif // _LIBCPP_HAS_NO_VARIADICS
3789
3790private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003791 virtual void __on_zero_shared() _NOEXCEPT;
3792 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003793public:
Howard Hinnant82894812010-09-22 16:48:34 +00003794 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003795 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003796};
3797
3798template <class _Tp, class _Alloc>
3799void
Howard Hinnant1694d232011-05-28 14:41:13 +00003800__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003801{
3802 __data_.second().~_Tp();
3803}
3804
3805template <class _Tp, class _Alloc>
3806void
Howard Hinnant1694d232011-05-28 14:41:13 +00003807__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003808{
3809 typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
3810 __data_.first().~_Alloc();
3811 __a.deallocate(this, 1);
3812}
3813
Howard Hinnant83eade62013-03-06 23:30:19 +00003814template<class _Tp> class _LIBCPP_TYPE_VIS enable_shared_from_this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003815
3816template<class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00003817class _LIBCPP_TYPE_VIS shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003818{
Howard Hinnant324bb032010-08-22 00:02:43 +00003819public:
3820 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003821private:
3822 element_type* __ptr_;
3823 __shared_weak_count* __cntrl_;
3824
3825 struct __nat {int __for_bool_;};
3826public:
Howard Hinnant46e94932012-07-07 20:56:04 +00003827 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
3828 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003829 template<class _Yp,
3830 class = typename enable_if
3831 <
3832 is_convertible<_Yp*, element_type*>::value
3833 >::type
3834 >
3835 explicit shared_ptr(_Yp* __p);
3836 template<class _Yp, class _Dp,
3837 class = typename enable_if
3838 <
3839 is_convertible<_Yp*, element_type*>::value
3840 >::type
3841 >
3842 shared_ptr(_Yp* __p, _Dp __d);
3843 template<class _Yp, class _Dp, class _Alloc,
3844 class = typename enable_if
3845 <
3846 is_convertible<_Yp*, element_type*>::value
3847 >::type
3848 >
3849 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003850 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3851 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant1694d232011-05-28 14:41:13 +00003852 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3853 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003854 template<class _Yp>
3855 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003856 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3857 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003858#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003859 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003860 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003861 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3862 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003863#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003864 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003865 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003866#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003867 template<class _Yp,
3868 class = typename enable_if
3869 <
3870 is_convertible<_Yp*, element_type*>::value
3871 >::type
3872 >
3873 shared_ptr(auto_ptr<_Yp>&& __r);
Howard Hinnant324bb032010-08-22 00:02:43 +00003874#else
Howard Hinnant57199402012-01-02 17:56:02 +00003875 template<class _Yp,
3876 class = typename enable_if
3877 <
3878 is_convertible<_Yp*, element_type*>::value
3879 >::type
3880 >
3881 shared_ptr(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003882#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003883#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003884 template <class _Yp, class _Dp,
3885 class = typename enable_if
3886 <
3887 !is_array<_Yp>::value &&
3888 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3889 >::type
3890 >
3891 shared_ptr(unique_ptr<_Yp, _Dp>&&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003892 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant57199402012-01-02 17:56:02 +00003893 template <class _Yp, class _Dp,
3894 class = typename enable_if
3895 <
3896 !is_array<_Yp>::value &&
3897 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3898 >::type
3899 >
3900 shared_ptr(unique_ptr<_Yp, _Dp>&&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003901 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003902#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003903 template <class _Yp, class _Dp,
3904 class = typename enable_if
3905 <
3906 !is_array<_Yp>::value &&
3907 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3908 >::type
3909 > shared_ptr(unique_ptr<_Yp, _Dp>,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003910 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant57199402012-01-02 17:56:02 +00003911 template <class _Yp, class _Dp,
3912 class = typename enable_if
3913 <
3914 !is_array<_Yp>::value &&
3915 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3916 >::type
3917 >
3918 shared_ptr(unique_ptr<_Yp, _Dp>,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003919 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003920#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003921
3922 ~shared_ptr();
3923
Howard Hinnant1694d232011-05-28 14:41:13 +00003924 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003925 template<class _Yp>
3926 typename enable_if
3927 <
3928 is_convertible<_Yp*, element_type*>::value,
3929 shared_ptr&
3930 >::type
3931 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003932#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003933 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003934 template<class _Yp>
3935 typename enable_if
3936 <
3937 is_convertible<_Yp*, element_type*>::value,
3938 shared_ptr<_Tp>&
3939 >::type
3940 operator=(shared_ptr<_Yp>&& __r);
3941 template<class _Yp>
3942 typename enable_if
3943 <
3944 !is_array<_Yp>::value &&
3945 is_convertible<_Yp*, element_type*>::value,
3946 shared_ptr&
3947 >::type
3948 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003949#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003950 template<class _Yp>
3951 typename enable_if
3952 <
3953 !is_array<_Yp>::value &&
3954 is_convertible<_Yp*, element_type*>::value,
3955 shared_ptr&
3956 >::type
3957 operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003958#endif
Howard Hinnant57199402012-01-02 17:56:02 +00003959 template <class _Yp, class _Dp>
3960 typename enable_if
3961 <
3962 !is_array<_Yp>::value &&
3963 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3964 shared_ptr&
3965 >::type
Howard Hinnant73d21a42010-09-04 23:28:19 +00003966#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003967 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003968#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003969 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003970#endif
3971
Howard Hinnant1694d232011-05-28 14:41:13 +00003972 void swap(shared_ptr& __r) _NOEXCEPT;
3973 void reset() _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003974 template<class _Yp>
3975 typename enable_if
3976 <
3977 is_convertible<_Yp*, element_type*>::value,
3978 void
3979 >::type
3980 reset(_Yp* __p);
3981 template<class _Yp, class _Dp>
3982 typename enable_if
3983 <
3984 is_convertible<_Yp*, element_type*>::value,
3985 void
3986 >::type
3987 reset(_Yp* __p, _Dp __d);
3988 template<class _Yp, class _Dp, class _Alloc>
3989 typename enable_if
3990 <
3991 is_convertible<_Yp*, element_type*>::value,
3992 void
3993 >::type
3994 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003995
Howard Hinnant82894812010-09-22 16:48:34 +00003996 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003997 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003998 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003999 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
4000 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004001 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004002 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004003 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004004 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00004005 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004006 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00004007 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant77861882012-02-21 21:46:43 +00004008 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant99968442011-11-29 18:15:50 +00004009 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00004010 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00004011 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004012 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant99968442011-11-29 18:15:50 +00004013 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00004014 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00004015 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004016 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant5fec82d2012-07-30 01:40:57 +00004017 _LIBCPP_INLINE_VISIBILITY
4018 bool
4019 __owner_equivalent(const shared_ptr& __p) const
4020 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004021
Howard Hinnantd4444702010-08-11 17:04:31 +00004022#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004023 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00004024 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004025 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004026 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00004027#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004028
4029#ifndef _LIBCPP_HAS_NO_VARIADICS
4030
4031 template<class ..._Args>
4032 static
4033 shared_ptr<_Tp>
4034 make_shared(_Args&& ...__args);
4035
4036 template<class _Alloc, class ..._Args>
4037 static
4038 shared_ptr<_Tp>
4039 allocate_shared(const _Alloc& __a, _Args&& ...__args);
4040
4041#else // _LIBCPP_HAS_NO_VARIADICS
4042
4043 static shared_ptr<_Tp> make_shared();
4044
4045 template<class _A0>
4046 static shared_ptr<_Tp> make_shared(_A0&);
4047
4048 template<class _A0, class _A1>
4049 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
4050
4051 template<class _A0, class _A1, class _A2>
4052 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
4053
4054 template<class _Alloc>
4055 static shared_ptr<_Tp>
4056 allocate_shared(const _Alloc& __a);
4057
4058 template<class _Alloc, class _A0>
4059 static shared_ptr<_Tp>
4060 allocate_shared(const _Alloc& __a, _A0& __a0);
4061
4062 template<class _Alloc, class _A0, class _A1>
4063 static shared_ptr<_Tp>
4064 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
4065
4066 template<class _Alloc, class _A0, class _A1, class _A2>
4067 static shared_ptr<_Tp>
4068 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
4069
4070#endif // _LIBCPP_HAS_NO_VARIADICS
4071
4072private:
4073
4074 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00004075 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004076 void
Howard Hinnant1694d232011-05-28 14:41:13 +00004077 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004078 {
4079 if (__e)
4080 __e->__weak_this_ = *this;
4081 }
4082
Howard Hinnant82894812010-09-22 16:48:34 +00004083 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004084 void __enable_weak_this(const void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004085
Howard Hinnant83eade62013-03-06 23:30:19 +00004086 template <class _Up> friend class _LIBCPP_TYPE_VIS shared_ptr;
4087 template <class _Up> friend class _LIBCPP_TYPE_VIS weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004088};
4089
4090template<class _Tp>
4091inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00004092_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004093shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004094 : __ptr_(0),
4095 __cntrl_(0)
4096{
4097}
4098
4099template<class _Tp>
4100inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00004101_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004102shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004103 : __ptr_(0),
4104 __cntrl_(0)
4105{
4106}
4107
4108template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004109template<class _Yp, class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004110shared_ptr<_Tp>::shared_ptr(_Yp* __p)
4111 : __ptr_(__p)
4112{
4113 unique_ptr<_Yp> __hold(__p);
4114 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4115 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
4116 __hold.release();
4117 __enable_weak_this(__p);
4118}
4119
4120template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004121template<class _Yp, class _Dp, class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004122shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
4123 : __ptr_(__p)
4124{
4125#ifndef _LIBCPP_NO_EXCEPTIONS
4126 try
4127 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004128#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004129 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4130 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
4131 __enable_weak_this(__p);
4132#ifndef _LIBCPP_NO_EXCEPTIONS
4133 }
4134 catch (...)
4135 {
4136 __d(__p);
4137 throw;
4138 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004139#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004140}
4141
4142template<class _Tp>
4143template<class _Dp>
4144shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4145 : __ptr_(0)
4146{
4147#ifndef _LIBCPP_NO_EXCEPTIONS
4148 try
4149 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004150#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004151 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4152 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4153#ifndef _LIBCPP_NO_EXCEPTIONS
4154 }
4155 catch (...)
4156 {
4157 __d(__p);
4158 throw;
4159 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004160#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004161}
4162
4163template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004164template<class _Yp, class _Dp, class _Alloc, class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004165shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
4166 : __ptr_(__p)
4167{
4168#ifndef _LIBCPP_NO_EXCEPTIONS
4169 try
4170 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004171#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004172 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
4173 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
4174 typedef __allocator_destructor<_A2> _D2;
4175 _A2 __a2(__a);
4176 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4177 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
4178 __cntrl_ = __hold2.release();
4179 __enable_weak_this(__p);
4180#ifndef _LIBCPP_NO_EXCEPTIONS
4181 }
4182 catch (...)
4183 {
4184 __d(__p);
4185 throw;
4186 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004187#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004188}
4189
4190template<class _Tp>
4191template<class _Dp, class _Alloc>
4192shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4193 : __ptr_(0)
4194{
4195#ifndef _LIBCPP_NO_EXCEPTIONS
4196 try
4197 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004198#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004199 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
4200 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
4201 typedef __allocator_destructor<_A2> _D2;
4202 _A2 __a2(__a);
4203 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4204 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
4205 __cntrl_ = __hold2.release();
4206#ifndef _LIBCPP_NO_EXCEPTIONS
4207 }
4208 catch (...)
4209 {
4210 __d(__p);
4211 throw;
4212 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004213#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004214}
4215
4216template<class _Tp>
4217template<class _Yp>
4218inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004219shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004220 : __ptr_(__p),
4221 __cntrl_(__r.__cntrl_)
4222{
4223 if (__cntrl_)
4224 __cntrl_->__add_shared();
4225}
4226
4227template<class _Tp>
4228inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004229shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004230 : __ptr_(__r.__ptr_),
4231 __cntrl_(__r.__cntrl_)
4232{
4233 if (__cntrl_)
4234 __cntrl_->__add_shared();
4235}
4236
4237template<class _Tp>
4238template<class _Yp>
4239inline _LIBCPP_INLINE_VISIBILITY
4240shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4241 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004242 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004243 : __ptr_(__r.__ptr_),
4244 __cntrl_(__r.__cntrl_)
4245{
4246 if (__cntrl_)
4247 __cntrl_->__add_shared();
4248}
4249
Howard Hinnant73d21a42010-09-04 23:28:19 +00004250#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004251
4252template<class _Tp>
4253inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004254shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004255 : __ptr_(__r.__ptr_),
4256 __cntrl_(__r.__cntrl_)
4257{
4258 __r.__ptr_ = 0;
4259 __r.__cntrl_ = 0;
4260}
4261
4262template<class _Tp>
4263template<class _Yp>
4264inline _LIBCPP_INLINE_VISIBILITY
4265shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4266 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004267 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004268 : __ptr_(__r.__ptr_),
4269 __cntrl_(__r.__cntrl_)
4270{
4271 __r.__ptr_ = 0;
4272 __r.__cntrl_ = 0;
4273}
4274
Howard Hinnant73d21a42010-09-04 23:28:19 +00004275#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004276
4277template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004278template<class _Yp, class>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004279#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004280shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
4281#else
Howard Hinnant92172b82010-08-21 21:14:53 +00004282shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004283#endif
4284 : __ptr_(__r.get())
4285{
4286 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4287 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
4288 __enable_weak_this(__r.get());
4289 __r.release();
4290}
4291
4292template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004293template <class _Yp, class _Dp, class>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004294#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004295shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4296#else
4297shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4298#endif
4299 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
4300 : __ptr_(__r.get())
4301{
4302 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4303 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
4304 __enable_weak_this(__r.get());
4305 __r.release();
4306}
4307
4308template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004309template <class _Yp, class _Dp, class>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004310#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004311shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4312#else
4313shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4314#endif
4315 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
4316 : __ptr_(__r.get())
4317{
4318 typedef __shared_ptr_pointer<_Yp*,
4319 reference_wrapper<typename remove_reference<_Dp>::type>,
4320 allocator<_Yp> > _CntrlBlk;
4321 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
4322 __enable_weak_this(__r.get());
4323 __r.release();
4324}
4325
4326#ifndef _LIBCPP_HAS_NO_VARIADICS
4327
4328template<class _Tp>
4329template<class ..._Args>
4330shared_ptr<_Tp>
4331shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4332{
4333 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4334 typedef allocator<_CntrlBlk> _A2;
4335 typedef __allocator_destructor<_A2> _D2;
4336 _A2 __a2;
4337 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004338 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004339 shared_ptr<_Tp> __r;
4340 __r.__ptr_ = __hold2.get()->get();
4341 __r.__cntrl_ = __hold2.release();
4342 __r.__enable_weak_this(__r.__ptr_);
4343 return __r;
4344}
4345
4346template<class _Tp>
4347template<class _Alloc, class ..._Args>
4348shared_ptr<_Tp>
4349shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4350{
4351 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4352 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
4353 typedef __allocator_destructor<_A2> _D2;
4354 _A2 __a2(__a);
4355 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004356 ::new(__hold2.get()) _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004357 shared_ptr<_Tp> __r;
4358 __r.__ptr_ = __hold2.get()->get();
4359 __r.__cntrl_ = __hold2.release();
4360 __r.__enable_weak_this(__r.__ptr_);
4361 return __r;
4362}
4363
4364#else // _LIBCPP_HAS_NO_VARIADICS
4365
4366template<class _Tp>
4367shared_ptr<_Tp>
4368shared_ptr<_Tp>::make_shared()
4369{
4370 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4371 typedef allocator<_CntrlBlk> _Alloc2;
4372 typedef __allocator_destructor<_Alloc2> _D2;
4373 _Alloc2 __alloc2;
4374 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4375 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4376 shared_ptr<_Tp> __r;
4377 __r.__ptr_ = __hold2.get()->get();
4378 __r.__cntrl_ = __hold2.release();
4379 __r.__enable_weak_this(__r.__ptr_);
4380 return __r;
4381}
4382
4383template<class _Tp>
4384template<class _A0>
4385shared_ptr<_Tp>
4386shared_ptr<_Tp>::make_shared(_A0& __a0)
4387{
4388 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4389 typedef allocator<_CntrlBlk> _Alloc2;
4390 typedef __allocator_destructor<_Alloc2> _D2;
4391 _Alloc2 __alloc2;
4392 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4393 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4394 shared_ptr<_Tp> __r;
4395 __r.__ptr_ = __hold2.get()->get();
4396 __r.__cntrl_ = __hold2.release();
4397 __r.__enable_weak_this(__r.__ptr_);
4398 return __r;
4399}
4400
4401template<class _Tp>
4402template<class _A0, class _A1>
4403shared_ptr<_Tp>
4404shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4405{
4406 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4407 typedef allocator<_CntrlBlk> _Alloc2;
4408 typedef __allocator_destructor<_Alloc2> _D2;
4409 _Alloc2 __alloc2;
4410 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4411 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4412 shared_ptr<_Tp> __r;
4413 __r.__ptr_ = __hold2.get()->get();
4414 __r.__cntrl_ = __hold2.release();
4415 __r.__enable_weak_this(__r.__ptr_);
4416 return __r;
4417}
4418
4419template<class _Tp>
4420template<class _A0, class _A1, class _A2>
4421shared_ptr<_Tp>
4422shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4423{
4424 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4425 typedef allocator<_CntrlBlk> _Alloc2;
4426 typedef __allocator_destructor<_Alloc2> _D2;
4427 _Alloc2 __alloc2;
4428 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4429 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4430 shared_ptr<_Tp> __r;
4431 __r.__ptr_ = __hold2.get()->get();
4432 __r.__cntrl_ = __hold2.release();
4433 __r.__enable_weak_this(__r.__ptr_);
4434 return __r;
4435}
4436
4437template<class _Tp>
4438template<class _Alloc>
4439shared_ptr<_Tp>
4440shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4441{
4442 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4443 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4444 typedef __allocator_destructor<_Alloc2> _D2;
4445 _Alloc2 __alloc2(__a);
4446 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4447 ::new(__hold2.get()) _CntrlBlk(__a);
4448 shared_ptr<_Tp> __r;
4449 __r.__ptr_ = __hold2.get()->get();
4450 __r.__cntrl_ = __hold2.release();
4451 __r.__enable_weak_this(__r.__ptr_);
4452 return __r;
4453}
4454
4455template<class _Tp>
4456template<class _Alloc, class _A0>
4457shared_ptr<_Tp>
4458shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4459{
4460 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4461 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4462 typedef __allocator_destructor<_Alloc2> _D2;
4463 _Alloc2 __alloc2(__a);
4464 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4465 ::new(__hold2.get()) _CntrlBlk(__a, __a0);
4466 shared_ptr<_Tp> __r;
4467 __r.__ptr_ = __hold2.get()->get();
4468 __r.__cntrl_ = __hold2.release();
4469 __r.__enable_weak_this(__r.__ptr_);
4470 return __r;
4471}
4472
4473template<class _Tp>
4474template<class _Alloc, class _A0, class _A1>
4475shared_ptr<_Tp>
4476shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4477{
4478 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4479 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4480 typedef __allocator_destructor<_Alloc2> _D2;
4481 _Alloc2 __alloc2(__a);
4482 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4483 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
4484 shared_ptr<_Tp> __r;
4485 __r.__ptr_ = __hold2.get()->get();
4486 __r.__cntrl_ = __hold2.release();
4487 __r.__enable_weak_this(__r.__ptr_);
4488 return __r;
4489}
4490
4491template<class _Tp>
4492template<class _Alloc, class _A0, class _A1, class _A2>
4493shared_ptr<_Tp>
4494shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4495{
4496 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4497 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4498 typedef __allocator_destructor<_Alloc2> _D2;
4499 _Alloc2 __alloc2(__a);
4500 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4501 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
4502 shared_ptr<_Tp> __r;
4503 __r.__ptr_ = __hold2.get()->get();
4504 __r.__cntrl_ = __hold2.release();
4505 __r.__enable_weak_this(__r.__ptr_);
4506 return __r;
4507}
4508
4509#endif // _LIBCPP_HAS_NO_VARIADICS
4510
4511template<class _Tp>
4512shared_ptr<_Tp>::~shared_ptr()
4513{
4514 if (__cntrl_)
4515 __cntrl_->__release_shared();
4516}
4517
4518template<class _Tp>
4519inline _LIBCPP_INLINE_VISIBILITY
4520shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004521shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004522{
4523 shared_ptr(__r).swap(*this);
4524 return *this;
4525}
4526
4527template<class _Tp>
4528template<class _Yp>
4529inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004530typename enable_if
4531<
4532 is_convertible<_Yp*, _Tp*>::value,
4533 shared_ptr<_Tp>&
4534>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004535shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004536{
4537 shared_ptr(__r).swap(*this);
4538 return *this;
4539}
4540
Howard Hinnant73d21a42010-09-04 23:28:19 +00004541#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004542
4543template<class _Tp>
4544inline _LIBCPP_INLINE_VISIBILITY
4545shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004546shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004547{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004548 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004549 return *this;
4550}
4551
4552template<class _Tp>
4553template<class _Yp>
4554inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004555typename enable_if
4556<
4557 is_convertible<_Yp*, _Tp*>::value,
4558 shared_ptr<_Tp>&
4559>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004560shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4561{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004562 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004563 return *this;
4564}
4565
4566template<class _Tp>
4567template<class _Yp>
4568inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004569typename enable_if
4570<
4571 !is_array<_Yp>::value &&
4572 is_convertible<_Yp*, _Tp*>::value,
4573 shared_ptr<_Tp>&
4574>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004575shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4576{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004577 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004578 return *this;
4579}
4580
4581template<class _Tp>
4582template <class _Yp, class _Dp>
4583inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004584typename enable_if
4585<
4586 !is_array<_Yp>::value &&
4587 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4588 shared_ptr<_Tp>&
4589>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004590shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4591{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004592 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004593 return *this;
4594}
4595
Howard Hinnant73d21a42010-09-04 23:28:19 +00004596#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004597
4598template<class _Tp>
4599template<class _Yp>
4600inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004601typename enable_if
4602<
4603 !is_array<_Yp>::value &&
4604 is_convertible<_Yp*, _Tp*>::value,
4605 shared_ptr<_Tp>&
4606>::type
Howard Hinnant324bb032010-08-22 00:02:43 +00004607shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004608{
4609 shared_ptr(__r).swap(*this);
4610 return *this;
4611}
4612
4613template<class _Tp>
4614template <class _Yp, class _Dp>
4615inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004616typename enable_if
4617<
4618 !is_array<_Yp>::value &&
4619 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4620 shared_ptr<_Tp>&
4621>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004622shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4623{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004624 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004625 return *this;
4626}
4627
Howard Hinnant73d21a42010-09-04 23:28:19 +00004628#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004629
4630template<class _Tp>
4631inline _LIBCPP_INLINE_VISIBILITY
4632void
Howard Hinnant1694d232011-05-28 14:41:13 +00004633shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004634{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004635 _VSTD::swap(__ptr_, __r.__ptr_);
4636 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004637}
4638
4639template<class _Tp>
4640inline _LIBCPP_INLINE_VISIBILITY
4641void
Howard Hinnant1694d232011-05-28 14:41:13 +00004642shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004643{
4644 shared_ptr().swap(*this);
4645}
4646
4647template<class _Tp>
4648template<class _Yp>
4649inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004650typename enable_if
4651<
4652 is_convertible<_Yp*, _Tp*>::value,
4653 void
4654>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004655shared_ptr<_Tp>::reset(_Yp* __p)
4656{
4657 shared_ptr(__p).swap(*this);
4658}
4659
4660template<class _Tp>
4661template<class _Yp, class _Dp>
4662inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004663typename enable_if
4664<
4665 is_convertible<_Yp*, _Tp*>::value,
4666 void
4667>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004668shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4669{
4670 shared_ptr(__p, __d).swap(*this);
4671}
4672
4673template<class _Tp>
4674template<class _Yp, class _Dp, class _Alloc>
4675inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004676typename enable_if
4677<
4678 is_convertible<_Yp*, _Tp*>::value,
4679 void
4680>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004681shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4682{
4683 shared_ptr(__p, __d, __a).swap(*this);
4684}
4685
4686#ifndef _LIBCPP_HAS_NO_VARIADICS
4687
Howard Hinnant324bb032010-08-22 00:02:43 +00004688template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004689inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004690typename enable_if
4691<
4692 !is_array<_Tp>::value,
4693 shared_ptr<_Tp>
4694>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004695make_shared(_Args&& ...__args)
4696{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004697 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004698}
4699
Howard Hinnant324bb032010-08-22 00:02:43 +00004700template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004701inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004702typename enable_if
4703<
4704 !is_array<_Tp>::value,
4705 shared_ptr<_Tp>
4706>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004707allocate_shared(const _Alloc& __a, _Args&& ...__args)
4708{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004709 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004710}
4711
4712#else // _LIBCPP_HAS_NO_VARIADICS
4713
4714template<class _Tp>
4715inline _LIBCPP_INLINE_VISIBILITY
4716shared_ptr<_Tp>
4717make_shared()
4718{
4719 return shared_ptr<_Tp>::make_shared();
4720}
4721
4722template<class _Tp, class _A0>
4723inline _LIBCPP_INLINE_VISIBILITY
4724shared_ptr<_Tp>
4725make_shared(_A0& __a0)
4726{
4727 return shared_ptr<_Tp>::make_shared(__a0);
4728}
4729
4730template<class _Tp, class _A0, class _A1>
4731inline _LIBCPP_INLINE_VISIBILITY
4732shared_ptr<_Tp>
4733make_shared(_A0& __a0, _A1& __a1)
4734{
4735 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4736}
4737
Howard Hinnant324bb032010-08-22 00:02:43 +00004738template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004739inline _LIBCPP_INLINE_VISIBILITY
4740shared_ptr<_Tp>
4741make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4742{
4743 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4744}
4745
4746template<class _Tp, class _Alloc>
4747inline _LIBCPP_INLINE_VISIBILITY
4748shared_ptr<_Tp>
4749allocate_shared(const _Alloc& __a)
4750{
4751 return shared_ptr<_Tp>::allocate_shared(__a);
4752}
4753
4754template<class _Tp, class _Alloc, class _A0>
4755inline _LIBCPP_INLINE_VISIBILITY
4756shared_ptr<_Tp>
4757allocate_shared(const _Alloc& __a, _A0& __a0)
4758{
4759 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4760}
4761
4762template<class _Tp, class _Alloc, class _A0, class _A1>
4763inline _LIBCPP_INLINE_VISIBILITY
4764shared_ptr<_Tp>
4765allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4766{
4767 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4768}
4769
4770template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4771inline _LIBCPP_INLINE_VISIBILITY
4772shared_ptr<_Tp>
4773allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4774{
4775 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4776}
4777
4778#endif // _LIBCPP_HAS_NO_VARIADICS
4779
4780template<class _Tp, class _Up>
4781inline _LIBCPP_INLINE_VISIBILITY
4782bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004783operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004784{
4785 return __x.get() == __y.get();
4786}
4787
4788template<class _Tp, class _Up>
4789inline _LIBCPP_INLINE_VISIBILITY
4790bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004791operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004792{
4793 return !(__x == __y);
4794}
4795
4796template<class _Tp, class _Up>
4797inline _LIBCPP_INLINE_VISIBILITY
4798bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004799operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004800{
Howard Hinnant3fadda32012-02-21 21:02:58 +00004801 typedef typename common_type<_Tp*, _Up*>::type _V;
4802 return less<_V>()(__x.get(), __y.get());
4803}
4804
4805template<class _Tp, class _Up>
4806inline _LIBCPP_INLINE_VISIBILITY
4807bool
4808operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4809{
4810 return __y < __x;
4811}
4812
4813template<class _Tp, class _Up>
4814inline _LIBCPP_INLINE_VISIBILITY
4815bool
4816operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4817{
4818 return !(__y < __x);
4819}
4820
4821template<class _Tp, class _Up>
4822inline _LIBCPP_INLINE_VISIBILITY
4823bool
4824operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4825{
4826 return !(__x < __y);
4827}
4828
4829template<class _Tp>
4830inline _LIBCPP_INLINE_VISIBILITY
4831bool
4832operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4833{
4834 return !__x;
4835}
4836
4837template<class _Tp>
4838inline _LIBCPP_INLINE_VISIBILITY
4839bool
4840operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4841{
4842 return !__x;
4843}
4844
4845template<class _Tp>
4846inline _LIBCPP_INLINE_VISIBILITY
4847bool
4848operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4849{
4850 return static_cast<bool>(__x);
4851}
4852
4853template<class _Tp>
4854inline _LIBCPP_INLINE_VISIBILITY
4855bool
4856operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4857{
4858 return static_cast<bool>(__x);
4859}
4860
4861template<class _Tp>
4862inline _LIBCPP_INLINE_VISIBILITY
4863bool
4864operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4865{
4866 return less<_Tp*>()(__x.get(), nullptr);
4867}
4868
4869template<class _Tp>
4870inline _LIBCPP_INLINE_VISIBILITY
4871bool
4872operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4873{
4874 return less<_Tp*>()(nullptr, __x.get());
4875}
4876
4877template<class _Tp>
4878inline _LIBCPP_INLINE_VISIBILITY
4879bool
4880operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4881{
4882 return nullptr < __x;
4883}
4884
4885template<class _Tp>
4886inline _LIBCPP_INLINE_VISIBILITY
4887bool
4888operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4889{
4890 return __x < nullptr;
4891}
4892
4893template<class _Tp>
4894inline _LIBCPP_INLINE_VISIBILITY
4895bool
4896operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4897{
4898 return !(nullptr < __x);
4899}
4900
4901template<class _Tp>
4902inline _LIBCPP_INLINE_VISIBILITY
4903bool
4904operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4905{
4906 return !(__x < nullptr);
4907}
4908
4909template<class _Tp>
4910inline _LIBCPP_INLINE_VISIBILITY
4911bool
4912operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4913{
4914 return !(__x < nullptr);
4915}
4916
4917template<class _Tp>
4918inline _LIBCPP_INLINE_VISIBILITY
4919bool
4920operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4921{
4922 return !(nullptr < __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004923}
4924
4925template<class _Tp>
4926inline _LIBCPP_INLINE_VISIBILITY
4927void
Howard Hinnant1694d232011-05-28 14:41:13 +00004928swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004929{
4930 __x.swap(__y);
4931}
4932
4933template<class _Tp, class _Up>
4934inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004935typename enable_if
4936<
4937 !is_array<_Tp>::value && !is_array<_Up>::value,
4938 shared_ptr<_Tp>
4939>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004940static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004941{
4942 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4943}
4944
4945template<class _Tp, class _Up>
4946inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004947typename enable_if
4948<
4949 !is_array<_Tp>::value && !is_array<_Up>::value,
4950 shared_ptr<_Tp>
4951>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004952dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004953{
4954 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4955 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4956}
4957
4958template<class _Tp, class _Up>
Howard Hinnant57199402012-01-02 17:56:02 +00004959typename enable_if
4960<
4961 is_array<_Tp>::value == is_array<_Up>::value,
4962 shared_ptr<_Tp>
4963>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004964const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004965{
Howard Hinnant57199402012-01-02 17:56:02 +00004966 typedef typename remove_extent<_Tp>::type _RTp;
4967 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004968}
4969
Howard Hinnantd4444702010-08-11 17:04:31 +00004970#ifndef _LIBCPP_NO_RTTI
4971
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004972template<class _Dp, class _Tp>
4973inline _LIBCPP_INLINE_VISIBILITY
4974_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00004975get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004976{
4977 return __p.template __get_deleter<_Dp>();
4978}
4979
Howard Hinnant324bb032010-08-22 00:02:43 +00004980#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00004981
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004982template<class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00004983class _LIBCPP_TYPE_VIS weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004984{
Howard Hinnant324bb032010-08-22 00:02:43 +00004985public:
4986 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004987private:
4988 element_type* __ptr_;
4989 __shared_weak_count* __cntrl_;
4990
Howard Hinnant324bb032010-08-22 00:02:43 +00004991public:
Howard Hinnant46e94932012-07-07 20:56:04 +00004992 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004993 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00004994 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4995 _NOEXCEPT;
4996 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004997 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00004998 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4999 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005000
Howard Hinnant57199402012-01-02 17:56:02 +00005001#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5002 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
5003 template<class _Yp> weak_ptr(weak_ptr<_Yp>&& __r,
5004 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5005 _NOEXCEPT;
5006#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00005007 ~weak_ptr();
5008
Howard Hinnant1694d232011-05-28 14:41:13 +00005009 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00005010 template<class _Yp>
5011 typename enable_if
5012 <
5013 is_convertible<_Yp*, element_type*>::value,
5014 weak_ptr&
5015 >::type
5016 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
5017
5018#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5019
5020 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
5021 template<class _Yp>
5022 typename enable_if
5023 <
5024 is_convertible<_Yp*, element_type*>::value,
5025 weak_ptr&
5026 >::type
5027 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
5028
5029#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5030
5031 template<class _Yp>
5032 typename enable_if
5033 <
5034 is_convertible<_Yp*, element_type*>::value,
5035 weak_ptr&
5036 >::type
5037 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005038
Howard Hinnant1694d232011-05-28 14:41:13 +00005039 void swap(weak_ptr& __r) _NOEXCEPT;
5040 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00005041
Howard Hinnant82894812010-09-22 16:48:34 +00005042 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005043 long use_count() const _NOEXCEPT
5044 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00005045 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005046 bool expired() const _NOEXCEPT
5047 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5048 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00005049 template<class _Up>
5050 _LIBCPP_INLINE_VISIBILITY
5051 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005052 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00005053 template<class _Up>
5054 _LIBCPP_INLINE_VISIBILITY
5055 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005056 {return __cntrl_ < __r.__cntrl_;}
5057
Howard Hinnant83eade62013-03-06 23:30:19 +00005058 template <class _Up> friend class _LIBCPP_TYPE_VIS weak_ptr;
5059 template <class _Up> friend class _LIBCPP_TYPE_VIS shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005060};
5061
5062template<class _Tp>
5063inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00005064_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005065weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005066 : __ptr_(0),
5067 __cntrl_(0)
5068{
5069}
5070
5071template<class _Tp>
5072inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005073weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005074 : __ptr_(__r.__ptr_),
5075 __cntrl_(__r.__cntrl_)
5076{
5077 if (__cntrl_)
5078 __cntrl_->__add_weak();
5079}
5080
5081template<class _Tp>
5082template<class _Yp>
5083inline _LIBCPP_INLINE_VISIBILITY
5084weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005085 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005086 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005087 : __ptr_(__r.__ptr_),
5088 __cntrl_(__r.__cntrl_)
5089{
5090 if (__cntrl_)
5091 __cntrl_->__add_weak();
5092}
5093
5094template<class _Tp>
5095template<class _Yp>
5096inline _LIBCPP_INLINE_VISIBILITY
5097weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005098 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005099 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005100 : __ptr_(__r.__ptr_),
5101 __cntrl_(__r.__cntrl_)
5102{
5103 if (__cntrl_)
5104 __cntrl_->__add_weak();
5105}
5106
Howard Hinnant57199402012-01-02 17:56:02 +00005107#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5108
5109template<class _Tp>
5110inline _LIBCPP_INLINE_VISIBILITY
5111weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5112 : __ptr_(__r.__ptr_),
5113 __cntrl_(__r.__cntrl_)
5114{
5115 __r.__ptr_ = 0;
5116 __r.__cntrl_ = 0;
5117}
5118
5119template<class _Tp>
5120template<class _Yp>
5121inline _LIBCPP_INLINE_VISIBILITY
5122weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5123 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5124 _NOEXCEPT
5125 : __ptr_(__r.__ptr_),
5126 __cntrl_(__r.__cntrl_)
5127{
5128 __r.__ptr_ = 0;
5129 __r.__cntrl_ = 0;
5130}
5131
5132#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5133
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005134template<class _Tp>
5135weak_ptr<_Tp>::~weak_ptr()
5136{
5137 if (__cntrl_)
5138 __cntrl_->__release_weak();
5139}
5140
5141template<class _Tp>
5142inline _LIBCPP_INLINE_VISIBILITY
5143weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00005144weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005145{
5146 weak_ptr(__r).swap(*this);
5147 return *this;
5148}
5149
5150template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005151template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005152inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005153typename enable_if
5154<
5155 is_convertible<_Yp*, _Tp*>::value,
5156 weak_ptr<_Tp>&
5157>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005158weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005159{
5160 weak_ptr(__r).swap(*this);
5161 return *this;
5162}
5163
Howard Hinnant57199402012-01-02 17:56:02 +00005164#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5165
5166template<class _Tp>
5167inline _LIBCPP_INLINE_VISIBILITY
5168weak_ptr<_Tp>&
5169weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5170{
5171 weak_ptr(_VSTD::move(__r)).swap(*this);
5172 return *this;
5173}
5174
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005175template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005176template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005177inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005178typename enable_if
5179<
5180 is_convertible<_Yp*, _Tp*>::value,
5181 weak_ptr<_Tp>&
5182>::type
5183weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5184{
5185 weak_ptr(_VSTD::move(__r)).swap(*this);
5186 return *this;
5187}
5188
5189#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5190
5191template<class _Tp>
5192template<class _Yp>
5193inline _LIBCPP_INLINE_VISIBILITY
5194typename enable_if
5195<
5196 is_convertible<_Yp*, _Tp*>::value,
5197 weak_ptr<_Tp>&
5198>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005199weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005200{
5201 weak_ptr(__r).swap(*this);
5202 return *this;
5203}
5204
5205template<class _Tp>
5206inline _LIBCPP_INLINE_VISIBILITY
5207void
Howard Hinnant1694d232011-05-28 14:41:13 +00005208weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005209{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005210 _VSTD::swap(__ptr_, __r.__ptr_);
5211 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005212}
5213
5214template<class _Tp>
5215inline _LIBCPP_INLINE_VISIBILITY
5216void
Howard Hinnant1694d232011-05-28 14:41:13 +00005217swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005218{
5219 __x.swap(__y);
5220}
5221
5222template<class _Tp>
5223inline _LIBCPP_INLINE_VISIBILITY
5224void
Howard Hinnant1694d232011-05-28 14:41:13 +00005225weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005226{
5227 weak_ptr().swap(*this);
5228}
5229
5230template<class _Tp>
5231template<class _Yp>
5232shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5233 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
5234 : __ptr_(__r.__ptr_),
5235 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5236{
5237 if (__cntrl_ == 0)
5238#ifndef _LIBCPP_NO_EXCEPTIONS
5239 throw bad_weak_ptr();
5240#else
5241 assert(!"bad_weak_ptr");
5242#endif
5243}
5244
5245template<class _Tp>
5246shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00005247weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005248{
5249 shared_ptr<_Tp> __r;
5250 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5251 if (__r.__cntrl_)
5252 __r.__ptr_ = __ptr_;
5253 return __r;
5254}
5255
Howard Hinnant324bb032010-08-22 00:02:43 +00005256template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005257
5258template <class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00005259struct _LIBCPP_TYPE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005260 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00005261{
5262 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005263 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005264 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5265 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005266 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005267 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5268 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005269 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005270 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5271 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00005272};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005273
5274template <class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00005275struct _LIBCPP_TYPE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005276 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5277{
Howard Hinnant324bb032010-08-22 00:02:43 +00005278 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005279 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005280 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5281 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005283 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5284 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005286 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5287 {return __x.owner_before(__y);}
5288};
5289
5290template<class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00005291class _LIBCPP_TYPE_VIS enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005292{
5293 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00005294protected:
Howard Hinnant46e94932012-07-07 20:56:04 +00005295 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005296 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005297 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005298 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005300 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5301 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00005302 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005303 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00005304public:
Howard Hinnant82894812010-09-22 16:48:34 +00005305 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005306 shared_ptr<_Tp> shared_from_this()
5307 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00005308 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005309 shared_ptr<_Tp const> shared_from_this() const
5310 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005311
5312 template <class _Up> friend class shared_ptr;
5313};
5314
Howard Hinnant21aefc32010-06-03 16:42:57 +00005315template <class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00005316struct _LIBCPP_TYPE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00005317{
5318 typedef shared_ptr<_Tp> argument_type;
5319 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005321 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00005322 {
5323 return hash<_Tp*>()(__ptr.get());
5324 }
5325};
5326
Howard Hinnant99968442011-11-29 18:15:50 +00005327template<class _CharT, class _Traits, class _Yp>
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005328inline _LIBCPP_INLINE_VISIBILITY
5329basic_ostream<_CharT, _Traits>&
Howard Hinnant99968442011-11-29 18:15:50 +00005330operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005331
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005332#if __has_feature(cxx_atomic)
5333
5334class __sp_mut
5335{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005336 void* __lx;
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005337public:
5338 void lock() _NOEXCEPT;
5339 void unlock() _NOEXCEPT;
5340
5341private:
5342 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5343 __sp_mut(const __sp_mut&);
5344 __sp_mut& operator=(const __sp_mut&);
5345
Howard Hinnant83eade62013-03-06 23:30:19 +00005346 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005347};
5348
Howard Hinnant83eade62013-03-06 23:30:19 +00005349_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005350
5351template <class _Tp>
5352inline _LIBCPP_INLINE_VISIBILITY
5353bool
5354atomic_is_lock_free(const shared_ptr<_Tp>*)
5355{
5356 return false;
5357}
5358
5359template <class _Tp>
5360shared_ptr<_Tp>
5361atomic_load(const shared_ptr<_Tp>* __p)
5362{
5363 __sp_mut& __m = __get_sp_mut(__p);
5364 __m.lock();
5365 shared_ptr<_Tp> __q = *__p;
5366 __m.unlock();
5367 return __q;
5368}
5369
5370template <class _Tp>
5371inline _LIBCPP_INLINE_VISIBILITY
5372shared_ptr<_Tp>
5373atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5374{
5375 return atomic_load(__p);
5376}
5377
5378template <class _Tp>
5379void
5380atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5381{
5382 __sp_mut& __m = __get_sp_mut(__p);
5383 __m.lock();
5384 __p->swap(__r);
5385 __m.unlock();
5386}
5387
5388template <class _Tp>
5389inline _LIBCPP_INLINE_VISIBILITY
5390void
5391atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5392{
5393 atomic_store(__p, __r);
5394}
5395
5396template <class _Tp>
5397shared_ptr<_Tp>
5398atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5399{
5400 __sp_mut& __m = __get_sp_mut(__p);
5401 __m.lock();
5402 __p->swap(__r);
5403 __m.unlock();
5404 return __r;
5405}
5406
5407template <class _Tp>
5408inline _LIBCPP_INLINE_VISIBILITY
5409shared_ptr<_Tp>
5410atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5411{
5412 return atomic_exchange(__p, __r);
5413}
5414
5415template <class _Tp>
5416bool
5417atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5418{
5419 __sp_mut& __m = __get_sp_mut(__p);
5420 __m.lock();
5421 if (__p->__owner_equivalent(*__v))
5422 {
5423 *__p = __w;
5424 __m.unlock();
5425 return true;
5426 }
5427 *__v = *__p;
5428 __m.unlock();
5429 return false;
5430}
5431
5432template <class _Tp>
5433inline _LIBCPP_INLINE_VISIBILITY
5434bool
5435atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5436{
5437 return atomic_compare_exchange_strong(__p, __v, __w);
5438}
5439
5440template <class _Tp>
5441inline _LIBCPP_INLINE_VISIBILITY
5442bool
5443atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5444 shared_ptr<_Tp> __w, memory_order, memory_order)
5445{
5446 return atomic_compare_exchange_strong(__p, __v, __w);
5447}
5448
5449template <class _Tp>
5450inline _LIBCPP_INLINE_VISIBILITY
5451bool
5452atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5453 shared_ptr<_Tp> __w, memory_order, memory_order)
5454{
5455 return atomic_compare_exchange_weak(__p, __v, __w);
5456}
5457
5458#endif // __has_feature(cxx_atomic)
5459
Howard Hinnant324bb032010-08-22 00:02:43 +00005460//enum class
Howard Hinnant83eade62013-03-06 23:30:19 +00005461struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005462{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005463 enum __lx
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005464 {
5465 relaxed,
5466 preferred,
5467 strict
5468 };
5469
Howard Hinnant9c0df142012-10-30 19:06:59 +00005470 __lx __v_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005471
Howard Hinnant82894812010-09-22 16:48:34 +00005472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9c0df142012-10-30 19:06:59 +00005473 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00005474 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005475 operator int() const {return __v_;}
5476};
5477
5478void declare_reachable(void* __p);
5479void declare_no_pointers(char* __p, size_t __n);
5480void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnant1694d232011-05-28 14:41:13 +00005481pointer_safety get_pointer_safety() _NOEXCEPT;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00005482void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005483
5484template <class _Tp>
5485inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005486_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005487undeclare_reachable(_Tp* __p)
5488{
5489 return static_cast<_Tp*>(__undeclare_reachable(__p));
5490}
5491
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00005492void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005493
5494_LIBCPP_END_NAMESPACE_STD
5495
5496#endif // _LIBCPP_MEMORY