blob: 0e14275c114f539be5bb6ddb16826b3850d1c1f1 [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 Hinnantbc8d3f92010-05-11 19:42:16 +0000600#if defined(_LIBCPP_NO_EXCEPTIONS)
601 #include <cassert>
602#endif
603
Howard Hinnant66c6f972011-11-29 16:45:27 +0000604#include <__undef_min_max>
605
Howard Hinnant08e17472011-10-17 20:05:10 +0000606#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000607#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000608#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000609
610_LIBCPP_BEGIN_NAMESPACE_STD
611
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000612// addressof
613
614template <class _Tp>
615inline _LIBCPP_INLINE_VISIBILITY
616_Tp*
Howard Hinnant1694d232011-05-28 14:41:13 +0000617addressof(_Tp& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000618{
619 return (_Tp*)&(char&)__x;
620}
621
Douglas Gregor35d2fcf2011-06-22 22:17:44 +0000622#if defined(_LIBCPP_HAS_OBJC_ARC) && !defined(_LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF)
623// Objective-C++ Automatic Reference Counting uses qualified pointers
624// that require special addressof() signatures. When
625// _LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF is defined, the compiler
626// itself is providing these definitions. Otherwise, we provide them.
627template <class _Tp>
628inline _LIBCPP_INLINE_VISIBILITY
629__strong _Tp*
630addressof(__strong _Tp& __x) _NOEXCEPT
631{
632 return &__x;
633}
634
635#ifdef _LIBCPP_HAS_OBJC_ARC_WEAK
636template <class _Tp>
637inline _LIBCPP_INLINE_VISIBILITY
638__weak _Tp*
639addressof(__weak _Tp& __x) _NOEXCEPT
640{
641 return &__x;
642}
643#endif
644
645template <class _Tp>
646inline _LIBCPP_INLINE_VISIBILITY
647__autoreleasing _Tp*
648addressof(__autoreleasing _Tp& __x) _NOEXCEPT
649{
650 return &__x;
651}
652
653template <class _Tp>
654inline _LIBCPP_INLINE_VISIBILITY
655__unsafe_unretained _Tp*
656addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT
657{
658 return &__x;
659}
660#endif
661
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000662template <class _Tp> class allocator;
663
664template <>
Howard Hinnant82894812010-09-22 16:48:34 +0000665class _LIBCPP_VISIBLE allocator<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000666{
667public:
668 typedef void* pointer;
669 typedef const void* const_pointer;
670 typedef void value_type;
671
672 template <class _Up> struct rebind {typedef allocator<_Up> other;};
673};
674
Howard Hinnanta1877872012-01-19 23:15:22 +0000675template <>
676class _LIBCPP_VISIBLE allocator<const void>
677{
678public:
679 typedef const void* pointer;
680 typedef const void* const_pointer;
681 typedef const void value_type;
682
683 template <class _Up> struct rebind {typedef allocator<_Up> other;};
684};
685
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000686// pointer_traits
687
688template <class _Tp>
689struct __has_element_type
690{
691private:
692 struct __two {char _; char __;};
693 template <class _Up> static __two __test(...);
694 template <class _Up> static char __test(typename _Up::element_type* = 0);
695public:
696 static const bool value = sizeof(__test<_Tp>(0)) == 1;
697};
698
699template <class _Ptr, bool = __has_element_type<_Ptr>::value>
700struct __pointer_traits_element_type;
701
702template <class _Ptr>
703struct __pointer_traits_element_type<_Ptr, true>
704{
705 typedef typename _Ptr::element_type type;
706};
707
708#ifndef _LIBCPP_HAS_NO_VARIADICS
709
710template <template <class, class...> class _Sp, class _Tp, class ..._Args>
711struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
712{
713 typedef typename _Sp<_Tp, _Args...>::element_type type;
714};
715
716template <template <class, class...> class _Sp, class _Tp, class ..._Args>
717struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
718{
719 typedef _Tp type;
720};
721
Howard Hinnant324bb032010-08-22 00:02:43 +0000722#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723
724template <template <class> class _Sp, class _Tp>
725struct __pointer_traits_element_type<_Sp<_Tp>, true>
726{
727 typedef typename _Sp<_Tp>::element_type type;
728};
729
730template <template <class> class _Sp, class _Tp>
731struct __pointer_traits_element_type<_Sp<_Tp>, false>
732{
733 typedef _Tp type;
734};
735
736template <template <class, class> class _Sp, class _Tp, class _A0>
737struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
738{
739 typedef typename _Sp<_Tp, _A0>::element_type type;
740};
741
742template <template <class, class> class _Sp, class _Tp, class _A0>
743struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
744{
745 typedef _Tp type;
746};
747
748template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
749struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
750{
751 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
752};
753
754template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
755struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
756{
757 typedef _Tp type;
758};
759
760template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
761 class _A1, class _A2>
762struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
763{
764 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
765};
766
767template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
768 class _A1, class _A2>
769struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
770{
771 typedef _Tp type;
772};
773
Howard Hinnant324bb032010-08-22 00:02:43 +0000774#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000775
776template <class _Tp>
777struct __has_difference_type
778{
779private:
780 struct __two {char _; char __;};
781 template <class _Up> static __two __test(...);
782 template <class _Up> static char __test(typename _Up::difference_type* = 0);
783public:
784 static const bool value = sizeof(__test<_Tp>(0)) == 1;
785};
786
787template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
788struct __pointer_traits_difference_type
789{
790 typedef ptrdiff_t type;
791};
792
793template <class _Ptr>
794struct __pointer_traits_difference_type<_Ptr, true>
795{
796 typedef typename _Ptr::difference_type type;
797};
798
799template <class _Tp, class _Up>
800struct __has_rebind
801{
802private:
803 struct __two {char _; char __;};
804 template <class _Xp> static __two __test(...);
805 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
806public:
807 static const bool value = sizeof(__test<_Tp>(0)) == 1;
808};
809
810template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
811struct __pointer_traits_rebind
812{
813#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
814 typedef typename _Tp::template rebind<_Up> type;
815#else
816 typedef typename _Tp::template rebind<_Up>::other type;
817#endif
818};
819
820#ifndef _LIBCPP_HAS_NO_VARIADICS
821
822template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
823struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
824{
825#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
826 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
827#else
828 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
829#endif
830};
831
832template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
833struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
834{
835 typedef _Sp<_Up, _Args...> type;
836};
837
Howard Hinnant324bb032010-08-22 00:02:43 +0000838#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000839
840template <template <class> class _Sp, class _Tp, class _Up>
841struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
842{
843#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
844 typedef typename _Sp<_Tp>::template rebind<_Up> type;
845#else
846 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
847#endif
848};
849
850template <template <class> class _Sp, class _Tp, class _Up>
851struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
852{
853 typedef _Sp<_Up> type;
854};
855
856template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
857struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
858{
859#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
860 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
861#else
862 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
863#endif
864};
865
866template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
867struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
868{
869 typedef _Sp<_Up, _A0> type;
870};
871
872template <template <class, class, class> class _Sp, class _Tp, class _A0,
873 class _A1, class _Up>
874struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
875{
876#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
877 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
878#else
879 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
880#endif
881};
882
883template <template <class, class, class> class _Sp, class _Tp, class _A0,
884 class _A1, class _Up>
885struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
886{
887 typedef _Sp<_Up, _A0, _A1> type;
888};
889
890template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
891 class _A1, class _A2, class _Up>
892struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
893{
894#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
895 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
896#else
897 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
898#endif
899};
900
901template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
902 class _A1, class _A2, class _Up>
903struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
904{
905 typedef _Sp<_Up, _A0, _A1, _A2> type;
906};
907
Howard Hinnant324bb032010-08-22 00:02:43 +0000908#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000909
910template <class _Ptr>
Howard Hinnant82894812010-09-22 16:48:34 +0000911struct _LIBCPP_VISIBLE pointer_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000912{
913 typedef _Ptr pointer;
914 typedef typename __pointer_traits_element_type<pointer>::type element_type;
915 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
916
917#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant6b41c602011-05-11 20:21:19 +0000918 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000919#else
920 template <class _Up> struct rebind
921 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant324bb032010-08-22 00:02:43 +0000922#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000923
924private:
925 struct __nat {};
926public:
Howard Hinnant82894812010-09-22 16:48:34 +0000927 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000928 static pointer pointer_to(typename conditional<is_void<element_type>::value,
929 __nat, element_type>::type& __r)
930 {return pointer::pointer_to(__r);}
931};
932
933template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +0000934struct _LIBCPP_VISIBLE pointer_traits<_Tp*>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000935{
936 typedef _Tp* pointer;
937 typedef _Tp element_type;
938 typedef ptrdiff_t difference_type;
939
940#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
941 template <class _Up> using rebind = _Up*;
942#else
943 template <class _Up> struct rebind {typedef _Up* other;};
944#endif
945
946private:
947 struct __nat {};
948public:
Howard Hinnant82894812010-09-22 16:48:34 +0000949 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000950 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant1694d232011-05-28 14:41:13 +0000951 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000952 {return _VSTD::addressof(__r);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000953};
954
955// allocator_traits
956
957namespace __has_pointer_type_imp
958{
959 template <class _Up> static __two test(...);
960 template <class _Up> static char test(typename _Up::pointer* = 0);
961}
962
963template <class _Tp>
964struct __has_pointer_type
965 : public integral_constant<bool, sizeof(__has_pointer_type_imp::test<_Tp>(0)) == 1>
966{
967};
968
969namespace __pointer_type_imp
970{
971
972template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
973struct __pointer_type
974{
975 typedef typename _Dp::pointer type;
976};
977
978template <class _Tp, class _Dp>
979struct __pointer_type<_Tp, _Dp, false>
980{
981 typedef _Tp* type;
982};
983
Howard Hinnant47761072010-11-18 01:40:00 +0000984} // __pointer_type_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000985
986template <class _Tp, class _Dp>
987struct __pointer_type
988{
989 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
990};
991
992template <class _Tp>
993struct __has_const_pointer
994{
995private:
996 struct __two {char _; char __;};
997 template <class _Up> static __two __test(...);
998 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
999public:
1000 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1001};
1002
1003template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1004struct __const_pointer
1005{
1006 typedef typename _Alloc::const_pointer type;
1007};
1008
1009template <class _Tp, class _Ptr, class _Alloc>
1010struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1011{
1012#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1013 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1014#else
1015 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1016#endif
1017};
1018
1019template <class _Tp>
1020struct __has_void_pointer
1021{
1022private:
1023 struct __two {char _; char __;};
1024 template <class _Up> static __two __test(...);
1025 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1026public:
1027 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1028};
1029
1030template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1031struct __void_pointer
1032{
1033 typedef typename _Alloc::void_pointer type;
1034};
1035
1036template <class _Ptr, class _Alloc>
1037struct __void_pointer<_Ptr, _Alloc, false>
1038{
1039#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1040 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1041#else
1042 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1043#endif
1044};
1045
1046template <class _Tp>
1047struct __has_const_void_pointer
1048{
1049private:
1050 struct __two {char _; char __;};
1051 template <class _Up> static __two __test(...);
1052 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1053public:
1054 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1055};
1056
1057template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1058struct __const_void_pointer
1059{
1060 typedef typename _Alloc::const_void_pointer type;
1061};
1062
1063template <class _Ptr, class _Alloc>
1064struct __const_void_pointer<_Ptr, _Alloc, false>
1065{
1066#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1067 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1068#else
1069 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1070#endif
1071};
1072
Howard Hinnant99968442011-11-29 18:15:50 +00001073template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001075_Tp*
1076__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001077{
1078 return __p;
1079}
1080
1081template <class _Pointer>
1082inline _LIBCPP_INLINE_VISIBILITY
1083typename pointer_traits<_Pointer>::element_type*
Howard Hinnant1694d232011-05-28 14:41:13 +00001084__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001085{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001086 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001087}
1088
1089template <class _Tp>
1090struct __has_size_type
1091{
1092private:
1093 struct __two {char _; char __;};
1094 template <class _Up> static __two __test(...);
1095 template <class _Up> static char __test(typename _Up::size_type* = 0);
1096public:
1097 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1098};
1099
Howard Hinnant47761072010-11-18 01:40:00 +00001100template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001101struct __size_type
1102{
Howard Hinnant47761072010-11-18 01:40:00 +00001103 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001104};
1105
Howard Hinnant47761072010-11-18 01:40:00 +00001106template <class _Alloc, class _DiffType>
1107struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001108{
1109 typedef typename _Alloc::size_type type;
1110};
1111
1112template <class _Tp>
1113struct __has_propagate_on_container_copy_assignment
1114{
1115private:
1116 struct __two {char _; char __;};
1117 template <class _Up> static __two __test(...);
1118 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1119public:
1120 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1121};
1122
1123template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1124struct __propagate_on_container_copy_assignment
1125{
1126 typedef false_type type;
1127};
1128
1129template <class _Alloc>
1130struct __propagate_on_container_copy_assignment<_Alloc, true>
1131{
1132 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1133};
1134
1135template <class _Tp>
1136struct __has_propagate_on_container_move_assignment
1137{
1138private:
1139 struct __two {char _; char __;};
1140 template <class _Up> static __two __test(...);
1141 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1142public:
1143 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1144};
1145
1146template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1147struct __propagate_on_container_move_assignment
1148{
1149 typedef false_type type;
1150};
1151
1152template <class _Alloc>
1153struct __propagate_on_container_move_assignment<_Alloc, true>
1154{
1155 typedef typename _Alloc::propagate_on_container_move_assignment type;
1156};
1157
1158template <class _Tp>
1159struct __has_propagate_on_container_swap
1160{
1161private:
1162 struct __two {char _; char __;};
1163 template <class _Up> static __two __test(...);
1164 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1165public:
1166 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1167};
1168
1169template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1170struct __propagate_on_container_swap
1171{
1172 typedef false_type type;
1173};
1174
1175template <class _Alloc>
1176struct __propagate_on_container_swap<_Alloc, true>
1177{
1178 typedef typename _Alloc::propagate_on_container_swap type;
1179};
1180
1181template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1182struct __has_rebind_other
1183{
1184private:
1185 struct __two {char _; char __;};
1186 template <class _Xp> static __two __test(...);
1187 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1188public:
1189 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1190};
1191
1192template <class _Tp, class _Up>
1193struct __has_rebind_other<_Tp, _Up, false>
1194{
1195 static const bool value = false;
1196};
1197
1198template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1199struct __allocator_traits_rebind
1200{
1201 typedef typename _Tp::template rebind<_Up>::other type;
1202};
1203
1204#ifndef _LIBCPP_HAS_NO_VARIADICS
1205
1206template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1207struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1208{
1209 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1210};
1211
1212template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1213struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1214{
1215 typedef _Alloc<_Up, _Args...> type;
1216};
1217
Howard Hinnant324bb032010-08-22 00:02:43 +00001218#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001219
1220template <template <class> class _Alloc, class _Tp, class _Up>
1221struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1222{
1223 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1224};
1225
1226template <template <class> class _Alloc, class _Tp, class _Up>
1227struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1228{
1229 typedef _Alloc<_Up> type;
1230};
1231
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001232template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1233struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1234{
1235 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1236};
1237
1238template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1239struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1240{
1241 typedef _Alloc<_Up, _A0> type;
1242};
1243
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001244template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1245 class _A1, class _Up>
1246struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1247{
1248 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1249};
1250
1251template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1252 class _A1, class _Up>
1253struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1254{
1255 typedef _Alloc<_Up, _A0, _A1> type;
1256};
1257
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001258template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1259 class _A1, class _A2, class _Up>
1260struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1261{
1262 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1263};
1264
1265template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1266 class _A1, class _A2, class _Up>
1267struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1268{
1269 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1270};
1271
Howard Hinnant324bb032010-08-22 00:02:43 +00001272#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001273
1274#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1275
1276template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1277auto
1278__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1279 -> decltype(__a.allocate(__sz, __p), true_type());
1280
1281template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1282auto
1283__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1284 -> false_type;
1285
1286template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1287struct __has_allocate_hint
1288 : integral_constant<bool,
1289 is_same<
1290 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1291 declval<_SizeType>(),
1292 declval<_ConstVoidPtr>())),
1293 true_type>::value>
1294{
1295};
1296
Howard Hinnant324bb032010-08-22 00:02:43 +00001297#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001298
1299template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1300struct __has_allocate_hint
1301 : true_type
1302{
1303};
1304
Howard Hinnant324bb032010-08-22 00:02:43 +00001305#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001306
Howard Hinnant23369ee2011-07-29 21:35:53 +00001307#if !defined(_LIBCPP_HAS_NO_ADVANCED_SFINAE) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001308
1309template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnant0949eed2011-06-30 21:18:19 +00001310decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1311 _VSTD::declval<_Args>()...),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001312 true_type())
1313__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1314
1315template <class _Alloc, class _Pointer, class ..._Args>
1316false_type
1317__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1318
1319template <class _Alloc, class _Pointer, class ..._Args>
1320struct __has_construct
1321 : integral_constant<bool,
1322 is_same<
1323 decltype(__has_construct_test(declval<_Alloc>(),
1324 declval<_Pointer>(),
1325 declval<_Args>()...)),
1326 true_type>::value>
1327{
1328};
1329
1330template <class _Alloc, class _Pointer>
1331auto
1332__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1333 -> decltype(__a.destroy(__p), true_type());
1334
1335template <class _Alloc, class _Pointer>
1336auto
1337__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1338 -> false_type;
1339
1340template <class _Alloc, class _Pointer>
1341struct __has_destroy
1342 : integral_constant<bool,
1343 is_same<
1344 decltype(__has_destroy_test(declval<_Alloc>(),
1345 declval<_Pointer>())),
1346 true_type>::value>
1347{
1348};
1349
1350template <class _Alloc>
1351auto
1352__has_max_size_test(_Alloc&& __a)
1353 -> decltype(__a.max_size(), true_type());
1354
1355template <class _Alloc>
1356auto
1357__has_max_size_test(const volatile _Alloc& __a)
1358 -> false_type;
1359
1360template <class _Alloc>
1361struct __has_max_size
1362 : integral_constant<bool,
1363 is_same<
1364 decltype(__has_max_size_test(declval<_Alloc&>())),
1365 true_type>::value>
1366{
1367};
1368
1369template <class _Alloc>
1370auto
1371__has_select_on_container_copy_construction_test(_Alloc&& __a)
1372 -> decltype(__a.select_on_container_copy_construction(), true_type());
1373
1374template <class _Alloc>
1375auto
1376__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1377 -> false_type;
1378
1379template <class _Alloc>
1380struct __has_select_on_container_copy_construction
1381 : integral_constant<bool,
1382 is_same<
1383 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1384 true_type>::value>
1385{
1386};
1387
Howard Hinnant324bb032010-08-22 00:02:43 +00001388#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001389
1390#ifndef _LIBCPP_HAS_NO_VARIADICS
1391
1392template <class _Alloc, class _Pointer, class ..._Args>
1393struct __has_construct
1394 : false_type
1395{
1396};
1397
Howard Hinnant324bb032010-08-22 00:02:43 +00001398#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001399
1400template <class _Alloc, class _Pointer>
1401struct __has_destroy
1402 : false_type
1403{
1404};
1405
1406template <class _Alloc>
1407struct __has_max_size
1408 : true_type
1409{
1410};
1411
1412template <class _Alloc>
1413struct __has_select_on_container_copy_construction
1414 : false_type
1415{
1416};
1417
Howard Hinnant324bb032010-08-22 00:02:43 +00001418#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001419
Howard Hinnant47761072010-11-18 01:40:00 +00001420template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1421struct __alloc_traits_difference_type
1422{
1423 typedef typename pointer_traits<_Ptr>::difference_type type;
1424};
1425
1426template <class _Alloc, class _Ptr>
1427struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1428{
1429 typedef typename _Alloc::difference_type type;
1430};
1431
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001432template <class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001433struct _LIBCPP_VISIBLE allocator_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001434{
1435 typedef _Alloc allocator_type;
1436 typedef typename allocator_type::value_type value_type;
1437
1438 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1439 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1440 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1441 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1442
Howard Hinnant47761072010-11-18 01:40:00 +00001443 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1444 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001445
1446 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1447 propagate_on_container_copy_assignment;
1448 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1449 propagate_on_container_move_assignment;
1450 typedef typename __propagate_on_container_swap<allocator_type>::type
1451 propagate_on_container_swap;
1452
1453#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1454 template <class _Tp> using rebind_alloc =
Howard Hinnant6b41c602011-05-11 20:21:19 +00001455 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001456 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant324bb032010-08-22 00:02:43 +00001457#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001458 template <class _Tp> struct rebind_alloc
1459 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1460 template <class _Tp> struct rebind_traits
1461 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant324bb032010-08-22 00:02:43 +00001462#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001463
Howard Hinnant82894812010-09-22 16:48:34 +00001464 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001465 static pointer allocate(allocator_type& __a, size_type __n)
1466 {return __a.allocate(__n);}
Howard Hinnant82894812010-09-22 16:48:34 +00001467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001468 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1469 {return allocate(__a, __n, __hint,
1470 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1471
Howard Hinnant82894812010-09-22 16:48:34 +00001472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001473 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001474 {__a.deallocate(__p, __n);}
1475
1476#ifndef _LIBCPP_HAS_NO_VARIADICS
1477 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001478 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001479 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
1480 {__construct(__has_construct<allocator_type, pointer, _Args...>(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001481 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001482#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001483 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001484 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001485 static void construct(allocator_type& __a, _Tp* __p)
1486 {
1487 ::new ((void*)__p) _Tp();
1488 }
1489 template <class _Tp, class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00001490 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001491 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1492 {
1493 ::new ((void*)__p) _Tp(__a0);
1494 }
1495 template <class _Tp, class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00001496 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001497 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1498 const _A1& __a1)
1499 {
1500 ::new ((void*)__p) _Tp(__a0, __a1);
1501 }
1502 template <class _Tp, class _A0, class _A1, class _A2>
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 const _A1& __a1, const _A2& __a2)
1506 {
1507 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1508 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001509#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001510
1511 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001512 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001513 static void destroy(allocator_type& __a, _Tp* __p)
1514 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1515
Howard Hinnant82894812010-09-22 16:48:34 +00001516 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001517 static size_type max_size(const allocator_type& __a)
1518 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1519
Howard Hinnant82894812010-09-22 16:48:34 +00001520 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001521 static allocator_type
1522 select_on_container_copy_construction(const allocator_type& __a)
1523 {return select_on_container_copy_construction(
1524 __has_select_on_container_copy_construction<const allocator_type>(),
1525 __a);}
1526
1527private:
1528
Howard Hinnant82894812010-09-22 16:48:34 +00001529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530 static pointer allocate(allocator_type& __a, size_type __n,
1531 const_void_pointer __hint, true_type)
1532 {return __a.allocate(__n, __hint);}
Howard Hinnant82894812010-09-22 16:48:34 +00001533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001534 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnantec3773c2011-12-01 20:21:04 +00001535 const_void_pointer, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001536 {return __a.allocate(__n);}
1537
1538#ifndef _LIBCPP_HAS_NO_VARIADICS
1539 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001540 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001541 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001542 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001543 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001545 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1546 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001547 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001548 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001549#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001550
1551 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001553 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1554 {__a.destroy(__p);}
1555 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001557 static void __destroy(false_type, allocator_type&, _Tp* __p)
1558 {
1559 __p->~_Tp();
1560 }
1561
Howard Hinnant82894812010-09-22 16:48:34 +00001562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001563 static size_type __max_size(true_type, const allocator_type& __a)
1564 {return __a.max_size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001565 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001566 static size_type __max_size(false_type, const allocator_type&)
1567 {return numeric_limits<size_type>::max();}
1568
Howard Hinnant82894812010-09-22 16:48:34 +00001569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001570 static allocator_type
1571 select_on_container_copy_construction(true_type, const allocator_type& __a)
1572 {return __a.select_on_container_copy_construction();}
Howard Hinnant82894812010-09-22 16:48:34 +00001573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001574 static allocator_type
1575 select_on_container_copy_construction(false_type, const allocator_type& __a)
1576 {return __a;}
1577};
1578
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001579// allocator
1580
1581template <class _Tp>
Howard Hinnant36cdf022010-09-10 16:42:26 +00001582class _LIBCPP_VISIBLE allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001583{
1584public:
1585 typedef size_t size_type;
1586 typedef ptrdiff_t difference_type;
1587 typedef _Tp* pointer;
1588 typedef const _Tp* const_pointer;
1589 typedef _Tp& reference;
1590 typedef const _Tp& const_reference;
1591 typedef _Tp value_type;
1592
Howard Hinnant18884f42011-06-02 21:38:57 +00001593 typedef true_type propagate_on_container_move_assignment;
1594
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001595 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1596
Howard Hinnant1694d232011-05-28 14:41:13 +00001597 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1598 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1599 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001600 {return _VSTD::addressof(__x);}
Howard Hinnant1694d232011-05-28 14:41:13 +00001601 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001602 {return _VSTD::addressof(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001603 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1604 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
Howard Hinnant1694d232011-05-28 14:41:13 +00001605 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
1606 {::operator delete((void*)__p);}
1607 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1608 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001609#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001610 template <class _Up, class... _Args>
1611 _LIBCPP_INLINE_VISIBILITY
1612 void
1613 construct(_Up* __p, _Args&&... __args)
1614 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001615 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001616 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001617#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001618 _LIBCPP_INLINE_VISIBILITY
1619 void
1620 construct(pointer __p)
1621 {
1622 ::new((void*)__p) _Tp();
1623 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001624# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001625 template <class _A0>
1626 _LIBCPP_INLINE_VISIBILITY
1627 typename enable_if
1628 <
1629 !is_convertible<_A0, __rv<_A0> >::value,
1630 void
1631 >::type
1632 construct(pointer __p, _A0& __a0)
1633 {
1634 ::new((void*)__p) _Tp(__a0);
1635 }
1636 template <class _A0>
1637 _LIBCPP_INLINE_VISIBILITY
1638 typename enable_if
1639 <
1640 !is_convertible<_A0, __rv<_A0> >::value,
1641 void
1642 >::type
1643 construct(pointer __p, const _A0& __a0)
1644 {
1645 ::new((void*)__p) _Tp(__a0);
1646 }
1647 template <class _A0>
1648 _LIBCPP_INLINE_VISIBILITY
1649 typename enable_if
1650 <
1651 is_convertible<_A0, __rv<_A0> >::value,
1652 void
1653 >::type
1654 construct(pointer __p, _A0 __a0)
1655 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001656 ::new((void*)__p) _Tp(_VSTD::move(__a0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001657 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001658# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001659 template <class _A0, class _A1>
1660 _LIBCPP_INLINE_VISIBILITY
1661 void
1662 construct(pointer __p, _A0& __a0, _A1& __a1)
1663 {
1664 ::new((void*)__p) _Tp(__a0, __a1);
1665 }
1666 template <class _A0, class _A1>
1667 _LIBCPP_INLINE_VISIBILITY
1668 void
1669 construct(pointer __p, const _A0& __a0, _A1& __a1)
1670 {
1671 ::new((void*)__p) _Tp(__a0, __a1);
1672 }
1673 template <class _A0, class _A1>
1674 _LIBCPP_INLINE_VISIBILITY
1675 void
1676 construct(pointer __p, _A0& __a0, const _A1& __a1)
1677 {
1678 ::new((void*)__p) _Tp(__a0, __a1);
1679 }
1680 template <class _A0, class _A1>
1681 _LIBCPP_INLINE_VISIBILITY
1682 void
1683 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1684 {
1685 ::new((void*)__p) _Tp(__a0, __a1);
1686 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001687#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001688 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1689};
1690
Howard Hinnant57199402012-01-02 17:56:02 +00001691template <class _Tp>
1692class _LIBCPP_VISIBLE allocator<const _Tp>
1693{
1694public:
1695 typedef size_t size_type;
1696 typedef ptrdiff_t difference_type;
1697 typedef const _Tp* pointer;
1698 typedef const _Tp* const_pointer;
1699 typedef const _Tp& reference;
1700 typedef const _Tp& const_reference;
1701 typedef _Tp value_type;
1702
1703 typedef true_type propagate_on_container_move_assignment;
1704
1705 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1706
1707 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1708 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1709 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1710 {return _VSTD::addressof(__x);}
1711 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1712 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
1713 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
1714 {::operator delete((void*)__p);}
1715 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1716 {return size_type(~0) / sizeof(_Tp);}
1717#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1718 template <class _Up, class... _Args>
1719 _LIBCPP_INLINE_VISIBILITY
1720 void
1721 construct(_Up* __p, _Args&&... __args)
1722 {
1723 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1724 }
1725#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1726 _LIBCPP_INLINE_VISIBILITY
1727 void
1728 construct(pointer __p)
1729 {
1730 ::new((void*)__p) _Tp();
1731 }
1732# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1733 template <class _A0>
1734 _LIBCPP_INLINE_VISIBILITY
1735 typename enable_if
1736 <
1737 !is_convertible<_A0, __rv<_A0> >::value,
1738 void
1739 >::type
1740 construct(pointer __p, _A0& __a0)
1741 {
1742 ::new((void*)__p) _Tp(__a0);
1743 }
1744 template <class _A0>
1745 _LIBCPP_INLINE_VISIBILITY
1746 typename enable_if
1747 <
1748 !is_convertible<_A0, __rv<_A0> >::value,
1749 void
1750 >::type
1751 construct(pointer __p, const _A0& __a0)
1752 {
1753 ::new((void*)__p) _Tp(__a0);
1754 }
1755 template <class _A0>
1756 _LIBCPP_INLINE_VISIBILITY
1757 typename enable_if
1758 <
1759 is_convertible<_A0, __rv<_A0> >::value,
1760 void
1761 >::type
1762 construct(pointer __p, _A0 __a0)
1763 {
1764 ::new((void*)__p) _Tp(_VSTD::move(__a0));
1765 }
1766# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1767 template <class _A0, class _A1>
1768 _LIBCPP_INLINE_VISIBILITY
1769 void
1770 construct(pointer __p, _A0& __a0, _A1& __a1)
1771 {
1772 ::new((void*)__p) _Tp(__a0, __a1);
1773 }
1774 template <class _A0, class _A1>
1775 _LIBCPP_INLINE_VISIBILITY
1776 void
1777 construct(pointer __p, const _A0& __a0, _A1& __a1)
1778 {
1779 ::new((void*)__p) _Tp(__a0, __a1);
1780 }
1781 template <class _A0, class _A1>
1782 _LIBCPP_INLINE_VISIBILITY
1783 void
1784 construct(pointer __p, _A0& __a0, const _A1& __a1)
1785 {
1786 ::new((void*)__p) _Tp(__a0, __a1);
1787 }
1788 template <class _A0, class _A1>
1789 _LIBCPP_INLINE_VISIBILITY
1790 void
1791 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1792 {
1793 ::new((void*)__p) _Tp(__a0, __a1);
1794 }
1795#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1796 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1797};
1798
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001799template <class _Tp, class _Up>
1800inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001801bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001802
1803template <class _Tp, class _Up>
1804inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001805bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001806
1807template <class _OutputIterator, class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001808class _LIBCPP_VISIBLE raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001809 : public iterator<output_iterator_tag,
1810 _Tp, // purposefully not C++03
1811 ptrdiff_t, // purposefully not C++03
1812 _Tp*, // purposefully not C++03
1813 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1814{
1815private:
1816 _OutputIterator __x_;
1817public:
1818 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1819 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1820 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1821 {::new(&*__x_) _Tp(__element); return *this;}
1822 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1823 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1824 {raw_storage_iterator __t(*this); ++__x_; return __t;}
1825};
1826
1827template <class _Tp>
1828pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00001829get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001830{
1831 pair<_Tp*, ptrdiff_t> __r(0, 0);
1832 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1833 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1834 / sizeof(_Tp);
1835 if (__n > __m)
1836 __n = __m;
1837 while (__n > 0)
1838 {
1839 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1840 if (__r.first)
1841 {
1842 __r.second = __n;
1843 break;
1844 }
1845 __n /= 2;
1846 }
1847 return __r;
1848}
1849
1850template <class _Tp>
1851inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001852void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001853
1854template <class _Tp>
1855struct auto_ptr_ref
1856{
1857 _Tp* __ptr_;
1858};
1859
1860template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001861class _LIBCPP_VISIBLE auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001862{
1863private:
1864 _Tp* __ptr_;
1865public:
1866 typedef _Tp element_type;
1867
1868 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1869 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1870 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1871 : __ptr_(__p.release()) {}
1872 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1873 {reset(__p.release()); return *this;}
1874 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1875 {reset(__p.release()); return *this;}
1876 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1877 {reset(__p.__ptr_); return *this;}
1878 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1879
1880 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1881 {return *__ptr_;}
1882 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1883 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1884 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1885 {
1886 _Tp* __t = __ptr_;
1887 __ptr_ = 0;
1888 return __t;
1889 }
1890 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1891 {
1892 if (__ptr_ != __p)
1893 delete __ptr_;
1894 __ptr_ = __p;
1895 }
1896
1897 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1898 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1899 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1900 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1901 {return auto_ptr<_Up>(release());}
1902};
1903
1904template <>
Howard Hinnant82894812010-09-22 16:48:34 +00001905class _LIBCPP_VISIBLE auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001906{
1907public:
1908 typedef void element_type;
1909};
1910
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001911template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1912 typename remove_cv<_T2>::type>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00001913 bool = is_empty<_T1>::value
1914#if __has_feature(is_final)
1915 && !__is_final(_T1)
1916#endif
1917 ,
1918 bool = is_empty<_T2>::value
1919#if __has_feature(is_final)
1920 && !__is_final(_T2)
1921#endif
1922 >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001923struct __libcpp_compressed_pair_switch;
1924
1925template <class _T1, class _T2, bool IsSame>
1926struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1927
1928template <class _T1, class _T2, bool IsSame>
1929struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1930
1931template <class _T1, class _T2, bool IsSame>
1932struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1933
1934template <class _T1, class _T2>
1935struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
1936
1937template <class _T1, class _T2>
1938struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
1939
1940template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
1941class __libcpp_compressed_pair_imp;
1942
1943template <class _T1, class _T2>
1944class __libcpp_compressed_pair_imp<_T1, _T2, 0>
1945{
1946private:
1947 _T1 __first_;
1948 _T2 __second_;
1949public:
1950 typedef _T1 _T1_param;
1951 typedef _T2 _T2_param;
1952
1953 typedef typename remove_reference<_T1>::type& _T1_reference;
1954 typedef typename remove_reference<_T2>::type& _T2_reference;
1955
1956 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1957 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1958
1959 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant57199402012-01-02 17:56:02 +00001960 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001961 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00001962 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001963 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001964 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001965 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001966
Howard Hinnant61aa6012011-07-01 19:24:36 +00001967#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
1968
1969 _LIBCPP_INLINE_VISIBILITY
1970 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
1971 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
1972 is_nothrow_copy_constructible<_T2>::value)
1973 : __first_(__p.first()),
1974 __second_(__p.second()) {}
1975
1976 _LIBCPP_INLINE_VISIBILITY
1977 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
1978 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
1979 is_nothrow_copy_assignable<_T2>::value)
1980 {
1981 __first_ = __p.first();
1982 __second_ = __p.second();
1983 return *this;
1984 }
1985
Howard Hinnant73d21a42010-09-04 23:28:19 +00001986#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00001987
1988 _LIBCPP_INLINE_VISIBILITY
1989 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00001990 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
1991 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant61aa6012011-07-01 19:24:36 +00001992 : __first_(_VSTD::forward<_T1>(__p.first())),
1993 __second_(_VSTD::forward<_T2>(__p.second())) {}
1994
1995 _LIBCPP_INLINE_VISIBILITY
1996 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
1997 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
1998 is_nothrow_move_assignable<_T2>::value)
1999 {
2000 __first_ = _VSTD::forward<_T1>(__p.first());
2001 __second_ = _VSTD::forward<_T2>(__p.second());
2002 return *this;
2003 }
2004
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002005#ifndef _LIBCPP_HAS_NO_VARIADICS
2006
2007 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2008 _LIBCPP_INLINE_VISIBILITY
2009 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2010 tuple<_Args1...> __first_args,
2011 tuple<_Args2...> __second_args,
2012 __tuple_indices<_I1...>,
2013 __tuple_indices<_I2...>)
2014 : __first_(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
2015 __second_(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
2016 {}
2017
2018#endif // _LIBCPP_HAS_NO_VARIADICS
2019
Howard Hinnant73d21a42010-09-04 23:28:19 +00002020#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002021
Howard Hinnant61aa6012011-07-01 19:24:36 +00002022#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2023
Howard Hinnant1694d232011-05-28 14:41:13 +00002024 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2025 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002026
Howard Hinnant1694d232011-05-28 14:41:13 +00002027 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2028 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002029
2030 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002031 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2032 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002033 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002034 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002035 swap(__first_, __x.__first_);
2036 swap(__second_, __x.__second_);
2037 }
2038};
2039
2040template <class _T1, class _T2>
2041class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2042 : private _T1
2043{
2044private:
2045 _T2 __second_;
2046public:
2047 typedef _T1 _T1_param;
2048 typedef _T2 _T2_param;
2049
2050 typedef _T1& _T1_reference;
2051 typedef typename remove_reference<_T2>::type& _T2_reference;
2052
2053 typedef const _T1& _T1_const_reference;
2054 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2055
2056 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002057 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002058 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002059 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002060 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002061 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002062 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002063
Howard Hinnant61aa6012011-07-01 19:24:36 +00002064#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2065
2066 _LIBCPP_INLINE_VISIBILITY
2067 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2068 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2069 is_nothrow_copy_constructible<_T2>::value)
2070 : _T1(__p.first()), __second_(__p.second()) {}
2071
2072 _LIBCPP_INLINE_VISIBILITY
2073 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2074 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2075 is_nothrow_copy_assignable<_T2>::value)
2076 {
2077 _T1::operator=(__p.first());
2078 __second_ = __p.second();
2079 return *this;
2080 }
2081
Howard Hinnant73d21a42010-09-04 23:28:19 +00002082#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002083
2084 _LIBCPP_INLINE_VISIBILITY
2085 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002086 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2087 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002088 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002089
2090 _LIBCPP_INLINE_VISIBILITY
2091 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2092 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2093 is_nothrow_move_assignable<_T2>::value)
2094 {
2095 _T1::operator=(_VSTD::move(__p.first()));
2096 __second_ = _VSTD::forward<_T2>(__p.second());
2097 return *this;
2098 }
2099
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002100#ifndef _LIBCPP_HAS_NO_VARIADICS
2101
2102 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2103 _LIBCPP_INLINE_VISIBILITY
2104 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2105 tuple<_Args1...> __first_args,
2106 tuple<_Args2...> __second_args,
2107 __tuple_indices<_I1...>,
2108 __tuple_indices<_I2...>)
2109 : _T1(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
2110 __second_(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
2111 {}
2112
2113#endif // _LIBCPP_HAS_NO_VARIADICS
2114
Howard Hinnant73d21a42010-09-04 23:28:19 +00002115#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002116
Howard Hinnant61aa6012011-07-01 19:24:36 +00002117#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2118
Howard Hinnant1694d232011-05-28 14:41:13 +00002119 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2120 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002121
Howard Hinnant1694d232011-05-28 14:41:13 +00002122 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2123 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002124
2125 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002126 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2127 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002128 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002129 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002130 swap(__second_, __x.__second_);
2131 }
2132};
2133
2134template <class _T1, class _T2>
2135class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2136 : private _T2
2137{
2138private:
2139 _T1 __first_;
2140public:
2141 typedef _T1 _T1_param;
2142 typedef _T2 _T2_param;
2143
2144 typedef typename remove_reference<_T1>::type& _T1_reference;
2145 typedef _T2& _T2_reference;
2146
2147 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2148 typedef const _T2& _T2_const_reference;
2149
2150 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2151 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002152 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002153 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002154 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002155 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00002156 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2157 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002158 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002159
Howard Hinnant61aa6012011-07-01 19:24:36 +00002160#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2161
2162 _LIBCPP_INLINE_VISIBILITY
2163 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2164 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2165 is_nothrow_copy_constructible<_T2>::value)
2166 : _T2(__p.second()), __first_(__p.first()) {}
2167
2168 _LIBCPP_INLINE_VISIBILITY
2169 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2170 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2171 is_nothrow_copy_assignable<_T2>::value)
2172 {
2173 _T2::operator=(__p.second());
2174 __first_ = __p.first();
2175 return *this;
2176 }
2177
Howard Hinnant73d21a42010-09-04 23:28:19 +00002178#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002179
2180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002181 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002182 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2183 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002184 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002185
2186 _LIBCPP_INLINE_VISIBILITY
2187 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2188 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2189 is_nothrow_move_assignable<_T2>::value)
2190 {
2191 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2192 __first_ = _VSTD::move(__p.first());
2193 return *this;
2194 }
2195
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002196#ifndef _LIBCPP_HAS_NO_VARIADICS
2197
2198 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2199 _LIBCPP_INLINE_VISIBILITY
2200 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2201 tuple<_Args1...> __first_args,
2202 tuple<_Args2...> __second_args,
2203 __tuple_indices<_I1...>,
2204 __tuple_indices<_I2...>)
2205 : _T2(_VSTD::forward<_Args2>(get<_I2>(__second_args))...),
2206 __first_(_VSTD::forward<_Args1>(get<_I1>(__first_args))...)
2207
2208 {}
2209
2210#endif // _LIBCPP_HAS_NO_VARIADICS
2211
Howard Hinnant73d21a42010-09-04 23:28:19 +00002212#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002213
Howard Hinnant61aa6012011-07-01 19:24:36 +00002214#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2215
Howard Hinnant1694d232011-05-28 14:41:13 +00002216 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2217 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002218
Howard Hinnant1694d232011-05-28 14:41:13 +00002219 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2220 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002221
2222 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002223 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2224 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002225 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002226 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002227 swap(__first_, __x.__first_);
2228 }
2229};
2230
2231template <class _T1, class _T2>
2232class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2233 : private _T1,
2234 private _T2
2235{
2236public:
2237 typedef _T1 _T1_param;
2238 typedef _T2 _T2_param;
2239
2240 typedef _T1& _T1_reference;
2241 typedef _T2& _T2_reference;
2242
2243 typedef const _T1& _T1_const_reference;
2244 typedef const _T2& _T2_const_reference;
2245
2246 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2247 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002248 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002249 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002250 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002251 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002252 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002253
Howard Hinnant61aa6012011-07-01 19:24:36 +00002254#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2255
2256 _LIBCPP_INLINE_VISIBILITY
2257 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2258 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2259 is_nothrow_copy_constructible<_T2>::value)
2260 : _T1(__p.first()), _T2(__p.second()) {}
2261
2262 _LIBCPP_INLINE_VISIBILITY
2263 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2264 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2265 is_nothrow_copy_assignable<_T2>::value)
2266 {
2267 _T1::operator=(__p.first());
2268 _T2::operator=(__p.second());
2269 return *this;
2270 }
2271
Howard Hinnant73d21a42010-09-04 23:28:19 +00002272#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002273
2274 _LIBCPP_INLINE_VISIBILITY
2275 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002276 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2277 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002278 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002279
2280 _LIBCPP_INLINE_VISIBILITY
2281 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2282 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2283 is_nothrow_move_assignable<_T2>::value)
2284 {
2285 _T1::operator=(_VSTD::move(__p.first()));
2286 _T2::operator=(_VSTD::move(__p.second()));
2287 return *this;
2288 }
2289
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002290#ifndef _LIBCPP_HAS_NO_VARIADICS
2291
2292 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2293 _LIBCPP_INLINE_VISIBILITY
2294 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2295 tuple<_Args1...> __first_args,
2296 tuple<_Args2...> __second_args,
2297 __tuple_indices<_I1...>,
2298 __tuple_indices<_I2...>)
2299 : _T1(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
2300 _T2(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
2301 {}
2302
2303#endif // _LIBCPP_HAS_NO_VARIADICS
2304
Howard Hinnant73d21a42010-09-04 23:28:19 +00002305#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002306
Howard Hinnant61aa6012011-07-01 19:24:36 +00002307#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2308
Howard Hinnant1694d232011-05-28 14:41:13 +00002309 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2310 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002311
Howard Hinnant1694d232011-05-28 14:41:13 +00002312 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2313 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002314
Howard Hinnantec3773c2011-12-01 20:21:04 +00002315 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant1694d232011-05-28 14:41:13 +00002316 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2317 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002318 {
2319 }
2320};
2321
2322template <class _T1, class _T2>
2323class __compressed_pair
2324 : private __libcpp_compressed_pair_imp<_T1, _T2>
2325{
2326 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2327public:
2328 typedef typename base::_T1_param _T1_param;
2329 typedef typename base::_T2_param _T2_param;
2330
2331 typedef typename base::_T1_reference _T1_reference;
2332 typedef typename base::_T2_reference _T2_reference;
2333
2334 typedef typename base::_T1_const_reference _T1_const_reference;
2335 typedef typename base::_T2_const_reference _T2_const_reference;
2336
2337 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002338 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002339 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002340 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002341 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002342 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002343 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002344
Howard Hinnant61aa6012011-07-01 19:24:36 +00002345#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2346
2347 _LIBCPP_INLINE_VISIBILITY
2348 __compressed_pair(const __compressed_pair& __p)
2349 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2350 is_nothrow_copy_constructible<_T2>::value)
2351 : base(__p) {}
2352
2353 _LIBCPP_INLINE_VISIBILITY
2354 __compressed_pair& operator=(const __compressed_pair& __p)
2355 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2356 is_nothrow_copy_assignable<_T2>::value)
2357 {
2358 base::operator=(__p);
2359 return *this;
2360 }
2361
Howard Hinnant73d21a42010-09-04 23:28:19 +00002362#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002363 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002364 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002365 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2366 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002367 : base(_VSTD::move(__p)) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002368
2369 _LIBCPP_INLINE_VISIBILITY
2370 __compressed_pair& operator=(__compressed_pair&& __p)
2371 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2372 is_nothrow_move_assignable<_T2>::value)
2373 {
2374 base::operator=(_VSTD::move(__p));
2375 return *this;
2376 }
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002377
2378#ifndef _LIBCPP_HAS_NO_VARIADICS
2379
2380 template <class... _Args1, class... _Args2>
2381 _LIBCPP_INLINE_VISIBILITY
2382 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2383 tuple<_Args2...> __second_args)
Howard Hinnant57199402012-01-02 17:56:02 +00002384 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002385 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2386 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2387 {}
2388
2389#endif // _LIBCPP_HAS_NO_VARIADICS
2390
Howard Hinnant73d21a42010-09-04 23:28:19 +00002391#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002392
Howard Hinnant61aa6012011-07-01 19:24:36 +00002393#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2394
Howard Hinnant1694d232011-05-28 14:41:13 +00002395 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2396 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002397
Howard Hinnant1694d232011-05-28 14:41:13 +00002398 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2399 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002400
Howard Hinnant1694d232011-05-28 14:41:13 +00002401 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2402 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2403 __is_nothrow_swappable<_T1>::value)
2404 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002405};
2406
2407template <class _T1, class _T2>
2408inline _LIBCPP_INLINE_VISIBILITY
2409void
2410swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002411 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2412 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002413 {__x.swap(__y);}
2414
Howard Hinnant57199402012-01-02 17:56:02 +00002415// __same_or_less_cv_qualified
2416
2417template <class _Ptr1, class _Ptr2,
2418 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2419 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2420 >::value
2421 >
2422struct __same_or_less_cv_qualified_imp
2423 : is_convertible<_Ptr1, _Ptr2> {};
2424
2425template <class _Ptr1, class _Ptr2>
2426struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2427 : false_type {};
2428
2429template <class _Ptr1, class _Ptr2, bool = is_scalar<_Ptr1>::value &&
2430 !is_pointer<_Ptr1>::value>
2431struct __same_or_less_cv_qualified
2432 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2433
2434template <class _Ptr1, class _Ptr2>
2435struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, true>
2436 : false_type {};
2437
2438// default_delete
2439
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002440template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002441struct _LIBCPP_VISIBLE default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002442{
Howard Hinnant1694d232011-05-28 14:41:13 +00002443 _LIBCPP_INLINE_VISIBILITY default_delete() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002444 template <class _Up>
2445 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002446 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2447 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002448 {
2449 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2450 delete __ptr;
2451 }
2452};
2453
2454template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002455struct _LIBCPP_VISIBLE default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002456{
Howard Hinnant8e843502011-12-18 21:19:44 +00002457public:
2458 _LIBCPP_INLINE_VISIBILITY default_delete() _NOEXCEPT {}
2459 template <class _Up>
2460 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant57199402012-01-02 17:56:02 +00002461 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant8e843502011-12-18 21:19:44 +00002462 template <class _Up>
2463 _LIBCPP_INLINE_VISIBILITY
2464 void operator() (_Up* __ptr,
Howard Hinnant57199402012-01-02 17:56:02 +00002465 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002466 {
2467 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2468 delete [] __ptr;
2469 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002470};
2471
2472template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant82894812010-09-22 16:48:34 +00002473class _LIBCPP_VISIBLE unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002474{
2475public:
2476 typedef _Tp element_type;
2477 typedef _Dp deleter_type;
2478 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2479private:
2480 __compressed_pair<pointer, deleter_type> __ptr_;
2481
Howard Hinnant57199402012-01-02 17:56:02 +00002482#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002483 unique_ptr(unique_ptr&);
2484 template <class _Up, class _Ep>
2485 unique_ptr(unique_ptr<_Up, _Ep>&);
2486 unique_ptr& operator=(unique_ptr&);
2487 template <class _Up, class _Ep>
2488 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002489#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002490
2491 struct __nat {int __for_bool_;};
2492
2493 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2494 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2495public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002496 _LIBCPP_INLINE_VISIBILITY unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002497 : __ptr_(pointer())
2498 {
2499 static_assert(!is_pointer<deleter_type>::value,
2500 "unique_ptr constructed with null function pointer deleter");
2501 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002502 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002503 : __ptr_(pointer())
2504 {
2505 static_assert(!is_pointer<deleter_type>::value,
2506 "unique_ptr constructed with null function pointer deleter");
2507 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002508 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002509 : __ptr_(_VSTD::move(__p))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002510 {
2511 static_assert(!is_pointer<deleter_type>::value,
2512 "unique_ptr constructed with null function pointer deleter");
2513 }
2514
Howard Hinnant73d21a42010-09-04 23:28:19 +00002515#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002516 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2517 is_reference<deleter_type>::value,
2518 deleter_type,
2519 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002520 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002521 : __ptr_(__p, __d) {}
2522
2523 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002524 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002525 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002526 {
2527 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2528 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002529 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002530 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002531 template <class _Up, class _Ep>
2532 _LIBCPP_INLINE_VISIBILITY
2533 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2534 typename enable_if
2535 <
2536 !is_array<_Up>::value &&
2537 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2538 is_convertible<_Ep, deleter_type>::value &&
2539 (
2540 !is_reference<deleter_type>::value ||
2541 is_same<deleter_type, _Ep>::value
2542 ),
2543 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002544 >::type = __nat()) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002545 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002546
2547 template <class _Up>
2548 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2549 typename enable_if<
2550 is_convertible<_Up*, _Tp*>::value &&
2551 is_same<_Dp, default_delete<_Tp> >::value,
2552 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002553 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002554 : __ptr_(__p.release())
2555 {
2556 }
2557
Howard Hinnant1694d232011-05-28 14:41:13 +00002558 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002559 {
2560 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002561 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002562 return *this;
2563 }
2564
2565 template <class _Up, class _Ep>
2566 _LIBCPP_INLINE_VISIBILITY
2567 typename enable_if
2568 <
Howard Hinnant57199402012-01-02 17:56:02 +00002569 !is_array<_Up>::value &&
2570 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2571 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002572 unique_ptr&
2573 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002574 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002575 {
2576 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002577 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002578 return *this;
2579 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002580#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002581
2582 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2583 {
2584 return __rv<unique_ptr>(*this);
2585 }
2586
2587 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002588 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002589
2590 template <class _Up, class _Ep>
2591 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2592 {
2593 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002594 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002595 return *this;
2596 }
2597
2598 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002599 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002600
2601 template <class _Up>
2602 _LIBCPP_INLINE_VISIBILITY
2603 typename enable_if<
2604 is_convertible<_Up*, _Tp*>::value &&
2605 is_same<_Dp, default_delete<_Tp> >::value,
2606 unique_ptr&
2607 >::type
2608 operator=(auto_ptr<_Up> __p)
2609 {reset(__p.release()); return *this;}
2610
Howard Hinnant73d21a42010-09-04 23:28:19 +00002611#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002612 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2613
Howard Hinnant1694d232011-05-28 14:41:13 +00002614 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002615 {
2616 reset();
2617 return *this;
2618 }
2619
2620 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2621 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002622 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2623 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2624 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2625 {return __ptr_.second();}
2626 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2627 {return __ptr_.second();}
2628 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const
2629 _NOEXCEPT
2630 {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002631
Howard Hinnant1694d232011-05-28 14:41:13 +00002632 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002633 {
2634 pointer __t = __ptr_.first();
2635 __ptr_.first() = pointer();
2636 return __t;
2637 }
2638
Howard Hinnant1694d232011-05-28 14:41:13 +00002639 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002640 {
2641 pointer __tmp = __ptr_.first();
2642 __ptr_.first() = __p;
2643 if (__tmp)
2644 __ptr_.second()(__tmp);
2645 }
2646
Howard Hinnant1694d232011-05-28 14:41:13 +00002647 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2648 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002649};
2650
2651template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002652class _LIBCPP_VISIBLE unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002653{
2654public:
2655 typedef _Tp element_type;
2656 typedef _Dp deleter_type;
2657 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2658private:
2659 __compressed_pair<pointer, deleter_type> __ptr_;
2660
Howard Hinnant57199402012-01-02 17:56:02 +00002661#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002662 unique_ptr(unique_ptr&);
2663 template <class _Up>
2664 unique_ptr(unique_ptr<_Up>&);
2665 unique_ptr& operator=(unique_ptr&);
2666 template <class _Up>
2667 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002668#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002669
2670 struct __nat {int __for_bool_;};
2671
2672 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2673 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2674public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002675 _LIBCPP_INLINE_VISIBILITY unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002676 : __ptr_(pointer())
2677 {
2678 static_assert(!is_pointer<deleter_type>::value,
2679 "unique_ptr constructed with null function pointer deleter");
2680 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002681 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002682 : __ptr_(pointer())
2683 {
2684 static_assert(!is_pointer<deleter_type>::value,
2685 "unique_ptr constructed with null function pointer deleter");
2686 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002687#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002688 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002689 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002690 >
Howard Hinnant99968442011-11-29 18:15:50 +00002691 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002692 : __ptr_(__p)
2693 {
2694 static_assert(!is_pointer<deleter_type>::value,
2695 "unique_ptr constructed with null function pointer deleter");
2696 }
2697
Howard Hinnant99968442011-11-29 18:15:50 +00002698 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002699 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002700 >
Howard Hinnant99968442011-11-29 18:15:50 +00002701 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002702 is_reference<deleter_type>::value,
2703 deleter_type,
2704 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002705 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002706 : __ptr_(__p, __d) {}
2707
2708 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2709 is_reference<deleter_type>::value,
2710 deleter_type,
2711 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002712 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002713 : __ptr_(pointer(), __d) {}
2714
Howard Hinnant99968442011-11-29 18:15:50 +00002715 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002716 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002717 >
Howard Hinnant99968442011-11-29 18:15:50 +00002718 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002719 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002720 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002721 {
2722 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2723 }
2724
2725 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002726 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002727 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002728 {
2729 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2730 }
2731
Howard Hinnant1694d232011-05-28 14:41:13 +00002732 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002733 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002734
Howard Hinnant1694d232011-05-28 14:41:13 +00002735 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002736 {
2737 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002738 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002739 return *this;
2740 }
Howard Hinnant8e843502011-12-18 21:19:44 +00002741
2742 template <class _Up, class _Ep>
2743 _LIBCPP_INLINE_VISIBILITY
2744 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2745 typename enable_if
2746 <
2747 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002748 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant8e843502011-12-18 21:19:44 +00002749 && is_convertible<_Ep, deleter_type>::value &&
2750 (
2751 !is_reference<deleter_type>::value ||
2752 is_same<deleter_type, _Ep>::value
2753 ),
2754 __nat
2755 >::type = __nat()
2756 ) _NOEXCEPT
2757 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2758
2759
2760 template <class _Up, class _Ep>
2761 _LIBCPP_INLINE_VISIBILITY
2762 typename enable_if
2763 <
2764 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002765 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2766 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant8e843502011-12-18 21:19:44 +00002767 unique_ptr&
2768 >::type
2769 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2770 {
2771 reset(__u.release());
2772 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2773 return *this;
2774 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002775#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002776
2777 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2778 : __ptr_(__p)
2779 {
2780 static_assert(!is_pointer<deleter_type>::value,
2781 "unique_ptr constructed with null function pointer deleter");
2782 }
2783
2784 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002785 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002786
2787 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002788 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002789
2790 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2791 {
2792 return __rv<unique_ptr>(*this);
2793 }
2794
2795 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002796 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002797
2798 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2799 {
2800 reset(__u->release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002801 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002802 return *this;
2803 }
2804
Howard Hinnant73d21a42010-09-04 23:28:19 +00002805#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002806 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2807
Howard Hinnant1694d232011-05-28 14:41:13 +00002808 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002809 {
2810 reset();
2811 return *this;
2812 }
2813
2814 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2815 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002816 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2817 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2818 {return __ptr_.second();}
2819 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2820 {return __ptr_.second();}
2821 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const _NOEXCEPT
2822 {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002823
Howard Hinnant1694d232011-05-28 14:41:13 +00002824 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002825 {
2826 pointer __t = __ptr_.first();
2827 __ptr_.first() = pointer();
2828 return __t;
2829 }
2830
Howard Hinnant73d21a42010-09-04 23:28:19 +00002831#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002832 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002833 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002834 >
Howard Hinnant99968442011-11-29 18:15:50 +00002835 _LIBCPP_INLINE_VISIBILITY void reset(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002836 {
2837 pointer __tmp = __ptr_.first();
2838 __ptr_.first() = __p;
2839 if (__tmp)
2840 __ptr_.second()(__tmp);
2841 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002842 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002843 {
2844 pointer __tmp = __ptr_.first();
2845 __ptr_.first() = nullptr;
2846 if (__tmp)
2847 __ptr_.second()(__tmp);
2848 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002849 _LIBCPP_INLINE_VISIBILITY void reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002850 {
2851 pointer __tmp = __ptr_.first();
2852 __ptr_.first() = nullptr;
2853 if (__tmp)
2854 __ptr_.second()(__tmp);
2855 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002856#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002857 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2858 {
2859 pointer __tmp = __ptr_.first();
2860 __ptr_.first() = __p;
2861 if (__tmp)
2862 __ptr_.second()(__tmp);
2863 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002864#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002865
2866 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2867private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002868
Howard Hinnant73d21a42010-09-04 23:28:19 +00002869#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002870 template <class _Up>
2871 explicit unique_ptr(_Up);
2872 template <class _Up>
2873 unique_ptr(_Up __u,
2874 typename conditional<
2875 is_reference<deleter_type>::value,
2876 deleter_type,
2877 typename add_lvalue_reference<const deleter_type>::type>::type,
2878 typename enable_if
2879 <
2880 is_convertible<_Up, pointer>::value,
2881 __nat
2882 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002883#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002884};
2885
2886template <class _Tp, class _Dp>
2887inline _LIBCPP_INLINE_VISIBILITY
2888void
Howard Hinnant1694d232011-05-28 14:41:13 +00002889swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002890
2891template <class _T1, class _D1, class _T2, class _D2>
2892inline _LIBCPP_INLINE_VISIBILITY
2893bool
2894operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2895
2896template <class _T1, class _D1, class _T2, class _D2>
2897inline _LIBCPP_INLINE_VISIBILITY
2898bool
2899operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2900
2901template <class _T1, class _D1, class _T2, class _D2>
2902inline _LIBCPP_INLINE_VISIBILITY
2903bool
2904operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() < __y.get();}
2905
2906template <class _T1, class _D1, class _T2, class _D2>
2907inline _LIBCPP_INLINE_VISIBILITY
2908bool
2909operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2910
2911template <class _T1, class _D1, class _T2, class _D2>
2912inline _LIBCPP_INLINE_VISIBILITY
2913bool
2914operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2915
2916template <class _T1, class _D1, class _T2, class _D2>
2917inline _LIBCPP_INLINE_VISIBILITY
2918bool
2919operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2920
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00002921template <class _Tp> struct hash;
Howard Hinnant21aefc32010-06-03 16:42:57 +00002922
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002923// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
2924// is 64 bits. This is because cityhash64 uses 64bit x 64bit
2925// multiplication, which can be very slow on 32-bit systems.
Howard Hinnant40c13d32011-12-05 00:08:45 +00002926template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002927struct __murmur2_or_cityhash;
Howard Hinnant40c13d32011-12-05 00:08:45 +00002928
2929template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002930struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnant40c13d32011-12-05 00:08:45 +00002931{
2932 _Size operator()(const void* __key, _Size __len);
2933};
2934
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002935// murmur2
Howard Hinnant40c13d32011-12-05 00:08:45 +00002936template <class _Size>
2937_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002938__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00002939{
2940 const _Size __m = 0x5bd1e995;
2941 const _Size __r = 24;
2942 _Size __h = __len;
2943 const unsigned char* __data = static_cast<const unsigned char*>(__key);
2944 for (; __len >= 4; __data += 4, __len -= 4)
2945 {
2946 _Size __k = *(const _Size*)__data;
2947 __k *= __m;
2948 __k ^= __k >> __r;
2949 __k *= __m;
2950 __h *= __m;
2951 __h ^= __k;
2952 }
2953 switch (__len)
2954 {
2955 case 3:
2956 __h ^= __data[2] << 16;
2957 case 2:
2958 __h ^= __data[1] << 8;
2959 case 1:
2960 __h ^= __data[0];
2961 __h *= __m;
2962 }
2963 __h ^= __h >> 13;
2964 __h *= __m;
2965 __h ^= __h >> 15;
2966 return __h;
2967}
2968
2969template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002970struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnant40c13d32011-12-05 00:08:45 +00002971{
2972 _Size operator()(const void* __key, _Size __len);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002973
2974 private:
2975 // Some primes between 2^63 and 2^64.
2976 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
2977 static const _Size __k1 = 0xb492b66fbe98f273ULL;
2978 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
2979 static const _Size __k3 = 0xc949d7c7509e6557ULL;
2980
2981 static _Size __rotate(_Size __val, int __shift) {
2982 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
2983 }
2984
2985 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
2986 return (__val >> __shift) | (__val << (64 - __shift));
2987 }
2988
2989 static _Size __shift_mix(_Size __val) {
2990 return __val ^ (__val >> 47);
2991 }
2992
2993 static _Size __hash_len_16(_Size __u, _Size __v) {
2994 const _Size __mul = 0x9ddfea08eb382d69ULL;
2995 _Size __a = (__u ^ __v) * __mul;
2996 __a ^= (__a >> 47);
2997 _Size __b = (__v ^ __a) * __mul;
2998 __b ^= (__b >> 47);
2999 __b *= __mul;
3000 return __b;
3001 }
3002
3003 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
3004 if (__len > 8) {
3005 const _Size __a = *(const _Size*)__s;
3006 const _Size __b = *(const _Size*)(__s + __len - 8);
3007 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
3008 }
3009 if (__len >= 4) {
3010 const uint32_t __a = *(const uint32_t*)(__s);
3011 const uint32_t __b = *(const uint32_t*)(__s + __len - 4);
3012 return __hash_len_16(__len + (__a << 3), __b);
3013 }
3014 if (__len > 0) {
3015 const unsigned char __a = __s[0];
3016 const unsigned char __b = __s[__len >> 1];
3017 const unsigned char __c = __s[__len - 1];
3018 const uint32_t __y = static_cast<uint32_t>(__a) +
3019 (static_cast<uint32_t>(__b) << 8);
3020 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3021 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3022 }
3023 return __k2;
3024 }
3025
3026 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
3027 const _Size __a = *(const _Size*)(__s) * __k1;
3028 const _Size __b = *(const _Size*)(__s + 8);
3029 const _Size __c = *(const _Size*)(__s + __len - 8) * __k2;
3030 const _Size __d = *(const _Size*)(__s + __len - 16) * __k0;
3031 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3032 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3033 }
3034
3035 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3036 // Callers do best to use "random-looking" values for a and b.
3037 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3038 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3039 __a += __w;
3040 __b = __rotate(__b + __a + __z, 21);
3041 const _Size __c = __a;
3042 __a += __x;
3043 __a += __y;
3044 __b += __rotate(__a, 44);
3045 return pair<_Size, _Size>(__a + __z, __b + __c);
3046 }
3047
3048 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3049 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3050 const char* __s, _Size __a, _Size __b) {
3051 return __weak_hash_len_32_with_seeds(*(const _Size*)(__s),
3052 *(const _Size*)(__s + 8),
3053 *(const _Size*)(__s + 16),
3054 *(const _Size*)(__s + 24),
3055 __a,
3056 __b);
3057 }
3058
3059 // Return an 8-byte hash for 33 to 64 bytes.
3060 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
3061 _Size __z = *(const _Size*)(__s + 24);
3062 _Size __a = *(const _Size*)(__s) +
3063 (__len + *(const _Size*)(__s + __len - 16)) * __k0;
3064 _Size __b = __rotate(__a + __z, 52);
3065 _Size __c = __rotate(__a, 37);
3066 __a += *(const _Size*)(__s + 8);
3067 __c += __rotate(__a, 7);
3068 __a += *(const _Size*)(__s + 16);
3069 _Size __vf = __a + __z;
3070 _Size __vs = __b + __rotate(__a, 31) + __c;
3071 __a = *(const _Size*)(__s + 16) + *(const _Size*)(__s + __len - 32);
3072 __z += *(const _Size*)(__s + __len - 8);
3073 __b = __rotate(__a + __z, 52);
3074 __c = __rotate(__a, 37);
3075 __a += *(const _Size*)(__s + __len - 24);
3076 __c += __rotate(__a, 7);
3077 __a += *(const _Size*)(__s + __len - 16);
3078 _Size __wf = __a + __z;
3079 _Size __ws = __b + __rotate(__a, 31) + __c;
3080 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3081 return __shift_mix(__r * __k0 + __vs) * __k2;
3082 }
Howard Hinnant40c13d32011-12-05 00:08:45 +00003083};
3084
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003085// cityhash64
Howard Hinnant40c13d32011-12-05 00:08:45 +00003086template <class _Size>
3087_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003088__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003089{
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003090 const char* __s = static_cast<const char*>(__key);
3091 if (__len <= 32) {
3092 if (__len <= 16) {
3093 return __hash_len_0_to_16(__s, __len);
3094 } else {
3095 return __hash_len_17_to_32(__s, __len);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003096 }
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003097 } else if (__len <= 64) {
3098 return __hash_len_33_to_64(__s, __len);
3099 }
3100
3101 // For strings over 64 bytes we hash the end first, and then as we
3102 // loop we keep 56 bytes of state: v, w, x, y, and z.
3103 _Size __x = *(const _Size*)(__s + __len - 40);
3104 _Size __y = *(const _Size*)(__s + __len - 16) +
3105 *(const _Size*)(__s + __len - 56);
3106 _Size __z = __hash_len_16(*(const _Size*)(__s + __len - 48) + __len,
3107 *(const _Size*)(__s + __len - 24));
3108 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3109 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
3110 __x = __x * __k1 + *(const _Size*)(__s);
3111
3112 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3113 __len = (__len - 1) & ~static_cast<_Size>(63);
3114 do {
3115 __x = __rotate(__x + __y + __v.first + *(const _Size*)(__s + 8), 37) * __k1;
3116 __y = __rotate(__y + __v.second + *(const _Size*)(__s + 48), 42) * __k1;
3117 __x ^= __w.second;
3118 __y += __v.first + *(const _Size*)(__s + 40);
3119 __z = __rotate(__z + __w.first, 33) * __k1;
3120 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3121 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
3122 __y + *(const _Size*)(__s + 16));
3123 std::swap(__z, __x);
3124 __s += 64;
3125 __len -= 64;
3126 } while (__len != 0);
3127 return __hash_len_16(
3128 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3129 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003130}
3131
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003132template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3133struct __scalar_hash;
3134
3135template <class _Tp>
3136struct __scalar_hash<_Tp, 0>
3137 : public unary_function<_Tp, size_t>
Howard Hinnant21aefc32010-06-03 16:42:57 +00003138{
Howard Hinnant82894812010-09-22 16:48:34 +00003139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003140 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003141 {
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003142 union
3143 {
3144 _Tp __t;
3145 size_t __a;
3146 } __u;
3147 __u.__a = 0;
3148 __u.__t = __v;
3149 return __u.__a;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003150 }
3151};
3152
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003153template <class _Tp>
3154struct __scalar_hash<_Tp, 1>
3155 : public unary_function<_Tp, size_t>
3156{
3157 _LIBCPP_INLINE_VISIBILITY
3158 size_t operator()(_Tp __v) const _NOEXCEPT
3159 {
3160 union
3161 {
3162 _Tp __t;
3163 size_t __a;
3164 } __u;
3165 __u.__t = __v;
3166 return __u.__a;
3167 }
3168};
3169
3170template <class _Tp>
3171struct __scalar_hash<_Tp, 2>
3172 : public unary_function<_Tp, size_t>
3173{
3174 _LIBCPP_INLINE_VISIBILITY
3175 size_t operator()(_Tp __v) const _NOEXCEPT
3176 {
3177 union
3178 {
3179 _Tp __t;
3180 struct
3181 {
3182 size_t __a;
3183 size_t __b;
3184 };
3185 } __u;
3186 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003187 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003188 }
3189};
3190
3191template <class _Tp>
3192struct __scalar_hash<_Tp, 3>
3193 : public unary_function<_Tp, size_t>
3194{
3195 _LIBCPP_INLINE_VISIBILITY
3196 size_t operator()(_Tp __v) const _NOEXCEPT
3197 {
3198 union
3199 {
3200 _Tp __t;
3201 struct
3202 {
3203 size_t __a;
3204 size_t __b;
3205 size_t __c;
3206 };
3207 } __u;
3208 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003209 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003210 }
3211};
3212
3213template <class _Tp>
3214struct __scalar_hash<_Tp, 4>
3215 : public unary_function<_Tp, size_t>
3216{
3217 _LIBCPP_INLINE_VISIBILITY
3218 size_t operator()(_Tp __v) const _NOEXCEPT
3219 {
3220 union
3221 {
3222 _Tp __t;
3223 struct
3224 {
3225 size_t __a;
3226 size_t __b;
3227 size_t __c;
3228 size_t __d;
3229 };
3230 } __u;
3231 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003232 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003233 }
3234};
3235
3236template<class _Tp>
3237struct _LIBCPP_VISIBLE hash<_Tp*>
3238 : public __scalar_hash<_Tp*>
3239{
3240};
3241
Howard Hinnant21aefc32010-06-03 16:42:57 +00003242template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00003243struct _LIBCPP_VISIBLE hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003244{
3245 typedef unique_ptr<_Tp, _Dp> argument_type;
3246 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003248 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003249 {
3250 typedef typename argument_type::pointer pointer;
3251 return hash<pointer>()(__ptr.get());
3252 }
3253};
3254
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003255struct __destruct_n
3256{
3257private:
3258 size_t size;
3259
3260 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003261 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003262 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3263
3264 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003265 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003266 {}
3267
Howard Hinnant1694d232011-05-28 14:41:13 +00003268 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003269 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003270 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003271 {}
3272
Howard Hinnant1694d232011-05-28 14:41:13 +00003273 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003274 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003275 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003276 {}
3277public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003278 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3279 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003280
3281 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003282 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003283 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003284
3285 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003286 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003287 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003288
3289 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003290 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003291 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003292};
3293
3294template <class _Alloc>
3295class __allocator_destructor
3296{
3297 typedef allocator_traits<_Alloc> __alloc_traits;
3298public:
3299 typedef typename __alloc_traits::pointer pointer;
3300 typedef typename __alloc_traits::size_type size_type;
3301private:
3302 _Alloc& __alloc_;
3303 size_type __s_;
3304public:
3305 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00003306 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003307 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003308 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003309 void operator()(pointer __p) _NOEXCEPT
3310 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003311};
3312
3313template <class _InputIterator, class _ForwardIterator>
3314_ForwardIterator
3315uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3316{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003317 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003318#ifndef _LIBCPP_NO_EXCEPTIONS
3319 _ForwardIterator __s = __r;
3320 try
3321 {
3322#endif
3323 for (; __f != __l; ++__f, ++__r)
3324 ::new(&*__r) value_type(*__f);
3325#ifndef _LIBCPP_NO_EXCEPTIONS
3326 }
3327 catch (...)
3328 {
3329 for (; __s != __r; ++__s)
3330 __s->~value_type();
3331 throw;
3332 }
3333#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003334 return __r;
3335}
3336
3337template <class _InputIterator, class _Size, class _ForwardIterator>
3338_ForwardIterator
3339uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3340{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003341 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003342#ifndef _LIBCPP_NO_EXCEPTIONS
3343 _ForwardIterator __s = __r;
3344 try
3345 {
3346#endif
3347 for (; __n > 0; ++__f, ++__r, --__n)
3348 ::new(&*__r) value_type(*__f);
3349#ifndef _LIBCPP_NO_EXCEPTIONS
3350 }
3351 catch (...)
3352 {
3353 for (; __s != __r; ++__s)
3354 __s->~value_type();
3355 throw;
3356 }
3357#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003358 return __r;
3359}
3360
3361template <class _ForwardIterator, class _Tp>
3362void
3363uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3364{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003365 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003366#ifndef _LIBCPP_NO_EXCEPTIONS
3367 _ForwardIterator __s = __f;
3368 try
3369 {
3370#endif
3371 for (; __f != __l; ++__f)
3372 ::new(&*__f) value_type(__x);
3373#ifndef _LIBCPP_NO_EXCEPTIONS
3374 }
3375 catch (...)
3376 {
3377 for (; __s != __f; ++__s)
3378 __s->~value_type();
3379 throw;
3380 }
3381#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003382}
3383
3384template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003385_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003386uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3387{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003388 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003389#ifndef _LIBCPP_NO_EXCEPTIONS
3390 _ForwardIterator __s = __f;
3391 try
3392 {
3393#endif
3394 for (; __n > 0; ++__f, --__n)
3395 ::new(&*__f) value_type(__x);
3396#ifndef _LIBCPP_NO_EXCEPTIONS
3397 }
3398 catch (...)
3399 {
3400 for (; __s != __f; ++__s)
3401 __s->~value_type();
3402 throw;
3403 }
3404#endif
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003405 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003406}
3407
Howard Hinnant82894812010-09-22 16:48:34 +00003408class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003409 : public std::exception
3410{
3411public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003412 virtual ~bad_weak_ptr() _NOEXCEPT;
3413 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003414};
3415
3416template<class _Tp> class weak_ptr;
3417
3418class __shared_count
3419{
3420 __shared_count(const __shared_count&);
3421 __shared_count& operator=(const __shared_count&);
3422
3423protected:
3424 long __shared_owners_;
3425 virtual ~__shared_count();
3426private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003427 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003428
3429public:
Howard Hinnant82894812010-09-22 16:48:34 +00003430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003431 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003432 : __shared_owners_(__refs) {}
3433
Howard Hinnant1694d232011-05-28 14:41:13 +00003434 void __add_shared() _NOEXCEPT;
3435 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003436 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003437 long use_count() const _NOEXCEPT {return __shared_owners_ + 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003438};
3439
3440class __shared_weak_count
3441 : private __shared_count
3442{
3443 long __shared_weak_owners_;
3444
3445public:
Howard Hinnant82894812010-09-22 16:48:34 +00003446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003447 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003448 : __shared_count(__refs),
3449 __shared_weak_owners_(__refs) {}
3450protected:
3451 virtual ~__shared_weak_count();
3452
3453public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003454 void __add_shared() _NOEXCEPT;
3455 void __add_weak() _NOEXCEPT;
3456 void __release_shared() _NOEXCEPT;
3457 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003458 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003459 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3460 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003461
Howard Hinnant1694d232011-05-28 14:41:13 +00003462 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003463private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003464 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003465};
3466
3467template <class _Tp, class _Dp, class _Alloc>
3468class __shared_ptr_pointer
3469 : public __shared_weak_count
3470{
3471 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3472public:
Howard Hinnant82894812010-09-22 16:48:34 +00003473 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003474 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003475 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003476
Howard Hinnantd4444702010-08-11 17:04:31 +00003477#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00003478 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00003479#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003480
3481private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003482 virtual void __on_zero_shared() _NOEXCEPT;
3483 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003484};
3485
Howard Hinnantd4444702010-08-11 17:04:31 +00003486#ifndef _LIBCPP_NO_RTTI
3487
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003488template <class _Tp, class _Dp, class _Alloc>
3489const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00003490__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003491{
3492 return __t == typeid(_Dp) ? &__data_.first().second() : 0;
3493}
3494
Howard Hinnant324bb032010-08-22 00:02:43 +00003495#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003496
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003497template <class _Tp, class _Dp, class _Alloc>
3498void
Howard Hinnant1694d232011-05-28 14:41:13 +00003499__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003500{
3501 __data_.first().second()(__data_.first().first());
3502 __data_.first().second().~_Dp();
3503}
3504
3505template <class _Tp, class _Dp, class _Alloc>
3506void
Howard Hinnant1694d232011-05-28 14:41:13 +00003507__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003508{
3509 typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
3510 __data_.second().~_Alloc();
3511 __a.deallocate(this, 1);
3512}
3513
3514template <class _Tp, class _Alloc>
3515class __shared_ptr_emplace
3516 : public __shared_weak_count
3517{
3518 __compressed_pair<_Alloc, _Tp> __data_;
3519public:
3520#ifndef _LIBCPP_HAS_NO_VARIADICS
3521
Howard Hinnant82894812010-09-22 16:48:34 +00003522 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003523 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003524 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003525
3526 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00003527 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003528 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00003529 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3530 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003531
3532#else // _LIBCPP_HAS_NO_VARIADICS
3533
Howard Hinnant82894812010-09-22 16:48:34 +00003534 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003535 __shared_ptr_emplace(_Alloc __a)
3536 : __data_(__a) {}
3537
3538 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00003539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003540 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3541 : __data_(__a, _Tp(__a0)) {}
3542
3543 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00003544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003545 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3546 : __data_(__a, _Tp(__a0, __a1)) {}
3547
3548 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00003549 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003550 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3551 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3552
3553#endif // _LIBCPP_HAS_NO_VARIADICS
3554
3555private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003556 virtual void __on_zero_shared() _NOEXCEPT;
3557 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003558public:
Howard Hinnant82894812010-09-22 16:48:34 +00003559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003560 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003561};
3562
3563template <class _Tp, class _Alloc>
3564void
Howard Hinnant1694d232011-05-28 14:41:13 +00003565__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003566{
3567 __data_.second().~_Tp();
3568}
3569
3570template <class _Tp, class _Alloc>
3571void
Howard Hinnant1694d232011-05-28 14:41:13 +00003572__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003573{
3574 typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
3575 __data_.first().~_Alloc();
3576 __a.deallocate(this, 1);
3577}
3578
3579template<class _Tp> class enable_shared_from_this;
3580
3581template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003582class _LIBCPP_VISIBLE shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003583{
Howard Hinnant324bb032010-08-22 00:02:43 +00003584public:
3585 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003586private:
3587 element_type* __ptr_;
3588 __shared_weak_count* __cntrl_;
3589
3590 struct __nat {int __for_bool_;};
3591public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003592 shared_ptr() _NOEXCEPT;
3593 shared_ptr(nullptr_t) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003594 template<class _Yp,
3595 class = typename enable_if
3596 <
3597 is_convertible<_Yp*, element_type*>::value
3598 >::type
3599 >
3600 explicit shared_ptr(_Yp* __p);
3601 template<class _Yp, class _Dp,
3602 class = typename enable_if
3603 <
3604 is_convertible<_Yp*, element_type*>::value
3605 >::type
3606 >
3607 shared_ptr(_Yp* __p, _Dp __d);
3608 template<class _Yp, class _Dp, class _Alloc,
3609 class = typename enable_if
3610 <
3611 is_convertible<_Yp*, element_type*>::value
3612 >::type
3613 >
3614 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003615 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3616 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant1694d232011-05-28 14:41:13 +00003617 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3618 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003619 template<class _Yp>
3620 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003621 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3622 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003623#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003624 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003625 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003626 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3627 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003628#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003629 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003630 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003631#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003632 template<class _Yp,
3633 class = typename enable_if
3634 <
3635 is_convertible<_Yp*, element_type*>::value
3636 >::type
3637 >
3638 shared_ptr(auto_ptr<_Yp>&& __r);
Howard Hinnant324bb032010-08-22 00:02:43 +00003639#else
Howard Hinnant57199402012-01-02 17:56:02 +00003640 template<class _Yp,
3641 class = typename enable_if
3642 <
3643 is_convertible<_Yp*, element_type*>::value
3644 >::type
3645 >
3646 shared_ptr(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003647#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003648#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003649 template <class _Yp, class _Dp,
3650 class = typename enable_if
3651 <
3652 !is_array<_Yp>::value &&
3653 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3654 >::type
3655 >
3656 shared_ptr(unique_ptr<_Yp, _Dp>&&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003657 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant57199402012-01-02 17:56:02 +00003658 template <class _Yp, class _Dp,
3659 class = typename enable_if
3660 <
3661 !is_array<_Yp>::value &&
3662 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3663 >::type
3664 >
3665 shared_ptr(unique_ptr<_Yp, _Dp>&&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003666 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003667#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003668 template <class _Yp, class _Dp,
3669 class = typename enable_if
3670 <
3671 !is_array<_Yp>::value &&
3672 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3673 >::type
3674 > shared_ptr(unique_ptr<_Yp, _Dp>,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003675 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant57199402012-01-02 17:56:02 +00003676 template <class _Yp, class _Dp,
3677 class = typename enable_if
3678 <
3679 !is_array<_Yp>::value &&
3680 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3681 >::type
3682 >
3683 shared_ptr(unique_ptr<_Yp, _Dp>,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003684 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003685#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003686
3687 ~shared_ptr();
3688
Howard Hinnant1694d232011-05-28 14:41:13 +00003689 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003690 template<class _Yp>
3691 typename enable_if
3692 <
3693 is_convertible<_Yp*, element_type*>::value,
3694 shared_ptr&
3695 >::type
3696 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003697#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003698 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003699 template<class _Yp>
3700 typename enable_if
3701 <
3702 is_convertible<_Yp*, element_type*>::value,
3703 shared_ptr<_Tp>&
3704 >::type
3705 operator=(shared_ptr<_Yp>&& __r);
3706 template<class _Yp>
3707 typename enable_if
3708 <
3709 !is_array<_Yp>::value &&
3710 is_convertible<_Yp*, element_type*>::value,
3711 shared_ptr&
3712 >::type
3713 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003714#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003715 template<class _Yp>
3716 typename enable_if
3717 <
3718 !is_array<_Yp>::value &&
3719 is_convertible<_Yp*, element_type*>::value,
3720 shared_ptr&
3721 >::type
3722 operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003723#endif
Howard Hinnant57199402012-01-02 17:56:02 +00003724 template <class _Yp, class _Dp>
3725 typename enable_if
3726 <
3727 !is_array<_Yp>::value &&
3728 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3729 shared_ptr&
3730 >::type
Howard Hinnant73d21a42010-09-04 23:28:19 +00003731#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003732 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003733#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003734 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003735#endif
3736
Howard Hinnant1694d232011-05-28 14:41:13 +00003737 void swap(shared_ptr& __r) _NOEXCEPT;
3738 void reset() _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003739 template<class _Yp>
3740 typename enable_if
3741 <
3742 is_convertible<_Yp*, element_type*>::value,
3743 void
3744 >::type
3745 reset(_Yp* __p);
3746 template<class _Yp, class _Dp>
3747 typename enable_if
3748 <
3749 is_convertible<_Yp*, element_type*>::value,
3750 void
3751 >::type
3752 reset(_Yp* __p, _Dp __d);
3753 template<class _Yp, class _Dp, class _Alloc>
3754 typename enable_if
3755 <
3756 is_convertible<_Yp*, element_type*>::value,
3757 void
3758 >::type
3759 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003760
Howard Hinnant82894812010-09-22 16:48:34 +00003761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003762 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003763 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003764 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3765 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003766 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003767 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003768 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003769 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003770 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003771 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00003772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003773 /*explicit*/ operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant99968442011-11-29 18:15:50 +00003774 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00003775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003776 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003777 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant99968442011-11-29 18:15:50 +00003778 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00003779 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003780 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003781 {return __cntrl_ < __p.__cntrl_;}
3782
Howard Hinnantd4444702010-08-11 17:04:31 +00003783#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003784 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00003785 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003786 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003787 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00003788#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003789
3790#ifndef _LIBCPP_HAS_NO_VARIADICS
3791
3792 template<class ..._Args>
3793 static
3794 shared_ptr<_Tp>
3795 make_shared(_Args&& ...__args);
3796
3797 template<class _Alloc, class ..._Args>
3798 static
3799 shared_ptr<_Tp>
3800 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3801
3802#else // _LIBCPP_HAS_NO_VARIADICS
3803
3804 static shared_ptr<_Tp> make_shared();
3805
3806 template<class _A0>
3807 static shared_ptr<_Tp> make_shared(_A0&);
3808
3809 template<class _A0, class _A1>
3810 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3811
3812 template<class _A0, class _A1, class _A2>
3813 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3814
3815 template<class _Alloc>
3816 static shared_ptr<_Tp>
3817 allocate_shared(const _Alloc& __a);
3818
3819 template<class _Alloc, class _A0>
3820 static shared_ptr<_Tp>
3821 allocate_shared(const _Alloc& __a, _A0& __a0);
3822
3823 template<class _Alloc, class _A0, class _A1>
3824 static shared_ptr<_Tp>
3825 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3826
3827 template<class _Alloc, class _A0, class _A1, class _A2>
3828 static shared_ptr<_Tp>
3829 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3830
3831#endif // _LIBCPP_HAS_NO_VARIADICS
3832
3833private:
3834
3835 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00003836 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003837 void
Howard Hinnant1694d232011-05-28 14:41:13 +00003838 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003839 {
3840 if (__e)
3841 __e->__weak_this_ = *this;
3842 }
3843
Howard Hinnant82894812010-09-22 16:48:34 +00003844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003845 void __enable_weak_this(const void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003846
Howard Hinnant82894812010-09-22 16:48:34 +00003847 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
3848 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003849};
3850
3851template<class _Tp>
3852inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003853shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003854 : __ptr_(0),
3855 __cntrl_(0)
3856{
3857}
3858
3859template<class _Tp>
3860inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003861shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003862 : __ptr_(0),
3863 __cntrl_(0)
3864{
3865}
3866
3867template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00003868template<class _Yp, class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003869shared_ptr<_Tp>::shared_ptr(_Yp* __p)
3870 : __ptr_(__p)
3871{
3872 unique_ptr<_Yp> __hold(__p);
3873 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3874 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
3875 __hold.release();
3876 __enable_weak_this(__p);
3877}
3878
3879template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00003880template<class _Yp, class _Dp, class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003881shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
3882 : __ptr_(__p)
3883{
3884#ifndef _LIBCPP_NO_EXCEPTIONS
3885 try
3886 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003887#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003888 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3889 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
3890 __enable_weak_this(__p);
3891#ifndef _LIBCPP_NO_EXCEPTIONS
3892 }
3893 catch (...)
3894 {
3895 __d(__p);
3896 throw;
3897 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003898#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003899}
3900
3901template<class _Tp>
3902template<class _Dp>
3903shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3904 : __ptr_(0)
3905{
3906#ifndef _LIBCPP_NO_EXCEPTIONS
3907 try
3908 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003909#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003910 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
3911 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
3912#ifndef _LIBCPP_NO_EXCEPTIONS
3913 }
3914 catch (...)
3915 {
3916 __d(__p);
3917 throw;
3918 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003919#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003920}
3921
3922template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00003923template<class _Yp, class _Dp, class _Alloc, class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003924shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
3925 : __ptr_(__p)
3926{
3927#ifndef _LIBCPP_NO_EXCEPTIONS
3928 try
3929 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003930#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003931 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
3932 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3933 typedef __allocator_destructor<_A2> _D2;
3934 _A2 __a2(__a);
3935 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3936 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
3937 __cntrl_ = __hold2.release();
3938 __enable_weak_this(__p);
3939#ifndef _LIBCPP_NO_EXCEPTIONS
3940 }
3941 catch (...)
3942 {
3943 __d(__p);
3944 throw;
3945 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003946#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003947}
3948
3949template<class _Tp>
3950template<class _Dp, class _Alloc>
3951shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
3952 : __ptr_(0)
3953{
3954#ifndef _LIBCPP_NO_EXCEPTIONS
3955 try
3956 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003957#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003958 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
3959 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3960 typedef __allocator_destructor<_A2> _D2;
3961 _A2 __a2(__a);
3962 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3963 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
3964 __cntrl_ = __hold2.release();
3965#ifndef _LIBCPP_NO_EXCEPTIONS
3966 }
3967 catch (...)
3968 {
3969 __d(__p);
3970 throw;
3971 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003972#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003973}
3974
3975template<class _Tp>
3976template<class _Yp>
3977inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003978shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003979 : __ptr_(__p),
3980 __cntrl_(__r.__cntrl_)
3981{
3982 if (__cntrl_)
3983 __cntrl_->__add_shared();
3984}
3985
3986template<class _Tp>
3987inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003988shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003989 : __ptr_(__r.__ptr_),
3990 __cntrl_(__r.__cntrl_)
3991{
3992 if (__cntrl_)
3993 __cntrl_->__add_shared();
3994}
3995
3996template<class _Tp>
3997template<class _Yp>
3998inline _LIBCPP_INLINE_VISIBILITY
3999shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4000 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004001 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004002 : __ptr_(__r.__ptr_),
4003 __cntrl_(__r.__cntrl_)
4004{
4005 if (__cntrl_)
4006 __cntrl_->__add_shared();
4007}
4008
Howard Hinnant73d21a42010-09-04 23:28:19 +00004009#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004010
4011template<class _Tp>
4012inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004013shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004014 : __ptr_(__r.__ptr_),
4015 __cntrl_(__r.__cntrl_)
4016{
4017 __r.__ptr_ = 0;
4018 __r.__cntrl_ = 0;
4019}
4020
4021template<class _Tp>
4022template<class _Yp>
4023inline _LIBCPP_INLINE_VISIBILITY
4024shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4025 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004026 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004027 : __ptr_(__r.__ptr_),
4028 __cntrl_(__r.__cntrl_)
4029{
4030 __r.__ptr_ = 0;
4031 __r.__cntrl_ = 0;
4032}
4033
Howard Hinnant73d21a42010-09-04 23:28:19 +00004034#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004035
4036template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004037template<class _Yp, class>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004038#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004039shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
4040#else
Howard Hinnant92172b82010-08-21 21:14:53 +00004041shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004042#endif
4043 : __ptr_(__r.get())
4044{
4045 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4046 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
4047 __enable_weak_this(__r.get());
4048 __r.release();
4049}
4050
4051template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004052template <class _Yp, class _Dp, class>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004053#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004054shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4055#else
4056shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4057#endif
4058 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
4059 : __ptr_(__r.get())
4060{
4061 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4062 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
4063 __enable_weak_this(__r.get());
4064 __r.release();
4065}
4066
4067template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004068template <class _Yp, class _Dp, class>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004069#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004070shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4071#else
4072shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4073#endif
4074 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
4075 : __ptr_(__r.get())
4076{
4077 typedef __shared_ptr_pointer<_Yp*,
4078 reference_wrapper<typename remove_reference<_Dp>::type>,
4079 allocator<_Yp> > _CntrlBlk;
4080 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
4081 __enable_weak_this(__r.get());
4082 __r.release();
4083}
4084
4085#ifndef _LIBCPP_HAS_NO_VARIADICS
4086
4087template<class _Tp>
4088template<class ..._Args>
4089shared_ptr<_Tp>
4090shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4091{
4092 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4093 typedef allocator<_CntrlBlk> _A2;
4094 typedef __allocator_destructor<_A2> _D2;
4095 _A2 __a2;
4096 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004097 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004098 shared_ptr<_Tp> __r;
4099 __r.__ptr_ = __hold2.get()->get();
4100 __r.__cntrl_ = __hold2.release();
4101 __r.__enable_weak_this(__r.__ptr_);
4102 return __r;
4103}
4104
4105template<class _Tp>
4106template<class _Alloc, class ..._Args>
4107shared_ptr<_Tp>
4108shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4109{
4110 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4111 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
4112 typedef __allocator_destructor<_A2> _D2;
4113 _A2 __a2(__a);
4114 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004115 ::new(__hold2.get()) _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004116 shared_ptr<_Tp> __r;
4117 __r.__ptr_ = __hold2.get()->get();
4118 __r.__cntrl_ = __hold2.release();
4119 __r.__enable_weak_this(__r.__ptr_);
4120 return __r;
4121}
4122
4123#else // _LIBCPP_HAS_NO_VARIADICS
4124
4125template<class _Tp>
4126shared_ptr<_Tp>
4127shared_ptr<_Tp>::make_shared()
4128{
4129 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4130 typedef allocator<_CntrlBlk> _Alloc2;
4131 typedef __allocator_destructor<_Alloc2> _D2;
4132 _Alloc2 __alloc2;
4133 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4134 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4135 shared_ptr<_Tp> __r;
4136 __r.__ptr_ = __hold2.get()->get();
4137 __r.__cntrl_ = __hold2.release();
4138 __r.__enable_weak_this(__r.__ptr_);
4139 return __r;
4140}
4141
4142template<class _Tp>
4143template<class _A0>
4144shared_ptr<_Tp>
4145shared_ptr<_Tp>::make_shared(_A0& __a0)
4146{
4147 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4148 typedef allocator<_CntrlBlk> _Alloc2;
4149 typedef __allocator_destructor<_Alloc2> _D2;
4150 _Alloc2 __alloc2;
4151 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4152 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4153 shared_ptr<_Tp> __r;
4154 __r.__ptr_ = __hold2.get()->get();
4155 __r.__cntrl_ = __hold2.release();
4156 __r.__enable_weak_this(__r.__ptr_);
4157 return __r;
4158}
4159
4160template<class _Tp>
4161template<class _A0, class _A1>
4162shared_ptr<_Tp>
4163shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4164{
4165 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4166 typedef allocator<_CntrlBlk> _Alloc2;
4167 typedef __allocator_destructor<_Alloc2> _D2;
4168 _Alloc2 __alloc2;
4169 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4170 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4171 shared_ptr<_Tp> __r;
4172 __r.__ptr_ = __hold2.get()->get();
4173 __r.__cntrl_ = __hold2.release();
4174 __r.__enable_weak_this(__r.__ptr_);
4175 return __r;
4176}
4177
4178template<class _Tp>
4179template<class _A0, class _A1, class _A2>
4180shared_ptr<_Tp>
4181shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4182{
4183 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4184 typedef allocator<_CntrlBlk> _Alloc2;
4185 typedef __allocator_destructor<_Alloc2> _D2;
4186 _Alloc2 __alloc2;
4187 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4188 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4189 shared_ptr<_Tp> __r;
4190 __r.__ptr_ = __hold2.get()->get();
4191 __r.__cntrl_ = __hold2.release();
4192 __r.__enable_weak_this(__r.__ptr_);
4193 return __r;
4194}
4195
4196template<class _Tp>
4197template<class _Alloc>
4198shared_ptr<_Tp>
4199shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4200{
4201 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4202 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4203 typedef __allocator_destructor<_Alloc2> _D2;
4204 _Alloc2 __alloc2(__a);
4205 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4206 ::new(__hold2.get()) _CntrlBlk(__a);
4207 shared_ptr<_Tp> __r;
4208 __r.__ptr_ = __hold2.get()->get();
4209 __r.__cntrl_ = __hold2.release();
4210 __r.__enable_weak_this(__r.__ptr_);
4211 return __r;
4212}
4213
4214template<class _Tp>
4215template<class _Alloc, class _A0>
4216shared_ptr<_Tp>
4217shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4218{
4219 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4220 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4221 typedef __allocator_destructor<_Alloc2> _D2;
4222 _Alloc2 __alloc2(__a);
4223 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4224 ::new(__hold2.get()) _CntrlBlk(__a, __a0);
4225 shared_ptr<_Tp> __r;
4226 __r.__ptr_ = __hold2.get()->get();
4227 __r.__cntrl_ = __hold2.release();
4228 __r.__enable_weak_this(__r.__ptr_);
4229 return __r;
4230}
4231
4232template<class _Tp>
4233template<class _Alloc, class _A0, class _A1>
4234shared_ptr<_Tp>
4235shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4236{
4237 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4238 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4239 typedef __allocator_destructor<_Alloc2> _D2;
4240 _Alloc2 __alloc2(__a);
4241 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4242 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
4243 shared_ptr<_Tp> __r;
4244 __r.__ptr_ = __hold2.get()->get();
4245 __r.__cntrl_ = __hold2.release();
4246 __r.__enable_weak_this(__r.__ptr_);
4247 return __r;
4248}
4249
4250template<class _Tp>
4251template<class _Alloc, class _A0, class _A1, class _A2>
4252shared_ptr<_Tp>
4253shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4254{
4255 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4256 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4257 typedef __allocator_destructor<_Alloc2> _D2;
4258 _Alloc2 __alloc2(__a);
4259 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4260 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
4261 shared_ptr<_Tp> __r;
4262 __r.__ptr_ = __hold2.get()->get();
4263 __r.__cntrl_ = __hold2.release();
4264 __r.__enable_weak_this(__r.__ptr_);
4265 return __r;
4266}
4267
4268#endif // _LIBCPP_HAS_NO_VARIADICS
4269
4270template<class _Tp>
4271shared_ptr<_Tp>::~shared_ptr()
4272{
4273 if (__cntrl_)
4274 __cntrl_->__release_shared();
4275}
4276
4277template<class _Tp>
4278inline _LIBCPP_INLINE_VISIBILITY
4279shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004280shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004281{
4282 shared_ptr(__r).swap(*this);
4283 return *this;
4284}
4285
4286template<class _Tp>
4287template<class _Yp>
4288inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004289typename enable_if
4290<
4291 is_convertible<_Yp*, _Tp*>::value,
4292 shared_ptr<_Tp>&
4293>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004294shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004295{
4296 shared_ptr(__r).swap(*this);
4297 return *this;
4298}
4299
Howard Hinnant73d21a42010-09-04 23:28:19 +00004300#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004301
4302template<class _Tp>
4303inline _LIBCPP_INLINE_VISIBILITY
4304shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004305shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004306{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004307 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004308 return *this;
4309}
4310
4311template<class _Tp>
4312template<class _Yp>
4313inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004314typename enable_if
4315<
4316 is_convertible<_Yp*, _Tp*>::value,
4317 shared_ptr<_Tp>&
4318>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004319shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4320{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004321 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004322 return *this;
4323}
4324
4325template<class _Tp>
4326template<class _Yp>
4327inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004328typename enable_if
4329<
4330 !is_array<_Yp>::value &&
4331 is_convertible<_Yp*, _Tp*>::value,
4332 shared_ptr<_Tp>&
4333>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004334shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4335{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004336 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004337 return *this;
4338}
4339
4340template<class _Tp>
4341template <class _Yp, class _Dp>
4342inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004343typename enable_if
4344<
4345 !is_array<_Yp>::value &&
4346 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4347 shared_ptr<_Tp>&
4348>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004349shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4350{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004351 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004352 return *this;
4353}
4354
Howard Hinnant73d21a42010-09-04 23:28:19 +00004355#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004356
4357template<class _Tp>
4358template<class _Yp>
4359inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004360typename enable_if
4361<
4362 !is_array<_Yp>::value &&
4363 is_convertible<_Yp*, _Tp*>::value,
4364 shared_ptr<_Tp>&
4365>::type
Howard Hinnant324bb032010-08-22 00:02:43 +00004366shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004367{
4368 shared_ptr(__r).swap(*this);
4369 return *this;
4370}
4371
4372template<class _Tp>
4373template <class _Yp, class _Dp>
4374inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004375typename enable_if
4376<
4377 !is_array<_Yp>::value &&
4378 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4379 shared_ptr<_Tp>&
4380>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004381shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4382{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004383 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004384 return *this;
4385}
4386
Howard Hinnant73d21a42010-09-04 23:28:19 +00004387#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004388
4389template<class _Tp>
4390inline _LIBCPP_INLINE_VISIBILITY
4391void
Howard Hinnant1694d232011-05-28 14:41:13 +00004392shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004393{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004394 _VSTD::swap(__ptr_, __r.__ptr_);
4395 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004396}
4397
4398template<class _Tp>
4399inline _LIBCPP_INLINE_VISIBILITY
4400void
Howard Hinnant1694d232011-05-28 14:41:13 +00004401shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004402{
4403 shared_ptr().swap(*this);
4404}
4405
4406template<class _Tp>
4407template<class _Yp>
4408inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004409typename enable_if
4410<
4411 is_convertible<_Yp*, _Tp*>::value,
4412 void
4413>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004414shared_ptr<_Tp>::reset(_Yp* __p)
4415{
4416 shared_ptr(__p).swap(*this);
4417}
4418
4419template<class _Tp>
4420template<class _Yp, class _Dp>
4421inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004422typename enable_if
4423<
4424 is_convertible<_Yp*, _Tp*>::value,
4425 void
4426>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004427shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4428{
4429 shared_ptr(__p, __d).swap(*this);
4430}
4431
4432template<class _Tp>
4433template<class _Yp, class _Dp, class _Alloc>
4434inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004435typename enable_if
4436<
4437 is_convertible<_Yp*, _Tp*>::value,
4438 void
4439>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004440shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4441{
4442 shared_ptr(__p, __d, __a).swap(*this);
4443}
4444
4445#ifndef _LIBCPP_HAS_NO_VARIADICS
4446
Howard Hinnant324bb032010-08-22 00:02:43 +00004447template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004448inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004449typename enable_if
4450<
4451 !is_array<_Tp>::value,
4452 shared_ptr<_Tp>
4453>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004454make_shared(_Args&& ...__args)
4455{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004456 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004457}
4458
Howard Hinnant324bb032010-08-22 00:02:43 +00004459template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004460inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004461typename enable_if
4462<
4463 !is_array<_Tp>::value,
4464 shared_ptr<_Tp>
4465>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004466allocate_shared(const _Alloc& __a, _Args&& ...__args)
4467{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004468 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004469}
4470
4471#else // _LIBCPP_HAS_NO_VARIADICS
4472
4473template<class _Tp>
4474inline _LIBCPP_INLINE_VISIBILITY
4475shared_ptr<_Tp>
4476make_shared()
4477{
4478 return shared_ptr<_Tp>::make_shared();
4479}
4480
4481template<class _Tp, class _A0>
4482inline _LIBCPP_INLINE_VISIBILITY
4483shared_ptr<_Tp>
4484make_shared(_A0& __a0)
4485{
4486 return shared_ptr<_Tp>::make_shared(__a0);
4487}
4488
4489template<class _Tp, class _A0, class _A1>
4490inline _LIBCPP_INLINE_VISIBILITY
4491shared_ptr<_Tp>
4492make_shared(_A0& __a0, _A1& __a1)
4493{
4494 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4495}
4496
Howard Hinnant324bb032010-08-22 00:02:43 +00004497template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004498inline _LIBCPP_INLINE_VISIBILITY
4499shared_ptr<_Tp>
4500make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4501{
4502 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4503}
4504
4505template<class _Tp, class _Alloc>
4506inline _LIBCPP_INLINE_VISIBILITY
4507shared_ptr<_Tp>
4508allocate_shared(const _Alloc& __a)
4509{
4510 return shared_ptr<_Tp>::allocate_shared(__a);
4511}
4512
4513template<class _Tp, class _Alloc, class _A0>
4514inline _LIBCPP_INLINE_VISIBILITY
4515shared_ptr<_Tp>
4516allocate_shared(const _Alloc& __a, _A0& __a0)
4517{
4518 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4519}
4520
4521template<class _Tp, class _Alloc, class _A0, class _A1>
4522inline _LIBCPP_INLINE_VISIBILITY
4523shared_ptr<_Tp>
4524allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4525{
4526 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4527}
4528
4529template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4530inline _LIBCPP_INLINE_VISIBILITY
4531shared_ptr<_Tp>
4532allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4533{
4534 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4535}
4536
4537#endif // _LIBCPP_HAS_NO_VARIADICS
4538
4539template<class _Tp, class _Up>
4540inline _LIBCPP_INLINE_VISIBILITY
4541bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004542operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004543{
4544 return __x.get() == __y.get();
4545}
4546
4547template<class _Tp, class _Up>
4548inline _LIBCPP_INLINE_VISIBILITY
4549bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004550operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004551{
4552 return !(__x == __y);
4553}
4554
4555template<class _Tp, class _Up>
4556inline _LIBCPP_INLINE_VISIBILITY
4557bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004558operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004559{
4560 return __x.get() < __y.get();
4561}
4562
4563template<class _Tp>
4564inline _LIBCPP_INLINE_VISIBILITY
4565void
Howard Hinnant1694d232011-05-28 14:41:13 +00004566swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004567{
4568 __x.swap(__y);
4569}
4570
4571template<class _Tp, class _Up>
4572inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004573typename enable_if
4574<
4575 !is_array<_Tp>::value && !is_array<_Up>::value,
4576 shared_ptr<_Tp>
4577>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004578static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004579{
4580 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4581}
4582
4583template<class _Tp, class _Up>
4584inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004585typename enable_if
4586<
4587 !is_array<_Tp>::value && !is_array<_Up>::value,
4588 shared_ptr<_Tp>
4589>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004590dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004591{
4592 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4593 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4594}
4595
4596template<class _Tp, class _Up>
Howard Hinnant57199402012-01-02 17:56:02 +00004597typename enable_if
4598<
4599 is_array<_Tp>::value == is_array<_Up>::value,
4600 shared_ptr<_Tp>
4601>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004602const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004603{
Howard Hinnant57199402012-01-02 17:56:02 +00004604 typedef typename remove_extent<_Tp>::type _RTp;
4605 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004606}
4607
Howard Hinnantd4444702010-08-11 17:04:31 +00004608#ifndef _LIBCPP_NO_RTTI
4609
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004610template<class _Dp, class _Tp>
4611inline _LIBCPP_INLINE_VISIBILITY
4612_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00004613get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004614{
4615 return __p.template __get_deleter<_Dp>();
4616}
4617
Howard Hinnant324bb032010-08-22 00:02:43 +00004618#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00004619
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004620template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004621class _LIBCPP_VISIBLE weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004622{
Howard Hinnant324bb032010-08-22 00:02:43 +00004623public:
4624 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004625private:
4626 element_type* __ptr_;
4627 __shared_weak_count* __cntrl_;
4628
Howard Hinnant324bb032010-08-22 00:02:43 +00004629public:
Howard Hinnant1694d232011-05-28 14:41:13 +00004630 weak_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004631 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00004632 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4633 _NOEXCEPT;
4634 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004635 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00004636 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4637 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004638
Howard Hinnant57199402012-01-02 17:56:02 +00004639#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4640 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
4641 template<class _Yp> weak_ptr(weak_ptr<_Yp>&& __r,
4642 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4643 _NOEXCEPT;
4644#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00004645 ~weak_ptr();
4646
Howard Hinnant1694d232011-05-28 14:41:13 +00004647 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00004648 template<class _Yp>
4649 typename enable_if
4650 <
4651 is_convertible<_Yp*, element_type*>::value,
4652 weak_ptr&
4653 >::type
4654 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4655
4656#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4657
4658 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4659 template<class _Yp>
4660 typename enable_if
4661 <
4662 is_convertible<_Yp*, element_type*>::value,
4663 weak_ptr&
4664 >::type
4665 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4666
4667#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4668
4669 template<class _Yp>
4670 typename enable_if
4671 <
4672 is_convertible<_Yp*, element_type*>::value,
4673 weak_ptr&
4674 >::type
4675 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004676
Howard Hinnant1694d232011-05-28 14:41:13 +00004677 void swap(weak_ptr& __r) _NOEXCEPT;
4678 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004679
Howard Hinnant82894812010-09-22 16:48:34 +00004680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004681 long use_count() const _NOEXCEPT
4682 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00004683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004684 bool expired() const _NOEXCEPT
4685 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4686 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00004687 template<class _Up>
4688 _LIBCPP_INLINE_VISIBILITY
4689 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004690 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004691 template<class _Up>
4692 _LIBCPP_INLINE_VISIBILITY
4693 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004694 {return __cntrl_ < __r.__cntrl_;}
4695
Howard Hinnant82894812010-09-22 16:48:34 +00004696 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
4697 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004698};
4699
4700template<class _Tp>
4701inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004702weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004703 : __ptr_(0),
4704 __cntrl_(0)
4705{
4706}
4707
4708template<class _Tp>
4709inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004710weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004711 : __ptr_(__r.__ptr_),
4712 __cntrl_(__r.__cntrl_)
4713{
4714 if (__cntrl_)
4715 __cntrl_->__add_weak();
4716}
4717
4718template<class _Tp>
4719template<class _Yp>
4720inline _LIBCPP_INLINE_VISIBILITY
4721weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00004722 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004723 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004724 : __ptr_(__r.__ptr_),
4725 __cntrl_(__r.__cntrl_)
4726{
4727 if (__cntrl_)
4728 __cntrl_->__add_weak();
4729}
4730
4731template<class _Tp>
4732template<class _Yp>
4733inline _LIBCPP_INLINE_VISIBILITY
4734weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00004735 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004736 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004737 : __ptr_(__r.__ptr_),
4738 __cntrl_(__r.__cntrl_)
4739{
4740 if (__cntrl_)
4741 __cntrl_->__add_weak();
4742}
4743
Howard Hinnant57199402012-01-02 17:56:02 +00004744#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4745
4746template<class _Tp>
4747inline _LIBCPP_INLINE_VISIBILITY
4748weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4749 : __ptr_(__r.__ptr_),
4750 __cntrl_(__r.__cntrl_)
4751{
4752 __r.__ptr_ = 0;
4753 __r.__cntrl_ = 0;
4754}
4755
4756template<class _Tp>
4757template<class _Yp>
4758inline _LIBCPP_INLINE_VISIBILITY
4759weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4760 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4761 _NOEXCEPT
4762 : __ptr_(__r.__ptr_),
4763 __cntrl_(__r.__cntrl_)
4764{
4765 __r.__ptr_ = 0;
4766 __r.__cntrl_ = 0;
4767}
4768
4769#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4770
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004771template<class _Tp>
4772weak_ptr<_Tp>::~weak_ptr()
4773{
4774 if (__cntrl_)
4775 __cntrl_->__release_weak();
4776}
4777
4778template<class _Tp>
4779inline _LIBCPP_INLINE_VISIBILITY
4780weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004781weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004782{
4783 weak_ptr(__r).swap(*this);
4784 return *this;
4785}
4786
4787template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00004788template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004789inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004790typename enable_if
4791<
4792 is_convertible<_Yp*, _Tp*>::value,
4793 weak_ptr<_Tp>&
4794>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004795weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004796{
4797 weak_ptr(__r).swap(*this);
4798 return *this;
4799}
4800
Howard Hinnant57199402012-01-02 17:56:02 +00004801#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4802
4803template<class _Tp>
4804inline _LIBCPP_INLINE_VISIBILITY
4805weak_ptr<_Tp>&
4806weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4807{
4808 weak_ptr(_VSTD::move(__r)).swap(*this);
4809 return *this;
4810}
4811
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004812template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00004813template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004814inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004815typename enable_if
4816<
4817 is_convertible<_Yp*, _Tp*>::value,
4818 weak_ptr<_Tp>&
4819>::type
4820weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4821{
4822 weak_ptr(_VSTD::move(__r)).swap(*this);
4823 return *this;
4824}
4825
4826#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4827
4828template<class _Tp>
4829template<class _Yp>
4830inline _LIBCPP_INLINE_VISIBILITY
4831typename enable_if
4832<
4833 is_convertible<_Yp*, _Tp*>::value,
4834 weak_ptr<_Tp>&
4835>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004836weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004837{
4838 weak_ptr(__r).swap(*this);
4839 return *this;
4840}
4841
4842template<class _Tp>
4843inline _LIBCPP_INLINE_VISIBILITY
4844void
Howard Hinnant1694d232011-05-28 14:41:13 +00004845weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004846{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004847 _VSTD::swap(__ptr_, __r.__ptr_);
4848 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004849}
4850
4851template<class _Tp>
4852inline _LIBCPP_INLINE_VISIBILITY
4853void
Howard Hinnant1694d232011-05-28 14:41:13 +00004854swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004855{
4856 __x.swap(__y);
4857}
4858
4859template<class _Tp>
4860inline _LIBCPP_INLINE_VISIBILITY
4861void
Howard Hinnant1694d232011-05-28 14:41:13 +00004862weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004863{
4864 weak_ptr().swap(*this);
4865}
4866
4867template<class _Tp>
4868template<class _Yp>
4869shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
4870 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
4871 : __ptr_(__r.__ptr_),
4872 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4873{
4874 if (__cntrl_ == 0)
4875#ifndef _LIBCPP_NO_EXCEPTIONS
4876 throw bad_weak_ptr();
4877#else
4878 assert(!"bad_weak_ptr");
4879#endif
4880}
4881
4882template<class _Tp>
4883shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00004884weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004885{
4886 shared_ptr<_Tp> __r;
4887 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4888 if (__r.__cntrl_)
4889 __r.__ptr_ = __ptr_;
4890 return __r;
4891}
4892
Howard Hinnant324bb032010-08-22 00:02:43 +00004893template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004894
4895template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004896struct _LIBCPP_VISIBLE owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004897 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00004898{
4899 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00004900 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004901 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
4902 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004903 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004904 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
4905 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004906 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004907 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
4908 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00004909};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004910
4911template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004912struct _LIBCPP_VISIBLE owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004913 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4914{
Howard Hinnant324bb032010-08-22 00:02:43 +00004915 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00004916 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004917 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
4918 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004919 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004920 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
4921 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004922 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004923 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
4924 {return __x.owner_before(__y);}
4925};
4926
4927template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004928class _LIBCPP_VISIBLE enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004929{
4930 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00004931protected:
Howard Hinnant82894812010-09-22 16:48:34 +00004932 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004933 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00004934 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004935 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00004936 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004937 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
4938 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00004939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004940 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00004941public:
Howard Hinnant82894812010-09-22 16:48:34 +00004942 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004943 shared_ptr<_Tp> shared_from_this()
4944 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00004945 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004946 shared_ptr<_Tp const> shared_from_this() const
4947 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004948
4949 template <class _Up> friend class shared_ptr;
4950};
4951
Howard Hinnant21aefc32010-06-03 16:42:57 +00004952template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004953struct _LIBCPP_VISIBLE hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00004954{
4955 typedef shared_ptr<_Tp> argument_type;
4956 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00004957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004958 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00004959 {
4960 return hash<_Tp*>()(__ptr.get());
4961 }
4962};
4963
Howard Hinnant99968442011-11-29 18:15:50 +00004964template<class _CharT, class _Traits, class _Yp>
Howard Hinnant464aa5c2011-07-18 15:51:59 +00004965inline _LIBCPP_INLINE_VISIBILITY
4966basic_ostream<_CharT, _Traits>&
Howard Hinnant99968442011-11-29 18:15:50 +00004967operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnant464aa5c2011-07-18 15:51:59 +00004968
Howard Hinnant324bb032010-08-22 00:02:43 +00004969//enum class
Howard Hinnant82894812010-09-22 16:48:34 +00004970struct _LIBCPP_VISIBLE pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004971{
4972 enum _
4973 {
4974 relaxed,
4975 preferred,
4976 strict
4977 };
4978
4979 _ __v_;
4980
Howard Hinnant82894812010-09-22 16:48:34 +00004981 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004982 pointer_safety(_ __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00004983 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004984 operator int() const {return __v_;}
4985};
4986
4987void declare_reachable(void* __p);
4988void declare_no_pointers(char* __p, size_t __n);
4989void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnant1694d232011-05-28 14:41:13 +00004990pointer_safety get_pointer_safety() _NOEXCEPT;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00004991void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004992
4993template <class _Tp>
4994inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00004995_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004996undeclare_reachable(_Tp* __p)
4997{
4998 return static_cast<_Tp*>(__undeclare_reachable(__p));
4999}
5000
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00005001void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005002
5003_LIBCPP_END_NAMESPACE_STD
5004
5005#endif // _LIBCPP_MEMORY