blob: d15d81a2a0f7168ebba9b183771b9bc3777eb28a [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{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003218 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003219#ifndef _LIBCPP_NO_EXCEPTIONS
3220 _ForwardIterator __s = __r;
3221 try
3222 {
3223#endif
3224 for (; __f != __l; ++__f, ++__r)
3225 ::new(&*__r) value_type(*__f);
3226#ifndef _LIBCPP_NO_EXCEPTIONS
3227 }
3228 catch (...)
3229 {
3230 for (; __s != __r; ++__s)
3231 __s->~value_type();
3232 throw;
3233 }
3234#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003235 return __r;
3236}
3237
3238template <class _InputIterator, class _Size, class _ForwardIterator>
3239_ForwardIterator
3240uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3241{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003242 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003243#ifndef _LIBCPP_NO_EXCEPTIONS
3244 _ForwardIterator __s = __r;
3245 try
3246 {
3247#endif
3248 for (; __n > 0; ++__f, ++__r, --__n)
3249 ::new(&*__r) value_type(*__f);
3250#ifndef _LIBCPP_NO_EXCEPTIONS
3251 }
3252 catch (...)
3253 {
3254 for (; __s != __r; ++__s)
3255 __s->~value_type();
3256 throw;
3257 }
3258#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003259 return __r;
3260}
3261
3262template <class _ForwardIterator, class _Tp>
3263void
3264uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3265{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003266 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003267#ifndef _LIBCPP_NO_EXCEPTIONS
3268 _ForwardIterator __s = __f;
3269 try
3270 {
3271#endif
3272 for (; __f != __l; ++__f)
3273 ::new(&*__f) value_type(__x);
3274#ifndef _LIBCPP_NO_EXCEPTIONS
3275 }
3276 catch (...)
3277 {
3278 for (; __s != __f; ++__s)
3279 __s->~value_type();
3280 throw;
3281 }
3282#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003283}
3284
3285template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003286_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003287uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3288{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003289 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003290#ifndef _LIBCPP_NO_EXCEPTIONS
3291 _ForwardIterator __s = __f;
3292 try
3293 {
3294#endif
3295 for (; __n > 0; ++__f, --__n)
3296 ::new(&*__f) value_type(__x);
3297#ifndef _LIBCPP_NO_EXCEPTIONS
3298 }
3299 catch (...)
3300 {
3301 for (; __s != __f; ++__s)
3302 __s->~value_type();
3303 throw;
3304 }
3305#endif
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003306 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003307}
3308
Howard Hinnant82894812010-09-22 16:48:34 +00003309class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003310 : public std::exception
3311{
3312public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003313 virtual ~bad_weak_ptr() _NOEXCEPT;
3314 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003315};
3316
3317template<class _Tp> class weak_ptr;
3318
3319class __shared_count
3320{
3321 __shared_count(const __shared_count&);
3322 __shared_count& operator=(const __shared_count&);
3323
3324protected:
3325 long __shared_owners_;
3326 virtual ~__shared_count();
3327private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003328 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003329
3330public:
Howard Hinnant82894812010-09-22 16:48:34 +00003331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003332 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003333 : __shared_owners_(__refs) {}
3334
Howard Hinnant1694d232011-05-28 14:41:13 +00003335 void __add_shared() _NOEXCEPT;
3336 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003338 long use_count() const _NOEXCEPT {return __shared_owners_ + 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003339};
3340
3341class __shared_weak_count
3342 : private __shared_count
3343{
3344 long __shared_weak_owners_;
3345
3346public:
Howard Hinnant82894812010-09-22 16:48:34 +00003347 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003348 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003349 : __shared_count(__refs),
3350 __shared_weak_owners_(__refs) {}
3351protected:
3352 virtual ~__shared_weak_count();
3353
3354public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003355 void __add_shared() _NOEXCEPT;
3356 void __add_weak() _NOEXCEPT;
3357 void __release_shared() _NOEXCEPT;
3358 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003360 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3361 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003362
Howard Hinnant1694d232011-05-28 14:41:13 +00003363 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003364private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003365 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003366};
3367
3368template <class _Tp, class _Dp, class _Alloc>
3369class __shared_ptr_pointer
3370 : public __shared_weak_count
3371{
3372 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3373public:
Howard Hinnant82894812010-09-22 16:48:34 +00003374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003375 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003376 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003377
Howard Hinnantd4444702010-08-11 17:04:31 +00003378#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00003379 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00003380#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003381
3382private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003383 virtual void __on_zero_shared() _NOEXCEPT;
3384 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003385};
3386
Howard Hinnantd4444702010-08-11 17:04:31 +00003387#ifndef _LIBCPP_NO_RTTI
3388
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003389template <class _Tp, class _Dp, class _Alloc>
3390const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00003391__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003392{
3393 return __t == typeid(_Dp) ? &__data_.first().second() : 0;
3394}
3395
Howard Hinnant324bb032010-08-22 00:02:43 +00003396#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003397
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003398template <class _Tp, class _Dp, class _Alloc>
3399void
Howard Hinnant1694d232011-05-28 14:41:13 +00003400__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003401{
3402 __data_.first().second()(__data_.first().first());
3403 __data_.first().second().~_Dp();
3404}
3405
3406template <class _Tp, class _Dp, class _Alloc>
3407void
Howard Hinnant1694d232011-05-28 14:41:13 +00003408__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003409{
3410 typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
3411 __data_.second().~_Alloc();
3412 __a.deallocate(this, 1);
3413}
3414
3415template <class _Tp, class _Alloc>
3416class __shared_ptr_emplace
3417 : public __shared_weak_count
3418{
3419 __compressed_pair<_Alloc, _Tp> __data_;
3420public:
3421#ifndef _LIBCPP_HAS_NO_VARIADICS
3422
Howard Hinnant82894812010-09-22 16:48:34 +00003423 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003424 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003425 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003426
3427 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00003428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003429 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00003430 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3431 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003432
3433#else // _LIBCPP_HAS_NO_VARIADICS
3434
Howard Hinnant82894812010-09-22 16:48:34 +00003435 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003436 __shared_ptr_emplace(_Alloc __a)
3437 : __data_(__a) {}
3438
3439 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00003440 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003441 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3442 : __data_(__a, _Tp(__a0)) {}
3443
3444 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00003445 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003446 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3447 : __data_(__a, _Tp(__a0, __a1)) {}
3448
3449 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00003450 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003451 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3452 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3453
3454#endif // _LIBCPP_HAS_NO_VARIADICS
3455
3456private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003457 virtual void __on_zero_shared() _NOEXCEPT;
3458 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003459public:
Howard Hinnant82894812010-09-22 16:48:34 +00003460 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003461 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003462};
3463
3464template <class _Tp, class _Alloc>
3465void
Howard Hinnant1694d232011-05-28 14:41:13 +00003466__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003467{
3468 __data_.second().~_Tp();
3469}
3470
3471template <class _Tp, class _Alloc>
3472void
Howard Hinnant1694d232011-05-28 14:41:13 +00003473__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003474{
3475 typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
3476 __data_.first().~_Alloc();
3477 __a.deallocate(this, 1);
3478}
3479
3480template<class _Tp> class enable_shared_from_this;
3481
3482template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003483class _LIBCPP_VISIBLE shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003484{
Howard Hinnant324bb032010-08-22 00:02:43 +00003485public:
3486 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003487private:
3488 element_type* __ptr_;
3489 __shared_weak_count* __cntrl_;
3490
3491 struct __nat {int __for_bool_;};
3492public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003493 shared_ptr() _NOEXCEPT;
3494 shared_ptr(nullptr_t) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003495 template<class _Yp> explicit shared_ptr(_Yp* __p);
Howard Hinnant324bb032010-08-22 00:02:43 +00003496 template<class _Yp, class _Dp> shared_ptr(_Yp* __p, _Dp __d);
3497 template<class _Yp, class _Dp, class _Alloc> shared_ptr(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003498 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3499 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant1694d232011-05-28 14:41:13 +00003500 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3501 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003502 template<class _Yp>
3503 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003504 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3505 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003506#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003507 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003508 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003509 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3510 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003511#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003512 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003513 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003514#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00003515 template<class _Yp> shared_ptr(auto_ptr<_Yp>&& __r);
3516#else
3517 template<class _Yp> shared_ptr(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003518#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003519#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003520private:
Howard Hinnant324bb032010-08-22 00:02:43 +00003521 template <class _Yp, class _Dp> shared_ptr(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003522public:
3523 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
3524 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
3525 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
3526 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003527#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003528 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
3529 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
3530 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
3531 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003532#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003533
3534 ~shared_ptr();
3535
Howard Hinnant1694d232011-05-28 14:41:13 +00003536 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
3537 template<class _Yp> shared_ptr& operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003538#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003539 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00003540 template<class _Yp> shared_ptr& operator=(shared_ptr<_Yp>&& __r);
3541 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003542#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00003543 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003544#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003545#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003546private:
Howard Hinnant324bb032010-08-22 00:02:43 +00003547 template <class _Yp, class _Dp> shared_ptr& operator=(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003548public:
Howard Hinnant324bb032010-08-22 00:02:43 +00003549 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003550#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00003551 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003552#endif
3553
Howard Hinnant1694d232011-05-28 14:41:13 +00003554 void swap(shared_ptr& __r) _NOEXCEPT;
3555 void reset() _NOEXCEPT;
Howard Hinnantec3773c2011-12-01 20:21:04 +00003556 template<class _Yp> void reset(_Yp* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003557 template<class _Yp, class _Dp> void reset(_Yp* __p, _Dp __d);
3558 template<class _Yp, class _Dp, class _Alloc> void reset(_Yp* __p, _Dp __d, _Alloc __a);
3559
Howard Hinnant82894812010-09-22 16:48:34 +00003560 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003561 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003563 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3564 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003565 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003566 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003568 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003570 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00003571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003572 /*explicit*/ operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant99968442011-11-29 18:15:50 +00003573 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00003574 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003575 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003576 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant99968442011-11-29 18:15:50 +00003577 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00003578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003579 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003580 {return __cntrl_ < __p.__cntrl_;}
3581
Howard Hinnantd4444702010-08-11 17:04:31 +00003582#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003583 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00003584 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003585 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003586 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00003587#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003588
3589#ifndef _LIBCPP_HAS_NO_VARIADICS
3590
3591 template<class ..._Args>
3592 static
3593 shared_ptr<_Tp>
3594 make_shared(_Args&& ...__args);
3595
3596 template<class _Alloc, class ..._Args>
3597 static
3598 shared_ptr<_Tp>
3599 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3600
3601#else // _LIBCPP_HAS_NO_VARIADICS
3602
3603 static shared_ptr<_Tp> make_shared();
3604
3605 template<class _A0>
3606 static shared_ptr<_Tp> make_shared(_A0&);
3607
3608 template<class _A0, class _A1>
3609 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3610
3611 template<class _A0, class _A1, class _A2>
3612 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3613
3614 template<class _Alloc>
3615 static shared_ptr<_Tp>
3616 allocate_shared(const _Alloc& __a);
3617
3618 template<class _Alloc, class _A0>
3619 static shared_ptr<_Tp>
3620 allocate_shared(const _Alloc& __a, _A0& __a0);
3621
3622 template<class _Alloc, class _A0, class _A1>
3623 static shared_ptr<_Tp>
3624 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3625
3626 template<class _Alloc, class _A0, class _A1, class _A2>
3627 static shared_ptr<_Tp>
3628 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3629
3630#endif // _LIBCPP_HAS_NO_VARIADICS
3631
3632private:
3633
3634 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00003635 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003636 void
Howard Hinnant1694d232011-05-28 14:41:13 +00003637 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003638 {
3639 if (__e)
3640 __e->__weak_this_ = *this;
3641 }
3642
Howard Hinnant82894812010-09-22 16:48:34 +00003643 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003644 void __enable_weak_this(const void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003645
Howard Hinnant82894812010-09-22 16:48:34 +00003646 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
3647 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003648};
3649
3650template<class _Tp>
3651inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003652shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003653 : __ptr_(0),
3654 __cntrl_(0)
3655{
3656}
3657
3658template<class _Tp>
3659inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003660shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003661 : __ptr_(0),
3662 __cntrl_(0)
3663{
3664}
3665
3666template<class _Tp>
3667template<class _Yp>
3668shared_ptr<_Tp>::shared_ptr(_Yp* __p)
3669 : __ptr_(__p)
3670{
3671 unique_ptr<_Yp> __hold(__p);
3672 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3673 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
3674 __hold.release();
3675 __enable_weak_this(__p);
3676}
3677
3678template<class _Tp>
3679template<class _Yp, class _Dp>
3680shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
3681 : __ptr_(__p)
3682{
3683#ifndef _LIBCPP_NO_EXCEPTIONS
3684 try
3685 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003686#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003687 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3688 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
3689 __enable_weak_this(__p);
3690#ifndef _LIBCPP_NO_EXCEPTIONS
3691 }
3692 catch (...)
3693 {
3694 __d(__p);
3695 throw;
3696 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003697#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003698}
3699
3700template<class _Tp>
3701template<class _Dp>
3702shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3703 : __ptr_(0)
3704{
3705#ifndef _LIBCPP_NO_EXCEPTIONS
3706 try
3707 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003708#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003709 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
3710 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
3711#ifndef _LIBCPP_NO_EXCEPTIONS
3712 }
3713 catch (...)
3714 {
3715 __d(__p);
3716 throw;
3717 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003718#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003719}
3720
3721template<class _Tp>
3722template<class _Yp, class _Dp, class _Alloc>
3723shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
3724 : __ptr_(__p)
3725{
3726#ifndef _LIBCPP_NO_EXCEPTIONS
3727 try
3728 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003729#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003730 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
3731 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3732 typedef __allocator_destructor<_A2> _D2;
3733 _A2 __a2(__a);
3734 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3735 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
3736 __cntrl_ = __hold2.release();
3737 __enable_weak_this(__p);
3738#ifndef _LIBCPP_NO_EXCEPTIONS
3739 }
3740 catch (...)
3741 {
3742 __d(__p);
3743 throw;
3744 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003745#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003746}
3747
3748template<class _Tp>
3749template<class _Dp, class _Alloc>
3750shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
3751 : __ptr_(0)
3752{
3753#ifndef _LIBCPP_NO_EXCEPTIONS
3754 try
3755 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003756#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003757 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
3758 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3759 typedef __allocator_destructor<_A2> _D2;
3760 _A2 __a2(__a);
3761 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3762 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
3763 __cntrl_ = __hold2.release();
3764#ifndef _LIBCPP_NO_EXCEPTIONS
3765 }
3766 catch (...)
3767 {
3768 __d(__p);
3769 throw;
3770 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003771#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003772}
3773
3774template<class _Tp>
3775template<class _Yp>
3776inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003777shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003778 : __ptr_(__p),
3779 __cntrl_(__r.__cntrl_)
3780{
3781 if (__cntrl_)
3782 __cntrl_->__add_shared();
3783}
3784
3785template<class _Tp>
3786inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003787shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003788 : __ptr_(__r.__ptr_),
3789 __cntrl_(__r.__cntrl_)
3790{
3791 if (__cntrl_)
3792 __cntrl_->__add_shared();
3793}
3794
3795template<class _Tp>
3796template<class _Yp>
3797inline _LIBCPP_INLINE_VISIBILITY
3798shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
3799 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003800 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003801 : __ptr_(__r.__ptr_),
3802 __cntrl_(__r.__cntrl_)
3803{
3804 if (__cntrl_)
3805 __cntrl_->__add_shared();
3806}
3807
Howard Hinnant73d21a42010-09-04 23:28:19 +00003808#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003809
3810template<class _Tp>
3811inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003812shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003813 : __ptr_(__r.__ptr_),
3814 __cntrl_(__r.__cntrl_)
3815{
3816 __r.__ptr_ = 0;
3817 __r.__cntrl_ = 0;
3818}
3819
3820template<class _Tp>
3821template<class _Yp>
3822inline _LIBCPP_INLINE_VISIBILITY
3823shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
3824 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003825 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003826 : __ptr_(__r.__ptr_),
3827 __cntrl_(__r.__cntrl_)
3828{
3829 __r.__ptr_ = 0;
3830 __r.__cntrl_ = 0;
3831}
3832
Howard Hinnant73d21a42010-09-04 23:28:19 +00003833#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003834
3835template<class _Tp>
3836template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003837#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003838shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
3839#else
Howard Hinnant92172b82010-08-21 21:14:53 +00003840shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003841#endif
3842 : __ptr_(__r.get())
3843{
3844 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3845 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
3846 __enable_weak_this(__r.get());
3847 __r.release();
3848}
3849
3850template<class _Tp>
3851template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003852#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003853shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3854#else
3855shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3856#endif
3857 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
3858 : __ptr_(__r.get())
3859{
3860 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3861 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
3862 __enable_weak_this(__r.get());
3863 __r.release();
3864}
3865
3866template<class _Tp>
3867template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003868#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003869shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3870#else
3871shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3872#endif
3873 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
3874 : __ptr_(__r.get())
3875{
3876 typedef __shared_ptr_pointer<_Yp*,
3877 reference_wrapper<typename remove_reference<_Dp>::type>,
3878 allocator<_Yp> > _CntrlBlk;
3879 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
3880 __enable_weak_this(__r.get());
3881 __r.release();
3882}
3883
3884#ifndef _LIBCPP_HAS_NO_VARIADICS
3885
3886template<class _Tp>
3887template<class ..._Args>
3888shared_ptr<_Tp>
3889shared_ptr<_Tp>::make_shared(_Args&& ...__args)
3890{
3891 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3892 typedef allocator<_CntrlBlk> _A2;
3893 typedef __allocator_destructor<_A2> _D2;
3894 _A2 __a2;
3895 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003896 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003897 shared_ptr<_Tp> __r;
3898 __r.__ptr_ = __hold2.get()->get();
3899 __r.__cntrl_ = __hold2.release();
3900 __r.__enable_weak_this(__r.__ptr_);
3901 return __r;
3902}
3903
3904template<class _Tp>
3905template<class _Alloc, class ..._Args>
3906shared_ptr<_Tp>
3907shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
3908{
3909 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3910 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3911 typedef __allocator_destructor<_A2> _D2;
3912 _A2 __a2(__a);
3913 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003914 ::new(__hold2.get()) _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003915 shared_ptr<_Tp> __r;
3916 __r.__ptr_ = __hold2.get()->get();
3917 __r.__cntrl_ = __hold2.release();
3918 __r.__enable_weak_this(__r.__ptr_);
3919 return __r;
3920}
3921
3922#else // _LIBCPP_HAS_NO_VARIADICS
3923
3924template<class _Tp>
3925shared_ptr<_Tp>
3926shared_ptr<_Tp>::make_shared()
3927{
3928 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3929 typedef allocator<_CntrlBlk> _Alloc2;
3930 typedef __allocator_destructor<_Alloc2> _D2;
3931 _Alloc2 __alloc2;
3932 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3933 ::new(__hold2.get()) _CntrlBlk(__alloc2);
3934 shared_ptr<_Tp> __r;
3935 __r.__ptr_ = __hold2.get()->get();
3936 __r.__cntrl_ = __hold2.release();
3937 __r.__enable_weak_this(__r.__ptr_);
3938 return __r;
3939}
3940
3941template<class _Tp>
3942template<class _A0>
3943shared_ptr<_Tp>
3944shared_ptr<_Tp>::make_shared(_A0& __a0)
3945{
3946 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3947 typedef allocator<_CntrlBlk> _Alloc2;
3948 typedef __allocator_destructor<_Alloc2> _D2;
3949 _Alloc2 __alloc2;
3950 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3951 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
3952 shared_ptr<_Tp> __r;
3953 __r.__ptr_ = __hold2.get()->get();
3954 __r.__cntrl_ = __hold2.release();
3955 __r.__enable_weak_this(__r.__ptr_);
3956 return __r;
3957}
3958
3959template<class _Tp>
3960template<class _A0, class _A1>
3961shared_ptr<_Tp>
3962shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
3963{
3964 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3965 typedef allocator<_CntrlBlk> _Alloc2;
3966 typedef __allocator_destructor<_Alloc2> _D2;
3967 _Alloc2 __alloc2;
3968 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3969 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
3970 shared_ptr<_Tp> __r;
3971 __r.__ptr_ = __hold2.get()->get();
3972 __r.__cntrl_ = __hold2.release();
3973 __r.__enable_weak_this(__r.__ptr_);
3974 return __r;
3975}
3976
3977template<class _Tp>
3978template<class _A0, class _A1, class _A2>
3979shared_ptr<_Tp>
3980shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3981{
3982 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3983 typedef allocator<_CntrlBlk> _Alloc2;
3984 typedef __allocator_destructor<_Alloc2> _D2;
3985 _Alloc2 __alloc2;
3986 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3987 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
3988 shared_ptr<_Tp> __r;
3989 __r.__ptr_ = __hold2.get()->get();
3990 __r.__cntrl_ = __hold2.release();
3991 __r.__enable_weak_this(__r.__ptr_);
3992 return __r;
3993}
3994
3995template<class _Tp>
3996template<class _Alloc>
3997shared_ptr<_Tp>
3998shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
3999{
4000 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4001 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4002 typedef __allocator_destructor<_Alloc2> _D2;
4003 _Alloc2 __alloc2(__a);
4004 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4005 ::new(__hold2.get()) _CntrlBlk(__a);
4006 shared_ptr<_Tp> __r;
4007 __r.__ptr_ = __hold2.get()->get();
4008 __r.__cntrl_ = __hold2.release();
4009 __r.__enable_weak_this(__r.__ptr_);
4010 return __r;
4011}
4012
4013template<class _Tp>
4014template<class _Alloc, class _A0>
4015shared_ptr<_Tp>
4016shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4017{
4018 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4019 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4020 typedef __allocator_destructor<_Alloc2> _D2;
4021 _Alloc2 __alloc2(__a);
4022 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4023 ::new(__hold2.get()) _CntrlBlk(__a, __a0);
4024 shared_ptr<_Tp> __r;
4025 __r.__ptr_ = __hold2.get()->get();
4026 __r.__cntrl_ = __hold2.release();
4027 __r.__enable_weak_this(__r.__ptr_);
4028 return __r;
4029}
4030
4031template<class _Tp>
4032template<class _Alloc, class _A0, class _A1>
4033shared_ptr<_Tp>
4034shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4035{
4036 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4037 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4038 typedef __allocator_destructor<_Alloc2> _D2;
4039 _Alloc2 __alloc2(__a);
4040 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4041 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
4042 shared_ptr<_Tp> __r;
4043 __r.__ptr_ = __hold2.get()->get();
4044 __r.__cntrl_ = __hold2.release();
4045 __r.__enable_weak_this(__r.__ptr_);
4046 return __r;
4047}
4048
4049template<class _Tp>
4050template<class _Alloc, class _A0, class _A1, class _A2>
4051shared_ptr<_Tp>
4052shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4053{
4054 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4055 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4056 typedef __allocator_destructor<_Alloc2> _D2;
4057 _Alloc2 __alloc2(__a);
4058 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4059 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
4060 shared_ptr<_Tp> __r;
4061 __r.__ptr_ = __hold2.get()->get();
4062 __r.__cntrl_ = __hold2.release();
4063 __r.__enable_weak_this(__r.__ptr_);
4064 return __r;
4065}
4066
4067#endif // _LIBCPP_HAS_NO_VARIADICS
4068
4069template<class _Tp>
4070shared_ptr<_Tp>::~shared_ptr()
4071{
4072 if (__cntrl_)
4073 __cntrl_->__release_shared();
4074}
4075
4076template<class _Tp>
4077inline _LIBCPP_INLINE_VISIBILITY
4078shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004079shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004080{
4081 shared_ptr(__r).swap(*this);
4082 return *this;
4083}
4084
4085template<class _Tp>
4086template<class _Yp>
4087inline _LIBCPP_INLINE_VISIBILITY
4088shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004089shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004090{
4091 shared_ptr(__r).swap(*this);
4092 return *this;
4093}
4094
Howard Hinnant73d21a42010-09-04 23:28:19 +00004095#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004096
4097template<class _Tp>
4098inline _LIBCPP_INLINE_VISIBILITY
4099shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004100shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004101{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004102 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004103 return *this;
4104}
4105
4106template<class _Tp>
4107template<class _Yp>
4108inline _LIBCPP_INLINE_VISIBILITY
4109shared_ptr<_Tp>&
4110shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4111{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004112 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004113 return *this;
4114}
4115
4116template<class _Tp>
4117template<class _Yp>
4118inline _LIBCPP_INLINE_VISIBILITY
4119shared_ptr<_Tp>&
4120shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4121{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004122 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004123 return *this;
4124}
4125
4126template<class _Tp>
4127template <class _Yp, class _Dp>
4128inline _LIBCPP_INLINE_VISIBILITY
4129shared_ptr<_Tp>&
4130shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4131{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004132 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004133 return *this;
4134}
4135
Howard Hinnant73d21a42010-09-04 23:28:19 +00004136#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004137
4138template<class _Tp>
4139template<class _Yp>
4140inline _LIBCPP_INLINE_VISIBILITY
4141shared_ptr<_Tp>&
Howard Hinnant324bb032010-08-22 00:02:43 +00004142shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004143{
4144 shared_ptr(__r).swap(*this);
4145 return *this;
4146}
4147
4148template<class _Tp>
4149template <class _Yp, class _Dp>
4150inline _LIBCPP_INLINE_VISIBILITY
4151shared_ptr<_Tp>&
4152shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4153{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004154 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004155 return *this;
4156}
4157
Howard Hinnant73d21a42010-09-04 23:28:19 +00004158#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004159
4160template<class _Tp>
4161inline _LIBCPP_INLINE_VISIBILITY
4162void
Howard Hinnant1694d232011-05-28 14:41:13 +00004163shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004164{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004165 _VSTD::swap(__ptr_, __r.__ptr_);
4166 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004167}
4168
4169template<class _Tp>
4170inline _LIBCPP_INLINE_VISIBILITY
4171void
Howard Hinnant1694d232011-05-28 14:41:13 +00004172shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004173{
4174 shared_ptr().swap(*this);
4175}
4176
4177template<class _Tp>
4178template<class _Yp>
4179inline _LIBCPP_INLINE_VISIBILITY
4180void
4181shared_ptr<_Tp>::reset(_Yp* __p)
4182{
4183 shared_ptr(__p).swap(*this);
4184}
4185
4186template<class _Tp>
4187template<class _Yp, class _Dp>
4188inline _LIBCPP_INLINE_VISIBILITY
4189void
4190shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4191{
4192 shared_ptr(__p, __d).swap(*this);
4193}
4194
4195template<class _Tp>
4196template<class _Yp, class _Dp, class _Alloc>
4197inline _LIBCPP_INLINE_VISIBILITY
4198void
4199shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4200{
4201 shared_ptr(__p, __d, __a).swap(*this);
4202}
4203
4204#ifndef _LIBCPP_HAS_NO_VARIADICS
4205
Howard Hinnant324bb032010-08-22 00:02:43 +00004206template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004207inline _LIBCPP_INLINE_VISIBILITY
4208shared_ptr<_Tp>
4209make_shared(_Args&& ...__args)
4210{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004211 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004212}
4213
Howard Hinnant324bb032010-08-22 00:02:43 +00004214template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004215inline _LIBCPP_INLINE_VISIBILITY
4216shared_ptr<_Tp>
4217allocate_shared(const _Alloc& __a, _Args&& ...__args)
4218{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004219 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004220}
4221
4222#else // _LIBCPP_HAS_NO_VARIADICS
4223
4224template<class _Tp>
4225inline _LIBCPP_INLINE_VISIBILITY
4226shared_ptr<_Tp>
4227make_shared()
4228{
4229 return shared_ptr<_Tp>::make_shared();
4230}
4231
4232template<class _Tp, class _A0>
4233inline _LIBCPP_INLINE_VISIBILITY
4234shared_ptr<_Tp>
4235make_shared(_A0& __a0)
4236{
4237 return shared_ptr<_Tp>::make_shared(__a0);
4238}
4239
4240template<class _Tp, class _A0, class _A1>
4241inline _LIBCPP_INLINE_VISIBILITY
4242shared_ptr<_Tp>
4243make_shared(_A0& __a0, _A1& __a1)
4244{
4245 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4246}
4247
Howard Hinnant324bb032010-08-22 00:02:43 +00004248template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004249inline _LIBCPP_INLINE_VISIBILITY
4250shared_ptr<_Tp>
4251make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4252{
4253 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4254}
4255
4256template<class _Tp, class _Alloc>
4257inline _LIBCPP_INLINE_VISIBILITY
4258shared_ptr<_Tp>
4259allocate_shared(const _Alloc& __a)
4260{
4261 return shared_ptr<_Tp>::allocate_shared(__a);
4262}
4263
4264template<class _Tp, class _Alloc, class _A0>
4265inline _LIBCPP_INLINE_VISIBILITY
4266shared_ptr<_Tp>
4267allocate_shared(const _Alloc& __a, _A0& __a0)
4268{
4269 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4270}
4271
4272template<class _Tp, class _Alloc, class _A0, class _A1>
4273inline _LIBCPP_INLINE_VISIBILITY
4274shared_ptr<_Tp>
4275allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4276{
4277 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4278}
4279
4280template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4281inline _LIBCPP_INLINE_VISIBILITY
4282shared_ptr<_Tp>
4283allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4284{
4285 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4286}
4287
4288#endif // _LIBCPP_HAS_NO_VARIADICS
4289
4290template<class _Tp, class _Up>
4291inline _LIBCPP_INLINE_VISIBILITY
4292bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004293operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004294{
4295 return __x.get() == __y.get();
4296}
4297
4298template<class _Tp, class _Up>
4299inline _LIBCPP_INLINE_VISIBILITY
4300bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004301operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004302{
4303 return !(__x == __y);
4304}
4305
4306template<class _Tp, class _Up>
4307inline _LIBCPP_INLINE_VISIBILITY
4308bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004309operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004310{
4311 return __x.get() < __y.get();
4312}
4313
4314template<class _Tp>
4315inline _LIBCPP_INLINE_VISIBILITY
4316void
Howard Hinnant1694d232011-05-28 14:41:13 +00004317swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004318{
4319 __x.swap(__y);
4320}
4321
4322template<class _Tp, class _Up>
4323inline _LIBCPP_INLINE_VISIBILITY
4324shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00004325static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004326{
4327 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4328}
4329
4330template<class _Tp, class _Up>
4331inline _LIBCPP_INLINE_VISIBILITY
4332shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00004333dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004334{
4335 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4336 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4337}
4338
4339template<class _Tp, class _Up>
4340shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00004341const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004342{
4343 return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get()));
4344}
4345
Howard Hinnantd4444702010-08-11 17:04:31 +00004346#ifndef _LIBCPP_NO_RTTI
4347
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004348template<class _Dp, class _Tp>
4349inline _LIBCPP_INLINE_VISIBILITY
4350_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00004351get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004352{
4353 return __p.template __get_deleter<_Dp>();
4354}
4355
Howard Hinnant324bb032010-08-22 00:02:43 +00004356#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00004357
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004358template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004359class _LIBCPP_VISIBLE weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004360{
Howard Hinnant324bb032010-08-22 00:02:43 +00004361public:
4362 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004363private:
4364 element_type* __ptr_;
4365 __shared_weak_count* __cntrl_;
4366
Howard Hinnant324bb032010-08-22 00:02:43 +00004367public:
Howard Hinnant1694d232011-05-28 14:41:13 +00004368 weak_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004369 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00004370 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4371 _NOEXCEPT;
4372 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004373 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00004374 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4375 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004376
4377 ~weak_ptr();
4378
Howard Hinnant1694d232011-05-28 14:41:13 +00004379 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
4380 template<class _Yp> weak_ptr& operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4381 template<class _Yp> weak_ptr& operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004382
Howard Hinnant1694d232011-05-28 14:41:13 +00004383 void swap(weak_ptr& __r) _NOEXCEPT;
4384 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004385
Howard Hinnant82894812010-09-22 16:48:34 +00004386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004387 long use_count() const _NOEXCEPT
4388 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00004389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004390 bool expired() const _NOEXCEPT
4391 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4392 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00004393 template<class _Up>
4394 _LIBCPP_INLINE_VISIBILITY
4395 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004396 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004397 template<class _Up>
4398 _LIBCPP_INLINE_VISIBILITY
4399 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004400 {return __cntrl_ < __r.__cntrl_;}
4401
Howard Hinnant82894812010-09-22 16:48:34 +00004402 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
4403 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004404};
4405
4406template<class _Tp>
4407inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004408weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004409 : __ptr_(0),
4410 __cntrl_(0)
4411{
4412}
4413
4414template<class _Tp>
4415inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004416weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004417 : __ptr_(__r.__ptr_),
4418 __cntrl_(__r.__cntrl_)
4419{
4420 if (__cntrl_)
4421 __cntrl_->__add_weak();
4422}
4423
4424template<class _Tp>
4425template<class _Yp>
4426inline _LIBCPP_INLINE_VISIBILITY
4427weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00004428 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004429 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004430 : __ptr_(__r.__ptr_),
4431 __cntrl_(__r.__cntrl_)
4432{
4433 if (__cntrl_)
4434 __cntrl_->__add_weak();
4435}
4436
4437template<class _Tp>
4438template<class _Yp>
4439inline _LIBCPP_INLINE_VISIBILITY
4440weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00004441 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004442 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004443 : __ptr_(__r.__ptr_),
4444 __cntrl_(__r.__cntrl_)
4445{
4446 if (__cntrl_)
4447 __cntrl_->__add_weak();
4448}
4449
4450template<class _Tp>
4451weak_ptr<_Tp>::~weak_ptr()
4452{
4453 if (__cntrl_)
4454 __cntrl_->__release_weak();
4455}
4456
4457template<class _Tp>
4458inline _LIBCPP_INLINE_VISIBILITY
4459weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004460weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004461{
4462 weak_ptr(__r).swap(*this);
4463 return *this;
4464}
4465
4466template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00004467template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004468inline _LIBCPP_INLINE_VISIBILITY
4469weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004470weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004471{
4472 weak_ptr(__r).swap(*this);
4473 return *this;
4474}
4475
4476template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00004477template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004478inline _LIBCPP_INLINE_VISIBILITY
4479weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004480weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004481{
4482 weak_ptr(__r).swap(*this);
4483 return *this;
4484}
4485
4486template<class _Tp>
4487inline _LIBCPP_INLINE_VISIBILITY
4488void
Howard Hinnant1694d232011-05-28 14:41:13 +00004489weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004490{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004491 _VSTD::swap(__ptr_, __r.__ptr_);
4492 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004493}
4494
4495template<class _Tp>
4496inline _LIBCPP_INLINE_VISIBILITY
4497void
Howard Hinnant1694d232011-05-28 14:41:13 +00004498swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004499{
4500 __x.swap(__y);
4501}
4502
4503template<class _Tp>
4504inline _LIBCPP_INLINE_VISIBILITY
4505void
Howard Hinnant1694d232011-05-28 14:41:13 +00004506weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004507{
4508 weak_ptr().swap(*this);
4509}
4510
4511template<class _Tp>
4512template<class _Yp>
4513shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
4514 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
4515 : __ptr_(__r.__ptr_),
4516 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4517{
4518 if (__cntrl_ == 0)
4519#ifndef _LIBCPP_NO_EXCEPTIONS
4520 throw bad_weak_ptr();
4521#else
4522 assert(!"bad_weak_ptr");
4523#endif
4524}
4525
4526template<class _Tp>
4527shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00004528weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004529{
4530 shared_ptr<_Tp> __r;
4531 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4532 if (__r.__cntrl_)
4533 __r.__ptr_ = __ptr_;
4534 return __r;
4535}
4536
Howard Hinnant324bb032010-08-22 00:02:43 +00004537template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004538
4539template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004540struct _LIBCPP_VISIBLE owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004541 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00004542{
4543 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00004544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004545 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
4546 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004548 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
4549 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004551 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
4552 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00004553};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004554
4555template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004556struct _LIBCPP_VISIBLE owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004557 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4558{
Howard Hinnant324bb032010-08-22 00:02:43 +00004559 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00004560 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004561 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
4562 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004564 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
4565 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004566 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004567 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
4568 {return __x.owner_before(__y);}
4569};
4570
4571template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004572class _LIBCPP_VISIBLE enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004573{
4574 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00004575protected:
Howard Hinnant82894812010-09-22 16:48:34 +00004576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004577 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00004578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004579 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00004580 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004581 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
4582 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00004583 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004584 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00004585public:
Howard Hinnant82894812010-09-22 16:48:34 +00004586 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004587 shared_ptr<_Tp> shared_from_this()
4588 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00004589 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004590 shared_ptr<_Tp const> shared_from_this() const
4591 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004592
4593 template <class _Up> friend class shared_ptr;
4594};
4595
Howard Hinnant21aefc32010-06-03 16:42:57 +00004596template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004597struct _LIBCPP_VISIBLE hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00004598{
4599 typedef shared_ptr<_Tp> argument_type;
4600 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00004601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004602 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00004603 {
4604 return hash<_Tp*>()(__ptr.get());
4605 }
4606};
4607
Howard Hinnant99968442011-11-29 18:15:50 +00004608template<class _CharT, class _Traits, class _Yp>
Howard Hinnant464aa5c2011-07-18 15:51:59 +00004609inline _LIBCPP_INLINE_VISIBILITY
4610basic_ostream<_CharT, _Traits>&
Howard Hinnant99968442011-11-29 18:15:50 +00004611operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnant464aa5c2011-07-18 15:51:59 +00004612
Howard Hinnant324bb032010-08-22 00:02:43 +00004613//enum class
Howard Hinnant82894812010-09-22 16:48:34 +00004614struct _LIBCPP_VISIBLE pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004615{
4616 enum _
4617 {
4618 relaxed,
4619 preferred,
4620 strict
4621 };
4622
4623 _ __v_;
4624
Howard Hinnant82894812010-09-22 16:48:34 +00004625 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004626 pointer_safety(_ __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00004627 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004628 operator int() const {return __v_;}
4629};
4630
4631void declare_reachable(void* __p);
4632void declare_no_pointers(char* __p, size_t __n);
4633void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnant1694d232011-05-28 14:41:13 +00004634pointer_safety get_pointer_safety() _NOEXCEPT;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00004635void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004636
4637template <class _Tp>
4638inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00004639_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004640undeclare_reachable(_Tp* __p)
4641{
4642 return static_cast<_Tp*>(__undeclare_reachable(__p));
4643}
4644
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00004645void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004646
4647_LIBCPP_END_NAMESPACE_STD
4648
4649#endif // _LIBCPP_MEMORY