blob: 912a43f64f35603e1b1c20e1a28bed3d4f10bd5e [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
353template<class T>
354class shared_ptr
355{
356public:
357 typedef T element_type;
358
359 // constructors:
Howard Hinnant1694d232011-05-28 14:41:13 +0000360 constexpr shared_ptr() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000361 template<class Y> explicit shared_ptr(Y* p);
362 template<class Y, class D> shared_ptr(Y* p, D d);
363 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
364 template <class D> shared_ptr(nullptr_t p, D d);
365 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant1694d232011-05-28 14:41:13 +0000366 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
367 shared_ptr(const shared_ptr& r) noexcept;
368 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
369 shared_ptr(shared_ptr&& r) noexcept;
370 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000371 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
372 template<class Y> shared_ptr(auto_ptr<Y>&& r);
373 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
374 shared_ptr(nullptr_t) : shared_ptr() { }
375
376 // destructor:
377 ~shared_ptr();
378
379 // assignment:
Howard Hinnant1694d232011-05-28 14:41:13 +0000380 shared_ptr& operator=(const shared_ptr& r) noexcept;
381 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
382 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000383 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
384 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r);
385 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
386
387 // modifiers:
Howard Hinnant1694d232011-05-28 14:41:13 +0000388 void swap(shared_ptr& r) noexcept;
389 void reset() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000390 template<class Y> void reset(Y* p);
391 template<class Y, class D> void reset(Y* p, D d);
392 template<class Y, class D, class A> void reset(Y* p, D d, A a);
393
Howard Hinnant1694d232011-05-28 14:41:13 +0000394 // observers:
395 T* get() const noexcept;
396 T& operator*() const noexcept;
397 T* operator->() const noexcept;
398 long use_count() const noexcept;
399 bool unique() const noexcept;
400 explicit operator bool() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000401 template<class U> bool owner_before(shared_ptr<U> const& b) const;
402 template<class U> bool owner_before(weak_ptr<U> const& b) const;
403};
404
405// shared_ptr comparisons:
406template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000407 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000408template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000409 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000410template<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;
418
419template <class T>
420 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
421template <class T>
422 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
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>
430bool 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>
434 bool 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;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000443
444// shared_ptr specialized algorithms:
Howard Hinnant1694d232011-05-28 14:41:13 +0000445template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000446
447// shared_ptr casts:
448template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000449 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000450template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000451 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000452template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000453 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000454
455// shared_ptr I/O:
456template<class E, class T, class Y>
457 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
458
459// shared_ptr get_deleter:
Howard Hinnant1694d232011-05-28 14:41:13 +0000460template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000461
462template<class T, class... Args>
463 shared_ptr<T> make_shared(Args&&... args);
464template<class T, class A, class... Args>
465 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
466
467template<class T>
468class weak_ptr
469{
470public:
471 typedef T element_type;
472
473 // constructors
Howard Hinnant1694d232011-05-28 14:41:13 +0000474 constexpr weak_ptr() noexcept;
475 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
476 weak_ptr(weak_ptr const& r) noexcept;
477 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000478
479 // destructor
480 ~weak_ptr();
481
482 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000483 weak_ptr& operator=(weak_ptr const& r) noexcept;
484 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
485 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000486
487 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000488 void swap(weak_ptr& r) noexcept;
489 void reset() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000490
491 // observers
Howard Hinnant1694d232011-05-28 14:41:13 +0000492 long use_count() const noexcept;
493 bool expired() const noexcept;
494 shared_ptr<T> lock() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000495 template<class U> bool owner_before(shared_ptr<U> const& b);
496 template<class U> bool owner_before(weak_ptr<U> const& b);
497};
498
499// weak_ptr specialized algorithms:
Howard Hinnant1694d232011-05-28 14:41:13 +0000500template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000501
502// class owner_less:
503template<class T> struct owner_less;
504
505template<class T>
506struct owner_less<shared_ptr<T>>
507 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
508{
509 typedef bool result_type;
510 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const;
511 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
512 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
513};
514
515template<class T>
516struct owner_less<weak_ptr<T>>
517 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
518{
519 typedef bool result_type;
520 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const;
521 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
522 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
523};
524
525template<class T>
526class enable_shared_from_this
527{
528protected:
Howard Hinnant1694d232011-05-28 14:41:13 +0000529 constexpr enable_shared_from_this() noexcept;
530 enable_shared_from_this(enable_shared_from_this const&) noexcept;
531 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000532 ~enable_shared_from_this();
533public:
534 shared_ptr<T> shared_from_this();
535 shared_ptr<T const> shared_from_this() const;
536};
537
538template<class T>
539 bool atomic_is_lock_free(const shared_ptr<T>* p);
540template<class T>
541 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
542template<class T>
543 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
544template<class T>
545 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
546template<class T>
547 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
548template<class T>
549 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
550template<class T>
551 shared_ptr<T>
552 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
553template<class T>
554 bool
555 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
556template<class T>
557 bool
558 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
559template<class T>
560 bool
561 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
562 shared_ptr<T> w, memory_order success,
563 memory_order failure);
564template<class T>
565 bool
566 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
567 shared_ptr<T> w, memory_order success,
568 memory_order failure);
569// Hash support
570template <class T> struct hash;
571template <class T, class D> struct hash<unique_ptr<T, D> >;
572template <class T> struct hash<shared_ptr<T> >;
573
574// Pointer safety
575enum class pointer_safety { relaxed, preferred, strict };
576void declare_reachable(void *p);
577template <class T> T *undeclare_reachable(T *p);
578void declare_no_pointers(char *p, size_t n);
579void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant1694d232011-05-28 14:41:13 +0000580pointer_safety get_pointer_safety() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000581
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000582void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
583
584} // std
585
586*/
587
588#include <__config>
589#include <type_traits>
590#include <typeinfo>
591#include <cstddef>
592#include <cstdint>
593#include <new>
594#include <utility>
595#include <limits>
596#include <iterator>
597#include <__functional_base>
Howard Hinnant464aa5c2011-07-18 15:51:59 +0000598#include <iosfwd>
Howard Hinnant6cc99fa2011-12-19 17:58:44 +0000599#include <tuple>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000600#include <cstring>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000601#if defined(_LIBCPP_NO_EXCEPTIONS)
602 #include <cassert>
603#endif
604
Howard Hinnant5fec82d2012-07-30 01:40:57 +0000605#if __has_feature(cxx_atomic)
606# include <atomic>
607#endif
608
Howard Hinnant66c6f972011-11-29 16:45:27 +0000609#include <__undef_min_max>
610
Howard Hinnant08e17472011-10-17 20:05:10 +0000611#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000612#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000613#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000614
615_LIBCPP_BEGIN_NAMESPACE_STD
616
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000617// addressof
618
619template <class _Tp>
620inline _LIBCPP_INLINE_VISIBILITY
621_Tp*
Howard Hinnant1694d232011-05-28 14:41:13 +0000622addressof(_Tp& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000623{
Howard Hinnant4313ec32013-04-16 17:27:56 +0000624 return (_Tp*)&reinterpret_cast<const volatile char&>(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000625}
626
Douglas Gregor35d2fcf2011-06-22 22:17:44 +0000627#if defined(_LIBCPP_HAS_OBJC_ARC) && !defined(_LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF)
628// Objective-C++ Automatic Reference Counting uses qualified pointers
629// that require special addressof() signatures. When
630// _LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF is defined, the compiler
631// itself is providing these definitions. Otherwise, we provide them.
632template <class _Tp>
633inline _LIBCPP_INLINE_VISIBILITY
634__strong _Tp*
635addressof(__strong _Tp& __x) _NOEXCEPT
636{
637 return &__x;
638}
639
640#ifdef _LIBCPP_HAS_OBJC_ARC_WEAK
641template <class _Tp>
642inline _LIBCPP_INLINE_VISIBILITY
643__weak _Tp*
644addressof(__weak _Tp& __x) _NOEXCEPT
645{
646 return &__x;
647}
648#endif
649
650template <class _Tp>
651inline _LIBCPP_INLINE_VISIBILITY
652__autoreleasing _Tp*
653addressof(__autoreleasing _Tp& __x) _NOEXCEPT
654{
655 return &__x;
656}
657
658template <class _Tp>
659inline _LIBCPP_INLINE_VISIBILITY
660__unsafe_unretained _Tp*
661addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT
662{
663 return &__x;
664}
665#endif
666
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000667template <class _Tp> class allocator;
668
669template <>
Howard Hinnant83eade62013-03-06 23:30:19 +0000670class _LIBCPP_TYPE_VIS allocator<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000671{
672public:
673 typedef void* pointer;
674 typedef const void* const_pointer;
675 typedef void value_type;
676
677 template <class _Up> struct rebind {typedef allocator<_Up> other;};
678};
679
Howard Hinnanta1877872012-01-19 23:15:22 +0000680template <>
Howard Hinnant83eade62013-03-06 23:30:19 +0000681class _LIBCPP_TYPE_VIS allocator<const void>
Howard Hinnanta1877872012-01-19 23:15:22 +0000682{
683public:
684 typedef const void* pointer;
685 typedef const void* const_pointer;
686 typedef const void value_type;
687
688 template <class _Up> struct rebind {typedef allocator<_Up> other;};
689};
690
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000691// pointer_traits
692
693template <class _Tp>
694struct __has_element_type
695{
696private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000697 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000698 template <class _Up> static __two __test(...);
699 template <class _Up> static char __test(typename _Up::element_type* = 0);
700public:
701 static const bool value = sizeof(__test<_Tp>(0)) == 1;
702};
703
704template <class _Ptr, bool = __has_element_type<_Ptr>::value>
705struct __pointer_traits_element_type;
706
707template <class _Ptr>
708struct __pointer_traits_element_type<_Ptr, true>
709{
710 typedef typename _Ptr::element_type type;
711};
712
713#ifndef _LIBCPP_HAS_NO_VARIADICS
714
715template <template <class, class...> class _Sp, class _Tp, class ..._Args>
716struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
717{
718 typedef typename _Sp<_Tp, _Args...>::element_type type;
719};
720
721template <template <class, class...> class _Sp, class _Tp, class ..._Args>
722struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
723{
724 typedef _Tp type;
725};
726
Howard Hinnant324bb032010-08-22 00:02:43 +0000727#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000728
729template <template <class> class _Sp, class _Tp>
730struct __pointer_traits_element_type<_Sp<_Tp>, true>
731{
732 typedef typename _Sp<_Tp>::element_type type;
733};
734
735template <template <class> class _Sp, class _Tp>
736struct __pointer_traits_element_type<_Sp<_Tp>, false>
737{
738 typedef _Tp type;
739};
740
741template <template <class, class> class _Sp, class _Tp, class _A0>
742struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
743{
744 typedef typename _Sp<_Tp, _A0>::element_type type;
745};
746
747template <template <class, class> class _Sp, class _Tp, class _A0>
748struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
749{
750 typedef _Tp type;
751};
752
753template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
754struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
755{
756 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
757};
758
759template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
760struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
761{
762 typedef _Tp type;
763};
764
765template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
766 class _A1, class _A2>
767struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
768{
769 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
770};
771
772template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
773 class _A1, class _A2>
774struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
775{
776 typedef _Tp type;
777};
778
Howard Hinnant324bb032010-08-22 00:02:43 +0000779#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000780
781template <class _Tp>
782struct __has_difference_type
783{
784private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000785 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000786 template <class _Up> static __two __test(...);
787 template <class _Up> static char __test(typename _Up::difference_type* = 0);
788public:
789 static const bool value = sizeof(__test<_Tp>(0)) == 1;
790};
791
792template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
793struct __pointer_traits_difference_type
794{
795 typedef ptrdiff_t type;
796};
797
798template <class _Ptr>
799struct __pointer_traits_difference_type<_Ptr, true>
800{
801 typedef typename _Ptr::difference_type type;
802};
803
804template <class _Tp, class _Up>
805struct __has_rebind
806{
807private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000808 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000809 template <class _Xp> static __two __test(...);
810 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
811public:
812 static const bool value = sizeof(__test<_Tp>(0)) == 1;
813};
814
815template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
816struct __pointer_traits_rebind
817{
818#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
819 typedef typename _Tp::template rebind<_Up> type;
820#else
821 typedef typename _Tp::template rebind<_Up>::other type;
822#endif
823};
824
825#ifndef _LIBCPP_HAS_NO_VARIADICS
826
827template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
828struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
829{
830#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
831 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
832#else
833 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
834#endif
835};
836
837template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
838struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
839{
840 typedef _Sp<_Up, _Args...> type;
841};
842
Howard Hinnant324bb032010-08-22 00:02:43 +0000843#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000844
845template <template <class> class _Sp, class _Tp, class _Up>
846struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
847{
848#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
849 typedef typename _Sp<_Tp>::template rebind<_Up> type;
850#else
851 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
852#endif
853};
854
855template <template <class> class _Sp, class _Tp, class _Up>
856struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
857{
858 typedef _Sp<_Up> type;
859};
860
861template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
862struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
863{
864#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
865 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
866#else
867 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
868#endif
869};
870
871template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
872struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
873{
874 typedef _Sp<_Up, _A0> type;
875};
876
877template <template <class, class, class> class _Sp, class _Tp, class _A0,
878 class _A1, class _Up>
879struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
880{
881#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
882 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
883#else
884 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
885#endif
886};
887
888template <template <class, class, class> class _Sp, class _Tp, class _A0,
889 class _A1, class _Up>
890struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
891{
892 typedef _Sp<_Up, _A0, _A1> type;
893};
894
895template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
896 class _A1, class _A2, class _Up>
897struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
898{
899#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
900 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
901#else
902 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
903#endif
904};
905
906template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
907 class _A1, class _A2, class _Up>
908struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
909{
910 typedef _Sp<_Up, _A0, _A1, _A2> type;
911};
912
Howard Hinnant324bb032010-08-22 00:02:43 +0000913#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000914
915template <class _Ptr>
Howard Hinnant83eade62013-03-06 23:30:19 +0000916struct _LIBCPP_TYPE_VIS pointer_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000917{
918 typedef _Ptr pointer;
919 typedef typename __pointer_traits_element_type<pointer>::type element_type;
920 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
921
922#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant6b41c602011-05-11 20:21:19 +0000923 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000924#else
925 template <class _Up> struct rebind
926 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant324bb032010-08-22 00:02:43 +0000927#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000928
929private:
930 struct __nat {};
931public:
Howard Hinnant82894812010-09-22 16:48:34 +0000932 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000933 static pointer pointer_to(typename conditional<is_void<element_type>::value,
934 __nat, element_type>::type& __r)
935 {return pointer::pointer_to(__r);}
936};
937
938template <class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +0000939struct _LIBCPP_TYPE_VIS pointer_traits<_Tp*>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000940{
941 typedef _Tp* pointer;
942 typedef _Tp element_type;
943 typedef ptrdiff_t difference_type;
944
945#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
946 template <class _Up> using rebind = _Up*;
947#else
948 template <class _Up> struct rebind {typedef _Up* other;};
949#endif
950
951private:
952 struct __nat {};
953public:
Howard Hinnant82894812010-09-22 16:48:34 +0000954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000955 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant1694d232011-05-28 14:41:13 +0000956 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000957 {return _VSTD::addressof(__r);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000958};
959
960// allocator_traits
961
962namespace __has_pointer_type_imp
963{
964 template <class _Up> static __two test(...);
965 template <class _Up> static char test(typename _Up::pointer* = 0);
966}
967
968template <class _Tp>
969struct __has_pointer_type
970 : public integral_constant<bool, sizeof(__has_pointer_type_imp::test<_Tp>(0)) == 1>
971{
972};
973
974namespace __pointer_type_imp
975{
976
977template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
978struct __pointer_type
979{
980 typedef typename _Dp::pointer type;
981};
982
983template <class _Tp, class _Dp>
984struct __pointer_type<_Tp, _Dp, false>
985{
986 typedef _Tp* type;
987};
988
Howard Hinnant47761072010-11-18 01:40:00 +0000989} // __pointer_type_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000990
991template <class _Tp, class _Dp>
992struct __pointer_type
993{
994 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
995};
996
997template <class _Tp>
998struct __has_const_pointer
999{
1000private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001001 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001002 template <class _Up> static __two __test(...);
1003 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
1004public:
1005 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1006};
1007
1008template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1009struct __const_pointer
1010{
1011 typedef typename _Alloc::const_pointer type;
1012};
1013
1014template <class _Tp, class _Ptr, class _Alloc>
1015struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1016{
1017#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1018 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1019#else
1020 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1021#endif
1022};
1023
1024template <class _Tp>
1025struct __has_void_pointer
1026{
1027private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001028 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001029 template <class _Up> static __two __test(...);
1030 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1031public:
1032 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1033};
1034
1035template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1036struct __void_pointer
1037{
1038 typedef typename _Alloc::void_pointer type;
1039};
1040
1041template <class _Ptr, class _Alloc>
1042struct __void_pointer<_Ptr, _Alloc, false>
1043{
1044#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1045 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1046#else
1047 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1048#endif
1049};
1050
1051template <class _Tp>
1052struct __has_const_void_pointer
1053{
1054private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001055 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001056 template <class _Up> static __two __test(...);
1057 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1058public:
1059 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1060};
1061
1062template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1063struct __const_void_pointer
1064{
1065 typedef typename _Alloc::const_void_pointer type;
1066};
1067
1068template <class _Ptr, class _Alloc>
1069struct __const_void_pointer<_Ptr, _Alloc, false>
1070{
1071#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1072 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1073#else
1074 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1075#endif
1076};
1077
Howard Hinnant99968442011-11-29 18:15:50 +00001078template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001079inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001080_Tp*
1081__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001082{
1083 return __p;
1084}
1085
1086template <class _Pointer>
1087inline _LIBCPP_INLINE_VISIBILITY
1088typename pointer_traits<_Pointer>::element_type*
Howard Hinnant1694d232011-05-28 14:41:13 +00001089__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001090{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001091 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001092}
1093
1094template <class _Tp>
1095struct __has_size_type
1096{
1097private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001098 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001099 template <class _Up> static __two __test(...);
1100 template <class _Up> static char __test(typename _Up::size_type* = 0);
1101public:
1102 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1103};
1104
Howard Hinnant47761072010-11-18 01:40:00 +00001105template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001106struct __size_type
1107{
Howard Hinnant47761072010-11-18 01:40:00 +00001108 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001109};
1110
Howard Hinnant47761072010-11-18 01:40:00 +00001111template <class _Alloc, class _DiffType>
1112struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001113{
1114 typedef typename _Alloc::size_type type;
1115};
1116
1117template <class _Tp>
1118struct __has_propagate_on_container_copy_assignment
1119{
1120private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001121 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001122 template <class _Up> static __two __test(...);
1123 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1124public:
1125 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1126};
1127
1128template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1129struct __propagate_on_container_copy_assignment
1130{
1131 typedef false_type type;
1132};
1133
1134template <class _Alloc>
1135struct __propagate_on_container_copy_assignment<_Alloc, true>
1136{
1137 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1138};
1139
1140template <class _Tp>
1141struct __has_propagate_on_container_move_assignment
1142{
1143private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001144 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001145 template <class _Up> static __two __test(...);
1146 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1147public:
1148 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1149};
1150
1151template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1152struct __propagate_on_container_move_assignment
1153{
1154 typedef false_type type;
1155};
1156
1157template <class _Alloc>
1158struct __propagate_on_container_move_assignment<_Alloc, true>
1159{
1160 typedef typename _Alloc::propagate_on_container_move_assignment type;
1161};
1162
1163template <class _Tp>
1164struct __has_propagate_on_container_swap
1165{
1166private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001167 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001168 template <class _Up> static __two __test(...);
1169 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1170public:
1171 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1172};
1173
1174template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1175struct __propagate_on_container_swap
1176{
1177 typedef false_type type;
1178};
1179
1180template <class _Alloc>
1181struct __propagate_on_container_swap<_Alloc, true>
1182{
1183 typedef typename _Alloc::propagate_on_container_swap type;
1184};
1185
1186template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1187struct __has_rebind_other
1188{
1189private:
Howard Hinnant9c0df142012-10-30 19:06:59 +00001190 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001191 template <class _Xp> static __two __test(...);
1192 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1193public:
1194 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1195};
1196
1197template <class _Tp, class _Up>
1198struct __has_rebind_other<_Tp, _Up, false>
1199{
1200 static const bool value = false;
1201};
1202
1203template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1204struct __allocator_traits_rebind
1205{
1206 typedef typename _Tp::template rebind<_Up>::other type;
1207};
1208
1209#ifndef _LIBCPP_HAS_NO_VARIADICS
1210
1211template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1212struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1213{
1214 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1215};
1216
1217template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1218struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1219{
1220 typedef _Alloc<_Up, _Args...> type;
1221};
1222
Howard Hinnant324bb032010-08-22 00:02:43 +00001223#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001224
1225template <template <class> class _Alloc, class _Tp, class _Up>
1226struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1227{
1228 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1229};
1230
1231template <template <class> class _Alloc, class _Tp, class _Up>
1232struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1233{
1234 typedef _Alloc<_Up> type;
1235};
1236
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001237template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1238struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1239{
1240 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1241};
1242
1243template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1244struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1245{
1246 typedef _Alloc<_Up, _A0> type;
1247};
1248
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001249template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1250 class _A1, class _Up>
1251struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1252{
1253 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1254};
1255
1256template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1257 class _A1, class _Up>
1258struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1259{
1260 typedef _Alloc<_Up, _A0, _A1> type;
1261};
1262
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001263template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1264 class _A1, class _A2, class _Up>
1265struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1266{
1267 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1268};
1269
1270template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1271 class _A1, class _A2, class _Up>
1272struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1273{
1274 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1275};
1276
Howard Hinnant324bb032010-08-22 00:02:43 +00001277#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001278
1279#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1280
1281template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1282auto
1283__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1284 -> decltype(__a.allocate(__sz, __p), true_type());
1285
1286template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1287auto
1288__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1289 -> false_type;
1290
1291template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1292struct __has_allocate_hint
1293 : integral_constant<bool,
1294 is_same<
1295 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1296 declval<_SizeType>(),
1297 declval<_ConstVoidPtr>())),
1298 true_type>::value>
1299{
1300};
1301
Howard Hinnant324bb032010-08-22 00:02:43 +00001302#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001303
1304template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1305struct __has_allocate_hint
1306 : true_type
1307{
1308};
1309
Howard Hinnant324bb032010-08-22 00:02:43 +00001310#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001311
Howard Hinnant23369ee2011-07-29 21:35:53 +00001312#if !defined(_LIBCPP_HAS_NO_ADVANCED_SFINAE) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001313
1314template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnant0949eed2011-06-30 21:18:19 +00001315decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1316 _VSTD::declval<_Args>()...),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001317 true_type())
1318__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1319
1320template <class _Alloc, class _Pointer, class ..._Args>
1321false_type
1322__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1323
1324template <class _Alloc, class _Pointer, class ..._Args>
1325struct __has_construct
1326 : integral_constant<bool,
1327 is_same<
1328 decltype(__has_construct_test(declval<_Alloc>(),
1329 declval<_Pointer>(),
1330 declval<_Args>()...)),
1331 true_type>::value>
1332{
1333};
1334
1335template <class _Alloc, class _Pointer>
1336auto
1337__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1338 -> decltype(__a.destroy(__p), true_type());
1339
1340template <class _Alloc, class _Pointer>
1341auto
1342__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1343 -> false_type;
1344
1345template <class _Alloc, class _Pointer>
1346struct __has_destroy
1347 : integral_constant<bool,
1348 is_same<
1349 decltype(__has_destroy_test(declval<_Alloc>(),
1350 declval<_Pointer>())),
1351 true_type>::value>
1352{
1353};
1354
1355template <class _Alloc>
1356auto
1357__has_max_size_test(_Alloc&& __a)
1358 -> decltype(__a.max_size(), true_type());
1359
1360template <class _Alloc>
1361auto
1362__has_max_size_test(const volatile _Alloc& __a)
1363 -> false_type;
1364
1365template <class _Alloc>
1366struct __has_max_size
1367 : integral_constant<bool,
1368 is_same<
1369 decltype(__has_max_size_test(declval<_Alloc&>())),
1370 true_type>::value>
1371{
1372};
1373
1374template <class _Alloc>
1375auto
1376__has_select_on_container_copy_construction_test(_Alloc&& __a)
1377 -> decltype(__a.select_on_container_copy_construction(), true_type());
1378
1379template <class _Alloc>
1380auto
1381__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1382 -> false_type;
1383
1384template <class _Alloc>
1385struct __has_select_on_container_copy_construction
1386 : integral_constant<bool,
1387 is_same<
1388 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1389 true_type>::value>
1390{
1391};
1392
Howard Hinnant324bb032010-08-22 00:02:43 +00001393#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001394
1395#ifndef _LIBCPP_HAS_NO_VARIADICS
1396
1397template <class _Alloc, class _Pointer, class ..._Args>
1398struct __has_construct
1399 : false_type
1400{
1401};
1402
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001403#else // _LIBCPP_HAS_NO_VARIADICS
1404
1405template <class _Alloc, class _Pointer, class _Args>
1406struct __has_construct
1407 : false_type
1408{
1409};
1410
Howard Hinnant324bb032010-08-22 00:02:43 +00001411#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001412
1413template <class _Alloc, class _Pointer>
1414struct __has_destroy
1415 : false_type
1416{
1417};
1418
1419template <class _Alloc>
1420struct __has_max_size
1421 : true_type
1422{
1423};
1424
1425template <class _Alloc>
1426struct __has_select_on_container_copy_construction
1427 : false_type
1428{
1429};
1430
Howard Hinnant324bb032010-08-22 00:02:43 +00001431#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001432
Howard Hinnant47761072010-11-18 01:40:00 +00001433template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1434struct __alloc_traits_difference_type
1435{
1436 typedef typename pointer_traits<_Ptr>::difference_type type;
1437};
1438
1439template <class _Alloc, class _Ptr>
1440struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1441{
1442 typedef typename _Alloc::difference_type type;
1443};
1444
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001445template <class _Alloc>
Howard Hinnant83eade62013-03-06 23:30:19 +00001446struct _LIBCPP_TYPE_VIS allocator_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001447{
1448 typedef _Alloc allocator_type;
1449 typedef typename allocator_type::value_type value_type;
1450
1451 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1452 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1453 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1454 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1455
Howard Hinnant47761072010-11-18 01:40:00 +00001456 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1457 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001458
1459 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1460 propagate_on_container_copy_assignment;
1461 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1462 propagate_on_container_move_assignment;
1463 typedef typename __propagate_on_container_swap<allocator_type>::type
1464 propagate_on_container_swap;
1465
1466#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1467 template <class _Tp> using rebind_alloc =
Howard Hinnant6b41c602011-05-11 20:21:19 +00001468 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001469 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant324bb032010-08-22 00:02:43 +00001470#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001471 template <class _Tp> struct rebind_alloc
1472 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1473 template <class _Tp> struct rebind_traits
1474 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant324bb032010-08-22 00:02:43 +00001475#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001476
Howard Hinnant82894812010-09-22 16:48:34 +00001477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001478 static pointer allocate(allocator_type& __a, size_type __n)
1479 {return __a.allocate(__n);}
Howard Hinnant82894812010-09-22 16:48:34 +00001480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001481 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1482 {return allocate(__a, __n, __hint,
1483 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1484
Howard Hinnant82894812010-09-22 16:48:34 +00001485 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001486 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001487 {__a.deallocate(__p, __n);}
1488
1489#ifndef _LIBCPP_HAS_NO_VARIADICS
1490 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001491 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001492 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
1493 {__construct(__has_construct<allocator_type, pointer, _Args...>(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001494 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001495#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001496 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001497 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001498 static void construct(allocator_type& __a, _Tp* __p)
1499 {
1500 ::new ((void*)__p) _Tp();
1501 }
1502 template <class _Tp, class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00001503 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001504 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1505 {
1506 ::new ((void*)__p) _Tp(__a0);
1507 }
1508 template <class _Tp, class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00001509 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001510 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1511 const _A1& __a1)
1512 {
1513 ::new ((void*)__p) _Tp(__a0, __a1);
1514 }
1515 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00001516 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001517 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1518 const _A1& __a1, const _A2& __a2)
1519 {
1520 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1521 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001522#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001523
1524 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001525 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001526 static void destroy(allocator_type& __a, _Tp* __p)
1527 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1528
Howard Hinnant82894812010-09-22 16:48:34 +00001529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530 static size_type max_size(const allocator_type& __a)
1531 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1532
Howard Hinnant82894812010-09-22 16:48:34 +00001533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001534 static allocator_type
1535 select_on_container_copy_construction(const allocator_type& __a)
1536 {return select_on_container_copy_construction(
1537 __has_select_on_container_copy_construction<const allocator_type>(),
1538 __a);}
1539
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001540 template <class _Ptr>
1541 _LIBCPP_INLINE_VISIBILITY
1542 static
1543 void
1544 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1545 {
1546 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1547 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1548 }
1549
1550 template <class _Tp>
1551 _LIBCPP_INLINE_VISIBILITY
1552 static
1553 typename enable_if
1554 <
1555 (is_same<allocator_type, allocator<_Tp> >::value
1556 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1557 is_trivially_move_constructible<_Tp>::value,
1558 void
1559 >::type
1560 __construct_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1561 {
1562 ptrdiff_t _Np = __end1 - __begin1;
1563 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
1564 __begin2 += _Np;
1565 }
1566
1567 template <class _Ptr>
1568 _LIBCPP_INLINE_VISIBILITY
1569 static
1570 void
1571 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1572 {
1573 while (__end1 != __begin1)
Howard Hinnantf619e232013-01-11 20:36:59 +00001574 {
1575 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1576 --__end2;
1577 }
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001578 }
1579
1580 template <class _Tp>
1581 _LIBCPP_INLINE_VISIBILITY
1582 static
1583 typename enable_if
1584 <
1585 (is_same<allocator_type, allocator<_Tp> >::value
1586 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1587 is_trivially_move_constructible<_Tp>::value,
1588 void
1589 >::type
1590 __construct_backward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
1591 {
1592 ptrdiff_t _Np = __end1 - __begin1;
1593 __end2 -= _Np;
1594 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
1595 }
1596
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001597private:
1598
Howard Hinnant82894812010-09-22 16:48:34 +00001599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001600 static pointer allocate(allocator_type& __a, size_type __n,
1601 const_void_pointer __hint, true_type)
1602 {return __a.allocate(__n, __hint);}
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,
Howard Hinnantec3773c2011-12-01 20:21:04 +00001605 const_void_pointer, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001606 {return __a.allocate(__n);}
1607
1608#ifndef _LIBCPP_HAS_NO_VARIADICS
1609 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001610 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001612 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001613 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(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1616 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001617 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001618 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001619#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001620
1621 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001622 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001623 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1624 {__a.destroy(__p);}
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(false_type, allocator_type&, _Tp* __p)
1628 {
1629 __p->~_Tp();
1630 }
1631
Howard Hinnant82894812010-09-22 16:48:34 +00001632 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001633 static size_type __max_size(true_type, const allocator_type& __a)
1634 {return __a.max_size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001635 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001636 static size_type __max_size(false_type, const allocator_type&)
1637 {return numeric_limits<size_type>::max();}
1638
Howard Hinnant82894812010-09-22 16:48:34 +00001639 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001640 static allocator_type
1641 select_on_container_copy_construction(true_type, const allocator_type& __a)
1642 {return __a.select_on_container_copy_construction();}
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(false_type, const allocator_type& __a)
1646 {return __a;}
1647};
1648
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001649// allocator
1650
1651template <class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00001652class _LIBCPP_TYPE_VIS allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001653{
1654public:
1655 typedef size_t size_type;
1656 typedef ptrdiff_t difference_type;
1657 typedef _Tp* pointer;
1658 typedef const _Tp* const_pointer;
1659 typedef _Tp& reference;
1660 typedef const _Tp& const_reference;
1661 typedef _Tp value_type;
1662
Howard Hinnant18884f42011-06-02 21:38:57 +00001663 typedef true_type propagate_on_container_move_assignment;
1664
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001665 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1666
Howard Hinnant1694d232011-05-28 14:41:13 +00001667 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1668 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1669 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001670 {return _VSTD::addressof(__x);}
Howard Hinnant1694d232011-05-28 14:41:13 +00001671 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001672 {return _VSTD::addressof(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001673 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1674 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
Howard Hinnant1694d232011-05-28 14:41:13 +00001675 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
1676 {::operator delete((void*)__p);}
1677 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1678 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001679#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001680 template <class _Up, class... _Args>
1681 _LIBCPP_INLINE_VISIBILITY
1682 void
1683 construct(_Up* __p, _Args&&... __args)
1684 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001685 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001686 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001687#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001688 _LIBCPP_INLINE_VISIBILITY
1689 void
1690 construct(pointer __p)
1691 {
1692 ::new((void*)__p) _Tp();
1693 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001694# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001695
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001696 template <class _A0>
1697 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001698 void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001699 construct(pointer __p, _A0& __a0)
1700 {
1701 ::new((void*)__p) _Tp(__a0);
1702 }
1703 template <class _A0>
1704 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001705 void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001706 construct(pointer __p, const _A0& __a0)
1707 {
1708 ::new((void*)__p) _Tp(__a0);
1709 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001710# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001711 template <class _A0, class _A1>
1712 _LIBCPP_INLINE_VISIBILITY
1713 void
1714 construct(pointer __p, _A0& __a0, _A1& __a1)
1715 {
1716 ::new((void*)__p) _Tp(__a0, __a1);
1717 }
1718 template <class _A0, class _A1>
1719 _LIBCPP_INLINE_VISIBILITY
1720 void
1721 construct(pointer __p, const _A0& __a0, _A1& __a1)
1722 {
1723 ::new((void*)__p) _Tp(__a0, __a1);
1724 }
1725 template <class _A0, class _A1>
1726 _LIBCPP_INLINE_VISIBILITY
1727 void
1728 construct(pointer __p, _A0& __a0, const _A1& __a1)
1729 {
1730 ::new((void*)__p) _Tp(__a0, __a1);
1731 }
1732 template <class _A0, class _A1>
1733 _LIBCPP_INLINE_VISIBILITY
1734 void
1735 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1736 {
1737 ::new((void*)__p) _Tp(__a0, __a1);
1738 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001739#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001740 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1741};
1742
Howard Hinnant57199402012-01-02 17:56:02 +00001743template <class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00001744class _LIBCPP_TYPE_VIS allocator<const _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00001745{
1746public:
1747 typedef size_t size_type;
1748 typedef ptrdiff_t difference_type;
1749 typedef const _Tp* pointer;
1750 typedef const _Tp* const_pointer;
1751 typedef const _Tp& reference;
1752 typedef const _Tp& const_reference;
1753 typedef _Tp value_type;
1754
1755 typedef true_type propagate_on_container_move_assignment;
1756
1757 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1758
1759 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1760 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1761 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1762 {return _VSTD::addressof(__x);}
1763 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1764 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
1765 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
1766 {::operator delete((void*)__p);}
1767 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1768 {return size_type(~0) / sizeof(_Tp);}
1769#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1770 template <class _Up, class... _Args>
1771 _LIBCPP_INLINE_VISIBILITY
1772 void
1773 construct(_Up* __p, _Args&&... __args)
1774 {
1775 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1776 }
1777#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1778 _LIBCPP_INLINE_VISIBILITY
1779 void
1780 construct(pointer __p)
1781 {
1782 ::new((void*)__p) _Tp();
1783 }
1784# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001785
Howard Hinnant57199402012-01-02 17:56:02 +00001786 template <class _A0>
1787 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001788 void
Howard Hinnant57199402012-01-02 17:56:02 +00001789 construct(pointer __p, _A0& __a0)
1790 {
1791 ::new((void*)__p) _Tp(__a0);
1792 }
1793 template <class _A0>
1794 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001795 void
Howard Hinnant57199402012-01-02 17:56:02 +00001796 construct(pointer __p, const _A0& __a0)
1797 {
1798 ::new((void*)__p) _Tp(__a0);
1799 }
Howard Hinnant57199402012-01-02 17:56:02 +00001800# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1801 template <class _A0, class _A1>
1802 _LIBCPP_INLINE_VISIBILITY
1803 void
1804 construct(pointer __p, _A0& __a0, _A1& __a1)
1805 {
1806 ::new((void*)__p) _Tp(__a0, __a1);
1807 }
1808 template <class _A0, class _A1>
1809 _LIBCPP_INLINE_VISIBILITY
1810 void
1811 construct(pointer __p, const _A0& __a0, _A1& __a1)
1812 {
1813 ::new((void*)__p) _Tp(__a0, __a1);
1814 }
1815 template <class _A0, class _A1>
1816 _LIBCPP_INLINE_VISIBILITY
1817 void
1818 construct(pointer __p, _A0& __a0, const _A1& __a1)
1819 {
1820 ::new((void*)__p) _Tp(__a0, __a1);
1821 }
1822 template <class _A0, class _A1>
1823 _LIBCPP_INLINE_VISIBILITY
1824 void
1825 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1826 {
1827 ::new((void*)__p) _Tp(__a0, __a1);
1828 }
1829#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1830 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1831};
1832
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001833template <class _Tp, class _Up>
1834inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001835bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001836
1837template <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 false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001840
1841template <class _OutputIterator, class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00001842class _LIBCPP_TYPE_VIS raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001843 : public iterator<output_iterator_tag,
1844 _Tp, // purposefully not C++03
1845 ptrdiff_t, // purposefully not C++03
1846 _Tp*, // purposefully not C++03
1847 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1848{
1849private:
1850 _OutputIterator __x_;
1851public:
1852 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1853 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1854 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1855 {::new(&*__x_) _Tp(__element); return *this;}
1856 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1857 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1858 {raw_storage_iterator __t(*this); ++__x_; return __t;}
1859};
1860
1861template <class _Tp>
1862pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00001863get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001864{
1865 pair<_Tp*, ptrdiff_t> __r(0, 0);
1866 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1867 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1868 / sizeof(_Tp);
1869 if (__n > __m)
1870 __n = __m;
1871 while (__n > 0)
1872 {
1873 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1874 if (__r.first)
1875 {
1876 __r.second = __n;
1877 break;
1878 }
1879 __n /= 2;
1880 }
1881 return __r;
1882}
1883
1884template <class _Tp>
1885inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001886void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001887
1888template <class _Tp>
1889struct auto_ptr_ref
1890{
1891 _Tp* __ptr_;
1892};
1893
1894template<class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00001895class _LIBCPP_TYPE_VIS auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001896{
1897private:
1898 _Tp* __ptr_;
1899public:
1900 typedef _Tp element_type;
1901
1902 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1903 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1904 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1905 : __ptr_(__p.release()) {}
1906 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1907 {reset(__p.release()); return *this;}
1908 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1909 {reset(__p.release()); return *this;}
1910 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1911 {reset(__p.__ptr_); return *this;}
1912 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1913
1914 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1915 {return *__ptr_;}
1916 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1917 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1918 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1919 {
1920 _Tp* __t = __ptr_;
1921 __ptr_ = 0;
1922 return __t;
1923 }
1924 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1925 {
1926 if (__ptr_ != __p)
1927 delete __ptr_;
1928 __ptr_ = __p;
1929 }
1930
1931 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1932 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1933 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1934 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1935 {return auto_ptr<_Up>(release());}
1936};
1937
1938template <>
Howard Hinnant83eade62013-03-06 23:30:19 +00001939class _LIBCPP_TYPE_VIS auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001940{
1941public:
1942 typedef void element_type;
1943};
1944
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001945template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1946 typename remove_cv<_T2>::type>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00001947 bool = is_empty<_T1>::value
1948#if __has_feature(is_final)
1949 && !__is_final(_T1)
1950#endif
1951 ,
1952 bool = is_empty<_T2>::value
1953#if __has_feature(is_final)
1954 && !__is_final(_T2)
1955#endif
1956 >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001957struct __libcpp_compressed_pair_switch;
1958
1959template <class _T1, class _T2, bool IsSame>
1960struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1961
1962template <class _T1, class _T2, bool IsSame>
1963struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1964
1965template <class _T1, class _T2, bool IsSame>
1966struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1967
1968template <class _T1, class _T2>
1969struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
1970
1971template <class _T1, class _T2>
1972struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
1973
1974template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
1975class __libcpp_compressed_pair_imp;
1976
1977template <class _T1, class _T2>
1978class __libcpp_compressed_pair_imp<_T1, _T2, 0>
1979{
1980private:
1981 _T1 __first_;
1982 _T2 __second_;
1983public:
1984 typedef _T1 _T1_param;
1985 typedef _T2 _T2_param;
1986
1987 typedef typename remove_reference<_T1>::type& _T1_reference;
1988 typedef typename remove_reference<_T2>::type& _T2_reference;
1989
1990 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1991 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1992
1993 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant57199402012-01-02 17:56:02 +00001994 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001995 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00001996 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001997 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001998 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001999 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002000
Howard Hinnant61aa6012011-07-01 19:24:36 +00002001#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2002
2003 _LIBCPP_INLINE_VISIBILITY
2004 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2005 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2006 is_nothrow_copy_constructible<_T2>::value)
2007 : __first_(__p.first()),
2008 __second_(__p.second()) {}
2009
2010 _LIBCPP_INLINE_VISIBILITY
2011 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2012 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2013 is_nothrow_copy_assignable<_T2>::value)
2014 {
2015 __first_ = __p.first();
2016 __second_ = __p.second();
2017 return *this;
2018 }
2019
Howard Hinnant73d21a42010-09-04 23:28:19 +00002020#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002021
2022 _LIBCPP_INLINE_VISIBILITY
2023 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002024 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2025 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002026 : __first_(_VSTD::forward<_T1>(__p.first())),
2027 __second_(_VSTD::forward<_T2>(__p.second())) {}
2028
2029 _LIBCPP_INLINE_VISIBILITY
2030 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2031 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2032 is_nothrow_move_assignable<_T2>::value)
2033 {
2034 __first_ = _VSTD::forward<_T1>(__p.first());
2035 __second_ = _VSTD::forward<_T2>(__p.second());
2036 return *this;
2037 }
2038
Howard Hinnant8c238192013-05-06 16:58:36 +00002039#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2040
2041#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2042
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002043#ifndef _LIBCPP_HAS_NO_VARIADICS
2044
2045 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2046 _LIBCPP_INLINE_VISIBILITY
2047 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2048 tuple<_Args1...> __first_args,
2049 tuple<_Args2...> __second_args,
2050 __tuple_indices<_I1...>,
2051 __tuple_indices<_I2...>)
2052 : __first_(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
2053 __second_(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
2054 {}
2055
2056#endif // _LIBCPP_HAS_NO_VARIADICS
2057
Howard Hinnant1694d232011-05-28 14:41:13 +00002058 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2059 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002060
Howard Hinnant1694d232011-05-28 14:41:13 +00002061 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2062 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002063
2064 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002065 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2066 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002067 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002068 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002069 swap(__first_, __x.__first_);
2070 swap(__second_, __x.__second_);
2071 }
2072};
2073
2074template <class _T1, class _T2>
2075class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2076 : private _T1
2077{
2078private:
2079 _T2 __second_;
2080public:
2081 typedef _T1 _T1_param;
2082 typedef _T2 _T2_param;
2083
2084 typedef _T1& _T1_reference;
2085 typedef typename remove_reference<_T2>::type& _T2_reference;
2086
2087 typedef const _T1& _T1_const_reference;
2088 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2089
2090 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002091 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002092 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002093 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002094 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002095 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002096 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002097
Howard Hinnant61aa6012011-07-01 19:24:36 +00002098#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2099
2100 _LIBCPP_INLINE_VISIBILITY
2101 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2102 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2103 is_nothrow_copy_constructible<_T2>::value)
2104 : _T1(__p.first()), __second_(__p.second()) {}
2105
2106 _LIBCPP_INLINE_VISIBILITY
2107 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2108 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2109 is_nothrow_copy_assignable<_T2>::value)
2110 {
2111 _T1::operator=(__p.first());
2112 __second_ = __p.second();
2113 return *this;
2114 }
2115
Howard Hinnant73d21a42010-09-04 23:28:19 +00002116#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002117
2118 _LIBCPP_INLINE_VISIBILITY
2119 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002120 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2121 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002122 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002123
2124 _LIBCPP_INLINE_VISIBILITY
2125 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2126 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2127 is_nothrow_move_assignable<_T2>::value)
2128 {
2129 _T1::operator=(_VSTD::move(__p.first()));
2130 __second_ = _VSTD::forward<_T2>(__p.second());
2131 return *this;
2132 }
2133
Howard Hinnant8c238192013-05-06 16:58:36 +00002134#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2135
2136#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2137
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002138#ifndef _LIBCPP_HAS_NO_VARIADICS
2139
2140 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2141 _LIBCPP_INLINE_VISIBILITY
2142 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2143 tuple<_Args1...> __first_args,
2144 tuple<_Args2...> __second_args,
2145 __tuple_indices<_I1...>,
2146 __tuple_indices<_I2...>)
2147 : _T1(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
2148 __second_(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
2149 {}
2150
2151#endif // _LIBCPP_HAS_NO_VARIADICS
2152
Howard Hinnant1694d232011-05-28 14:41:13 +00002153 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2154 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002155
Howard Hinnant1694d232011-05-28 14:41:13 +00002156 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2157 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002158
2159 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002160 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2161 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002162 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002163 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002164 swap(__second_, __x.__second_);
2165 }
2166};
2167
2168template <class _T1, class _T2>
2169class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2170 : private _T2
2171{
2172private:
2173 _T1 __first_;
2174public:
2175 typedef _T1 _T1_param;
2176 typedef _T2 _T2_param;
2177
2178 typedef typename remove_reference<_T1>::type& _T1_reference;
2179 typedef _T2& _T2_reference;
2180
2181 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2182 typedef const _T2& _T2_const_reference;
2183
2184 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2185 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002186 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002187 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002188 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002189 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00002190 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2191 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002192 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002193
Howard Hinnant61aa6012011-07-01 19:24:36 +00002194#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2195
2196 _LIBCPP_INLINE_VISIBILITY
2197 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2198 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2199 is_nothrow_copy_constructible<_T2>::value)
2200 : _T2(__p.second()), __first_(__p.first()) {}
2201
2202 _LIBCPP_INLINE_VISIBILITY
2203 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2204 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2205 is_nothrow_copy_assignable<_T2>::value)
2206 {
2207 _T2::operator=(__p.second());
2208 __first_ = __p.first();
2209 return *this;
2210 }
2211
Howard Hinnant73d21a42010-09-04 23:28:19 +00002212#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002213
2214 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002215 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002216 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2217 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002218 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002219
2220 _LIBCPP_INLINE_VISIBILITY
2221 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2222 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2223 is_nothrow_move_assignable<_T2>::value)
2224 {
2225 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2226 __first_ = _VSTD::move(__p.first());
2227 return *this;
2228 }
2229
Howard Hinnant8c238192013-05-06 16:58:36 +00002230#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2231
2232#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2233
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002234#ifndef _LIBCPP_HAS_NO_VARIADICS
2235
2236 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2237 _LIBCPP_INLINE_VISIBILITY
2238 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2239 tuple<_Args1...> __first_args,
2240 tuple<_Args2...> __second_args,
2241 __tuple_indices<_I1...>,
2242 __tuple_indices<_I2...>)
2243 : _T2(_VSTD::forward<_Args2>(get<_I2>(__second_args))...),
2244 __first_(_VSTD::forward<_Args1>(get<_I1>(__first_args))...)
2245
2246 {}
2247
2248#endif // _LIBCPP_HAS_NO_VARIADICS
2249
Howard Hinnant1694d232011-05-28 14:41:13 +00002250 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2251 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002252
Howard Hinnant1694d232011-05-28 14:41:13 +00002253 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2254 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002255
2256 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002257 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2258 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002259 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002260 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002261 swap(__first_, __x.__first_);
2262 }
2263};
2264
2265template <class _T1, class _T2>
2266class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2267 : private _T1,
2268 private _T2
2269{
2270public:
2271 typedef _T1 _T1_param;
2272 typedef _T2 _T2_param;
2273
2274 typedef _T1& _T1_reference;
2275 typedef _T2& _T2_reference;
2276
2277 typedef const _T1& _T1_const_reference;
2278 typedef const _T2& _T2_const_reference;
2279
2280 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2281 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002282 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002283 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002284 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002285 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002286 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002287
Howard Hinnant61aa6012011-07-01 19:24:36 +00002288#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2289
2290 _LIBCPP_INLINE_VISIBILITY
2291 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2292 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2293 is_nothrow_copy_constructible<_T2>::value)
2294 : _T1(__p.first()), _T2(__p.second()) {}
2295
2296 _LIBCPP_INLINE_VISIBILITY
2297 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2298 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2299 is_nothrow_copy_assignable<_T2>::value)
2300 {
2301 _T1::operator=(__p.first());
2302 _T2::operator=(__p.second());
2303 return *this;
2304 }
2305
Howard Hinnant73d21a42010-09-04 23:28:19 +00002306#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002307
2308 _LIBCPP_INLINE_VISIBILITY
2309 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002310 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2311 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002312 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002313
2314 _LIBCPP_INLINE_VISIBILITY
2315 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2316 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2317 is_nothrow_move_assignable<_T2>::value)
2318 {
2319 _T1::operator=(_VSTD::move(__p.first()));
2320 _T2::operator=(_VSTD::move(__p.second()));
2321 return *this;
2322 }
2323
Howard Hinnant8c238192013-05-06 16:58:36 +00002324#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2325
2326#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2327
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002328#ifndef _LIBCPP_HAS_NO_VARIADICS
2329
2330 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2331 _LIBCPP_INLINE_VISIBILITY
2332 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2333 tuple<_Args1...> __first_args,
2334 tuple<_Args2...> __second_args,
2335 __tuple_indices<_I1...>,
2336 __tuple_indices<_I2...>)
2337 : _T1(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
2338 _T2(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
2339 {}
2340
2341#endif // _LIBCPP_HAS_NO_VARIADICS
2342
Howard Hinnant1694d232011-05-28 14:41:13 +00002343 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2344 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002345
Howard Hinnant1694d232011-05-28 14:41:13 +00002346 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2347 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002348
Howard Hinnantec3773c2011-12-01 20:21:04 +00002349 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant1694d232011-05-28 14:41:13 +00002350 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2351 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002352 {
2353 }
2354};
2355
2356template <class _T1, class _T2>
2357class __compressed_pair
2358 : private __libcpp_compressed_pair_imp<_T1, _T2>
2359{
2360 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2361public:
2362 typedef typename base::_T1_param _T1_param;
2363 typedef typename base::_T2_param _T2_param;
2364
2365 typedef typename base::_T1_reference _T1_reference;
2366 typedef typename base::_T2_reference _T2_reference;
2367
2368 typedef typename base::_T1_const_reference _T1_const_reference;
2369 typedef typename base::_T2_const_reference _T2_const_reference;
2370
2371 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002372 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002373 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002374 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002375 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002376 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002377 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002378
Howard Hinnant61aa6012011-07-01 19:24:36 +00002379#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2380
2381 _LIBCPP_INLINE_VISIBILITY
2382 __compressed_pair(const __compressed_pair& __p)
2383 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2384 is_nothrow_copy_constructible<_T2>::value)
2385 : base(__p) {}
2386
2387 _LIBCPP_INLINE_VISIBILITY
2388 __compressed_pair& operator=(const __compressed_pair& __p)
2389 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2390 is_nothrow_copy_assignable<_T2>::value)
2391 {
2392 base::operator=(__p);
2393 return *this;
2394 }
2395
Howard Hinnant73d21a42010-09-04 23:28:19 +00002396#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002397 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002398 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002399 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2400 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002401 : base(_VSTD::move(__p)) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002402
2403 _LIBCPP_INLINE_VISIBILITY
2404 __compressed_pair& operator=(__compressed_pair&& __p)
2405 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2406 is_nothrow_move_assignable<_T2>::value)
2407 {
2408 base::operator=(_VSTD::move(__p));
2409 return *this;
2410 }
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002411
Howard Hinnant8c238192013-05-06 16:58:36 +00002412#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2413
2414#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2415
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002416#ifndef _LIBCPP_HAS_NO_VARIADICS
2417
2418 template <class... _Args1, class... _Args2>
2419 _LIBCPP_INLINE_VISIBILITY
2420 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2421 tuple<_Args2...> __second_args)
Howard Hinnant57199402012-01-02 17:56:02 +00002422 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002423 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2424 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2425 {}
2426
2427#endif // _LIBCPP_HAS_NO_VARIADICS
2428
Howard Hinnant1694d232011-05-28 14:41:13 +00002429 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2430 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002431
Howard Hinnant1694d232011-05-28 14:41:13 +00002432 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2433 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002434
Howard Hinnant1694d232011-05-28 14:41:13 +00002435 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2436 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2437 __is_nothrow_swappable<_T1>::value)
2438 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002439};
2440
2441template <class _T1, class _T2>
2442inline _LIBCPP_INLINE_VISIBILITY
2443void
2444swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002445 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2446 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002447 {__x.swap(__y);}
2448
Howard Hinnant57199402012-01-02 17:56:02 +00002449// __same_or_less_cv_qualified
2450
2451template <class _Ptr1, class _Ptr2,
2452 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2453 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2454 >::value
2455 >
2456struct __same_or_less_cv_qualified_imp
2457 : is_convertible<_Ptr1, _Ptr2> {};
2458
2459template <class _Ptr1, class _Ptr2>
2460struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2461 : false_type {};
2462
2463template <class _Ptr1, class _Ptr2, bool = is_scalar<_Ptr1>::value &&
2464 !is_pointer<_Ptr1>::value>
2465struct __same_or_less_cv_qualified
2466 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2467
2468template <class _Ptr1, class _Ptr2>
2469struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, true>
2470 : false_type {};
2471
2472// default_delete
2473
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002474template <class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00002475struct _LIBCPP_TYPE_VIS default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002476{
Howard Hinnant46e94932012-07-07 20:56:04 +00002477#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2478 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2479#else
2480 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2481#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002482 template <class _Up>
2483 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002484 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2485 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002486 {
2487 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant05e7d242013-04-24 19:44:26 +00002488 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002489 delete __ptr;
2490 }
2491};
2492
2493template <class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00002494struct _LIBCPP_TYPE_VIS default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002495{
Howard Hinnant8e843502011-12-18 21:19:44 +00002496public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002497#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2498 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2499#else
2500 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2501#endif
Howard Hinnant8e843502011-12-18 21:19:44 +00002502 template <class _Up>
2503 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant57199402012-01-02 17:56:02 +00002504 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant8e843502011-12-18 21:19:44 +00002505 template <class _Up>
2506 _LIBCPP_INLINE_VISIBILITY
2507 void operator() (_Up* __ptr,
Howard Hinnant57199402012-01-02 17:56:02 +00002508 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002509 {
2510 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant05e7d242013-04-24 19:44:26 +00002511 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002512 delete [] __ptr;
2513 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002514};
2515
2516template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant83eade62013-03-06 23:30:19 +00002517class _LIBCPP_TYPE_VIS unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002518{
2519public:
2520 typedef _Tp element_type;
2521 typedef _Dp deleter_type;
2522 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2523private:
2524 __compressed_pair<pointer, deleter_type> __ptr_;
2525
Howard Hinnant57199402012-01-02 17:56:02 +00002526#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002527 unique_ptr(unique_ptr&);
2528 template <class _Up, class _Ep>
2529 unique_ptr(unique_ptr<_Up, _Ep>&);
2530 unique_ptr& operator=(unique_ptr&);
2531 template <class _Up, class _Ep>
2532 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002533#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002534
2535 struct __nat {int __for_bool_;};
2536
2537 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2538 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2539public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002540 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002541 : __ptr_(pointer())
2542 {
2543 static_assert(!is_pointer<deleter_type>::value,
2544 "unique_ptr constructed with null function pointer deleter");
2545 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002546 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002547 : __ptr_(pointer())
2548 {
2549 static_assert(!is_pointer<deleter_type>::value,
2550 "unique_ptr constructed with null function pointer deleter");
2551 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002552 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002553 : __ptr_(_VSTD::move(__p))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002554 {
2555 static_assert(!is_pointer<deleter_type>::value,
2556 "unique_ptr constructed with null function pointer deleter");
2557 }
2558
Howard Hinnant73d21a42010-09-04 23:28:19 +00002559#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002560 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2561 is_reference<deleter_type>::value,
2562 deleter_type,
2563 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002564 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002565 : __ptr_(__p, __d) {}
2566
2567 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002568 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002569 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002570 {
2571 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2572 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002573 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002574 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002575 template <class _Up, class _Ep>
2576 _LIBCPP_INLINE_VISIBILITY
2577 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2578 typename enable_if
2579 <
2580 !is_array<_Up>::value &&
2581 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2582 is_convertible<_Ep, deleter_type>::value &&
2583 (
2584 !is_reference<deleter_type>::value ||
2585 is_same<deleter_type, _Ep>::value
2586 ),
2587 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002588 >::type = __nat()) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002589 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002590
2591 template <class _Up>
2592 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2593 typename enable_if<
2594 is_convertible<_Up*, _Tp*>::value &&
2595 is_same<_Dp, default_delete<_Tp> >::value,
2596 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002597 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002598 : __ptr_(__p.release())
2599 {
2600 }
2601
Howard Hinnant1694d232011-05-28 14:41:13 +00002602 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002603 {
2604 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002605 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002606 return *this;
2607 }
2608
2609 template <class _Up, class _Ep>
2610 _LIBCPP_INLINE_VISIBILITY
2611 typename enable_if
2612 <
Howard Hinnant57199402012-01-02 17:56:02 +00002613 !is_array<_Up>::value &&
2614 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2615 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002616 unique_ptr&
2617 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002618 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002619 {
2620 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002621 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002622 return *this;
2623 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002624#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002625
2626 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2627 {
2628 return __rv<unique_ptr>(*this);
2629 }
2630
2631 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002632 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002633
2634 template <class _Up, class _Ep>
2635 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2636 {
2637 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002638 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002639 return *this;
2640 }
2641
2642 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002643 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002644
2645 template <class _Up>
2646 _LIBCPP_INLINE_VISIBILITY
2647 typename enable_if<
2648 is_convertible<_Up*, _Tp*>::value &&
2649 is_same<_Dp, default_delete<_Tp> >::value,
2650 unique_ptr&
2651 >::type
2652 operator=(auto_ptr<_Up> __p)
2653 {reset(__p.release()); return *this;}
2654
Howard Hinnant73d21a42010-09-04 23:28:19 +00002655#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002656 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2657
Howard Hinnant1694d232011-05-28 14:41:13 +00002658 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002659 {
2660 reset();
2661 return *this;
2662 }
2663
2664 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2665 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002666 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2667 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2668 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2669 {return __ptr_.second();}
2670 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2671 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002672 _LIBCPP_INLINE_VISIBILITY
2673 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2674 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002675
Howard Hinnant1694d232011-05-28 14:41:13 +00002676 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002677 {
2678 pointer __t = __ptr_.first();
2679 __ptr_.first() = pointer();
2680 return __t;
2681 }
2682
Howard Hinnant1694d232011-05-28 14:41:13 +00002683 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002684 {
2685 pointer __tmp = __ptr_.first();
2686 __ptr_.first() = __p;
2687 if (__tmp)
2688 __ptr_.second()(__tmp);
2689 }
2690
Howard Hinnant1694d232011-05-28 14:41:13 +00002691 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2692 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002693};
2694
2695template <class _Tp, class _Dp>
Howard Hinnant83eade62013-03-06 23:30:19 +00002696class _LIBCPP_TYPE_VIS unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002697{
2698public:
2699 typedef _Tp element_type;
2700 typedef _Dp deleter_type;
2701 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2702private:
2703 __compressed_pair<pointer, deleter_type> __ptr_;
2704
Howard Hinnant57199402012-01-02 17:56:02 +00002705#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002706 unique_ptr(unique_ptr&);
2707 template <class _Up>
2708 unique_ptr(unique_ptr<_Up>&);
2709 unique_ptr& operator=(unique_ptr&);
2710 template <class _Up>
2711 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002712#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002713
2714 struct __nat {int __for_bool_;};
2715
2716 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2717 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2718public:
Howard Hinnant46e94932012-07-07 20:56:04 +00002719 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002720 : __ptr_(pointer())
2721 {
2722 static_assert(!is_pointer<deleter_type>::value,
2723 "unique_ptr constructed with null function pointer deleter");
2724 }
Howard Hinnant46e94932012-07-07 20:56:04 +00002725 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002726 : __ptr_(pointer())
2727 {
2728 static_assert(!is_pointer<deleter_type>::value,
2729 "unique_ptr constructed with null function pointer deleter");
2730 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002731#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002732 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002733 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002734 >
Howard Hinnant99968442011-11-29 18:15:50 +00002735 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002736 : __ptr_(__p)
2737 {
2738 static_assert(!is_pointer<deleter_type>::value,
2739 "unique_ptr constructed with null function pointer deleter");
2740 }
2741
Howard Hinnant99968442011-11-29 18:15:50 +00002742 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002743 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002744 >
Howard Hinnant99968442011-11-29 18:15:50 +00002745 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002746 is_reference<deleter_type>::value,
2747 deleter_type,
2748 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002749 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002750 : __ptr_(__p, __d) {}
2751
2752 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2753 is_reference<deleter_type>::value,
2754 deleter_type,
2755 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002756 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002757 : __ptr_(pointer(), __d) {}
2758
Howard Hinnant99968442011-11-29 18:15:50 +00002759 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002760 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002761 >
Howard Hinnant99968442011-11-29 18:15:50 +00002762 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002763 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002764 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002765 {
2766 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2767 }
2768
2769 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002770 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002771 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002772 {
2773 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2774 }
2775
Howard Hinnant1694d232011-05-28 14:41:13 +00002776 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002777 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002778
Howard Hinnant1694d232011-05-28 14:41:13 +00002779 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002780 {
2781 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002782 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002783 return *this;
2784 }
Howard Hinnant8e843502011-12-18 21:19:44 +00002785
2786 template <class _Up, class _Ep>
2787 _LIBCPP_INLINE_VISIBILITY
2788 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2789 typename enable_if
2790 <
2791 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002792 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant8e843502011-12-18 21:19:44 +00002793 && is_convertible<_Ep, deleter_type>::value &&
2794 (
2795 !is_reference<deleter_type>::value ||
2796 is_same<deleter_type, _Ep>::value
2797 ),
2798 __nat
2799 >::type = __nat()
2800 ) _NOEXCEPT
2801 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2802
2803
2804 template <class _Up, class _Ep>
2805 _LIBCPP_INLINE_VISIBILITY
2806 typename enable_if
2807 <
2808 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002809 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2810 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant8e843502011-12-18 21:19:44 +00002811 unique_ptr&
2812 >::type
2813 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2814 {
2815 reset(__u.release());
2816 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2817 return *this;
2818 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002819#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002820
2821 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2822 : __ptr_(__p)
2823 {
2824 static_assert(!is_pointer<deleter_type>::value,
2825 "unique_ptr constructed with null function pointer deleter");
2826 }
2827
2828 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002829 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002830
2831 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002832 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002833
2834 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2835 {
2836 return __rv<unique_ptr>(*this);
2837 }
2838
2839 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002840 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002841
2842 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2843 {
2844 reset(__u->release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002845 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002846 return *this;
2847 }
2848
Howard Hinnant73d21a42010-09-04 23:28:19 +00002849#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002850 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2851
Howard Hinnant1694d232011-05-28 14:41:13 +00002852 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002853 {
2854 reset();
2855 return *this;
2856 }
2857
2858 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2859 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002860 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2861 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2862 {return __ptr_.second();}
2863 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2864 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002865 _LIBCPP_INLINE_VISIBILITY
2866 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2867 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002868
Howard Hinnant1694d232011-05-28 14:41:13 +00002869 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002870 {
2871 pointer __t = __ptr_.first();
2872 __ptr_.first() = pointer();
2873 return __t;
2874 }
2875
Howard Hinnant73d21a42010-09-04 23:28:19 +00002876#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002877 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002878 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002879 >
Howard Hinnant99968442011-11-29 18:15:50 +00002880 _LIBCPP_INLINE_VISIBILITY void reset(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002881 {
2882 pointer __tmp = __ptr_.first();
2883 __ptr_.first() = __p;
2884 if (__tmp)
2885 __ptr_.second()(__tmp);
2886 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002887 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002888 {
2889 pointer __tmp = __ptr_.first();
2890 __ptr_.first() = nullptr;
2891 if (__tmp)
2892 __ptr_.second()(__tmp);
2893 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002894 _LIBCPP_INLINE_VISIBILITY void reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002895 {
2896 pointer __tmp = __ptr_.first();
2897 __ptr_.first() = nullptr;
2898 if (__tmp)
2899 __ptr_.second()(__tmp);
2900 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002901#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002902 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2903 {
2904 pointer __tmp = __ptr_.first();
2905 __ptr_.first() = __p;
2906 if (__tmp)
2907 __ptr_.second()(__tmp);
2908 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002909#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002910
2911 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2912private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002913
Howard Hinnant73d21a42010-09-04 23:28:19 +00002914#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002915 template <class _Up>
2916 explicit unique_ptr(_Up);
2917 template <class _Up>
2918 unique_ptr(_Up __u,
2919 typename conditional<
2920 is_reference<deleter_type>::value,
2921 deleter_type,
2922 typename add_lvalue_reference<const deleter_type>::type>::type,
2923 typename enable_if
2924 <
2925 is_convertible<_Up, pointer>::value,
2926 __nat
2927 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002928#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002929};
2930
2931template <class _Tp, class _Dp>
2932inline _LIBCPP_INLINE_VISIBILITY
2933void
Howard Hinnant1694d232011-05-28 14:41:13 +00002934swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002935
2936template <class _T1, class _D1, class _T2, class _D2>
2937inline _LIBCPP_INLINE_VISIBILITY
2938bool
2939operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2940
2941template <class _T1, class _D1, class _T2, class _D2>
2942inline _LIBCPP_INLINE_VISIBILITY
2943bool
2944operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2945
2946template <class _T1, class _D1, class _T2, class _D2>
2947inline _LIBCPP_INLINE_VISIBILITY
2948bool
Howard Hinnant3fadda32012-02-21 21:02:58 +00002949operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2950{
2951 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2952 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
2953 typedef typename common_type<_P1, _P2>::type _V;
2954 return less<_V>()(__x.get(), __y.get());
2955}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002956
2957template <class _T1, class _D1, class _T2, class _D2>
2958inline _LIBCPP_INLINE_VISIBILITY
2959bool
2960operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2961
2962template <class _T1, class _D1, class _T2, class _D2>
2963inline _LIBCPP_INLINE_VISIBILITY
2964bool
2965operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2966
2967template <class _T1, class _D1, class _T2, class _D2>
2968inline _LIBCPP_INLINE_VISIBILITY
2969bool
2970operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2971
Howard Hinnant3fadda32012-02-21 21:02:58 +00002972template <class _T1, class _D1>
2973inline _LIBCPP_INLINE_VISIBILITY
2974bool
Howard Hinnant46e94932012-07-07 20:56:04 +00002975operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00002976{
2977 return !__x;
2978}
2979
2980template <class _T1, class _D1>
2981inline _LIBCPP_INLINE_VISIBILITY
2982bool
Howard Hinnant46e94932012-07-07 20:56:04 +00002983operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00002984{
2985 return !__x;
2986}
2987
2988template <class _T1, class _D1>
2989inline _LIBCPP_INLINE_VISIBILITY
2990bool
Howard Hinnant46e94932012-07-07 20:56:04 +00002991operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00002992{
2993 return static_cast<bool>(__x);
2994}
2995
2996template <class _T1, class _D1>
2997inline _LIBCPP_INLINE_VISIBILITY
2998bool
Howard Hinnant46e94932012-07-07 20:56:04 +00002999operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnant3fadda32012-02-21 21:02:58 +00003000{
3001 return static_cast<bool>(__x);
3002}
3003
3004template <class _T1, class _D1>
3005inline _LIBCPP_INLINE_VISIBILITY
3006bool
3007operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3008{
3009 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3010 return less<_P1>()(__x.get(), nullptr);
3011}
3012
3013template <class _T1, class _D1>
3014inline _LIBCPP_INLINE_VISIBILITY
3015bool
3016operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3017{
3018 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3019 return less<_P1>()(nullptr, __x.get());
3020}
3021
3022template <class _T1, class _D1>
3023inline _LIBCPP_INLINE_VISIBILITY
3024bool
3025operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3026{
3027 return nullptr < __x;
3028}
3029
3030template <class _T1, class _D1>
3031inline _LIBCPP_INLINE_VISIBILITY
3032bool
3033operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3034{
3035 return __x < nullptr;
3036}
3037
3038template <class _T1, class _D1>
3039inline _LIBCPP_INLINE_VISIBILITY
3040bool
3041operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3042{
3043 return !(nullptr < __x);
3044}
3045
3046template <class _T1, class _D1>
3047inline _LIBCPP_INLINE_VISIBILITY
3048bool
3049operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3050{
3051 return !(__x < nullptr);
3052}
3053
3054template <class _T1, class _D1>
3055inline _LIBCPP_INLINE_VISIBILITY
3056bool
3057operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3058{
3059 return !(__x < nullptr);
3060}
3061
3062template <class _T1, class _D1>
3063inline _LIBCPP_INLINE_VISIBILITY
3064bool
3065operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3066{
3067 return !(nullptr < __x);
3068}
3069
Howard Hinnant87073e42012-05-01 15:37:54 +00003070#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3071
3072template <class _Tp, class _Dp>
3073inline _LIBCPP_INLINE_VISIBILITY
3074unique_ptr<_Tp, _Dp>
3075move(unique_ptr<_Tp, _Dp>& __t)
3076{
3077 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3078}
3079
3080#endif
3081
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00003082template <class _Tp> struct hash;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003083
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003084// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
3085// is 64 bits. This is because cityhash64 uses 64bit x 64bit
3086// multiplication, which can be very slow on 32-bit systems.
Howard Hinnant40c13d32011-12-05 00:08:45 +00003087template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003088struct __murmur2_or_cityhash;
Howard Hinnant40c13d32011-12-05 00:08:45 +00003089
3090template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003091struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003092{
3093 _Size operator()(const void* __key, _Size __len);
3094};
3095
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003096// murmur2
Howard Hinnant40c13d32011-12-05 00:08:45 +00003097template <class _Size>
3098_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003099__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003100{
3101 const _Size __m = 0x5bd1e995;
3102 const _Size __r = 24;
3103 _Size __h = __len;
3104 const unsigned char* __data = static_cast<const unsigned char*>(__key);
3105 for (; __len >= 4; __data += 4, __len -= 4)
3106 {
3107 _Size __k = *(const _Size*)__data;
3108 __k *= __m;
3109 __k ^= __k >> __r;
3110 __k *= __m;
3111 __h *= __m;
3112 __h ^= __k;
3113 }
3114 switch (__len)
3115 {
3116 case 3:
3117 __h ^= __data[2] << 16;
3118 case 2:
3119 __h ^= __data[1] << 8;
3120 case 1:
3121 __h ^= __data[0];
3122 __h *= __m;
3123 }
3124 __h ^= __h >> 13;
3125 __h *= __m;
3126 __h ^= __h >> 15;
3127 return __h;
3128}
3129
3130template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003131struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003132{
3133 _Size operator()(const void* __key, _Size __len);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003134
3135 private:
3136 // Some primes between 2^63 and 2^64.
3137 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
3138 static const _Size __k1 = 0xb492b66fbe98f273ULL;
3139 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
3140 static const _Size __k3 = 0xc949d7c7509e6557ULL;
3141
3142 static _Size __rotate(_Size __val, int __shift) {
3143 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
3144 }
3145
3146 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
3147 return (__val >> __shift) | (__val << (64 - __shift));
3148 }
3149
3150 static _Size __shift_mix(_Size __val) {
3151 return __val ^ (__val >> 47);
3152 }
3153
3154 static _Size __hash_len_16(_Size __u, _Size __v) {
3155 const _Size __mul = 0x9ddfea08eb382d69ULL;
3156 _Size __a = (__u ^ __v) * __mul;
3157 __a ^= (__a >> 47);
3158 _Size __b = (__v ^ __a) * __mul;
3159 __b ^= (__b >> 47);
3160 __b *= __mul;
3161 return __b;
3162 }
3163
3164 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
3165 if (__len > 8) {
3166 const _Size __a = *(const _Size*)__s;
3167 const _Size __b = *(const _Size*)(__s + __len - 8);
3168 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
3169 }
3170 if (__len >= 4) {
3171 const uint32_t __a = *(const uint32_t*)(__s);
3172 const uint32_t __b = *(const uint32_t*)(__s + __len - 4);
3173 return __hash_len_16(__len + (__a << 3), __b);
3174 }
3175 if (__len > 0) {
3176 const unsigned char __a = __s[0];
3177 const unsigned char __b = __s[__len >> 1];
3178 const unsigned char __c = __s[__len - 1];
3179 const uint32_t __y = static_cast<uint32_t>(__a) +
3180 (static_cast<uint32_t>(__b) << 8);
3181 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3182 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3183 }
3184 return __k2;
3185 }
3186
3187 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
3188 const _Size __a = *(const _Size*)(__s) * __k1;
3189 const _Size __b = *(const _Size*)(__s + 8);
3190 const _Size __c = *(const _Size*)(__s + __len - 8) * __k2;
3191 const _Size __d = *(const _Size*)(__s + __len - 16) * __k0;
3192 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3193 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3194 }
3195
3196 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3197 // Callers do best to use "random-looking" values for a and b.
3198 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3199 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3200 __a += __w;
3201 __b = __rotate(__b + __a + __z, 21);
3202 const _Size __c = __a;
3203 __a += __x;
3204 __a += __y;
3205 __b += __rotate(__a, 44);
3206 return pair<_Size, _Size>(__a + __z, __b + __c);
3207 }
3208
3209 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3210 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3211 const char* __s, _Size __a, _Size __b) {
3212 return __weak_hash_len_32_with_seeds(*(const _Size*)(__s),
3213 *(const _Size*)(__s + 8),
3214 *(const _Size*)(__s + 16),
3215 *(const _Size*)(__s + 24),
3216 __a,
3217 __b);
3218 }
3219
3220 // Return an 8-byte hash for 33 to 64 bytes.
3221 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
3222 _Size __z = *(const _Size*)(__s + 24);
3223 _Size __a = *(const _Size*)(__s) +
3224 (__len + *(const _Size*)(__s + __len - 16)) * __k0;
3225 _Size __b = __rotate(__a + __z, 52);
3226 _Size __c = __rotate(__a, 37);
3227 __a += *(const _Size*)(__s + 8);
3228 __c += __rotate(__a, 7);
3229 __a += *(const _Size*)(__s + 16);
3230 _Size __vf = __a + __z;
3231 _Size __vs = __b + __rotate(__a, 31) + __c;
3232 __a = *(const _Size*)(__s + 16) + *(const _Size*)(__s + __len - 32);
3233 __z += *(const _Size*)(__s + __len - 8);
3234 __b = __rotate(__a + __z, 52);
3235 __c = __rotate(__a, 37);
3236 __a += *(const _Size*)(__s + __len - 24);
3237 __c += __rotate(__a, 7);
3238 __a += *(const _Size*)(__s + __len - 16);
3239 _Size __wf = __a + __z;
3240 _Size __ws = __b + __rotate(__a, 31) + __c;
3241 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3242 return __shift_mix(__r * __k0 + __vs) * __k2;
3243 }
Howard Hinnant40c13d32011-12-05 00:08:45 +00003244};
3245
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003246// cityhash64
Howard Hinnant40c13d32011-12-05 00:08:45 +00003247template <class _Size>
3248_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003249__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003250{
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003251 const char* __s = static_cast<const char*>(__key);
3252 if (__len <= 32) {
3253 if (__len <= 16) {
3254 return __hash_len_0_to_16(__s, __len);
3255 } else {
3256 return __hash_len_17_to_32(__s, __len);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003257 }
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003258 } else if (__len <= 64) {
3259 return __hash_len_33_to_64(__s, __len);
3260 }
3261
3262 // For strings over 64 bytes we hash the end first, and then as we
3263 // loop we keep 56 bytes of state: v, w, x, y, and z.
3264 _Size __x = *(const _Size*)(__s + __len - 40);
3265 _Size __y = *(const _Size*)(__s + __len - 16) +
3266 *(const _Size*)(__s + __len - 56);
3267 _Size __z = __hash_len_16(*(const _Size*)(__s + __len - 48) + __len,
3268 *(const _Size*)(__s + __len - 24));
3269 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3270 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
3271 __x = __x * __k1 + *(const _Size*)(__s);
3272
3273 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3274 __len = (__len - 1) & ~static_cast<_Size>(63);
3275 do {
3276 __x = __rotate(__x + __y + __v.first + *(const _Size*)(__s + 8), 37) * __k1;
3277 __y = __rotate(__y + __v.second + *(const _Size*)(__s + 48), 42) * __k1;
3278 __x ^= __w.second;
3279 __y += __v.first + *(const _Size*)(__s + 40);
3280 __z = __rotate(__z + __w.first, 33) * __k1;
3281 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3282 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
3283 __y + *(const _Size*)(__s + 16));
3284 std::swap(__z, __x);
3285 __s += 64;
3286 __len -= 64;
3287 } while (__len != 0);
3288 return __hash_len_16(
3289 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3290 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003291}
3292
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003293template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3294struct __scalar_hash;
3295
3296template <class _Tp>
3297struct __scalar_hash<_Tp, 0>
3298 : public unary_function<_Tp, size_t>
Howard Hinnant21aefc32010-06-03 16:42:57 +00003299{
Howard Hinnant82894812010-09-22 16:48:34 +00003300 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003301 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003302 {
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003303 union
3304 {
3305 _Tp __t;
3306 size_t __a;
3307 } __u;
3308 __u.__a = 0;
3309 __u.__t = __v;
3310 return __u.__a;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003311 }
3312};
3313
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003314template <class _Tp>
3315struct __scalar_hash<_Tp, 1>
3316 : public unary_function<_Tp, size_t>
3317{
3318 _LIBCPP_INLINE_VISIBILITY
3319 size_t operator()(_Tp __v) const _NOEXCEPT
3320 {
3321 union
3322 {
3323 _Tp __t;
3324 size_t __a;
3325 } __u;
3326 __u.__t = __v;
3327 return __u.__a;
3328 }
3329};
3330
3331template <class _Tp>
3332struct __scalar_hash<_Tp, 2>
3333 : public unary_function<_Tp, size_t>
3334{
3335 _LIBCPP_INLINE_VISIBILITY
3336 size_t operator()(_Tp __v) const _NOEXCEPT
3337 {
3338 union
3339 {
3340 _Tp __t;
3341 struct
3342 {
3343 size_t __a;
3344 size_t __b;
3345 };
3346 } __u;
3347 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003348 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003349 }
3350};
3351
3352template <class _Tp>
3353struct __scalar_hash<_Tp, 3>
3354 : public unary_function<_Tp, size_t>
3355{
3356 _LIBCPP_INLINE_VISIBILITY
3357 size_t operator()(_Tp __v) const _NOEXCEPT
3358 {
3359 union
3360 {
3361 _Tp __t;
3362 struct
3363 {
3364 size_t __a;
3365 size_t __b;
3366 size_t __c;
3367 };
3368 } __u;
3369 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003370 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003371 }
3372};
3373
3374template <class _Tp>
3375struct __scalar_hash<_Tp, 4>
3376 : public unary_function<_Tp, size_t>
3377{
3378 _LIBCPP_INLINE_VISIBILITY
3379 size_t operator()(_Tp __v) const _NOEXCEPT
3380 {
3381 union
3382 {
3383 _Tp __t;
3384 struct
3385 {
3386 size_t __a;
3387 size_t __b;
3388 size_t __c;
3389 size_t __d;
3390 };
3391 } __u;
3392 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003393 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003394 }
3395};
3396
3397template<class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00003398struct _LIBCPP_TYPE_VIS hash<_Tp*>
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003399 : public unary_function<_Tp*, size_t>
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003400{
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003401 _LIBCPP_INLINE_VISIBILITY
3402 size_t operator()(_Tp* __v) const _NOEXCEPT
3403 {
3404 union
3405 {
3406 _Tp* __t;
3407 size_t __a;
3408 } __u;
3409 __u.__t = __v;
3410 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
3411 }
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003412};
3413
Howard Hinnant21aefc32010-06-03 16:42:57 +00003414template <class _Tp, class _Dp>
Howard Hinnant83eade62013-03-06 23:30:19 +00003415struct _LIBCPP_TYPE_VIS hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003416{
3417 typedef unique_ptr<_Tp, _Dp> argument_type;
3418 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003420 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003421 {
3422 typedef typename argument_type::pointer pointer;
3423 return hash<pointer>()(__ptr.get());
3424 }
3425};
3426
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003427struct __destruct_n
3428{
3429private:
3430 size_t size;
3431
3432 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003433 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003434 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3435
3436 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003437 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003438 {}
3439
Howard Hinnant1694d232011-05-28 14:41:13 +00003440 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003441 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003442 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003443 {}
3444
Howard Hinnant1694d232011-05-28 14:41:13 +00003445 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003446 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003447 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003448 {}
3449public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003450 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3451 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003452
3453 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003454 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003455 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003456
3457 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003458 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003459 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003460
3461 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003462 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003463 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003464};
3465
3466template <class _Alloc>
3467class __allocator_destructor
3468{
3469 typedef allocator_traits<_Alloc> __alloc_traits;
3470public:
3471 typedef typename __alloc_traits::pointer pointer;
3472 typedef typename __alloc_traits::size_type size_type;
3473private:
3474 _Alloc& __alloc_;
3475 size_type __s_;
3476public:
3477 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00003478 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003479 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003481 void operator()(pointer __p) _NOEXCEPT
3482 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003483};
3484
3485template <class _InputIterator, class _ForwardIterator>
3486_ForwardIterator
3487uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3488{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003489 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003490#ifndef _LIBCPP_NO_EXCEPTIONS
3491 _ForwardIterator __s = __r;
3492 try
3493 {
3494#endif
3495 for (; __f != __l; ++__f, ++__r)
3496 ::new(&*__r) value_type(*__f);
3497#ifndef _LIBCPP_NO_EXCEPTIONS
3498 }
3499 catch (...)
3500 {
3501 for (; __s != __r; ++__s)
3502 __s->~value_type();
3503 throw;
3504 }
3505#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003506 return __r;
3507}
3508
3509template <class _InputIterator, class _Size, class _ForwardIterator>
3510_ForwardIterator
3511uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3512{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003513 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003514#ifndef _LIBCPP_NO_EXCEPTIONS
3515 _ForwardIterator __s = __r;
3516 try
3517 {
3518#endif
3519 for (; __n > 0; ++__f, ++__r, --__n)
3520 ::new(&*__r) value_type(*__f);
3521#ifndef _LIBCPP_NO_EXCEPTIONS
3522 }
3523 catch (...)
3524 {
3525 for (; __s != __r; ++__s)
3526 __s->~value_type();
3527 throw;
3528 }
3529#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003530 return __r;
3531}
3532
3533template <class _ForwardIterator, class _Tp>
3534void
3535uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3536{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003537 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003538#ifndef _LIBCPP_NO_EXCEPTIONS
3539 _ForwardIterator __s = __f;
3540 try
3541 {
3542#endif
3543 for (; __f != __l; ++__f)
3544 ::new(&*__f) value_type(__x);
3545#ifndef _LIBCPP_NO_EXCEPTIONS
3546 }
3547 catch (...)
3548 {
3549 for (; __s != __f; ++__s)
3550 __s->~value_type();
3551 throw;
3552 }
3553#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003554}
3555
3556template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003557_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003558uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3559{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003560 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003561#ifndef _LIBCPP_NO_EXCEPTIONS
3562 _ForwardIterator __s = __f;
3563 try
3564 {
3565#endif
3566 for (; __n > 0; ++__f, --__n)
3567 ::new(&*__f) value_type(__x);
3568#ifndef _LIBCPP_NO_EXCEPTIONS
3569 }
3570 catch (...)
3571 {
3572 for (; __s != __f; ++__s)
3573 __s->~value_type();
3574 throw;
3575 }
3576#endif
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003577 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003578}
3579
Howard Hinnant82894812010-09-22 16:48:34 +00003580class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003581 : public std::exception
3582{
3583public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003584 virtual ~bad_weak_ptr() _NOEXCEPT;
3585 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003586};
3587
Howard Hinnant83eade62013-03-06 23:30:19 +00003588template<class _Tp> class _LIBCPP_TYPE_VIS weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003589
3590class __shared_count
3591{
3592 __shared_count(const __shared_count&);
3593 __shared_count& operator=(const __shared_count&);
3594
3595protected:
3596 long __shared_owners_;
3597 virtual ~__shared_count();
3598private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003599 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003600
3601public:
Howard Hinnant82894812010-09-22 16:48:34 +00003602 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003603 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003604 : __shared_owners_(__refs) {}
3605
Howard Hinnant1694d232011-05-28 14:41:13 +00003606 void __add_shared() _NOEXCEPT;
3607 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003608 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003609 long use_count() const _NOEXCEPT {return __shared_owners_ + 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003610};
3611
3612class __shared_weak_count
3613 : private __shared_count
3614{
3615 long __shared_weak_owners_;
3616
3617public:
Howard Hinnant82894812010-09-22 16:48:34 +00003618 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003619 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003620 : __shared_count(__refs),
3621 __shared_weak_owners_(__refs) {}
3622protected:
3623 virtual ~__shared_weak_count();
3624
3625public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003626 void __add_shared() _NOEXCEPT;
3627 void __add_weak() _NOEXCEPT;
3628 void __release_shared() _NOEXCEPT;
3629 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003630 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003631 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3632 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003633
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003634 // Define the function out only if we build static libc++ without RTTI.
3635 // Otherwise we may break clients who need to compile their projects with
3636 // -fno-rtti and yet link against a libc++.dylib compiled
3637 // without -fno-rtti.
3638#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant1694d232011-05-28 14:41:13 +00003639 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant4a0e74f2013-02-25 15:50:36 +00003640#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003641private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003642 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003643};
3644
3645template <class _Tp, class _Dp, class _Alloc>
3646class __shared_ptr_pointer
3647 : public __shared_weak_count
3648{
3649 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3650public:
Howard Hinnant82894812010-09-22 16:48:34 +00003651 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003652 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003653 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003654
Howard Hinnantd4444702010-08-11 17:04:31 +00003655#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00003656 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00003657#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003658
3659private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003660 virtual void __on_zero_shared() _NOEXCEPT;
3661 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003662};
3663
Howard Hinnantd4444702010-08-11 17:04:31 +00003664#ifndef _LIBCPP_NO_RTTI
3665
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003666template <class _Tp, class _Dp, class _Alloc>
3667const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00003668__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003669{
3670 return __t == typeid(_Dp) ? &__data_.first().second() : 0;
3671}
3672
Howard Hinnant324bb032010-08-22 00:02:43 +00003673#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003674
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003675template <class _Tp, class _Dp, class _Alloc>
3676void
Howard Hinnant1694d232011-05-28 14:41:13 +00003677__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003678{
3679 __data_.first().second()(__data_.first().first());
3680 __data_.first().second().~_Dp();
3681}
3682
3683template <class _Tp, class _Dp, class _Alloc>
3684void
Howard Hinnant1694d232011-05-28 14:41:13 +00003685__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003686{
3687 typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
3688 __data_.second().~_Alloc();
3689 __a.deallocate(this, 1);
3690}
3691
3692template <class _Tp, class _Alloc>
3693class __shared_ptr_emplace
3694 : public __shared_weak_count
3695{
3696 __compressed_pair<_Alloc, _Tp> __data_;
3697public:
3698#ifndef _LIBCPP_HAS_NO_VARIADICS
3699
Howard Hinnant82894812010-09-22 16:48:34 +00003700 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003701 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003702 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003703
3704 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00003705 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003706 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00003707 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3708 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003709
3710#else // _LIBCPP_HAS_NO_VARIADICS
3711
Howard Hinnant82894812010-09-22 16:48:34 +00003712 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003713 __shared_ptr_emplace(_Alloc __a)
3714 : __data_(__a) {}
3715
3716 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00003717 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003718 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3719 : __data_(__a, _Tp(__a0)) {}
3720
3721 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00003722 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003723 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3724 : __data_(__a, _Tp(__a0, __a1)) {}
3725
3726 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00003727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003728 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3729 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3730
3731#endif // _LIBCPP_HAS_NO_VARIADICS
3732
3733private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003734 virtual void __on_zero_shared() _NOEXCEPT;
3735 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003736public:
Howard Hinnant82894812010-09-22 16:48:34 +00003737 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003738 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003739};
3740
3741template <class _Tp, class _Alloc>
3742void
Howard Hinnant1694d232011-05-28 14:41:13 +00003743__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003744{
3745 __data_.second().~_Tp();
3746}
3747
3748template <class _Tp, class _Alloc>
3749void
Howard Hinnant1694d232011-05-28 14:41:13 +00003750__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003751{
3752 typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
3753 __data_.first().~_Alloc();
3754 __a.deallocate(this, 1);
3755}
3756
Howard Hinnant83eade62013-03-06 23:30:19 +00003757template<class _Tp> class _LIBCPP_TYPE_VIS enable_shared_from_this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003758
3759template<class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00003760class _LIBCPP_TYPE_VIS shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003761{
Howard Hinnant324bb032010-08-22 00:02:43 +00003762public:
3763 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003764private:
3765 element_type* __ptr_;
3766 __shared_weak_count* __cntrl_;
3767
3768 struct __nat {int __for_bool_;};
3769public:
Howard Hinnant46e94932012-07-07 20:56:04 +00003770 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
3771 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003772 template<class _Yp,
3773 class = typename enable_if
3774 <
3775 is_convertible<_Yp*, element_type*>::value
3776 >::type
3777 >
3778 explicit shared_ptr(_Yp* __p);
3779 template<class _Yp, class _Dp,
3780 class = typename enable_if
3781 <
3782 is_convertible<_Yp*, element_type*>::value
3783 >::type
3784 >
3785 shared_ptr(_Yp* __p, _Dp __d);
3786 template<class _Yp, class _Dp, class _Alloc,
3787 class = typename enable_if
3788 <
3789 is_convertible<_Yp*, element_type*>::value
3790 >::type
3791 >
3792 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003793 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3794 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant1694d232011-05-28 14:41:13 +00003795 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3796 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003797 template<class _Yp>
3798 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003799 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3800 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003801#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003802 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003803 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003804 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3805 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003806#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003807 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003808 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003809#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003810 template<class _Yp,
3811 class = typename enable_if
3812 <
3813 is_convertible<_Yp*, element_type*>::value
3814 >::type
3815 >
3816 shared_ptr(auto_ptr<_Yp>&& __r);
Howard Hinnant324bb032010-08-22 00:02:43 +00003817#else
Howard Hinnant57199402012-01-02 17:56:02 +00003818 template<class _Yp,
3819 class = typename enable_if
3820 <
3821 is_convertible<_Yp*, element_type*>::value
3822 >::type
3823 >
3824 shared_ptr(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003825#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003826#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003827 template <class _Yp, class _Dp,
3828 class = typename enable_if
3829 <
3830 !is_array<_Yp>::value &&
3831 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3832 >::type
3833 >
3834 shared_ptr(unique_ptr<_Yp, _Dp>&&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003835 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant57199402012-01-02 17:56:02 +00003836 template <class _Yp, class _Dp,
3837 class = typename enable_if
3838 <
3839 !is_array<_Yp>::value &&
3840 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3841 >::type
3842 >
3843 shared_ptr(unique_ptr<_Yp, _Dp>&&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003844 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003845#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003846 template <class _Yp, class _Dp,
3847 class = typename enable_if
3848 <
3849 !is_array<_Yp>::value &&
3850 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3851 >::type
3852 > shared_ptr(unique_ptr<_Yp, _Dp>,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003853 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant57199402012-01-02 17:56:02 +00003854 template <class _Yp, class _Dp,
3855 class = typename enable_if
3856 <
3857 !is_array<_Yp>::value &&
3858 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3859 >::type
3860 >
3861 shared_ptr(unique_ptr<_Yp, _Dp>,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003862 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003863#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003864
3865 ~shared_ptr();
3866
Howard Hinnant1694d232011-05-28 14:41:13 +00003867 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003868 template<class _Yp>
3869 typename enable_if
3870 <
3871 is_convertible<_Yp*, element_type*>::value,
3872 shared_ptr&
3873 >::type
3874 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003875#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003876 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003877 template<class _Yp>
3878 typename enable_if
3879 <
3880 is_convertible<_Yp*, element_type*>::value,
3881 shared_ptr<_Tp>&
3882 >::type
3883 operator=(shared_ptr<_Yp>&& __r);
3884 template<class _Yp>
3885 typename enable_if
3886 <
3887 !is_array<_Yp>::value &&
3888 is_convertible<_Yp*, element_type*>::value,
3889 shared_ptr&
3890 >::type
3891 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003892#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003893 template<class _Yp>
3894 typename enable_if
3895 <
3896 !is_array<_Yp>::value &&
3897 is_convertible<_Yp*, element_type*>::value,
3898 shared_ptr&
3899 >::type
3900 operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003901#endif
Howard Hinnant57199402012-01-02 17:56:02 +00003902 template <class _Yp, class _Dp>
3903 typename enable_if
3904 <
3905 !is_array<_Yp>::value &&
3906 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3907 shared_ptr&
3908 >::type
Howard Hinnant73d21a42010-09-04 23:28:19 +00003909#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003910 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003911#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003912 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003913#endif
3914
Howard Hinnant1694d232011-05-28 14:41:13 +00003915 void swap(shared_ptr& __r) _NOEXCEPT;
3916 void reset() _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003917 template<class _Yp>
3918 typename enable_if
3919 <
3920 is_convertible<_Yp*, element_type*>::value,
3921 void
3922 >::type
3923 reset(_Yp* __p);
3924 template<class _Yp, class _Dp>
3925 typename enable_if
3926 <
3927 is_convertible<_Yp*, element_type*>::value,
3928 void
3929 >::type
3930 reset(_Yp* __p, _Dp __d);
3931 template<class _Yp, class _Dp, class _Alloc>
3932 typename enable_if
3933 <
3934 is_convertible<_Yp*, element_type*>::value,
3935 void
3936 >::type
3937 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003938
Howard Hinnant82894812010-09-22 16:48:34 +00003939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003940 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003941 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003942 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3943 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003944 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003945 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003946 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003947 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003949 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00003950 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant77861882012-02-21 21:46:43 +00003951 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant99968442011-11-29 18:15:50 +00003952 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00003953 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003954 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003955 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant99968442011-11-29 18:15:50 +00003956 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00003957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003958 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003959 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant5fec82d2012-07-30 01:40:57 +00003960 _LIBCPP_INLINE_VISIBILITY
3961 bool
3962 __owner_equivalent(const shared_ptr& __p) const
3963 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003964
Howard Hinnantd4444702010-08-11 17:04:31 +00003965#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003966 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00003967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003968 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003969 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00003970#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003971
3972#ifndef _LIBCPP_HAS_NO_VARIADICS
3973
3974 template<class ..._Args>
3975 static
3976 shared_ptr<_Tp>
3977 make_shared(_Args&& ...__args);
3978
3979 template<class _Alloc, class ..._Args>
3980 static
3981 shared_ptr<_Tp>
3982 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3983
3984#else // _LIBCPP_HAS_NO_VARIADICS
3985
3986 static shared_ptr<_Tp> make_shared();
3987
3988 template<class _A0>
3989 static shared_ptr<_Tp> make_shared(_A0&);
3990
3991 template<class _A0, class _A1>
3992 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3993
3994 template<class _A0, class _A1, class _A2>
3995 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3996
3997 template<class _Alloc>
3998 static shared_ptr<_Tp>
3999 allocate_shared(const _Alloc& __a);
4000
4001 template<class _Alloc, class _A0>
4002 static shared_ptr<_Tp>
4003 allocate_shared(const _Alloc& __a, _A0& __a0);
4004
4005 template<class _Alloc, class _A0, class _A1>
4006 static shared_ptr<_Tp>
4007 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
4008
4009 template<class _Alloc, class _A0, class _A1, class _A2>
4010 static shared_ptr<_Tp>
4011 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
4012
4013#endif // _LIBCPP_HAS_NO_VARIADICS
4014
4015private:
4016
4017 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00004018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004019 void
Howard Hinnant1694d232011-05-28 14:41:13 +00004020 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004021 {
4022 if (__e)
4023 __e->__weak_this_ = *this;
4024 }
4025
Howard Hinnant82894812010-09-22 16:48:34 +00004026 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004027 void __enable_weak_this(const void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004028
Howard Hinnant83eade62013-03-06 23:30:19 +00004029 template <class _Up> friend class _LIBCPP_TYPE_VIS shared_ptr;
4030 template <class _Up> friend class _LIBCPP_TYPE_VIS weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004031};
4032
4033template<class _Tp>
4034inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00004035_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004036shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004037 : __ptr_(0),
4038 __cntrl_(0)
4039{
4040}
4041
4042template<class _Tp>
4043inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00004044_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00004045shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004046 : __ptr_(0),
4047 __cntrl_(0)
4048{
4049}
4050
4051template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004052template<class _Yp, class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004053shared_ptr<_Tp>::shared_ptr(_Yp* __p)
4054 : __ptr_(__p)
4055{
4056 unique_ptr<_Yp> __hold(__p);
4057 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4058 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
4059 __hold.release();
4060 __enable_weak_this(__p);
4061}
4062
4063template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004064template<class _Yp, class _Dp, class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004065shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
4066 : __ptr_(__p)
4067{
4068#ifndef _LIBCPP_NO_EXCEPTIONS
4069 try
4070 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004071#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004072 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4073 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
4074 __enable_weak_this(__p);
4075#ifndef _LIBCPP_NO_EXCEPTIONS
4076 }
4077 catch (...)
4078 {
4079 __d(__p);
4080 throw;
4081 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004082#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004083}
4084
4085template<class _Tp>
4086template<class _Dp>
4087shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4088 : __ptr_(0)
4089{
4090#ifndef _LIBCPP_NO_EXCEPTIONS
4091 try
4092 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004093#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004094 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4095 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4096#ifndef _LIBCPP_NO_EXCEPTIONS
4097 }
4098 catch (...)
4099 {
4100 __d(__p);
4101 throw;
4102 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004103#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004104}
4105
4106template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004107template<class _Yp, class _Dp, class _Alloc, class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004108shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
4109 : __ptr_(__p)
4110{
4111#ifndef _LIBCPP_NO_EXCEPTIONS
4112 try
4113 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004114#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004115 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
4116 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
4117 typedef __allocator_destructor<_A2> _D2;
4118 _A2 __a2(__a);
4119 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4120 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
4121 __cntrl_ = __hold2.release();
4122 __enable_weak_this(__p);
4123#ifndef _LIBCPP_NO_EXCEPTIONS
4124 }
4125 catch (...)
4126 {
4127 __d(__p);
4128 throw;
4129 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004130#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004131}
4132
4133template<class _Tp>
4134template<class _Dp, class _Alloc>
4135shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4136 : __ptr_(0)
4137{
4138#ifndef _LIBCPP_NO_EXCEPTIONS
4139 try
4140 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004141#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004142 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
4143 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
4144 typedef __allocator_destructor<_A2> _D2;
4145 _A2 __a2(__a);
4146 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4147 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
4148 __cntrl_ = __hold2.release();
4149#ifndef _LIBCPP_NO_EXCEPTIONS
4150 }
4151 catch (...)
4152 {
4153 __d(__p);
4154 throw;
4155 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004156#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004157}
4158
4159template<class _Tp>
4160template<class _Yp>
4161inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004162shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004163 : __ptr_(__p),
4164 __cntrl_(__r.__cntrl_)
4165{
4166 if (__cntrl_)
4167 __cntrl_->__add_shared();
4168}
4169
4170template<class _Tp>
4171inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004172shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004173 : __ptr_(__r.__ptr_),
4174 __cntrl_(__r.__cntrl_)
4175{
4176 if (__cntrl_)
4177 __cntrl_->__add_shared();
4178}
4179
4180template<class _Tp>
4181template<class _Yp>
4182inline _LIBCPP_INLINE_VISIBILITY
4183shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4184 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004185 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004186 : __ptr_(__r.__ptr_),
4187 __cntrl_(__r.__cntrl_)
4188{
4189 if (__cntrl_)
4190 __cntrl_->__add_shared();
4191}
4192
Howard Hinnant73d21a42010-09-04 23:28:19 +00004193#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004194
4195template<class _Tp>
4196inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004197shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004198 : __ptr_(__r.__ptr_),
4199 __cntrl_(__r.__cntrl_)
4200{
4201 __r.__ptr_ = 0;
4202 __r.__cntrl_ = 0;
4203}
4204
4205template<class _Tp>
4206template<class _Yp>
4207inline _LIBCPP_INLINE_VISIBILITY
4208shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4209 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004210 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004211 : __ptr_(__r.__ptr_),
4212 __cntrl_(__r.__cntrl_)
4213{
4214 __r.__ptr_ = 0;
4215 __r.__cntrl_ = 0;
4216}
4217
Howard Hinnant73d21a42010-09-04 23:28:19 +00004218#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004219
4220template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004221template<class _Yp, class>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004222#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004223shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
4224#else
Howard Hinnant92172b82010-08-21 21:14:53 +00004225shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004226#endif
4227 : __ptr_(__r.get())
4228{
4229 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4230 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
4231 __enable_weak_this(__r.get());
4232 __r.release();
4233}
4234
4235template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004236template <class _Yp, class _Dp, class>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004237#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004238shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4239#else
4240shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4241#endif
4242 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
4243 : __ptr_(__r.get())
4244{
4245 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4246 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
4247 __enable_weak_this(__r.get());
4248 __r.release();
4249}
4250
4251template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004252template <class _Yp, class _Dp, class>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004253#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004254shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4255#else
4256shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4257#endif
4258 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
4259 : __ptr_(__r.get())
4260{
4261 typedef __shared_ptr_pointer<_Yp*,
4262 reference_wrapper<typename remove_reference<_Dp>::type>,
4263 allocator<_Yp> > _CntrlBlk;
4264 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
4265 __enable_weak_this(__r.get());
4266 __r.release();
4267}
4268
4269#ifndef _LIBCPP_HAS_NO_VARIADICS
4270
4271template<class _Tp>
4272template<class ..._Args>
4273shared_ptr<_Tp>
4274shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4275{
4276 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4277 typedef allocator<_CntrlBlk> _A2;
4278 typedef __allocator_destructor<_A2> _D2;
4279 _A2 __a2;
4280 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004281 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004282 shared_ptr<_Tp> __r;
4283 __r.__ptr_ = __hold2.get()->get();
4284 __r.__cntrl_ = __hold2.release();
4285 __r.__enable_weak_this(__r.__ptr_);
4286 return __r;
4287}
4288
4289template<class _Tp>
4290template<class _Alloc, class ..._Args>
4291shared_ptr<_Tp>
4292shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4293{
4294 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4295 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
4296 typedef __allocator_destructor<_A2> _D2;
4297 _A2 __a2(__a);
4298 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004299 ::new(__hold2.get()) _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004300 shared_ptr<_Tp> __r;
4301 __r.__ptr_ = __hold2.get()->get();
4302 __r.__cntrl_ = __hold2.release();
4303 __r.__enable_weak_this(__r.__ptr_);
4304 return __r;
4305}
4306
4307#else // _LIBCPP_HAS_NO_VARIADICS
4308
4309template<class _Tp>
4310shared_ptr<_Tp>
4311shared_ptr<_Tp>::make_shared()
4312{
4313 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4314 typedef allocator<_CntrlBlk> _Alloc2;
4315 typedef __allocator_destructor<_Alloc2> _D2;
4316 _Alloc2 __alloc2;
4317 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4318 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4319 shared_ptr<_Tp> __r;
4320 __r.__ptr_ = __hold2.get()->get();
4321 __r.__cntrl_ = __hold2.release();
4322 __r.__enable_weak_this(__r.__ptr_);
4323 return __r;
4324}
4325
4326template<class _Tp>
4327template<class _A0>
4328shared_ptr<_Tp>
4329shared_ptr<_Tp>::make_shared(_A0& __a0)
4330{
4331 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4332 typedef allocator<_CntrlBlk> _Alloc2;
4333 typedef __allocator_destructor<_Alloc2> _D2;
4334 _Alloc2 __alloc2;
4335 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4336 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4337 shared_ptr<_Tp> __r;
4338 __r.__ptr_ = __hold2.get()->get();
4339 __r.__cntrl_ = __hold2.release();
4340 __r.__enable_weak_this(__r.__ptr_);
4341 return __r;
4342}
4343
4344template<class _Tp>
4345template<class _A0, class _A1>
4346shared_ptr<_Tp>
4347shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4348{
4349 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4350 typedef allocator<_CntrlBlk> _Alloc2;
4351 typedef __allocator_destructor<_Alloc2> _D2;
4352 _Alloc2 __alloc2;
4353 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4354 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4355 shared_ptr<_Tp> __r;
4356 __r.__ptr_ = __hold2.get()->get();
4357 __r.__cntrl_ = __hold2.release();
4358 __r.__enable_weak_this(__r.__ptr_);
4359 return __r;
4360}
4361
4362template<class _Tp>
4363template<class _A0, class _A1, class _A2>
4364shared_ptr<_Tp>
4365shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4366{
4367 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4368 typedef allocator<_CntrlBlk> _Alloc2;
4369 typedef __allocator_destructor<_Alloc2> _D2;
4370 _Alloc2 __alloc2;
4371 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4372 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4373 shared_ptr<_Tp> __r;
4374 __r.__ptr_ = __hold2.get()->get();
4375 __r.__cntrl_ = __hold2.release();
4376 __r.__enable_weak_this(__r.__ptr_);
4377 return __r;
4378}
4379
4380template<class _Tp>
4381template<class _Alloc>
4382shared_ptr<_Tp>
4383shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4384{
4385 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4386 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4387 typedef __allocator_destructor<_Alloc2> _D2;
4388 _Alloc2 __alloc2(__a);
4389 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4390 ::new(__hold2.get()) _CntrlBlk(__a);
4391 shared_ptr<_Tp> __r;
4392 __r.__ptr_ = __hold2.get()->get();
4393 __r.__cntrl_ = __hold2.release();
4394 __r.__enable_weak_this(__r.__ptr_);
4395 return __r;
4396}
4397
4398template<class _Tp>
4399template<class _Alloc, class _A0>
4400shared_ptr<_Tp>
4401shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4402{
4403 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4404 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4405 typedef __allocator_destructor<_Alloc2> _D2;
4406 _Alloc2 __alloc2(__a);
4407 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4408 ::new(__hold2.get()) _CntrlBlk(__a, __a0);
4409 shared_ptr<_Tp> __r;
4410 __r.__ptr_ = __hold2.get()->get();
4411 __r.__cntrl_ = __hold2.release();
4412 __r.__enable_weak_this(__r.__ptr_);
4413 return __r;
4414}
4415
4416template<class _Tp>
4417template<class _Alloc, class _A0, class _A1>
4418shared_ptr<_Tp>
4419shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4420{
4421 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4422 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4423 typedef __allocator_destructor<_Alloc2> _D2;
4424 _Alloc2 __alloc2(__a);
4425 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4426 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
4427 shared_ptr<_Tp> __r;
4428 __r.__ptr_ = __hold2.get()->get();
4429 __r.__cntrl_ = __hold2.release();
4430 __r.__enable_weak_this(__r.__ptr_);
4431 return __r;
4432}
4433
4434template<class _Tp>
4435template<class _Alloc, class _A0, class _A1, class _A2>
4436shared_ptr<_Tp>
4437shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4438{
4439 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4440 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4441 typedef __allocator_destructor<_Alloc2> _D2;
4442 _Alloc2 __alloc2(__a);
4443 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4444 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
4445 shared_ptr<_Tp> __r;
4446 __r.__ptr_ = __hold2.get()->get();
4447 __r.__cntrl_ = __hold2.release();
4448 __r.__enable_weak_this(__r.__ptr_);
4449 return __r;
4450}
4451
4452#endif // _LIBCPP_HAS_NO_VARIADICS
4453
4454template<class _Tp>
4455shared_ptr<_Tp>::~shared_ptr()
4456{
4457 if (__cntrl_)
4458 __cntrl_->__release_shared();
4459}
4460
4461template<class _Tp>
4462inline _LIBCPP_INLINE_VISIBILITY
4463shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004464shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004465{
4466 shared_ptr(__r).swap(*this);
4467 return *this;
4468}
4469
4470template<class _Tp>
4471template<class _Yp>
4472inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004473typename enable_if
4474<
4475 is_convertible<_Yp*, _Tp*>::value,
4476 shared_ptr<_Tp>&
4477>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004478shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004479{
4480 shared_ptr(__r).swap(*this);
4481 return *this;
4482}
4483
Howard Hinnant73d21a42010-09-04 23:28:19 +00004484#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004485
4486template<class _Tp>
4487inline _LIBCPP_INLINE_VISIBILITY
4488shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004489shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004490{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004491 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004492 return *this;
4493}
4494
4495template<class _Tp>
4496template<class _Yp>
4497inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004498typename enable_if
4499<
4500 is_convertible<_Yp*, _Tp*>::value,
4501 shared_ptr<_Tp>&
4502>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004503shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4504{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004505 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004506 return *this;
4507}
4508
4509template<class _Tp>
4510template<class _Yp>
4511inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004512typename enable_if
4513<
4514 !is_array<_Yp>::value &&
4515 is_convertible<_Yp*, _Tp*>::value,
4516 shared_ptr<_Tp>&
4517>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004518shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4519{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004520 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004521 return *this;
4522}
4523
4524template<class _Tp>
4525template <class _Yp, class _Dp>
4526inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004527typename enable_if
4528<
4529 !is_array<_Yp>::value &&
4530 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4531 shared_ptr<_Tp>&
4532>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004533shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4534{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004535 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004536 return *this;
4537}
4538
Howard Hinnant73d21a42010-09-04 23:28:19 +00004539#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004540
4541template<class _Tp>
4542template<class _Yp>
4543inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004544typename enable_if
4545<
4546 !is_array<_Yp>::value &&
4547 is_convertible<_Yp*, _Tp*>::value,
4548 shared_ptr<_Tp>&
4549>::type
Howard Hinnant324bb032010-08-22 00:02:43 +00004550shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004551{
4552 shared_ptr(__r).swap(*this);
4553 return *this;
4554}
4555
4556template<class _Tp>
4557template <class _Yp, class _Dp>
4558inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004559typename enable_if
4560<
4561 !is_array<_Yp>::value &&
4562 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4563 shared_ptr<_Tp>&
4564>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004565shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4566{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004567 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004568 return *this;
4569}
4570
Howard Hinnant73d21a42010-09-04 23:28:19 +00004571#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004572
4573template<class _Tp>
4574inline _LIBCPP_INLINE_VISIBILITY
4575void
Howard Hinnant1694d232011-05-28 14:41:13 +00004576shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004577{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004578 _VSTD::swap(__ptr_, __r.__ptr_);
4579 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004580}
4581
4582template<class _Tp>
4583inline _LIBCPP_INLINE_VISIBILITY
4584void
Howard Hinnant1694d232011-05-28 14:41:13 +00004585shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004586{
4587 shared_ptr().swap(*this);
4588}
4589
4590template<class _Tp>
4591template<class _Yp>
4592inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004593typename enable_if
4594<
4595 is_convertible<_Yp*, _Tp*>::value,
4596 void
4597>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004598shared_ptr<_Tp>::reset(_Yp* __p)
4599{
4600 shared_ptr(__p).swap(*this);
4601}
4602
4603template<class _Tp>
4604template<class _Yp, class _Dp>
4605inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004606typename enable_if
4607<
4608 is_convertible<_Yp*, _Tp*>::value,
4609 void
4610>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004611shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4612{
4613 shared_ptr(__p, __d).swap(*this);
4614}
4615
4616template<class _Tp>
4617template<class _Yp, class _Dp, class _Alloc>
4618inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004619typename enable_if
4620<
4621 is_convertible<_Yp*, _Tp*>::value,
4622 void
4623>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004624shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4625{
4626 shared_ptr(__p, __d, __a).swap(*this);
4627}
4628
4629#ifndef _LIBCPP_HAS_NO_VARIADICS
4630
Howard Hinnant324bb032010-08-22 00:02:43 +00004631template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004632inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004633typename enable_if
4634<
4635 !is_array<_Tp>::value,
4636 shared_ptr<_Tp>
4637>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004638make_shared(_Args&& ...__args)
4639{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004640 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004641}
4642
Howard Hinnant324bb032010-08-22 00:02:43 +00004643template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004644inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004645typename enable_if
4646<
4647 !is_array<_Tp>::value,
4648 shared_ptr<_Tp>
4649>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004650allocate_shared(const _Alloc& __a, _Args&& ...__args)
4651{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004652 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004653}
4654
4655#else // _LIBCPP_HAS_NO_VARIADICS
4656
4657template<class _Tp>
4658inline _LIBCPP_INLINE_VISIBILITY
4659shared_ptr<_Tp>
4660make_shared()
4661{
4662 return shared_ptr<_Tp>::make_shared();
4663}
4664
4665template<class _Tp, class _A0>
4666inline _LIBCPP_INLINE_VISIBILITY
4667shared_ptr<_Tp>
4668make_shared(_A0& __a0)
4669{
4670 return shared_ptr<_Tp>::make_shared(__a0);
4671}
4672
4673template<class _Tp, class _A0, class _A1>
4674inline _LIBCPP_INLINE_VISIBILITY
4675shared_ptr<_Tp>
4676make_shared(_A0& __a0, _A1& __a1)
4677{
4678 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4679}
4680
Howard Hinnant324bb032010-08-22 00:02:43 +00004681template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004682inline _LIBCPP_INLINE_VISIBILITY
4683shared_ptr<_Tp>
4684make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4685{
4686 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4687}
4688
4689template<class _Tp, class _Alloc>
4690inline _LIBCPP_INLINE_VISIBILITY
4691shared_ptr<_Tp>
4692allocate_shared(const _Alloc& __a)
4693{
4694 return shared_ptr<_Tp>::allocate_shared(__a);
4695}
4696
4697template<class _Tp, class _Alloc, class _A0>
4698inline _LIBCPP_INLINE_VISIBILITY
4699shared_ptr<_Tp>
4700allocate_shared(const _Alloc& __a, _A0& __a0)
4701{
4702 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4703}
4704
4705template<class _Tp, class _Alloc, class _A0, class _A1>
4706inline _LIBCPP_INLINE_VISIBILITY
4707shared_ptr<_Tp>
4708allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4709{
4710 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4711}
4712
4713template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4714inline _LIBCPP_INLINE_VISIBILITY
4715shared_ptr<_Tp>
4716allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4717{
4718 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4719}
4720
4721#endif // _LIBCPP_HAS_NO_VARIADICS
4722
4723template<class _Tp, class _Up>
4724inline _LIBCPP_INLINE_VISIBILITY
4725bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004726operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004727{
4728 return __x.get() == __y.get();
4729}
4730
4731template<class _Tp, class _Up>
4732inline _LIBCPP_INLINE_VISIBILITY
4733bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004734operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004735{
4736 return !(__x == __y);
4737}
4738
4739template<class _Tp, class _Up>
4740inline _LIBCPP_INLINE_VISIBILITY
4741bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004742operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004743{
Howard Hinnant3fadda32012-02-21 21:02:58 +00004744 typedef typename common_type<_Tp*, _Up*>::type _V;
4745 return less<_V>()(__x.get(), __y.get());
4746}
4747
4748template<class _Tp, class _Up>
4749inline _LIBCPP_INLINE_VISIBILITY
4750bool
4751operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4752{
4753 return __y < __x;
4754}
4755
4756template<class _Tp, class _Up>
4757inline _LIBCPP_INLINE_VISIBILITY
4758bool
4759operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4760{
4761 return !(__y < __x);
4762}
4763
4764template<class _Tp, class _Up>
4765inline _LIBCPP_INLINE_VISIBILITY
4766bool
4767operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4768{
4769 return !(__x < __y);
4770}
4771
4772template<class _Tp>
4773inline _LIBCPP_INLINE_VISIBILITY
4774bool
4775operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4776{
4777 return !__x;
4778}
4779
4780template<class _Tp>
4781inline _LIBCPP_INLINE_VISIBILITY
4782bool
4783operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4784{
4785 return !__x;
4786}
4787
4788template<class _Tp>
4789inline _LIBCPP_INLINE_VISIBILITY
4790bool
4791operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4792{
4793 return static_cast<bool>(__x);
4794}
4795
4796template<class _Tp>
4797inline _LIBCPP_INLINE_VISIBILITY
4798bool
4799operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4800{
4801 return static_cast<bool>(__x);
4802}
4803
4804template<class _Tp>
4805inline _LIBCPP_INLINE_VISIBILITY
4806bool
4807operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4808{
4809 return less<_Tp*>()(__x.get(), nullptr);
4810}
4811
4812template<class _Tp>
4813inline _LIBCPP_INLINE_VISIBILITY
4814bool
4815operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4816{
4817 return less<_Tp*>()(nullptr, __x.get());
4818}
4819
4820template<class _Tp>
4821inline _LIBCPP_INLINE_VISIBILITY
4822bool
4823operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4824{
4825 return nullptr < __x;
4826}
4827
4828template<class _Tp>
4829inline _LIBCPP_INLINE_VISIBILITY
4830bool
4831operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4832{
4833 return __x < nullptr;
4834}
4835
4836template<class _Tp>
4837inline _LIBCPP_INLINE_VISIBILITY
4838bool
4839operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4840{
4841 return !(nullptr < __x);
4842}
4843
4844template<class _Tp>
4845inline _LIBCPP_INLINE_VISIBILITY
4846bool
4847operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4848{
4849 return !(__x < nullptr);
4850}
4851
4852template<class _Tp>
4853inline _LIBCPP_INLINE_VISIBILITY
4854bool
4855operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4856{
4857 return !(__x < nullptr);
4858}
4859
4860template<class _Tp>
4861inline _LIBCPP_INLINE_VISIBILITY
4862bool
4863operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4864{
4865 return !(nullptr < __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004866}
4867
4868template<class _Tp>
4869inline _LIBCPP_INLINE_VISIBILITY
4870void
Howard Hinnant1694d232011-05-28 14:41:13 +00004871swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004872{
4873 __x.swap(__y);
4874}
4875
4876template<class _Tp, class _Up>
4877inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004878typename enable_if
4879<
4880 !is_array<_Tp>::value && !is_array<_Up>::value,
4881 shared_ptr<_Tp>
4882>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004883static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004884{
4885 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4886}
4887
4888template<class _Tp, class _Up>
4889inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004890typename enable_if
4891<
4892 !is_array<_Tp>::value && !is_array<_Up>::value,
4893 shared_ptr<_Tp>
4894>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004895dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004896{
4897 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4898 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4899}
4900
4901template<class _Tp, class _Up>
Howard Hinnant57199402012-01-02 17:56:02 +00004902typename enable_if
4903<
4904 is_array<_Tp>::value == is_array<_Up>::value,
4905 shared_ptr<_Tp>
4906>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004907const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004908{
Howard Hinnant57199402012-01-02 17:56:02 +00004909 typedef typename remove_extent<_Tp>::type _RTp;
4910 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004911}
4912
Howard Hinnantd4444702010-08-11 17:04:31 +00004913#ifndef _LIBCPP_NO_RTTI
4914
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004915template<class _Dp, class _Tp>
4916inline _LIBCPP_INLINE_VISIBILITY
4917_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00004918get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004919{
4920 return __p.template __get_deleter<_Dp>();
4921}
4922
Howard Hinnant324bb032010-08-22 00:02:43 +00004923#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00004924
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004925template<class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00004926class _LIBCPP_TYPE_VIS weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004927{
Howard Hinnant324bb032010-08-22 00:02:43 +00004928public:
4929 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004930private:
4931 element_type* __ptr_;
4932 __shared_weak_count* __cntrl_;
4933
Howard Hinnant324bb032010-08-22 00:02:43 +00004934public:
Howard Hinnant46e94932012-07-07 20:56:04 +00004935 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004936 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00004937 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4938 _NOEXCEPT;
4939 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004940 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00004941 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4942 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004943
Howard Hinnant57199402012-01-02 17:56:02 +00004944#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4945 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
4946 template<class _Yp> weak_ptr(weak_ptr<_Yp>&& __r,
4947 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4948 _NOEXCEPT;
4949#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00004950 ~weak_ptr();
4951
Howard Hinnant1694d232011-05-28 14:41:13 +00004952 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00004953 template<class _Yp>
4954 typename enable_if
4955 <
4956 is_convertible<_Yp*, element_type*>::value,
4957 weak_ptr&
4958 >::type
4959 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4960
4961#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4962
4963 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4964 template<class _Yp>
4965 typename enable_if
4966 <
4967 is_convertible<_Yp*, element_type*>::value,
4968 weak_ptr&
4969 >::type
4970 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4971
4972#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4973
4974 template<class _Yp>
4975 typename enable_if
4976 <
4977 is_convertible<_Yp*, element_type*>::value,
4978 weak_ptr&
4979 >::type
4980 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004981
Howard Hinnant1694d232011-05-28 14:41:13 +00004982 void swap(weak_ptr& __r) _NOEXCEPT;
4983 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004984
Howard Hinnant82894812010-09-22 16:48:34 +00004985 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004986 long use_count() const _NOEXCEPT
4987 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00004988 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004989 bool expired() const _NOEXCEPT
4990 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4991 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00004992 template<class _Up>
4993 _LIBCPP_INLINE_VISIBILITY
4994 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004995 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004996 template<class _Up>
4997 _LIBCPP_INLINE_VISIBILITY
4998 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004999 {return __cntrl_ < __r.__cntrl_;}
5000
Howard Hinnant83eade62013-03-06 23:30:19 +00005001 template <class _Up> friend class _LIBCPP_TYPE_VIS weak_ptr;
5002 template <class _Up> friend class _LIBCPP_TYPE_VIS shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005003};
5004
5005template<class _Tp>
5006inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant46e94932012-07-07 20:56:04 +00005007_LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005008weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005009 : __ptr_(0),
5010 __cntrl_(0)
5011{
5012}
5013
5014template<class _Tp>
5015inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005016weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005017 : __ptr_(__r.__ptr_),
5018 __cntrl_(__r.__cntrl_)
5019{
5020 if (__cntrl_)
5021 __cntrl_->__add_weak();
5022}
5023
5024template<class _Tp>
5025template<class _Yp>
5026inline _LIBCPP_INLINE_VISIBILITY
5027weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005028 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005029 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005030 : __ptr_(__r.__ptr_),
5031 __cntrl_(__r.__cntrl_)
5032{
5033 if (__cntrl_)
5034 __cntrl_->__add_weak();
5035}
5036
5037template<class _Tp>
5038template<class _Yp>
5039inline _LIBCPP_INLINE_VISIBILITY
5040weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005041 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005042 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005043 : __ptr_(__r.__ptr_),
5044 __cntrl_(__r.__cntrl_)
5045{
5046 if (__cntrl_)
5047 __cntrl_->__add_weak();
5048}
5049
Howard Hinnant57199402012-01-02 17:56:02 +00005050#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5051
5052template<class _Tp>
5053inline _LIBCPP_INLINE_VISIBILITY
5054weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5055 : __ptr_(__r.__ptr_),
5056 __cntrl_(__r.__cntrl_)
5057{
5058 __r.__ptr_ = 0;
5059 __r.__cntrl_ = 0;
5060}
5061
5062template<class _Tp>
5063template<class _Yp>
5064inline _LIBCPP_INLINE_VISIBILITY
5065weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5066 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5067 _NOEXCEPT
5068 : __ptr_(__r.__ptr_),
5069 __cntrl_(__r.__cntrl_)
5070{
5071 __r.__ptr_ = 0;
5072 __r.__cntrl_ = 0;
5073}
5074
5075#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5076
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005077template<class _Tp>
5078weak_ptr<_Tp>::~weak_ptr()
5079{
5080 if (__cntrl_)
5081 __cntrl_->__release_weak();
5082}
5083
5084template<class _Tp>
5085inline _LIBCPP_INLINE_VISIBILITY
5086weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00005087weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005088{
5089 weak_ptr(__r).swap(*this);
5090 return *this;
5091}
5092
5093template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005094template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005095inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005096typename enable_if
5097<
5098 is_convertible<_Yp*, _Tp*>::value,
5099 weak_ptr<_Tp>&
5100>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005101weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005102{
5103 weak_ptr(__r).swap(*this);
5104 return *this;
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>&
5112weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5113{
5114 weak_ptr(_VSTD::move(__r)).swap(*this);
5115 return *this;
5116}
5117
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005118template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005119template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005120inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005121typename enable_if
5122<
5123 is_convertible<_Yp*, _Tp*>::value,
5124 weak_ptr<_Tp>&
5125>::type
5126weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5127{
5128 weak_ptr(_VSTD::move(__r)).swap(*this);
5129 return *this;
5130}
5131
5132#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5133
5134template<class _Tp>
5135template<class _Yp>
5136inline _LIBCPP_INLINE_VISIBILITY
5137typename enable_if
5138<
5139 is_convertible<_Yp*, _Tp*>::value,
5140 weak_ptr<_Tp>&
5141>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005142weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005143{
5144 weak_ptr(__r).swap(*this);
5145 return *this;
5146}
5147
5148template<class _Tp>
5149inline _LIBCPP_INLINE_VISIBILITY
5150void
Howard Hinnant1694d232011-05-28 14:41:13 +00005151weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005152{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005153 _VSTD::swap(__ptr_, __r.__ptr_);
5154 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005155}
5156
5157template<class _Tp>
5158inline _LIBCPP_INLINE_VISIBILITY
5159void
Howard Hinnant1694d232011-05-28 14:41:13 +00005160swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005161{
5162 __x.swap(__y);
5163}
5164
5165template<class _Tp>
5166inline _LIBCPP_INLINE_VISIBILITY
5167void
Howard Hinnant1694d232011-05-28 14:41:13 +00005168weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005169{
5170 weak_ptr().swap(*this);
5171}
5172
5173template<class _Tp>
5174template<class _Yp>
5175shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5176 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
5177 : __ptr_(__r.__ptr_),
5178 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5179{
5180 if (__cntrl_ == 0)
5181#ifndef _LIBCPP_NO_EXCEPTIONS
5182 throw bad_weak_ptr();
5183#else
5184 assert(!"bad_weak_ptr");
5185#endif
5186}
5187
5188template<class _Tp>
5189shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00005190weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005191{
5192 shared_ptr<_Tp> __r;
5193 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5194 if (__r.__cntrl_)
5195 __r.__ptr_ = __ptr_;
5196 return __r;
5197}
5198
Howard Hinnant324bb032010-08-22 00:02:43 +00005199template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005200
5201template <class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00005202struct _LIBCPP_TYPE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005203 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00005204{
5205 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005206 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005207 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5208 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005210 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5211 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005212 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005213 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5214 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00005215};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005216
5217template <class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00005218struct _LIBCPP_TYPE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005219 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5220{
Howard Hinnant324bb032010-08-22 00:02:43 +00005221 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005222 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005223 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5224 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005225 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005226 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5227 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005228 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005229 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5230 {return __x.owner_before(__y);}
5231};
5232
5233template<class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00005234class _LIBCPP_TYPE_VIS enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005235{
5236 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00005237protected:
Howard Hinnant46e94932012-07-07 20:56:04 +00005238 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant1694d232011-05-28 14:41:13 +00005239 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005241 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005243 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5244 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00005245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005246 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00005247public:
Howard Hinnant82894812010-09-22 16:48:34 +00005248 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005249 shared_ptr<_Tp> shared_from_this()
5250 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00005251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005252 shared_ptr<_Tp const> shared_from_this() const
5253 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005254
5255 template <class _Up> friend class shared_ptr;
5256};
5257
Howard Hinnant21aefc32010-06-03 16:42:57 +00005258template <class _Tp>
Howard Hinnant83eade62013-03-06 23:30:19 +00005259struct _LIBCPP_TYPE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00005260{
5261 typedef shared_ptr<_Tp> argument_type;
5262 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005263 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005264 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00005265 {
5266 return hash<_Tp*>()(__ptr.get());
5267 }
5268};
5269
Howard Hinnant99968442011-11-29 18:15:50 +00005270template<class _CharT, class _Traits, class _Yp>
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005271inline _LIBCPP_INLINE_VISIBILITY
5272basic_ostream<_CharT, _Traits>&
Howard Hinnant99968442011-11-29 18:15:50 +00005273operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005274
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005275#if __has_feature(cxx_atomic)
5276
5277class __sp_mut
5278{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005279 void* __lx;
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005280public:
5281 void lock() _NOEXCEPT;
5282 void unlock() _NOEXCEPT;
5283
5284private:
5285 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5286 __sp_mut(const __sp_mut&);
5287 __sp_mut& operator=(const __sp_mut&);
5288
Howard Hinnant83eade62013-03-06 23:30:19 +00005289 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005290};
5291
Howard Hinnant83eade62013-03-06 23:30:19 +00005292_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant5fec82d2012-07-30 01:40:57 +00005293
5294template <class _Tp>
5295inline _LIBCPP_INLINE_VISIBILITY
5296bool
5297atomic_is_lock_free(const shared_ptr<_Tp>*)
5298{
5299 return false;
5300}
5301
5302template <class _Tp>
5303shared_ptr<_Tp>
5304atomic_load(const shared_ptr<_Tp>* __p)
5305{
5306 __sp_mut& __m = __get_sp_mut(__p);
5307 __m.lock();
5308 shared_ptr<_Tp> __q = *__p;
5309 __m.unlock();
5310 return __q;
5311}
5312
5313template <class _Tp>
5314inline _LIBCPP_INLINE_VISIBILITY
5315shared_ptr<_Tp>
5316atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5317{
5318 return atomic_load(__p);
5319}
5320
5321template <class _Tp>
5322void
5323atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5324{
5325 __sp_mut& __m = __get_sp_mut(__p);
5326 __m.lock();
5327 __p->swap(__r);
5328 __m.unlock();
5329}
5330
5331template <class _Tp>
5332inline _LIBCPP_INLINE_VISIBILITY
5333void
5334atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5335{
5336 atomic_store(__p, __r);
5337}
5338
5339template <class _Tp>
5340shared_ptr<_Tp>
5341atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5342{
5343 __sp_mut& __m = __get_sp_mut(__p);
5344 __m.lock();
5345 __p->swap(__r);
5346 __m.unlock();
5347 return __r;
5348}
5349
5350template <class _Tp>
5351inline _LIBCPP_INLINE_VISIBILITY
5352shared_ptr<_Tp>
5353atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5354{
5355 return atomic_exchange(__p, __r);
5356}
5357
5358template <class _Tp>
5359bool
5360atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5361{
5362 __sp_mut& __m = __get_sp_mut(__p);
5363 __m.lock();
5364 if (__p->__owner_equivalent(*__v))
5365 {
5366 *__p = __w;
5367 __m.unlock();
5368 return true;
5369 }
5370 *__v = *__p;
5371 __m.unlock();
5372 return false;
5373}
5374
5375template <class _Tp>
5376inline _LIBCPP_INLINE_VISIBILITY
5377bool
5378atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5379{
5380 return atomic_compare_exchange_strong(__p, __v, __w);
5381}
5382
5383template <class _Tp>
5384inline _LIBCPP_INLINE_VISIBILITY
5385bool
5386atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5387 shared_ptr<_Tp> __w, memory_order, memory_order)
5388{
5389 return atomic_compare_exchange_strong(__p, __v, __w);
5390}
5391
5392template <class _Tp>
5393inline _LIBCPP_INLINE_VISIBILITY
5394bool
5395atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5396 shared_ptr<_Tp> __w, memory_order, memory_order)
5397{
5398 return atomic_compare_exchange_weak(__p, __v, __w);
5399}
5400
5401#endif // __has_feature(cxx_atomic)
5402
Howard Hinnant324bb032010-08-22 00:02:43 +00005403//enum class
Howard Hinnant83eade62013-03-06 23:30:19 +00005404struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005405{
Howard Hinnant9c0df142012-10-30 19:06:59 +00005406 enum __lx
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005407 {
5408 relaxed,
5409 preferred,
5410 strict
5411 };
5412
Howard Hinnant9c0df142012-10-30 19:06:59 +00005413 __lx __v_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005414
Howard Hinnant82894812010-09-22 16:48:34 +00005415 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9c0df142012-10-30 19:06:59 +00005416 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00005417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005418 operator int() const {return __v_;}
5419};
5420
5421void declare_reachable(void* __p);
5422void declare_no_pointers(char* __p, size_t __n);
5423void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnant1694d232011-05-28 14:41:13 +00005424pointer_safety get_pointer_safety() _NOEXCEPT;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00005425void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005426
5427template <class _Tp>
5428inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005429_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005430undeclare_reachable(_Tp* __p)
5431{
5432 return static_cast<_Tp*>(__undeclare_reachable(__p));
5433}
5434
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00005435void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005436
5437_LIBCPP_END_NAMESPACE_STD
5438
5439#endif // _LIBCPP_MEMORY