blob: 565aa7d5b40ebfbffd62981d70d88dc7b3278604 [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 Hinnantbc8d3f92010-05-11 19:42:16 +0000675// pointer_traits
676
677template <class _Tp>
678struct __has_element_type
679{
680private:
681 struct __two {char _; char __;};
682 template <class _Up> static __two __test(...);
683 template <class _Up> static char __test(typename _Up::element_type* = 0);
684public:
685 static const bool value = sizeof(__test<_Tp>(0)) == 1;
686};
687
688template <class _Ptr, bool = __has_element_type<_Ptr>::value>
689struct __pointer_traits_element_type;
690
691template <class _Ptr>
692struct __pointer_traits_element_type<_Ptr, true>
693{
694 typedef typename _Ptr::element_type type;
695};
696
697#ifndef _LIBCPP_HAS_NO_VARIADICS
698
699template <template <class, class...> class _Sp, class _Tp, class ..._Args>
700struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
701{
702 typedef typename _Sp<_Tp, _Args...>::element_type type;
703};
704
705template <template <class, class...> class _Sp, class _Tp, class ..._Args>
706struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
707{
708 typedef _Tp type;
709};
710
Howard Hinnant324bb032010-08-22 00:02:43 +0000711#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000712
713template <template <class> class _Sp, class _Tp>
714struct __pointer_traits_element_type<_Sp<_Tp>, true>
715{
716 typedef typename _Sp<_Tp>::element_type type;
717};
718
719template <template <class> class _Sp, class _Tp>
720struct __pointer_traits_element_type<_Sp<_Tp>, false>
721{
722 typedef _Tp type;
723};
724
725template <template <class, class> class _Sp, class _Tp, class _A0>
726struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
727{
728 typedef typename _Sp<_Tp, _A0>::element_type type;
729};
730
731template <template <class, class> class _Sp, class _Tp, class _A0>
732struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
733{
734 typedef _Tp type;
735};
736
737template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
738struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
739{
740 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
741};
742
743template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
744struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
745{
746 typedef _Tp type;
747};
748
749template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
750 class _A1, class _A2>
751struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
752{
753 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
754};
755
756template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
757 class _A1, class _A2>
758struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
759{
760 typedef _Tp type;
761};
762
Howard Hinnant324bb032010-08-22 00:02:43 +0000763#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000764
765template <class _Tp>
766struct __has_difference_type
767{
768private:
769 struct __two {char _; char __;};
770 template <class _Up> static __two __test(...);
771 template <class _Up> static char __test(typename _Up::difference_type* = 0);
772public:
773 static const bool value = sizeof(__test<_Tp>(0)) == 1;
774};
775
776template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
777struct __pointer_traits_difference_type
778{
779 typedef ptrdiff_t type;
780};
781
782template <class _Ptr>
783struct __pointer_traits_difference_type<_Ptr, true>
784{
785 typedef typename _Ptr::difference_type type;
786};
787
788template <class _Tp, class _Up>
789struct __has_rebind
790{
791private:
792 struct __two {char _; char __;};
793 template <class _Xp> static __two __test(...);
794 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
795public:
796 static const bool value = sizeof(__test<_Tp>(0)) == 1;
797};
798
799template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
800struct __pointer_traits_rebind
801{
802#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
803 typedef typename _Tp::template rebind<_Up> type;
804#else
805 typedef typename _Tp::template rebind<_Up>::other type;
806#endif
807};
808
809#ifndef _LIBCPP_HAS_NO_VARIADICS
810
811template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
812struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
813{
814#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
815 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
816#else
817 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
818#endif
819};
820
821template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
822struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
823{
824 typedef _Sp<_Up, _Args...> type;
825};
826
Howard Hinnant324bb032010-08-22 00:02:43 +0000827#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000828
829template <template <class> class _Sp, class _Tp, class _Up>
830struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
831{
832#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
833 typedef typename _Sp<_Tp>::template rebind<_Up> type;
834#else
835 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
836#endif
837};
838
839template <template <class> class _Sp, class _Tp, class _Up>
840struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
841{
842 typedef _Sp<_Up> type;
843};
844
845template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
846struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
847{
848#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
849 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
850#else
851 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
852#endif
853};
854
855template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
856struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
857{
858 typedef _Sp<_Up, _A0> type;
859};
860
861template <template <class, class, class> class _Sp, class _Tp, class _A0,
862 class _A1, class _Up>
863struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
864{
865#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
866 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
867#else
868 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
869#endif
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, false>
875{
876 typedef _Sp<_Up, _A0, _A1> type;
877};
878
879template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
880 class _A1, class _A2, class _Up>
881struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
882{
883#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
884 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
885#else
886 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
887#endif
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, false>
893{
894 typedef _Sp<_Up, _A0, _A1, _A2> type;
895};
896
Howard Hinnant324bb032010-08-22 00:02:43 +0000897#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000898
899template <class _Ptr>
Howard Hinnant82894812010-09-22 16:48:34 +0000900struct _LIBCPP_VISIBLE pointer_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000901{
902 typedef _Ptr pointer;
903 typedef typename __pointer_traits_element_type<pointer>::type element_type;
904 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
905
906#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant6b41c602011-05-11 20:21:19 +0000907 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000908#else
909 template <class _Up> struct rebind
910 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant324bb032010-08-22 00:02:43 +0000911#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000912
913private:
914 struct __nat {};
915public:
Howard Hinnant82894812010-09-22 16:48:34 +0000916 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000917 static pointer pointer_to(typename conditional<is_void<element_type>::value,
918 __nat, element_type>::type& __r)
919 {return pointer::pointer_to(__r);}
920};
921
922template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +0000923struct _LIBCPP_VISIBLE pointer_traits<_Tp*>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000924{
925 typedef _Tp* pointer;
926 typedef _Tp element_type;
927 typedef ptrdiff_t difference_type;
928
929#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
930 template <class _Up> using rebind = _Up*;
931#else
932 template <class _Up> struct rebind {typedef _Up* other;};
933#endif
934
935private:
936 struct __nat {};
937public:
Howard Hinnant82894812010-09-22 16:48:34 +0000938 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000939 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant1694d232011-05-28 14:41:13 +0000940 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000941 {return _VSTD::addressof(__r);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000942};
943
944// allocator_traits
945
946namespace __has_pointer_type_imp
947{
948 template <class _Up> static __two test(...);
949 template <class _Up> static char test(typename _Up::pointer* = 0);
950}
951
952template <class _Tp>
953struct __has_pointer_type
954 : public integral_constant<bool, sizeof(__has_pointer_type_imp::test<_Tp>(0)) == 1>
955{
956};
957
958namespace __pointer_type_imp
959{
960
961template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
962struct __pointer_type
963{
964 typedef typename _Dp::pointer type;
965};
966
967template <class _Tp, class _Dp>
968struct __pointer_type<_Tp, _Dp, false>
969{
970 typedef _Tp* type;
971};
972
Howard Hinnant47761072010-11-18 01:40:00 +0000973} // __pointer_type_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000974
975template <class _Tp, class _Dp>
976struct __pointer_type
977{
978 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
979};
980
981template <class _Tp>
982struct __has_const_pointer
983{
984private:
985 struct __two {char _; char __;};
986 template <class _Up> static __two __test(...);
987 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
988public:
989 static const bool value = sizeof(__test<_Tp>(0)) == 1;
990};
991
992template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
993struct __const_pointer
994{
995 typedef typename _Alloc::const_pointer type;
996};
997
998template <class _Tp, class _Ptr, class _Alloc>
999struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1000{
1001#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1002 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1003#else
1004 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1005#endif
1006};
1007
1008template <class _Tp>
1009struct __has_void_pointer
1010{
1011private:
1012 struct __two {char _; char __;};
1013 template <class _Up> static __two __test(...);
1014 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1015public:
1016 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1017};
1018
1019template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1020struct __void_pointer
1021{
1022 typedef typename _Alloc::void_pointer type;
1023};
1024
1025template <class _Ptr, class _Alloc>
1026struct __void_pointer<_Ptr, _Alloc, false>
1027{
1028#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1029 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1030#else
1031 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1032#endif
1033};
1034
1035template <class _Tp>
1036struct __has_const_void_pointer
1037{
1038private:
1039 struct __two {char _; char __;};
1040 template <class _Up> static __two __test(...);
1041 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1042public:
1043 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1044};
1045
1046template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1047struct __const_void_pointer
1048{
1049 typedef typename _Alloc::const_void_pointer type;
1050};
1051
1052template <class _Ptr, class _Alloc>
1053struct __const_void_pointer<_Ptr, _Alloc, false>
1054{
1055#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1056 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1057#else
1058 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1059#endif
1060};
1061
Howard Hinnant99968442011-11-29 18:15:50 +00001062template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001063inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001064_Tp*
1065__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001066{
1067 return __p;
1068}
1069
1070template <class _Pointer>
1071inline _LIBCPP_INLINE_VISIBILITY
1072typename pointer_traits<_Pointer>::element_type*
Howard Hinnant1694d232011-05-28 14:41:13 +00001073__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001075 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001076}
1077
1078template <class _Tp>
1079struct __has_size_type
1080{
1081private:
1082 struct __two {char _; char __;};
1083 template <class _Up> static __two __test(...);
1084 template <class _Up> static char __test(typename _Up::size_type* = 0);
1085public:
1086 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1087};
1088
Howard Hinnant47761072010-11-18 01:40:00 +00001089template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001090struct __size_type
1091{
Howard Hinnant47761072010-11-18 01:40:00 +00001092 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001093};
1094
Howard Hinnant47761072010-11-18 01:40:00 +00001095template <class _Alloc, class _DiffType>
1096struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001097{
1098 typedef typename _Alloc::size_type type;
1099};
1100
1101template <class _Tp>
1102struct __has_propagate_on_container_copy_assignment
1103{
1104private:
1105 struct __two {char _; char __;};
1106 template <class _Up> static __two __test(...);
1107 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1108public:
1109 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1110};
1111
1112template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1113struct __propagate_on_container_copy_assignment
1114{
1115 typedef false_type type;
1116};
1117
1118template <class _Alloc>
1119struct __propagate_on_container_copy_assignment<_Alloc, true>
1120{
1121 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1122};
1123
1124template <class _Tp>
1125struct __has_propagate_on_container_move_assignment
1126{
1127private:
1128 struct __two {char _; char __;};
1129 template <class _Up> static __two __test(...);
1130 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1131public:
1132 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1133};
1134
1135template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1136struct __propagate_on_container_move_assignment
1137{
1138 typedef false_type type;
1139};
1140
1141template <class _Alloc>
1142struct __propagate_on_container_move_assignment<_Alloc, true>
1143{
1144 typedef typename _Alloc::propagate_on_container_move_assignment type;
1145};
1146
1147template <class _Tp>
1148struct __has_propagate_on_container_swap
1149{
1150private:
1151 struct __two {char _; char __;};
1152 template <class _Up> static __two __test(...);
1153 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1154public:
1155 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1156};
1157
1158template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1159struct __propagate_on_container_swap
1160{
1161 typedef false_type type;
1162};
1163
1164template <class _Alloc>
1165struct __propagate_on_container_swap<_Alloc, true>
1166{
1167 typedef typename _Alloc::propagate_on_container_swap type;
1168};
1169
1170template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1171struct __has_rebind_other
1172{
1173private:
1174 struct __two {char _; char __;};
1175 template <class _Xp> static __two __test(...);
1176 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1177public:
1178 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1179};
1180
1181template <class _Tp, class _Up>
1182struct __has_rebind_other<_Tp, _Up, false>
1183{
1184 static const bool value = false;
1185};
1186
1187template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1188struct __allocator_traits_rebind
1189{
1190 typedef typename _Tp::template rebind<_Up>::other type;
1191};
1192
1193#ifndef _LIBCPP_HAS_NO_VARIADICS
1194
1195template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1196struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1197{
1198 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1199};
1200
1201template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1202struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1203{
1204 typedef _Alloc<_Up, _Args...> type;
1205};
1206
Howard Hinnant324bb032010-08-22 00:02:43 +00001207#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001208
1209template <template <class> class _Alloc, class _Tp, class _Up>
1210struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1211{
1212 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1213};
1214
1215template <template <class> class _Alloc, class _Tp, class _Up>
1216struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1217{
1218 typedef _Alloc<_Up> type;
1219};
1220
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001221template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1222struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1223{
1224 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1225};
1226
1227template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1228struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1229{
1230 typedef _Alloc<_Up, _A0> type;
1231};
1232
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001233template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1234 class _A1, class _Up>
1235struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1236{
1237 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1238};
1239
1240template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1241 class _A1, class _Up>
1242struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1243{
1244 typedef _Alloc<_Up, _A0, _A1> type;
1245};
1246
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001247template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1248 class _A1, class _A2, class _Up>
1249struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1250{
1251 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1252};
1253
1254template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1255 class _A1, class _A2, class _Up>
1256struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1257{
1258 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1259};
1260
Howard Hinnant324bb032010-08-22 00:02:43 +00001261#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001262
1263#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1264
1265template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1266auto
1267__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1268 -> decltype(__a.allocate(__sz, __p), true_type());
1269
1270template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1271auto
1272__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1273 -> false_type;
1274
1275template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1276struct __has_allocate_hint
1277 : integral_constant<bool,
1278 is_same<
1279 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1280 declval<_SizeType>(),
1281 declval<_ConstVoidPtr>())),
1282 true_type>::value>
1283{
1284};
1285
Howard Hinnant324bb032010-08-22 00:02:43 +00001286#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001287
1288template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1289struct __has_allocate_hint
1290 : true_type
1291{
1292};
1293
Howard Hinnant324bb032010-08-22 00:02:43 +00001294#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001295
Howard Hinnant23369ee2011-07-29 21:35:53 +00001296#if !defined(_LIBCPP_HAS_NO_ADVANCED_SFINAE) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001297
1298template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnant0949eed2011-06-30 21:18:19 +00001299decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1300 _VSTD::declval<_Args>()...),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001301 true_type())
1302__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1303
1304template <class _Alloc, class _Pointer, class ..._Args>
1305false_type
1306__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1307
1308template <class _Alloc, class _Pointer, class ..._Args>
1309struct __has_construct
1310 : integral_constant<bool,
1311 is_same<
1312 decltype(__has_construct_test(declval<_Alloc>(),
1313 declval<_Pointer>(),
1314 declval<_Args>()...)),
1315 true_type>::value>
1316{
1317};
1318
1319template <class _Alloc, class _Pointer>
1320auto
1321__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1322 -> decltype(__a.destroy(__p), true_type());
1323
1324template <class _Alloc, class _Pointer>
1325auto
1326__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1327 -> false_type;
1328
1329template <class _Alloc, class _Pointer>
1330struct __has_destroy
1331 : integral_constant<bool,
1332 is_same<
1333 decltype(__has_destroy_test(declval<_Alloc>(),
1334 declval<_Pointer>())),
1335 true_type>::value>
1336{
1337};
1338
1339template <class _Alloc>
1340auto
1341__has_max_size_test(_Alloc&& __a)
1342 -> decltype(__a.max_size(), true_type());
1343
1344template <class _Alloc>
1345auto
1346__has_max_size_test(const volatile _Alloc& __a)
1347 -> false_type;
1348
1349template <class _Alloc>
1350struct __has_max_size
1351 : integral_constant<bool,
1352 is_same<
1353 decltype(__has_max_size_test(declval<_Alloc&>())),
1354 true_type>::value>
1355{
1356};
1357
1358template <class _Alloc>
1359auto
1360__has_select_on_container_copy_construction_test(_Alloc&& __a)
1361 -> decltype(__a.select_on_container_copy_construction(), true_type());
1362
1363template <class _Alloc>
1364auto
1365__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1366 -> false_type;
1367
1368template <class _Alloc>
1369struct __has_select_on_container_copy_construction
1370 : integral_constant<bool,
1371 is_same<
1372 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1373 true_type>::value>
1374{
1375};
1376
Howard Hinnant324bb032010-08-22 00:02:43 +00001377#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001378
1379#ifndef _LIBCPP_HAS_NO_VARIADICS
1380
1381template <class _Alloc, class _Pointer, class ..._Args>
1382struct __has_construct
1383 : false_type
1384{
1385};
1386
Howard Hinnant324bb032010-08-22 00:02:43 +00001387#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001388
1389template <class _Alloc, class _Pointer>
1390struct __has_destroy
1391 : false_type
1392{
1393};
1394
1395template <class _Alloc>
1396struct __has_max_size
1397 : true_type
1398{
1399};
1400
1401template <class _Alloc>
1402struct __has_select_on_container_copy_construction
1403 : false_type
1404{
1405};
1406
Howard Hinnant324bb032010-08-22 00:02:43 +00001407#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001408
Howard Hinnant47761072010-11-18 01:40:00 +00001409template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1410struct __alloc_traits_difference_type
1411{
1412 typedef typename pointer_traits<_Ptr>::difference_type type;
1413};
1414
1415template <class _Alloc, class _Ptr>
1416struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1417{
1418 typedef typename _Alloc::difference_type type;
1419};
1420
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001421template <class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001422struct _LIBCPP_VISIBLE allocator_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001423{
1424 typedef _Alloc allocator_type;
1425 typedef typename allocator_type::value_type value_type;
1426
1427 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1428 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1429 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1430 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1431
Howard Hinnant47761072010-11-18 01:40:00 +00001432 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1433 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001434
1435 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1436 propagate_on_container_copy_assignment;
1437 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1438 propagate_on_container_move_assignment;
1439 typedef typename __propagate_on_container_swap<allocator_type>::type
1440 propagate_on_container_swap;
1441
1442#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1443 template <class _Tp> using rebind_alloc =
Howard Hinnant6b41c602011-05-11 20:21:19 +00001444 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001445 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant324bb032010-08-22 00:02:43 +00001446#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001447 template <class _Tp> struct rebind_alloc
1448 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1449 template <class _Tp> struct rebind_traits
1450 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant324bb032010-08-22 00:02:43 +00001451#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001452
Howard Hinnant82894812010-09-22 16:48:34 +00001453 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001454 static pointer allocate(allocator_type& __a, size_type __n)
1455 {return __a.allocate(__n);}
Howard Hinnant82894812010-09-22 16:48:34 +00001456 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001457 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1458 {return allocate(__a, __n, __hint,
1459 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1460
Howard Hinnant82894812010-09-22 16:48:34 +00001461 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001462 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001463 {__a.deallocate(__p, __n);}
1464
1465#ifndef _LIBCPP_HAS_NO_VARIADICS
1466 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001468 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
1469 {__construct(__has_construct<allocator_type, pointer, _Args...>(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001470 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001471#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001472 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001473 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001474 static void construct(allocator_type& __a, _Tp* __p)
1475 {
1476 ::new ((void*)__p) _Tp();
1477 }
1478 template <class _Tp, class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00001479 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001480 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1481 {
1482 ::new ((void*)__p) _Tp(__a0);
1483 }
1484 template <class _Tp, class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00001485 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001486 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1487 const _A1& __a1)
1488 {
1489 ::new ((void*)__p) _Tp(__a0, __a1);
1490 }
1491 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00001492 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001493 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1494 const _A1& __a1, const _A2& __a2)
1495 {
1496 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1497 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001498#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001499
1500 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001501 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001502 static void destroy(allocator_type& __a, _Tp* __p)
1503 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1504
Howard Hinnant82894812010-09-22 16:48:34 +00001505 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001506 static size_type max_size(const allocator_type& __a)
1507 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1508
Howard Hinnant82894812010-09-22 16:48:34 +00001509 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001510 static allocator_type
1511 select_on_container_copy_construction(const allocator_type& __a)
1512 {return select_on_container_copy_construction(
1513 __has_select_on_container_copy_construction<const allocator_type>(),
1514 __a);}
1515
1516private:
1517
Howard Hinnant82894812010-09-22 16:48:34 +00001518 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001519 static pointer allocate(allocator_type& __a, size_type __n,
1520 const_void_pointer __hint, true_type)
1521 {return __a.allocate(__n, __hint);}
Howard Hinnant82894812010-09-22 16:48:34 +00001522 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001523 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnantec3773c2011-12-01 20:21:04 +00001524 const_void_pointer, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001525 {return __a.allocate(__n);}
1526
1527#ifndef _LIBCPP_HAS_NO_VARIADICS
1528 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001531 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001532 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001534 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1535 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001536 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001537 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001538#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001539
1540 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001542 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1543 {__a.destroy(__p);}
1544 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001546 static void __destroy(false_type, allocator_type&, _Tp* __p)
1547 {
1548 __p->~_Tp();
1549 }
1550
Howard Hinnant82894812010-09-22 16:48:34 +00001551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001552 static size_type __max_size(true_type, const allocator_type& __a)
1553 {return __a.max_size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001555 static size_type __max_size(false_type, const allocator_type&)
1556 {return numeric_limits<size_type>::max();}
1557
Howard Hinnant82894812010-09-22 16:48:34 +00001558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001559 static allocator_type
1560 select_on_container_copy_construction(true_type, const allocator_type& __a)
1561 {return __a.select_on_container_copy_construction();}
Howard Hinnant82894812010-09-22 16:48:34 +00001562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001563 static allocator_type
1564 select_on_container_copy_construction(false_type, const allocator_type& __a)
1565 {return __a;}
1566};
1567
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001568// allocator
1569
1570template <class _Tp>
Howard Hinnant36cdf022010-09-10 16:42:26 +00001571class _LIBCPP_VISIBLE allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001572{
1573public:
1574 typedef size_t size_type;
1575 typedef ptrdiff_t difference_type;
1576 typedef _Tp* pointer;
1577 typedef const _Tp* const_pointer;
1578 typedef _Tp& reference;
1579 typedef const _Tp& const_reference;
1580 typedef _Tp value_type;
1581
Howard Hinnant18884f42011-06-02 21:38:57 +00001582 typedef true_type propagate_on_container_move_assignment;
1583
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001584 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1585
Howard Hinnant1694d232011-05-28 14:41:13 +00001586 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1587 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1588 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001589 {return _VSTD::addressof(__x);}
Howard Hinnant1694d232011-05-28 14:41:13 +00001590 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001591 {return _VSTD::addressof(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001592 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1593 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
Howard Hinnant1694d232011-05-28 14:41:13 +00001594 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
1595 {::operator delete((void*)__p);}
1596 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1597 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001598#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001599 template <class _Up, class... _Args>
1600 _LIBCPP_INLINE_VISIBILITY
1601 void
1602 construct(_Up* __p, _Args&&... __args)
1603 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001604 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001605 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001606#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001607 _LIBCPP_INLINE_VISIBILITY
1608 void
1609 construct(pointer __p)
1610 {
1611 ::new((void*)__p) _Tp();
1612 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001613# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001614 template <class _A0>
1615 _LIBCPP_INLINE_VISIBILITY
1616 typename enable_if
1617 <
1618 !is_convertible<_A0, __rv<_A0> >::value,
1619 void
1620 >::type
1621 construct(pointer __p, _A0& __a0)
1622 {
1623 ::new((void*)__p) _Tp(__a0);
1624 }
1625 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, const _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, _A0 __a0)
1644 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001645 ::new((void*)__p) _Tp(_VSTD::move(__a0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001646 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001647# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001648 template <class _A0, class _A1>
1649 _LIBCPP_INLINE_VISIBILITY
1650 void
1651 construct(pointer __p, _A0& __a0, _A1& __a1)
1652 {
1653 ::new((void*)__p) _Tp(__a0, __a1);
1654 }
1655 template <class _A0, class _A1>
1656 _LIBCPP_INLINE_VISIBILITY
1657 void
1658 construct(pointer __p, const _A0& __a0, _A1& __a1)
1659 {
1660 ::new((void*)__p) _Tp(__a0, __a1);
1661 }
1662 template <class _A0, class _A1>
1663 _LIBCPP_INLINE_VISIBILITY
1664 void
1665 construct(pointer __p, _A0& __a0, const _A1& __a1)
1666 {
1667 ::new((void*)__p) _Tp(__a0, __a1);
1668 }
1669 template <class _A0, class _A1>
1670 _LIBCPP_INLINE_VISIBILITY
1671 void
1672 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1673 {
1674 ::new((void*)__p) _Tp(__a0, __a1);
1675 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001676#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001677 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1678};
1679
1680template <class _Tp, class _Up>
1681inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001682bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001683
1684template <class _Tp, class _Up>
1685inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001686bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001687
1688template <class _OutputIterator, class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001689class _LIBCPP_VISIBLE raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001690 : public iterator<output_iterator_tag,
1691 _Tp, // purposefully not C++03
1692 ptrdiff_t, // purposefully not C++03
1693 _Tp*, // purposefully not C++03
1694 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1695{
1696private:
1697 _OutputIterator __x_;
1698public:
1699 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1700 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1701 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1702 {::new(&*__x_) _Tp(__element); return *this;}
1703 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1704 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1705 {raw_storage_iterator __t(*this); ++__x_; return __t;}
1706};
1707
1708template <class _Tp>
1709pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00001710get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001711{
1712 pair<_Tp*, ptrdiff_t> __r(0, 0);
1713 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1714 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1715 / sizeof(_Tp);
1716 if (__n > __m)
1717 __n = __m;
1718 while (__n > 0)
1719 {
1720 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1721 if (__r.first)
1722 {
1723 __r.second = __n;
1724 break;
1725 }
1726 __n /= 2;
1727 }
1728 return __r;
1729}
1730
1731template <class _Tp>
1732inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001733void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001734
1735template <class _Tp>
1736struct auto_ptr_ref
1737{
1738 _Tp* __ptr_;
1739};
1740
1741template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001742class _LIBCPP_VISIBLE auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001743{
1744private:
1745 _Tp* __ptr_;
1746public:
1747 typedef _Tp element_type;
1748
1749 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1750 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1751 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1752 : __ptr_(__p.release()) {}
1753 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1754 {reset(__p.release()); return *this;}
1755 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1756 {reset(__p.release()); return *this;}
1757 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1758 {reset(__p.__ptr_); return *this;}
1759 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1760
1761 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1762 {return *__ptr_;}
1763 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1764 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1765 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1766 {
1767 _Tp* __t = __ptr_;
1768 __ptr_ = 0;
1769 return __t;
1770 }
1771 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1772 {
1773 if (__ptr_ != __p)
1774 delete __ptr_;
1775 __ptr_ = __p;
1776 }
1777
1778 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1779 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1780 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1781 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1782 {return auto_ptr<_Up>(release());}
1783};
1784
1785template <>
Howard Hinnant82894812010-09-22 16:48:34 +00001786class _LIBCPP_VISIBLE auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001787{
1788public:
1789 typedef void element_type;
1790};
1791
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001792template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1793 typename remove_cv<_T2>::type>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00001794 bool = is_empty<_T1>::value
1795#if __has_feature(is_final)
1796 && !__is_final(_T1)
1797#endif
1798 ,
1799 bool = is_empty<_T2>::value
1800#if __has_feature(is_final)
1801 && !__is_final(_T2)
1802#endif
1803 >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001804struct __libcpp_compressed_pair_switch;
1805
1806template <class _T1, class _T2, bool IsSame>
1807struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1808
1809template <class _T1, class _T2, bool IsSame>
1810struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1811
1812template <class _T1, class _T2, bool IsSame>
1813struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1814
1815template <class _T1, class _T2>
1816struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
1817
1818template <class _T1, class _T2>
1819struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
1820
1821template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
1822class __libcpp_compressed_pair_imp;
1823
1824template <class _T1, class _T2>
1825class __libcpp_compressed_pair_imp<_T1, _T2, 0>
1826{
1827private:
1828 _T1 __first_;
1829 _T2 __second_;
1830public:
1831 typedef _T1 _T1_param;
1832 typedef _T2 _T2_param;
1833
1834 typedef typename remove_reference<_T1>::type& _T1_reference;
1835 typedef typename remove_reference<_T2>::type& _T2_reference;
1836
1837 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1838 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1839
1840 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1841 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001842 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001843 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001844 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001845 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001846 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001847
Howard Hinnant61aa6012011-07-01 19:24:36 +00001848#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
1849
1850 _LIBCPP_INLINE_VISIBILITY
1851 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
1852 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
1853 is_nothrow_copy_constructible<_T2>::value)
1854 : __first_(__p.first()),
1855 __second_(__p.second()) {}
1856
1857 _LIBCPP_INLINE_VISIBILITY
1858 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
1859 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
1860 is_nothrow_copy_assignable<_T2>::value)
1861 {
1862 __first_ = __p.first();
1863 __second_ = __p.second();
1864 return *this;
1865 }
1866
Howard Hinnant73d21a42010-09-04 23:28:19 +00001867#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00001868
1869 _LIBCPP_INLINE_VISIBILITY
1870 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00001871 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
1872 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant61aa6012011-07-01 19:24:36 +00001873 : __first_(_VSTD::forward<_T1>(__p.first())),
1874 __second_(_VSTD::forward<_T2>(__p.second())) {}
1875
1876 _LIBCPP_INLINE_VISIBILITY
1877 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
1878 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
1879 is_nothrow_move_assignable<_T2>::value)
1880 {
1881 __first_ = _VSTD::forward<_T1>(__p.first());
1882 __second_ = _VSTD::forward<_T2>(__p.second());
1883 return *this;
1884 }
1885
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00001886#ifndef _LIBCPP_HAS_NO_VARIADICS
1887
1888 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
1889 _LIBCPP_INLINE_VISIBILITY
1890 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
1891 tuple<_Args1...> __first_args,
1892 tuple<_Args2...> __second_args,
1893 __tuple_indices<_I1...>,
1894 __tuple_indices<_I2...>)
1895 : __first_(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
1896 __second_(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
1897 {}
1898
1899#endif // _LIBCPP_HAS_NO_VARIADICS
1900
Howard Hinnant73d21a42010-09-04 23:28:19 +00001901#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001902
Howard Hinnant61aa6012011-07-01 19:24:36 +00001903#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
1904
Howard Hinnant1694d232011-05-28 14:41:13 +00001905 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
1906 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001907
Howard Hinnant1694d232011-05-28 14:41:13 +00001908 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
1909 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001910
1911 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00001912 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1913 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001914 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001915 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001916 swap(__first_, __x.__first_);
1917 swap(__second_, __x.__second_);
1918 }
1919};
1920
1921template <class _T1, class _T2>
1922class __libcpp_compressed_pair_imp<_T1, _T2, 1>
1923 : private _T1
1924{
1925private:
1926 _T2 __second_;
1927public:
1928 typedef _T1 _T1_param;
1929 typedef _T2 _T2_param;
1930
1931 typedef _T1& _T1_reference;
1932 typedef typename remove_reference<_T2>::type& _T2_reference;
1933
1934 typedef const _T1& _T1_const_reference;
1935 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1936
1937 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1938 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001939 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001940 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001941 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001942 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001943 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001944
Howard Hinnant61aa6012011-07-01 19:24:36 +00001945#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
1946
1947 _LIBCPP_INLINE_VISIBILITY
1948 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
1949 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
1950 is_nothrow_copy_constructible<_T2>::value)
1951 : _T1(__p.first()), __second_(__p.second()) {}
1952
1953 _LIBCPP_INLINE_VISIBILITY
1954 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
1955 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
1956 is_nothrow_copy_assignable<_T2>::value)
1957 {
1958 _T1::operator=(__p.first());
1959 __second_ = __p.second();
1960 return *this;
1961 }
1962
Howard Hinnant73d21a42010-09-04 23:28:19 +00001963#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00001964
1965 _LIBCPP_INLINE_VISIBILITY
1966 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00001967 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
1968 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001969 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00001970
1971 _LIBCPP_INLINE_VISIBILITY
1972 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
1973 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
1974 is_nothrow_move_assignable<_T2>::value)
1975 {
1976 _T1::operator=(_VSTD::move(__p.first()));
1977 __second_ = _VSTD::forward<_T2>(__p.second());
1978 return *this;
1979 }
1980
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00001981#ifndef _LIBCPP_HAS_NO_VARIADICS
1982
1983 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
1984 _LIBCPP_INLINE_VISIBILITY
1985 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
1986 tuple<_Args1...> __first_args,
1987 tuple<_Args2...> __second_args,
1988 __tuple_indices<_I1...>,
1989 __tuple_indices<_I2...>)
1990 : _T1(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
1991 __second_(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
1992 {}
1993
1994#endif // _LIBCPP_HAS_NO_VARIADICS
1995
Howard Hinnant73d21a42010-09-04 23:28:19 +00001996#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001997
Howard Hinnant61aa6012011-07-01 19:24:36 +00001998#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
1999
Howard Hinnant1694d232011-05-28 14:41:13 +00002000 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2001 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002002
Howard Hinnant1694d232011-05-28 14:41:13 +00002003 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2004 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002005
2006 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002007 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2008 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002009 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002010 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002011 swap(__second_, __x.__second_);
2012 }
2013};
2014
2015template <class _T1, class _T2>
2016class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2017 : private _T2
2018{
2019private:
2020 _T1 __first_;
2021public:
2022 typedef _T1 _T1_param;
2023 typedef _T2 _T2_param;
2024
2025 typedef typename remove_reference<_T1>::type& _T1_reference;
2026 typedef _T2& _T2_reference;
2027
2028 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2029 typedef const _T2& _T2_const_reference;
2030
2031 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2032 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002033 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002034 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002035 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002036 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00002037 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2038 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002039 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002040
Howard Hinnant61aa6012011-07-01 19:24:36 +00002041#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2042
2043 _LIBCPP_INLINE_VISIBILITY
2044 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2045 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2046 is_nothrow_copy_constructible<_T2>::value)
2047 : _T2(__p.second()), __first_(__p.first()) {}
2048
2049 _LIBCPP_INLINE_VISIBILITY
2050 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2051 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2052 is_nothrow_copy_assignable<_T2>::value)
2053 {
2054 _T2::operator=(__p.second());
2055 __first_ = __p.first();
2056 return *this;
2057 }
2058
Howard Hinnant73d21a42010-09-04 23:28:19 +00002059#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002060
2061 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002062 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002063 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2064 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002065 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002066
2067 _LIBCPP_INLINE_VISIBILITY
2068 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2069 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2070 is_nothrow_move_assignable<_T2>::value)
2071 {
2072 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2073 __first_ = _VSTD::move(__p.first());
2074 return *this;
2075 }
2076
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002077#ifndef _LIBCPP_HAS_NO_VARIADICS
2078
2079 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2080 _LIBCPP_INLINE_VISIBILITY
2081 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2082 tuple<_Args1...> __first_args,
2083 tuple<_Args2...> __second_args,
2084 __tuple_indices<_I1...>,
2085 __tuple_indices<_I2...>)
2086 : _T2(_VSTD::forward<_Args2>(get<_I2>(__second_args))...),
2087 __first_(_VSTD::forward<_Args1>(get<_I1>(__first_args))...)
2088
2089 {}
2090
2091#endif // _LIBCPP_HAS_NO_VARIADICS
2092
Howard Hinnant73d21a42010-09-04 23:28:19 +00002093#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002094
Howard Hinnant61aa6012011-07-01 19:24:36 +00002095#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2096
Howard Hinnant1694d232011-05-28 14:41:13 +00002097 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2098 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002099
Howard Hinnant1694d232011-05-28 14:41:13 +00002100 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2101 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002102
2103 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002104 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2105 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002106 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002107 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002108 swap(__first_, __x.__first_);
2109 }
2110};
2111
2112template <class _T1, class _T2>
2113class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2114 : private _T1,
2115 private _T2
2116{
2117public:
2118 typedef _T1 _T1_param;
2119 typedef _T2 _T2_param;
2120
2121 typedef _T1& _T1_reference;
2122 typedef _T2& _T2_reference;
2123
2124 typedef const _T1& _T1_const_reference;
2125 typedef const _T2& _T2_const_reference;
2126
2127 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2128 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002129 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002130 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002131 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002132 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002133 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002134
Howard Hinnant61aa6012011-07-01 19:24:36 +00002135#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2136
2137 _LIBCPP_INLINE_VISIBILITY
2138 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2139 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2140 is_nothrow_copy_constructible<_T2>::value)
2141 : _T1(__p.first()), _T2(__p.second()) {}
2142
2143 _LIBCPP_INLINE_VISIBILITY
2144 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2145 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2146 is_nothrow_copy_assignable<_T2>::value)
2147 {
2148 _T1::operator=(__p.first());
2149 _T2::operator=(__p.second());
2150 return *this;
2151 }
2152
Howard Hinnant73d21a42010-09-04 23:28:19 +00002153#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002154
2155 _LIBCPP_INLINE_VISIBILITY
2156 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002157 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2158 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002159 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002160
2161 _LIBCPP_INLINE_VISIBILITY
2162 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2163 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2164 is_nothrow_move_assignable<_T2>::value)
2165 {
2166 _T1::operator=(_VSTD::move(__p.first()));
2167 _T2::operator=(_VSTD::move(__p.second()));
2168 return *this;
2169 }
2170
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002171#ifndef _LIBCPP_HAS_NO_VARIADICS
2172
2173 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2174 _LIBCPP_INLINE_VISIBILITY
2175 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2176 tuple<_Args1...> __first_args,
2177 tuple<_Args2...> __second_args,
2178 __tuple_indices<_I1...>,
2179 __tuple_indices<_I2...>)
2180 : _T1(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
2181 _T2(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
2182 {}
2183
2184#endif // _LIBCPP_HAS_NO_VARIADICS
2185
Howard Hinnant73d21a42010-09-04 23:28:19 +00002186#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002187
Howard Hinnant61aa6012011-07-01 19:24:36 +00002188#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2189
Howard Hinnant1694d232011-05-28 14:41:13 +00002190 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2191 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002192
Howard Hinnant1694d232011-05-28 14:41:13 +00002193 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2194 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002195
Howard Hinnantec3773c2011-12-01 20:21:04 +00002196 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant1694d232011-05-28 14:41:13 +00002197 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2198 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002199 {
2200 }
2201};
2202
2203template <class _T1, class _T2>
2204class __compressed_pair
2205 : private __libcpp_compressed_pair_imp<_T1, _T2>
2206{
2207 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2208public:
2209 typedef typename base::_T1_param _T1_param;
2210 typedef typename base::_T2_param _T2_param;
2211
2212 typedef typename base::_T1_reference _T1_reference;
2213 typedef typename base::_T2_reference _T2_reference;
2214
2215 typedef typename base::_T1_const_reference _T1_const_reference;
2216 typedef typename base::_T2_const_reference _T2_const_reference;
2217
2218 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
2219 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1, int = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002220 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002221 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2, int* = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002222 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002223 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002224 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002225
Howard Hinnant61aa6012011-07-01 19:24:36 +00002226#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2227
2228 _LIBCPP_INLINE_VISIBILITY
2229 __compressed_pair(const __compressed_pair& __p)
2230 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2231 is_nothrow_copy_constructible<_T2>::value)
2232 : base(__p) {}
2233
2234 _LIBCPP_INLINE_VISIBILITY
2235 __compressed_pair& operator=(const __compressed_pair& __p)
2236 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2237 is_nothrow_copy_assignable<_T2>::value)
2238 {
2239 base::operator=(__p);
2240 return *this;
2241 }
2242
Howard Hinnant73d21a42010-09-04 23:28:19 +00002243#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002244 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002245 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002246 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2247 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002248 : base(_VSTD::move(__p)) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002249
2250 _LIBCPP_INLINE_VISIBILITY
2251 __compressed_pair& operator=(__compressed_pair&& __p)
2252 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2253 is_nothrow_move_assignable<_T2>::value)
2254 {
2255 base::operator=(_VSTD::move(__p));
2256 return *this;
2257 }
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002258
2259#ifndef _LIBCPP_HAS_NO_VARIADICS
2260
2261 template <class... _Args1, class... _Args2>
2262 _LIBCPP_INLINE_VISIBILITY
2263 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2264 tuple<_Args2...> __second_args)
2265 : base(__pc, __first_args, __second_args,
2266 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2267 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2268 {}
2269
2270#endif // _LIBCPP_HAS_NO_VARIADICS
2271
Howard Hinnant73d21a42010-09-04 23:28:19 +00002272#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002273
Howard Hinnant61aa6012011-07-01 19:24:36 +00002274#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2275
Howard Hinnant1694d232011-05-28 14:41:13 +00002276 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2277 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002278
Howard Hinnant1694d232011-05-28 14:41:13 +00002279 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2280 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002281
Howard Hinnant1694d232011-05-28 14:41:13 +00002282 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2283 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2284 __is_nothrow_swappable<_T1>::value)
2285 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002286};
2287
2288template <class _T1, class _T2>
2289inline _LIBCPP_INLINE_VISIBILITY
2290void
2291swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002292 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2293 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002294 {__x.swap(__y);}
2295
2296template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002297struct _LIBCPP_VISIBLE default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002298{
Howard Hinnant1694d232011-05-28 14:41:13 +00002299 _LIBCPP_INLINE_VISIBILITY default_delete() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002300 template <class _Up>
2301 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002302 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2303 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002304 {
2305 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2306 delete __ptr;
2307 }
2308};
2309
2310template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002311struct _LIBCPP_VISIBLE default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002312{
Howard Hinnant8e843502011-12-18 21:19:44 +00002313private:
2314 template <class _P1,
2315 bool = is_same<typename remove_cv<typename pointer_traits<_P1>::element_type>::type,
2316 typename remove_cv<_Tp>::type>::value>
2317 struct __same_or_less_cv_qualified_imp
2318 : is_convertible<_P1, _Tp*> {};
2319 template <class _P1>
2320 struct __same_or_less_cv_qualified_imp<_P1, false>
2321 : false_type {};
2322
2323 template <class _P1, bool = is_scalar<_P1>::value && !is_pointer<_P1>::value>
2324 struct __same_or_less_cv_qualified
2325 : __same_or_less_cv_qualified_imp<_P1> {};
2326
2327 template <class _P1>
2328 struct __same_or_less_cv_qualified<_P1, true>
2329 : false_type {};
2330
2331public:
2332 _LIBCPP_INLINE_VISIBILITY default_delete() _NOEXCEPT {}
2333 template <class _Up>
2334 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
2335 typename enable_if<__same_or_less_cv_qualified<_Up*>::value>::type* = 0) _NOEXCEPT {}
2336 template <class _Up>
2337 _LIBCPP_INLINE_VISIBILITY
2338 void operator() (_Up* __ptr,
2339 typename enable_if<__same_or_less_cv_qualified<_Up*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002340 {
2341 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2342 delete [] __ptr;
2343 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002344};
2345
2346template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant82894812010-09-22 16:48:34 +00002347class _LIBCPP_VISIBLE unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002348{
2349public:
2350 typedef _Tp element_type;
2351 typedef _Dp deleter_type;
2352 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2353private:
2354 __compressed_pair<pointer, deleter_type> __ptr_;
2355
Howard Hinnant73d21a42010-09-04 23:28:19 +00002356#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002357 unique_ptr(const unique_ptr&);
2358 unique_ptr& operator=(const unique_ptr&);
2359 template <class _Up, class _Ep>
2360 unique_ptr(const unique_ptr<_Up, _Ep>&);
2361 template <class _Up, class _Ep>
2362 unique_ptr& operator=(const unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002363#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002364 unique_ptr(unique_ptr&);
2365 template <class _Up, class _Ep>
2366 unique_ptr(unique_ptr<_Up, _Ep>&);
2367 unique_ptr& operator=(unique_ptr&);
2368 template <class _Up, class _Ep>
2369 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002370#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002371
2372 struct __nat {int __for_bool_;};
2373
2374 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2375 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2376public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002377 _LIBCPP_INLINE_VISIBILITY unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002378 : __ptr_(pointer())
2379 {
2380 static_assert(!is_pointer<deleter_type>::value,
2381 "unique_ptr constructed with null function pointer deleter");
2382 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002383 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002384 : __ptr_(pointer())
2385 {
2386 static_assert(!is_pointer<deleter_type>::value,
2387 "unique_ptr constructed with null function pointer deleter");
2388 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002389 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002390 : __ptr_(_VSTD::move(__p))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002391 {
2392 static_assert(!is_pointer<deleter_type>::value,
2393 "unique_ptr constructed with null function pointer deleter");
2394 }
2395
Howard Hinnant73d21a42010-09-04 23:28:19 +00002396#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002397 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2398 is_reference<deleter_type>::value,
2399 deleter_type,
2400 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002401 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002402 : __ptr_(__p, __d) {}
2403
2404 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002405 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002406 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002407 {
2408 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2409 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002410 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002411 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002412 template <class _Up, class _Ep>
2413 _LIBCPP_INLINE_VISIBILITY
2414 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2415 typename enable_if
2416 <
2417 !is_array<_Up>::value &&
2418 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2419 is_convertible<_Ep, deleter_type>::value &&
2420 (
2421 !is_reference<deleter_type>::value ||
2422 is_same<deleter_type, _Ep>::value
2423 ),
2424 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002425 >::type = __nat()) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002426 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002427
2428 template <class _Up>
2429 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2430 typename enable_if<
2431 is_convertible<_Up*, _Tp*>::value &&
2432 is_same<_Dp, default_delete<_Tp> >::value,
2433 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002434 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002435 : __ptr_(__p.release())
2436 {
2437 }
2438
Howard Hinnant1694d232011-05-28 14:41:13 +00002439 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002440 {
2441 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002442 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002443 return *this;
2444 }
2445
2446 template <class _Up, class _Ep>
2447 _LIBCPP_INLINE_VISIBILITY
2448 typename enable_if
2449 <
2450 !is_array<_Up>::value,
2451 unique_ptr&
2452 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002453 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002454 {
2455 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002456 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002457 return *this;
2458 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002459#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002460
2461 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2462 {
2463 return __rv<unique_ptr>(*this);
2464 }
2465
2466 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002467 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002468
2469 template <class _Up, class _Ep>
2470 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2471 {
2472 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002473 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002474 return *this;
2475 }
2476
2477 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002478 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002479
2480 template <class _Up>
2481 _LIBCPP_INLINE_VISIBILITY
2482 typename enable_if<
2483 is_convertible<_Up*, _Tp*>::value &&
2484 is_same<_Dp, default_delete<_Tp> >::value,
2485 unique_ptr&
2486 >::type
2487 operator=(auto_ptr<_Up> __p)
2488 {reset(__p.release()); return *this;}
2489
Howard Hinnant73d21a42010-09-04 23:28:19 +00002490#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002491 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2492
Howard Hinnant1694d232011-05-28 14:41:13 +00002493 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002494 {
2495 reset();
2496 return *this;
2497 }
2498
2499 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2500 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002501 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2502 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2503 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2504 {return __ptr_.second();}
2505 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2506 {return __ptr_.second();}
2507 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const
2508 _NOEXCEPT
2509 {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002510
Howard Hinnant1694d232011-05-28 14:41:13 +00002511 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002512 {
2513 pointer __t = __ptr_.first();
2514 __ptr_.first() = pointer();
2515 return __t;
2516 }
2517
Howard Hinnant1694d232011-05-28 14:41:13 +00002518 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002519 {
2520 pointer __tmp = __ptr_.first();
2521 __ptr_.first() = __p;
2522 if (__tmp)
2523 __ptr_.second()(__tmp);
2524 }
2525
Howard Hinnant1694d232011-05-28 14:41:13 +00002526 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2527 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002528};
2529
2530template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002531class _LIBCPP_VISIBLE unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002532{
2533public:
2534 typedef _Tp element_type;
2535 typedef _Dp deleter_type;
2536 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2537private:
2538 __compressed_pair<pointer, deleter_type> __ptr_;
2539
Howard Hinnant73d21a42010-09-04 23:28:19 +00002540#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002541 unique_ptr(const unique_ptr&);
2542 unique_ptr& operator=(const unique_ptr&);
Howard Hinnant0a631192011-12-16 15:37:23 +00002543
Howard Hinnant8e843502011-12-18 21:19:44 +00002544 template <class _P1,
2545 bool = is_same<typename remove_cv<typename pointer_traits<_P1>::element_type>::type,
2546 typename remove_cv<element_type>::type>::value>
2547 struct __same_or_less_cv_qualified_imp
2548 : is_convertible<_P1, pointer> {};
2549 template <class _P1>
2550 struct __same_or_less_cv_qualified_imp<_P1, false>
2551 : false_type {};
2552
2553 template <class _P1, bool = is_scalar<_P1>::value && !is_pointer<_P1>::value>
2554 struct __same_or_less_cv_qualified
2555 : __same_or_less_cv_qualified_imp<_P1> {};
2556
2557 template <class _P1>
2558 struct __same_or_less_cv_qualified<_P1, true>
2559 : false_type {};
Howard Hinnant0a631192011-12-16 15:37:23 +00002560
Howard Hinnant73d21a42010-09-04 23:28:19 +00002561#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002562 unique_ptr(unique_ptr&);
2563 template <class _Up>
2564 unique_ptr(unique_ptr<_Up>&);
2565 unique_ptr& operator=(unique_ptr&);
2566 template <class _Up>
2567 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002568#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002569
2570 struct __nat {int __for_bool_;};
2571
2572 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2573 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2574public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002575 _LIBCPP_INLINE_VISIBILITY unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002576 : __ptr_(pointer())
2577 {
2578 static_assert(!is_pointer<deleter_type>::value,
2579 "unique_ptr constructed with null function pointer deleter");
2580 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002581 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002582 : __ptr_(pointer())
2583 {
2584 static_assert(!is_pointer<deleter_type>::value,
2585 "unique_ptr constructed with null function pointer deleter");
2586 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002587#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002588 template <class _Pp,
Howard Hinnant0a631192011-12-16 15:37:23 +00002589 class = typename enable_if<__same_or_less_cv_qualified<_Pp>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002590 >
Howard Hinnant99968442011-11-29 18:15:50 +00002591 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002592 : __ptr_(__p)
2593 {
2594 static_assert(!is_pointer<deleter_type>::value,
2595 "unique_ptr constructed with null function pointer deleter");
2596 }
2597
Howard Hinnant99968442011-11-29 18:15:50 +00002598 template <class _Pp,
Howard Hinnant0a631192011-12-16 15:37:23 +00002599 class = typename enable_if<__same_or_less_cv_qualified<_Pp>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002600 >
Howard Hinnant99968442011-11-29 18:15:50 +00002601 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002602 is_reference<deleter_type>::value,
2603 deleter_type,
2604 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002605 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002606 : __ptr_(__p, __d) {}
2607
2608 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2609 is_reference<deleter_type>::value,
2610 deleter_type,
2611 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002612 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002613 : __ptr_(pointer(), __d) {}
2614
Howard Hinnant99968442011-11-29 18:15:50 +00002615 template <class _Pp,
Howard Hinnant0a631192011-12-16 15:37:23 +00002616 class = typename enable_if<__same_or_less_cv_qualified<_Pp>::value ||
Howard Hinnant99968442011-11-29 18:15:50 +00002617 is_same<_Pp, nullptr_t>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002618 >
Howard Hinnant99968442011-11-29 18:15:50 +00002619 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002620 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002621 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002622 {
2623 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2624 }
2625
2626 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002627 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002628 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002629 {
2630 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2631 }
2632
Howard Hinnant1694d232011-05-28 14:41:13 +00002633 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002634 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002635
Howard Hinnant1694d232011-05-28 14:41:13 +00002636 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002637 {
2638 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002639 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002640 return *this;
2641 }
Howard Hinnant8e843502011-12-18 21:19:44 +00002642
2643 template <class _Up, class _Ep>
2644 _LIBCPP_INLINE_VISIBILITY
2645 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2646 typename enable_if
2647 <
2648 is_array<_Up>::value &&
2649 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer>::value
2650 && is_convertible<_Ep, deleter_type>::value &&
2651 (
2652 !is_reference<deleter_type>::value ||
2653 is_same<deleter_type, _Ep>::value
2654 ),
2655 __nat
2656 >::type = __nat()
2657 ) _NOEXCEPT
2658 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2659
2660
2661 template <class _Up, class _Ep>
2662 _LIBCPP_INLINE_VISIBILITY
2663 typename enable_if
2664 <
2665 is_array<_Up>::value &&
2666 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer>::value &&
2667 is_assignable<deleter_type, _Ep>::value,
2668 unique_ptr&
2669 >::type
2670 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2671 {
2672 reset(__u.release());
2673 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2674 return *this;
2675 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002676#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002677
2678 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2679 : __ptr_(__p)
2680 {
2681 static_assert(!is_pointer<deleter_type>::value,
2682 "unique_ptr constructed with null function pointer deleter");
2683 }
2684
2685 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002686 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002687
2688 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002689 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002690
2691 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2692 {
2693 return __rv<unique_ptr>(*this);
2694 }
2695
2696 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002697 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002698
2699 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2700 {
2701 reset(__u->release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002702 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002703 return *this;
2704 }
2705
Howard Hinnant73d21a42010-09-04 23:28:19 +00002706#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002707 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2708
Howard Hinnant1694d232011-05-28 14:41:13 +00002709 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002710 {
2711 reset();
2712 return *this;
2713 }
2714
2715 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2716 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002717 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2718 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2719 {return __ptr_.second();}
2720 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2721 {return __ptr_.second();}
2722 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const _NOEXCEPT
2723 {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002724
Howard Hinnant1694d232011-05-28 14:41:13 +00002725 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002726 {
2727 pointer __t = __ptr_.first();
2728 __ptr_.first() = pointer();
2729 return __t;
2730 }
2731
Howard Hinnant73d21a42010-09-04 23:28:19 +00002732#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002733 template <class _Pp,
Howard Hinnant0a631192011-12-16 15:37:23 +00002734 class = typename enable_if<__same_or_less_cv_qualified<_Pp>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002735 >
Howard Hinnant99968442011-11-29 18:15:50 +00002736 _LIBCPP_INLINE_VISIBILITY void reset(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002737 {
2738 pointer __tmp = __ptr_.first();
2739 __ptr_.first() = __p;
2740 if (__tmp)
2741 __ptr_.second()(__tmp);
2742 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002743 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002744 {
2745 pointer __tmp = __ptr_.first();
2746 __ptr_.first() = nullptr;
2747 if (__tmp)
2748 __ptr_.second()(__tmp);
2749 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002750 _LIBCPP_INLINE_VISIBILITY void reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002751 {
2752 pointer __tmp = __ptr_.first();
2753 __ptr_.first() = nullptr;
2754 if (__tmp)
2755 __ptr_.second()(__tmp);
2756 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002757#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002758 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2759 {
2760 pointer __tmp = __ptr_.first();
2761 __ptr_.first() = __p;
2762 if (__tmp)
2763 __ptr_.second()(__tmp);
2764 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002765#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002766
2767 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2768private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002769
Howard Hinnant73d21a42010-09-04 23:28:19 +00002770#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002771 template <class _Up>
2772 explicit unique_ptr(_Up);
2773 template <class _Up>
2774 unique_ptr(_Up __u,
2775 typename conditional<
2776 is_reference<deleter_type>::value,
2777 deleter_type,
2778 typename add_lvalue_reference<const deleter_type>::type>::type,
2779 typename enable_if
2780 <
2781 is_convertible<_Up, pointer>::value,
2782 __nat
2783 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002784#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002785};
2786
2787template <class _Tp, class _Dp>
2788inline _LIBCPP_INLINE_VISIBILITY
2789void
Howard Hinnant1694d232011-05-28 14:41:13 +00002790swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002791
2792template <class _T1, class _D1, class _T2, class _D2>
2793inline _LIBCPP_INLINE_VISIBILITY
2794bool
2795operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2796
2797template <class _T1, class _D1, class _T2, class _D2>
2798inline _LIBCPP_INLINE_VISIBILITY
2799bool
2800operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2801
2802template <class _T1, class _D1, class _T2, class _D2>
2803inline _LIBCPP_INLINE_VISIBILITY
2804bool
2805operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() < __y.get();}
2806
2807template <class _T1, class _D1, class _T2, class _D2>
2808inline _LIBCPP_INLINE_VISIBILITY
2809bool
2810operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2811
2812template <class _T1, class _D1, class _T2, class _D2>
2813inline _LIBCPP_INLINE_VISIBILITY
2814bool
2815operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2816
2817template <class _T1, class _D1, class _T2, class _D2>
2818inline _LIBCPP_INLINE_VISIBILITY
2819bool
2820operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2821
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00002822template <class _Tp> struct hash;
Howard Hinnant21aefc32010-06-03 16:42:57 +00002823
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002824// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
2825// is 64 bits. This is because cityhash64 uses 64bit x 64bit
2826// multiplication, which can be very slow on 32-bit systems.
Howard Hinnant40c13d32011-12-05 00:08:45 +00002827template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002828struct __murmur2_or_cityhash;
Howard Hinnant40c13d32011-12-05 00:08:45 +00002829
2830template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002831struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnant40c13d32011-12-05 00:08:45 +00002832{
2833 _Size operator()(const void* __key, _Size __len);
2834};
2835
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002836// murmur2
Howard Hinnant40c13d32011-12-05 00:08:45 +00002837template <class _Size>
2838_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002839__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00002840{
2841 const _Size __m = 0x5bd1e995;
2842 const _Size __r = 24;
2843 _Size __h = __len;
2844 const unsigned char* __data = static_cast<const unsigned char*>(__key);
2845 for (; __len >= 4; __data += 4, __len -= 4)
2846 {
2847 _Size __k = *(const _Size*)__data;
2848 __k *= __m;
2849 __k ^= __k >> __r;
2850 __k *= __m;
2851 __h *= __m;
2852 __h ^= __k;
2853 }
2854 switch (__len)
2855 {
2856 case 3:
2857 __h ^= __data[2] << 16;
2858 case 2:
2859 __h ^= __data[1] << 8;
2860 case 1:
2861 __h ^= __data[0];
2862 __h *= __m;
2863 }
2864 __h ^= __h >> 13;
2865 __h *= __m;
2866 __h ^= __h >> 15;
2867 return __h;
2868}
2869
2870template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002871struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnant40c13d32011-12-05 00:08:45 +00002872{
2873 _Size operator()(const void* __key, _Size __len);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002874
2875 private:
2876 // Some primes between 2^63 and 2^64.
2877 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
2878 static const _Size __k1 = 0xb492b66fbe98f273ULL;
2879 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
2880 static const _Size __k3 = 0xc949d7c7509e6557ULL;
2881
2882 static _Size __rotate(_Size __val, int __shift) {
2883 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
2884 }
2885
2886 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
2887 return (__val >> __shift) | (__val << (64 - __shift));
2888 }
2889
2890 static _Size __shift_mix(_Size __val) {
2891 return __val ^ (__val >> 47);
2892 }
2893
2894 static _Size __hash_len_16(_Size __u, _Size __v) {
2895 const _Size __mul = 0x9ddfea08eb382d69ULL;
2896 _Size __a = (__u ^ __v) * __mul;
2897 __a ^= (__a >> 47);
2898 _Size __b = (__v ^ __a) * __mul;
2899 __b ^= (__b >> 47);
2900 __b *= __mul;
2901 return __b;
2902 }
2903
2904 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
2905 if (__len > 8) {
2906 const _Size __a = *(const _Size*)__s;
2907 const _Size __b = *(const _Size*)(__s + __len - 8);
2908 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
2909 }
2910 if (__len >= 4) {
2911 const uint32_t __a = *(const uint32_t*)(__s);
2912 const uint32_t __b = *(const uint32_t*)(__s + __len - 4);
2913 return __hash_len_16(__len + (__a << 3), __b);
2914 }
2915 if (__len > 0) {
2916 const unsigned char __a = __s[0];
2917 const unsigned char __b = __s[__len >> 1];
2918 const unsigned char __c = __s[__len - 1];
2919 const uint32_t __y = static_cast<uint32_t>(__a) +
2920 (static_cast<uint32_t>(__b) << 8);
2921 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
2922 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
2923 }
2924 return __k2;
2925 }
2926
2927 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
2928 const _Size __a = *(const _Size*)(__s) * __k1;
2929 const _Size __b = *(const _Size*)(__s + 8);
2930 const _Size __c = *(const _Size*)(__s + __len - 8) * __k2;
2931 const _Size __d = *(const _Size*)(__s + __len - 16) * __k0;
2932 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
2933 __a + __rotate(__b ^ __k3, 20) - __c + __len);
2934 }
2935
2936 // Return a 16-byte hash for 48 bytes. Quick and dirty.
2937 // Callers do best to use "random-looking" values for a and b.
2938 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
2939 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
2940 __a += __w;
2941 __b = __rotate(__b + __a + __z, 21);
2942 const _Size __c = __a;
2943 __a += __x;
2944 __a += __y;
2945 __b += __rotate(__a, 44);
2946 return pair<_Size, _Size>(__a + __z, __b + __c);
2947 }
2948
2949 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
2950 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
2951 const char* __s, _Size __a, _Size __b) {
2952 return __weak_hash_len_32_with_seeds(*(const _Size*)(__s),
2953 *(const _Size*)(__s + 8),
2954 *(const _Size*)(__s + 16),
2955 *(const _Size*)(__s + 24),
2956 __a,
2957 __b);
2958 }
2959
2960 // Return an 8-byte hash for 33 to 64 bytes.
2961 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
2962 _Size __z = *(const _Size*)(__s + 24);
2963 _Size __a = *(const _Size*)(__s) +
2964 (__len + *(const _Size*)(__s + __len - 16)) * __k0;
2965 _Size __b = __rotate(__a + __z, 52);
2966 _Size __c = __rotate(__a, 37);
2967 __a += *(const _Size*)(__s + 8);
2968 __c += __rotate(__a, 7);
2969 __a += *(const _Size*)(__s + 16);
2970 _Size __vf = __a + __z;
2971 _Size __vs = __b + __rotate(__a, 31) + __c;
2972 __a = *(const _Size*)(__s + 16) + *(const _Size*)(__s + __len - 32);
2973 __z += *(const _Size*)(__s + __len - 8);
2974 __b = __rotate(__a + __z, 52);
2975 __c = __rotate(__a, 37);
2976 __a += *(const _Size*)(__s + __len - 24);
2977 __c += __rotate(__a, 7);
2978 __a += *(const _Size*)(__s + __len - 16);
2979 _Size __wf = __a + __z;
2980 _Size __ws = __b + __rotate(__a, 31) + __c;
2981 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
2982 return __shift_mix(__r * __k0 + __vs) * __k2;
2983 }
Howard Hinnant40c13d32011-12-05 00:08:45 +00002984};
2985
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002986// cityhash64
Howard Hinnant40c13d32011-12-05 00:08:45 +00002987template <class _Size>
2988_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002989__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00002990{
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002991 const char* __s = static_cast<const char*>(__key);
2992 if (__len <= 32) {
2993 if (__len <= 16) {
2994 return __hash_len_0_to_16(__s, __len);
2995 } else {
2996 return __hash_len_17_to_32(__s, __len);
Howard Hinnant40c13d32011-12-05 00:08:45 +00002997 }
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002998 } else if (__len <= 64) {
2999 return __hash_len_33_to_64(__s, __len);
3000 }
3001
3002 // For strings over 64 bytes we hash the end first, and then as we
3003 // loop we keep 56 bytes of state: v, w, x, y, and z.
3004 _Size __x = *(const _Size*)(__s + __len - 40);
3005 _Size __y = *(const _Size*)(__s + __len - 16) +
3006 *(const _Size*)(__s + __len - 56);
3007 _Size __z = __hash_len_16(*(const _Size*)(__s + __len - 48) + __len,
3008 *(const _Size*)(__s + __len - 24));
3009 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3010 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
3011 __x = __x * __k1 + *(const _Size*)(__s);
3012
3013 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3014 __len = (__len - 1) & ~static_cast<_Size>(63);
3015 do {
3016 __x = __rotate(__x + __y + __v.first + *(const _Size*)(__s + 8), 37) * __k1;
3017 __y = __rotate(__y + __v.second + *(const _Size*)(__s + 48), 42) * __k1;
3018 __x ^= __w.second;
3019 __y += __v.first + *(const _Size*)(__s + 40);
3020 __z = __rotate(__z + __w.first, 33) * __k1;
3021 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3022 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
3023 __y + *(const _Size*)(__s + 16));
3024 std::swap(__z, __x);
3025 __s += 64;
3026 __len -= 64;
3027 } while (__len != 0);
3028 return __hash_len_16(
3029 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3030 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003031}
3032
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003033template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3034struct __scalar_hash;
3035
3036template <class _Tp>
3037struct __scalar_hash<_Tp, 0>
3038 : public unary_function<_Tp, size_t>
Howard Hinnant21aefc32010-06-03 16:42:57 +00003039{
Howard Hinnant82894812010-09-22 16:48:34 +00003040 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003041 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003042 {
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003043 union
3044 {
3045 _Tp __t;
3046 size_t __a;
3047 } __u;
3048 __u.__a = 0;
3049 __u.__t = __v;
3050 return __u.__a;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003051 }
3052};
3053
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003054template <class _Tp>
3055struct __scalar_hash<_Tp, 1>
3056 : public unary_function<_Tp, size_t>
3057{
3058 _LIBCPP_INLINE_VISIBILITY
3059 size_t operator()(_Tp __v) const _NOEXCEPT
3060 {
3061 union
3062 {
3063 _Tp __t;
3064 size_t __a;
3065 } __u;
3066 __u.__t = __v;
3067 return __u.__a;
3068 }
3069};
3070
3071template <class _Tp>
3072struct __scalar_hash<_Tp, 2>
3073 : public unary_function<_Tp, size_t>
3074{
3075 _LIBCPP_INLINE_VISIBILITY
3076 size_t operator()(_Tp __v) const _NOEXCEPT
3077 {
3078 union
3079 {
3080 _Tp __t;
3081 struct
3082 {
3083 size_t __a;
3084 size_t __b;
3085 };
3086 } __u;
3087 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003088 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003089 }
3090};
3091
3092template <class _Tp>
3093struct __scalar_hash<_Tp, 3>
3094 : public unary_function<_Tp, size_t>
3095{
3096 _LIBCPP_INLINE_VISIBILITY
3097 size_t operator()(_Tp __v) const _NOEXCEPT
3098 {
3099 union
3100 {
3101 _Tp __t;
3102 struct
3103 {
3104 size_t __a;
3105 size_t __b;
3106 size_t __c;
3107 };
3108 } __u;
3109 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003110 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003111 }
3112};
3113
3114template <class _Tp>
3115struct __scalar_hash<_Tp, 4>
3116 : public unary_function<_Tp, size_t>
3117{
3118 _LIBCPP_INLINE_VISIBILITY
3119 size_t operator()(_Tp __v) const _NOEXCEPT
3120 {
3121 union
3122 {
3123 _Tp __t;
3124 struct
3125 {
3126 size_t __a;
3127 size_t __b;
3128 size_t __c;
3129 size_t __d;
3130 };
3131 } __u;
3132 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003133 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003134 }
3135};
3136
3137template<class _Tp>
3138struct _LIBCPP_VISIBLE hash<_Tp*>
3139 : public __scalar_hash<_Tp*>
3140{
3141};
3142
Howard Hinnant21aefc32010-06-03 16:42:57 +00003143template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00003144struct _LIBCPP_VISIBLE hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003145{
3146 typedef unique_ptr<_Tp, _Dp> argument_type;
3147 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003148 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003149 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003150 {
3151 typedef typename argument_type::pointer pointer;
3152 return hash<pointer>()(__ptr.get());
3153 }
3154};
3155
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003156struct __destruct_n
3157{
3158private:
3159 size_t size;
3160
3161 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003162 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003163 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3164
3165 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003166 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003167 {}
3168
Howard Hinnant1694d232011-05-28 14:41:13 +00003169 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003170 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003171 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003172 {}
3173
Howard Hinnant1694d232011-05-28 14:41:13 +00003174 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003175 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003176 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003177 {}
3178public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003179 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3180 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003181
3182 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003183 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003184 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003185
3186 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003187 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003188 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003189
3190 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003191 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003192 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003193};
3194
3195template <class _Alloc>
3196class __allocator_destructor
3197{
3198 typedef allocator_traits<_Alloc> __alloc_traits;
3199public:
3200 typedef typename __alloc_traits::pointer pointer;
3201 typedef typename __alloc_traits::size_type size_type;
3202private:
3203 _Alloc& __alloc_;
3204 size_type __s_;
3205public:
3206 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00003207 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003208 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003210 void operator()(pointer __p) _NOEXCEPT
3211 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003212};
3213
3214template <class _InputIterator, class _ForwardIterator>
3215_ForwardIterator
3216uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3217{
3218 __destruct_n __d(0);
3219 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3220 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
3221 for (; __f != __l; ++__f, ++__r, __d.__incr((value_type*)0))
3222 ::new(&*__r) value_type(*__f);
3223 __h.release();
3224 return __r;
3225}
3226
3227template <class _InputIterator, class _Size, class _ForwardIterator>
3228_ForwardIterator
3229uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3230{
3231 __destruct_n __d(0);
3232 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3233 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
3234 for (; __n > 0; ++__f, ++__r, __d.__incr((value_type*)0), --__n)
3235 ::new(&*__r) value_type(*__f);
3236 __h.release();
3237 return __r;
3238}
3239
3240template <class _ForwardIterator, class _Tp>
3241void
3242uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3243{
3244 __destruct_n __d(0);
3245 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3246 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
3247 for (; __f != __l; ++__f, __d.__incr((value_type*)0))
3248 ::new(&*__f) value_type(__x);
3249 __h.release();
3250}
3251
3252template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003253_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003254uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3255{
3256 __destruct_n __d(0);
3257 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3258 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
3259 for (; __n > 0; ++__f, --__n, __d.__incr((value_type*)0))
3260 ::new(&*__f) value_type(__x);
3261 __h.release();
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003262 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003263}
3264
Howard Hinnant82894812010-09-22 16:48:34 +00003265class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003266 : public std::exception
3267{
3268public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003269 virtual ~bad_weak_ptr() _NOEXCEPT;
3270 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003271};
3272
3273template<class _Tp> class weak_ptr;
3274
3275class __shared_count
3276{
3277 __shared_count(const __shared_count&);
3278 __shared_count& operator=(const __shared_count&);
3279
3280protected:
3281 long __shared_owners_;
3282 virtual ~__shared_count();
3283private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003284 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003285
3286public:
Howard Hinnant82894812010-09-22 16:48:34 +00003287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003288 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003289 : __shared_owners_(__refs) {}
3290
Howard Hinnant1694d232011-05-28 14:41:13 +00003291 void __add_shared() _NOEXCEPT;
3292 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003293 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003294 long use_count() const _NOEXCEPT {return __shared_owners_ + 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003295};
3296
3297class __shared_weak_count
3298 : private __shared_count
3299{
3300 long __shared_weak_owners_;
3301
3302public:
Howard Hinnant82894812010-09-22 16:48:34 +00003303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003304 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003305 : __shared_count(__refs),
3306 __shared_weak_owners_(__refs) {}
3307protected:
3308 virtual ~__shared_weak_count();
3309
3310public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003311 void __add_shared() _NOEXCEPT;
3312 void __add_weak() _NOEXCEPT;
3313 void __release_shared() _NOEXCEPT;
3314 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003316 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3317 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003318
Howard Hinnant1694d232011-05-28 14:41:13 +00003319 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003320private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003321 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003322};
3323
3324template <class _Tp, class _Dp, class _Alloc>
3325class __shared_ptr_pointer
3326 : public __shared_weak_count
3327{
3328 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3329public:
Howard Hinnant82894812010-09-22 16:48:34 +00003330 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003331 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003332 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003333
Howard Hinnantd4444702010-08-11 17:04:31 +00003334#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00003335 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00003336#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003337
3338private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003339 virtual void __on_zero_shared() _NOEXCEPT;
3340 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003341};
3342
Howard Hinnantd4444702010-08-11 17:04:31 +00003343#ifndef _LIBCPP_NO_RTTI
3344
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003345template <class _Tp, class _Dp, class _Alloc>
3346const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00003347__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003348{
3349 return __t == typeid(_Dp) ? &__data_.first().second() : 0;
3350}
3351
Howard Hinnant324bb032010-08-22 00:02:43 +00003352#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003353
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003354template <class _Tp, class _Dp, class _Alloc>
3355void
Howard Hinnant1694d232011-05-28 14:41:13 +00003356__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003357{
3358 __data_.first().second()(__data_.first().first());
3359 __data_.first().second().~_Dp();
3360}
3361
3362template <class _Tp, class _Dp, class _Alloc>
3363void
Howard Hinnant1694d232011-05-28 14:41:13 +00003364__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003365{
3366 typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
3367 __data_.second().~_Alloc();
3368 __a.deallocate(this, 1);
3369}
3370
3371template <class _Tp, class _Alloc>
3372class __shared_ptr_emplace
3373 : public __shared_weak_count
3374{
3375 __compressed_pair<_Alloc, _Tp> __data_;
3376public:
3377#ifndef _LIBCPP_HAS_NO_VARIADICS
3378
Howard Hinnant82894812010-09-22 16:48:34 +00003379 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003380 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003381 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003382
3383 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00003384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003385 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00003386 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3387 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003388
3389#else // _LIBCPP_HAS_NO_VARIADICS
3390
Howard Hinnant82894812010-09-22 16:48:34 +00003391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003392 __shared_ptr_emplace(_Alloc __a)
3393 : __data_(__a) {}
3394
3395 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00003396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003397 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3398 : __data_(__a, _Tp(__a0)) {}
3399
3400 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00003401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003402 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3403 : __data_(__a, _Tp(__a0, __a1)) {}
3404
3405 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00003406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003407 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3408 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3409
3410#endif // _LIBCPP_HAS_NO_VARIADICS
3411
3412private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003413 virtual void __on_zero_shared() _NOEXCEPT;
3414 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003415public:
Howard Hinnant82894812010-09-22 16:48:34 +00003416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003417 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003418};
3419
3420template <class _Tp, class _Alloc>
3421void
Howard Hinnant1694d232011-05-28 14:41:13 +00003422__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003423{
3424 __data_.second().~_Tp();
3425}
3426
3427template <class _Tp, class _Alloc>
3428void
Howard Hinnant1694d232011-05-28 14:41:13 +00003429__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003430{
3431 typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
3432 __data_.first().~_Alloc();
3433 __a.deallocate(this, 1);
3434}
3435
3436template<class _Tp> class enable_shared_from_this;
3437
3438template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003439class _LIBCPP_VISIBLE shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003440{
Howard Hinnant324bb032010-08-22 00:02:43 +00003441public:
3442 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003443private:
3444 element_type* __ptr_;
3445 __shared_weak_count* __cntrl_;
3446
3447 struct __nat {int __for_bool_;};
3448public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003449 shared_ptr() _NOEXCEPT;
3450 shared_ptr(nullptr_t) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003451 template<class _Yp> explicit shared_ptr(_Yp* __p);
Howard Hinnant324bb032010-08-22 00:02:43 +00003452 template<class _Yp, class _Dp> shared_ptr(_Yp* __p, _Dp __d);
3453 template<class _Yp, class _Dp, class _Alloc> shared_ptr(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003454 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3455 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant1694d232011-05-28 14:41:13 +00003456 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3457 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003458 template<class _Yp>
3459 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003460 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3461 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003462#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003463 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003464 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003465 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3466 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003467#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003468 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003469 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003470#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00003471 template<class _Yp> shared_ptr(auto_ptr<_Yp>&& __r);
3472#else
3473 template<class _Yp> shared_ptr(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003474#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003475#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003476private:
Howard Hinnant324bb032010-08-22 00:02:43 +00003477 template <class _Yp, class _Dp> shared_ptr(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003478public:
3479 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
3480 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
3481 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
3482 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003483#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003484 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
3485 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
3486 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
3487 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003488#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003489
3490 ~shared_ptr();
3491
Howard Hinnant1694d232011-05-28 14:41:13 +00003492 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
3493 template<class _Yp> shared_ptr& operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003494#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003495 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00003496 template<class _Yp> shared_ptr& operator=(shared_ptr<_Yp>&& __r);
3497 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003498#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00003499 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003500#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003501#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003502private:
Howard Hinnant324bb032010-08-22 00:02:43 +00003503 template <class _Yp, class _Dp> shared_ptr& operator=(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003504public:
Howard Hinnant324bb032010-08-22 00:02:43 +00003505 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003506#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00003507 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003508#endif
3509
Howard Hinnant1694d232011-05-28 14:41:13 +00003510 void swap(shared_ptr& __r) _NOEXCEPT;
3511 void reset() _NOEXCEPT;
Howard Hinnantec3773c2011-12-01 20:21:04 +00003512 template<class _Yp> void reset(_Yp* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003513 template<class _Yp, class _Dp> void reset(_Yp* __p, _Dp __d);
3514 template<class _Yp, class _Dp, class _Alloc> void reset(_Yp* __p, _Dp __d, _Alloc __a);
3515
Howard Hinnant82894812010-09-22 16:48:34 +00003516 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003517 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003518 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003519 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3520 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003521 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003522 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003523 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003524 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003525 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003526 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00003527 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003528 /*explicit*/ operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant99968442011-11-29 18:15:50 +00003529 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00003530 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003531 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003532 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant99968442011-11-29 18:15:50 +00003533 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00003534 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003535 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003536 {return __cntrl_ < __p.__cntrl_;}
3537
Howard Hinnantd4444702010-08-11 17:04:31 +00003538#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003539 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00003540 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003541 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003542 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00003543#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003544
3545#ifndef _LIBCPP_HAS_NO_VARIADICS
3546
3547 template<class ..._Args>
3548 static
3549 shared_ptr<_Tp>
3550 make_shared(_Args&& ...__args);
3551
3552 template<class _Alloc, class ..._Args>
3553 static
3554 shared_ptr<_Tp>
3555 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3556
3557#else // _LIBCPP_HAS_NO_VARIADICS
3558
3559 static shared_ptr<_Tp> make_shared();
3560
3561 template<class _A0>
3562 static shared_ptr<_Tp> make_shared(_A0&);
3563
3564 template<class _A0, class _A1>
3565 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3566
3567 template<class _A0, class _A1, class _A2>
3568 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3569
3570 template<class _Alloc>
3571 static shared_ptr<_Tp>
3572 allocate_shared(const _Alloc& __a);
3573
3574 template<class _Alloc, class _A0>
3575 static shared_ptr<_Tp>
3576 allocate_shared(const _Alloc& __a, _A0& __a0);
3577
3578 template<class _Alloc, class _A0, class _A1>
3579 static shared_ptr<_Tp>
3580 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3581
3582 template<class _Alloc, class _A0, class _A1, class _A2>
3583 static shared_ptr<_Tp>
3584 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3585
3586#endif // _LIBCPP_HAS_NO_VARIADICS
3587
3588private:
3589
3590 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00003591 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003592 void
Howard Hinnant1694d232011-05-28 14:41:13 +00003593 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003594 {
3595 if (__e)
3596 __e->__weak_this_ = *this;
3597 }
3598
Howard Hinnant82894812010-09-22 16:48:34 +00003599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003600 void __enable_weak_this(const void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003601
Howard Hinnant82894812010-09-22 16:48:34 +00003602 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
3603 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003604};
3605
3606template<class _Tp>
3607inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003608shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003609 : __ptr_(0),
3610 __cntrl_(0)
3611{
3612}
3613
3614template<class _Tp>
3615inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003616shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003617 : __ptr_(0),
3618 __cntrl_(0)
3619{
3620}
3621
3622template<class _Tp>
3623template<class _Yp>
3624shared_ptr<_Tp>::shared_ptr(_Yp* __p)
3625 : __ptr_(__p)
3626{
3627 unique_ptr<_Yp> __hold(__p);
3628 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3629 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
3630 __hold.release();
3631 __enable_weak_this(__p);
3632}
3633
3634template<class _Tp>
3635template<class _Yp, class _Dp>
3636shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
3637 : __ptr_(__p)
3638{
3639#ifndef _LIBCPP_NO_EXCEPTIONS
3640 try
3641 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003642#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003643 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3644 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
3645 __enable_weak_this(__p);
3646#ifndef _LIBCPP_NO_EXCEPTIONS
3647 }
3648 catch (...)
3649 {
3650 __d(__p);
3651 throw;
3652 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003653#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003654}
3655
3656template<class _Tp>
3657template<class _Dp>
3658shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3659 : __ptr_(0)
3660{
3661#ifndef _LIBCPP_NO_EXCEPTIONS
3662 try
3663 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003664#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003665 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
3666 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
3667#ifndef _LIBCPP_NO_EXCEPTIONS
3668 }
3669 catch (...)
3670 {
3671 __d(__p);
3672 throw;
3673 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003674#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003675}
3676
3677template<class _Tp>
3678template<class _Yp, class _Dp, class _Alloc>
3679shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
3680 : __ptr_(__p)
3681{
3682#ifndef _LIBCPP_NO_EXCEPTIONS
3683 try
3684 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003685#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003686 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
3687 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3688 typedef __allocator_destructor<_A2> _D2;
3689 _A2 __a2(__a);
3690 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3691 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
3692 __cntrl_ = __hold2.release();
3693 __enable_weak_this(__p);
3694#ifndef _LIBCPP_NO_EXCEPTIONS
3695 }
3696 catch (...)
3697 {
3698 __d(__p);
3699 throw;
3700 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003701#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003702}
3703
3704template<class _Tp>
3705template<class _Dp, class _Alloc>
3706shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
3707 : __ptr_(0)
3708{
3709#ifndef _LIBCPP_NO_EXCEPTIONS
3710 try
3711 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003712#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003713 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
3714 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3715 typedef __allocator_destructor<_A2> _D2;
3716 _A2 __a2(__a);
3717 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3718 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
3719 __cntrl_ = __hold2.release();
3720#ifndef _LIBCPP_NO_EXCEPTIONS
3721 }
3722 catch (...)
3723 {
3724 __d(__p);
3725 throw;
3726 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003727#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003728}
3729
3730template<class _Tp>
3731template<class _Yp>
3732inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003733shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003734 : __ptr_(__p),
3735 __cntrl_(__r.__cntrl_)
3736{
3737 if (__cntrl_)
3738 __cntrl_->__add_shared();
3739}
3740
3741template<class _Tp>
3742inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003743shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003744 : __ptr_(__r.__ptr_),
3745 __cntrl_(__r.__cntrl_)
3746{
3747 if (__cntrl_)
3748 __cntrl_->__add_shared();
3749}
3750
3751template<class _Tp>
3752template<class _Yp>
3753inline _LIBCPP_INLINE_VISIBILITY
3754shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
3755 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003756 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003757 : __ptr_(__r.__ptr_),
3758 __cntrl_(__r.__cntrl_)
3759{
3760 if (__cntrl_)
3761 __cntrl_->__add_shared();
3762}
3763
Howard Hinnant73d21a42010-09-04 23:28:19 +00003764#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003765
3766template<class _Tp>
3767inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003768shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003769 : __ptr_(__r.__ptr_),
3770 __cntrl_(__r.__cntrl_)
3771{
3772 __r.__ptr_ = 0;
3773 __r.__cntrl_ = 0;
3774}
3775
3776template<class _Tp>
3777template<class _Yp>
3778inline _LIBCPP_INLINE_VISIBILITY
3779shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
3780 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003781 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003782 : __ptr_(__r.__ptr_),
3783 __cntrl_(__r.__cntrl_)
3784{
3785 __r.__ptr_ = 0;
3786 __r.__cntrl_ = 0;
3787}
3788
Howard Hinnant73d21a42010-09-04 23:28:19 +00003789#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003790
3791template<class _Tp>
3792template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003793#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003794shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
3795#else
Howard Hinnant92172b82010-08-21 21:14:53 +00003796shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003797#endif
3798 : __ptr_(__r.get())
3799{
3800 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3801 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
3802 __enable_weak_this(__r.get());
3803 __r.release();
3804}
3805
3806template<class _Tp>
3807template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003808#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003809shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3810#else
3811shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3812#endif
3813 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
3814 : __ptr_(__r.get())
3815{
3816 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3817 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
3818 __enable_weak_this(__r.get());
3819 __r.release();
3820}
3821
3822template<class _Tp>
3823template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003824#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003825shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3826#else
3827shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3828#endif
3829 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
3830 : __ptr_(__r.get())
3831{
3832 typedef __shared_ptr_pointer<_Yp*,
3833 reference_wrapper<typename remove_reference<_Dp>::type>,
3834 allocator<_Yp> > _CntrlBlk;
3835 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
3836 __enable_weak_this(__r.get());
3837 __r.release();
3838}
3839
3840#ifndef _LIBCPP_HAS_NO_VARIADICS
3841
3842template<class _Tp>
3843template<class ..._Args>
3844shared_ptr<_Tp>
3845shared_ptr<_Tp>::make_shared(_Args&& ...__args)
3846{
3847 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3848 typedef allocator<_CntrlBlk> _A2;
3849 typedef __allocator_destructor<_A2> _D2;
3850 _A2 __a2;
3851 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003852 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003853 shared_ptr<_Tp> __r;
3854 __r.__ptr_ = __hold2.get()->get();
3855 __r.__cntrl_ = __hold2.release();
3856 __r.__enable_weak_this(__r.__ptr_);
3857 return __r;
3858}
3859
3860template<class _Tp>
3861template<class _Alloc, class ..._Args>
3862shared_ptr<_Tp>
3863shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
3864{
3865 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3866 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3867 typedef __allocator_destructor<_A2> _D2;
3868 _A2 __a2(__a);
3869 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003870 ::new(__hold2.get()) _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003871 shared_ptr<_Tp> __r;
3872 __r.__ptr_ = __hold2.get()->get();
3873 __r.__cntrl_ = __hold2.release();
3874 __r.__enable_weak_this(__r.__ptr_);
3875 return __r;
3876}
3877
3878#else // _LIBCPP_HAS_NO_VARIADICS
3879
3880template<class _Tp>
3881shared_ptr<_Tp>
3882shared_ptr<_Tp>::make_shared()
3883{
3884 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3885 typedef allocator<_CntrlBlk> _Alloc2;
3886 typedef __allocator_destructor<_Alloc2> _D2;
3887 _Alloc2 __alloc2;
3888 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3889 ::new(__hold2.get()) _CntrlBlk(__alloc2);
3890 shared_ptr<_Tp> __r;
3891 __r.__ptr_ = __hold2.get()->get();
3892 __r.__cntrl_ = __hold2.release();
3893 __r.__enable_weak_this(__r.__ptr_);
3894 return __r;
3895}
3896
3897template<class _Tp>
3898template<class _A0>
3899shared_ptr<_Tp>
3900shared_ptr<_Tp>::make_shared(_A0& __a0)
3901{
3902 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3903 typedef allocator<_CntrlBlk> _Alloc2;
3904 typedef __allocator_destructor<_Alloc2> _D2;
3905 _Alloc2 __alloc2;
3906 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3907 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
3908 shared_ptr<_Tp> __r;
3909 __r.__ptr_ = __hold2.get()->get();
3910 __r.__cntrl_ = __hold2.release();
3911 __r.__enable_weak_this(__r.__ptr_);
3912 return __r;
3913}
3914
3915template<class _Tp>
3916template<class _A0, class _A1>
3917shared_ptr<_Tp>
3918shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
3919{
3920 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3921 typedef allocator<_CntrlBlk> _Alloc2;
3922 typedef __allocator_destructor<_Alloc2> _D2;
3923 _Alloc2 __alloc2;
3924 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3925 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
3926 shared_ptr<_Tp> __r;
3927 __r.__ptr_ = __hold2.get()->get();
3928 __r.__cntrl_ = __hold2.release();
3929 __r.__enable_weak_this(__r.__ptr_);
3930 return __r;
3931}
3932
3933template<class _Tp>
3934template<class _A0, class _A1, class _A2>
3935shared_ptr<_Tp>
3936shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3937{
3938 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3939 typedef allocator<_CntrlBlk> _Alloc2;
3940 typedef __allocator_destructor<_Alloc2> _D2;
3941 _Alloc2 __alloc2;
3942 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3943 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
3944 shared_ptr<_Tp> __r;
3945 __r.__ptr_ = __hold2.get()->get();
3946 __r.__cntrl_ = __hold2.release();
3947 __r.__enable_weak_this(__r.__ptr_);
3948 return __r;
3949}
3950
3951template<class _Tp>
3952template<class _Alloc>
3953shared_ptr<_Tp>
3954shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
3955{
3956 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3957 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3958 typedef __allocator_destructor<_Alloc2> _D2;
3959 _Alloc2 __alloc2(__a);
3960 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3961 ::new(__hold2.get()) _CntrlBlk(__a);
3962 shared_ptr<_Tp> __r;
3963 __r.__ptr_ = __hold2.get()->get();
3964 __r.__cntrl_ = __hold2.release();
3965 __r.__enable_weak_this(__r.__ptr_);
3966 return __r;
3967}
3968
3969template<class _Tp>
3970template<class _Alloc, class _A0>
3971shared_ptr<_Tp>
3972shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
3973{
3974 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3975 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3976 typedef __allocator_destructor<_Alloc2> _D2;
3977 _Alloc2 __alloc2(__a);
3978 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3979 ::new(__hold2.get()) _CntrlBlk(__a, __a0);
3980 shared_ptr<_Tp> __r;
3981 __r.__ptr_ = __hold2.get()->get();
3982 __r.__cntrl_ = __hold2.release();
3983 __r.__enable_weak_this(__r.__ptr_);
3984 return __r;
3985}
3986
3987template<class _Tp>
3988template<class _Alloc, class _A0, class _A1>
3989shared_ptr<_Tp>
3990shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3991{
3992 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3993 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3994 typedef __allocator_destructor<_Alloc2> _D2;
3995 _Alloc2 __alloc2(__a);
3996 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3997 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
3998 shared_ptr<_Tp> __r;
3999 __r.__ptr_ = __hold2.get()->get();
4000 __r.__cntrl_ = __hold2.release();
4001 __r.__enable_weak_this(__r.__ptr_);
4002 return __r;
4003}
4004
4005template<class _Tp>
4006template<class _Alloc, class _A0, class _A1, class _A2>
4007shared_ptr<_Tp>
4008shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4009{
4010 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4011 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4012 typedef __allocator_destructor<_Alloc2> _D2;
4013 _Alloc2 __alloc2(__a);
4014 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4015 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
4016 shared_ptr<_Tp> __r;
4017 __r.__ptr_ = __hold2.get()->get();
4018 __r.__cntrl_ = __hold2.release();
4019 __r.__enable_weak_this(__r.__ptr_);
4020 return __r;
4021}
4022
4023#endif // _LIBCPP_HAS_NO_VARIADICS
4024
4025template<class _Tp>
4026shared_ptr<_Tp>::~shared_ptr()
4027{
4028 if (__cntrl_)
4029 __cntrl_->__release_shared();
4030}
4031
4032template<class _Tp>
4033inline _LIBCPP_INLINE_VISIBILITY
4034shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004035shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004036{
4037 shared_ptr(__r).swap(*this);
4038 return *this;
4039}
4040
4041template<class _Tp>
4042template<class _Yp>
4043inline _LIBCPP_INLINE_VISIBILITY
4044shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004045shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004046{
4047 shared_ptr(__r).swap(*this);
4048 return *this;
4049}
4050
Howard Hinnant73d21a42010-09-04 23:28:19 +00004051#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004052
4053template<class _Tp>
4054inline _LIBCPP_INLINE_VISIBILITY
4055shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004056shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004057{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004058 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004059 return *this;
4060}
4061
4062template<class _Tp>
4063template<class _Yp>
4064inline _LIBCPP_INLINE_VISIBILITY
4065shared_ptr<_Tp>&
4066shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4067{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004068 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004069 return *this;
4070}
4071
4072template<class _Tp>
4073template<class _Yp>
4074inline _LIBCPP_INLINE_VISIBILITY
4075shared_ptr<_Tp>&
4076shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4077{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004078 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004079 return *this;
4080}
4081
4082template<class _Tp>
4083template <class _Yp, class _Dp>
4084inline _LIBCPP_INLINE_VISIBILITY
4085shared_ptr<_Tp>&
4086shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4087{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004088 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004089 return *this;
4090}
4091
Howard Hinnant73d21a42010-09-04 23:28:19 +00004092#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004093
4094template<class _Tp>
4095template<class _Yp>
4096inline _LIBCPP_INLINE_VISIBILITY
4097shared_ptr<_Tp>&
Howard Hinnant324bb032010-08-22 00:02:43 +00004098shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004099{
4100 shared_ptr(__r).swap(*this);
4101 return *this;
4102}
4103
4104template<class _Tp>
4105template <class _Yp, class _Dp>
4106inline _LIBCPP_INLINE_VISIBILITY
4107shared_ptr<_Tp>&
4108shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4109{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004110 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004111 return *this;
4112}
4113
Howard Hinnant73d21a42010-09-04 23:28:19 +00004114#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004115
4116template<class _Tp>
4117inline _LIBCPP_INLINE_VISIBILITY
4118void
Howard Hinnant1694d232011-05-28 14:41:13 +00004119shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004120{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004121 _VSTD::swap(__ptr_, __r.__ptr_);
4122 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004123}
4124
4125template<class _Tp>
4126inline _LIBCPP_INLINE_VISIBILITY
4127void
Howard Hinnant1694d232011-05-28 14:41:13 +00004128shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004129{
4130 shared_ptr().swap(*this);
4131}
4132
4133template<class _Tp>
4134template<class _Yp>
4135inline _LIBCPP_INLINE_VISIBILITY
4136void
4137shared_ptr<_Tp>::reset(_Yp* __p)
4138{
4139 shared_ptr(__p).swap(*this);
4140}
4141
4142template<class _Tp>
4143template<class _Yp, class _Dp>
4144inline _LIBCPP_INLINE_VISIBILITY
4145void
4146shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4147{
4148 shared_ptr(__p, __d).swap(*this);
4149}
4150
4151template<class _Tp>
4152template<class _Yp, class _Dp, class _Alloc>
4153inline _LIBCPP_INLINE_VISIBILITY
4154void
4155shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4156{
4157 shared_ptr(__p, __d, __a).swap(*this);
4158}
4159
4160#ifndef _LIBCPP_HAS_NO_VARIADICS
4161
Howard Hinnant324bb032010-08-22 00:02:43 +00004162template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004163inline _LIBCPP_INLINE_VISIBILITY
4164shared_ptr<_Tp>
4165make_shared(_Args&& ...__args)
4166{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004167 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004168}
4169
Howard Hinnant324bb032010-08-22 00:02:43 +00004170template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004171inline _LIBCPP_INLINE_VISIBILITY
4172shared_ptr<_Tp>
4173allocate_shared(const _Alloc& __a, _Args&& ...__args)
4174{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004175 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004176}
4177
4178#else // _LIBCPP_HAS_NO_VARIADICS
4179
4180template<class _Tp>
4181inline _LIBCPP_INLINE_VISIBILITY
4182shared_ptr<_Tp>
4183make_shared()
4184{
4185 return shared_ptr<_Tp>::make_shared();
4186}
4187
4188template<class _Tp, class _A0>
4189inline _LIBCPP_INLINE_VISIBILITY
4190shared_ptr<_Tp>
4191make_shared(_A0& __a0)
4192{
4193 return shared_ptr<_Tp>::make_shared(__a0);
4194}
4195
4196template<class _Tp, class _A0, class _A1>
4197inline _LIBCPP_INLINE_VISIBILITY
4198shared_ptr<_Tp>
4199make_shared(_A0& __a0, _A1& __a1)
4200{
4201 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4202}
4203
Howard Hinnant324bb032010-08-22 00:02:43 +00004204template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004205inline _LIBCPP_INLINE_VISIBILITY
4206shared_ptr<_Tp>
4207make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4208{
4209 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4210}
4211
4212template<class _Tp, class _Alloc>
4213inline _LIBCPP_INLINE_VISIBILITY
4214shared_ptr<_Tp>
4215allocate_shared(const _Alloc& __a)
4216{
4217 return shared_ptr<_Tp>::allocate_shared(__a);
4218}
4219
4220template<class _Tp, class _Alloc, class _A0>
4221inline _LIBCPP_INLINE_VISIBILITY
4222shared_ptr<_Tp>
4223allocate_shared(const _Alloc& __a, _A0& __a0)
4224{
4225 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4226}
4227
4228template<class _Tp, class _Alloc, class _A0, class _A1>
4229inline _LIBCPP_INLINE_VISIBILITY
4230shared_ptr<_Tp>
4231allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4232{
4233 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4234}
4235
4236template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4237inline _LIBCPP_INLINE_VISIBILITY
4238shared_ptr<_Tp>
4239allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4240{
4241 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4242}
4243
4244#endif // _LIBCPP_HAS_NO_VARIADICS
4245
4246template<class _Tp, class _Up>
4247inline _LIBCPP_INLINE_VISIBILITY
4248bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004249operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004250{
4251 return __x.get() == __y.get();
4252}
4253
4254template<class _Tp, class _Up>
4255inline _LIBCPP_INLINE_VISIBILITY
4256bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004257operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004258{
4259 return !(__x == __y);
4260}
4261
4262template<class _Tp, class _Up>
4263inline _LIBCPP_INLINE_VISIBILITY
4264bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004265operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004266{
4267 return __x.get() < __y.get();
4268}
4269
4270template<class _Tp>
4271inline _LIBCPP_INLINE_VISIBILITY
4272void
Howard Hinnant1694d232011-05-28 14:41:13 +00004273swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004274{
4275 __x.swap(__y);
4276}
4277
4278template<class _Tp, class _Up>
4279inline _LIBCPP_INLINE_VISIBILITY
4280shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00004281static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004282{
4283 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4284}
4285
4286template<class _Tp, class _Up>
4287inline _LIBCPP_INLINE_VISIBILITY
4288shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00004289dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004290{
4291 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4292 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4293}
4294
4295template<class _Tp, class _Up>
4296shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00004297const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004298{
4299 return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get()));
4300}
4301
Howard Hinnantd4444702010-08-11 17:04:31 +00004302#ifndef _LIBCPP_NO_RTTI
4303
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004304template<class _Dp, class _Tp>
4305inline _LIBCPP_INLINE_VISIBILITY
4306_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00004307get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004308{
4309 return __p.template __get_deleter<_Dp>();
4310}
4311
Howard Hinnant324bb032010-08-22 00:02:43 +00004312#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00004313
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004314template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004315class _LIBCPP_VISIBLE weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004316{
Howard Hinnant324bb032010-08-22 00:02:43 +00004317public:
4318 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004319private:
4320 element_type* __ptr_;
4321 __shared_weak_count* __cntrl_;
4322
Howard Hinnant324bb032010-08-22 00:02:43 +00004323public:
Howard Hinnant1694d232011-05-28 14:41:13 +00004324 weak_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004325 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00004326 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4327 _NOEXCEPT;
4328 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004329 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00004330 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4331 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004332
4333 ~weak_ptr();
4334
Howard Hinnant1694d232011-05-28 14:41:13 +00004335 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
4336 template<class _Yp> weak_ptr& operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4337 template<class _Yp> weak_ptr& operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004338
Howard Hinnant1694d232011-05-28 14:41:13 +00004339 void swap(weak_ptr& __r) _NOEXCEPT;
4340 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004341
Howard Hinnant82894812010-09-22 16:48:34 +00004342 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004343 long use_count() const _NOEXCEPT
4344 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00004345 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004346 bool expired() const _NOEXCEPT
4347 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4348 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00004349 template<class _Up>
4350 _LIBCPP_INLINE_VISIBILITY
4351 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004352 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004353 template<class _Up>
4354 _LIBCPP_INLINE_VISIBILITY
4355 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004356 {return __cntrl_ < __r.__cntrl_;}
4357
Howard Hinnant82894812010-09-22 16:48:34 +00004358 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
4359 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004360};
4361
4362template<class _Tp>
4363inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004364weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004365 : __ptr_(0),
4366 __cntrl_(0)
4367{
4368}
4369
4370template<class _Tp>
4371inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004372weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004373 : __ptr_(__r.__ptr_),
4374 __cntrl_(__r.__cntrl_)
4375{
4376 if (__cntrl_)
4377 __cntrl_->__add_weak();
4378}
4379
4380template<class _Tp>
4381template<class _Yp>
4382inline _LIBCPP_INLINE_VISIBILITY
4383weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00004384 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004385 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004386 : __ptr_(__r.__ptr_),
4387 __cntrl_(__r.__cntrl_)
4388{
4389 if (__cntrl_)
4390 __cntrl_->__add_weak();
4391}
4392
4393template<class _Tp>
4394template<class _Yp>
4395inline _LIBCPP_INLINE_VISIBILITY
4396weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00004397 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004398 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004399 : __ptr_(__r.__ptr_),
4400 __cntrl_(__r.__cntrl_)
4401{
4402 if (__cntrl_)
4403 __cntrl_->__add_weak();
4404}
4405
4406template<class _Tp>
4407weak_ptr<_Tp>::~weak_ptr()
4408{
4409 if (__cntrl_)
4410 __cntrl_->__release_weak();
4411}
4412
4413template<class _Tp>
4414inline _LIBCPP_INLINE_VISIBILITY
4415weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004416weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004417{
4418 weak_ptr(__r).swap(*this);
4419 return *this;
4420}
4421
4422template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00004423template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004424inline _LIBCPP_INLINE_VISIBILITY
4425weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004426weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004427{
4428 weak_ptr(__r).swap(*this);
4429 return *this;
4430}
4431
4432template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00004433template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004434inline _LIBCPP_INLINE_VISIBILITY
4435weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004436weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004437{
4438 weak_ptr(__r).swap(*this);
4439 return *this;
4440}
4441
4442template<class _Tp>
4443inline _LIBCPP_INLINE_VISIBILITY
4444void
Howard Hinnant1694d232011-05-28 14:41:13 +00004445weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004446{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004447 _VSTD::swap(__ptr_, __r.__ptr_);
4448 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004449}
4450
4451template<class _Tp>
4452inline _LIBCPP_INLINE_VISIBILITY
4453void
Howard Hinnant1694d232011-05-28 14:41:13 +00004454swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004455{
4456 __x.swap(__y);
4457}
4458
4459template<class _Tp>
4460inline _LIBCPP_INLINE_VISIBILITY
4461void
Howard Hinnant1694d232011-05-28 14:41:13 +00004462weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004463{
4464 weak_ptr().swap(*this);
4465}
4466
4467template<class _Tp>
4468template<class _Yp>
4469shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
4470 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
4471 : __ptr_(__r.__ptr_),
4472 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4473{
4474 if (__cntrl_ == 0)
4475#ifndef _LIBCPP_NO_EXCEPTIONS
4476 throw bad_weak_ptr();
4477#else
4478 assert(!"bad_weak_ptr");
4479#endif
4480}
4481
4482template<class _Tp>
4483shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00004484weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004485{
4486 shared_ptr<_Tp> __r;
4487 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4488 if (__r.__cntrl_)
4489 __r.__ptr_ = __ptr_;
4490 return __r;
4491}
4492
Howard Hinnant324bb032010-08-22 00:02:43 +00004493template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004494
4495template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004496struct _LIBCPP_VISIBLE owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004497 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00004498{
4499 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00004500 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004501 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
4502 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004503 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004504 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
4505 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004506 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004507 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
4508 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00004509};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004510
4511template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004512struct _LIBCPP_VISIBLE owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004513 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4514{
Howard Hinnant324bb032010-08-22 00:02:43 +00004515 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00004516 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004517 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
4518 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004519 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004520 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
4521 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004522 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004523 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
4524 {return __x.owner_before(__y);}
4525};
4526
4527template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004528class _LIBCPP_VISIBLE enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004529{
4530 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00004531protected:
Howard Hinnant82894812010-09-22 16:48:34 +00004532 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004533 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00004534 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004535 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00004536 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004537 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
4538 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00004539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004540 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00004541public:
Howard Hinnant82894812010-09-22 16:48:34 +00004542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004543 shared_ptr<_Tp> shared_from_this()
4544 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00004545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004546 shared_ptr<_Tp const> shared_from_this() const
4547 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004548
4549 template <class _Up> friend class shared_ptr;
4550};
4551
Howard Hinnant21aefc32010-06-03 16:42:57 +00004552template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004553struct _LIBCPP_VISIBLE hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00004554{
4555 typedef shared_ptr<_Tp> argument_type;
4556 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00004557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004558 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00004559 {
4560 return hash<_Tp*>()(__ptr.get());
4561 }
4562};
4563
Howard Hinnant99968442011-11-29 18:15:50 +00004564template<class _CharT, class _Traits, class _Yp>
Howard Hinnant464aa5c2011-07-18 15:51:59 +00004565inline _LIBCPP_INLINE_VISIBILITY
4566basic_ostream<_CharT, _Traits>&
Howard Hinnant99968442011-11-29 18:15:50 +00004567operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnant464aa5c2011-07-18 15:51:59 +00004568
Howard Hinnant324bb032010-08-22 00:02:43 +00004569//enum class
Howard Hinnant82894812010-09-22 16:48:34 +00004570struct _LIBCPP_VISIBLE pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004571{
4572 enum _
4573 {
4574 relaxed,
4575 preferred,
4576 strict
4577 };
4578
4579 _ __v_;
4580
Howard Hinnant82894812010-09-22 16:48:34 +00004581 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004582 pointer_safety(_ __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00004583 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004584 operator int() const {return __v_;}
4585};
4586
4587void declare_reachable(void* __p);
4588void declare_no_pointers(char* __p, size_t __n);
4589void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnant1694d232011-05-28 14:41:13 +00004590pointer_safety get_pointer_safety() _NOEXCEPT;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00004591void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004592
4593template <class _Tp>
4594inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00004595_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004596undeclare_reachable(_Tp* __p)
4597{
4598 return static_cast<_Tp*>(__undeclare_reachable(__p));
4599}
4600
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00004601void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004602
4603_LIBCPP_END_NAMESPACE_STD
4604
4605#endif // _LIBCPP_MEMORY