blob: 1d7b7456c93daa8dd4a63cc100047ceff7b2d05c [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
Howard Hinnant57199402012-01-02 17:56:02 +00001680template <class _Tp>
1681class _LIBCPP_VISIBLE allocator<const _Tp>
1682{
1683public:
1684 typedef size_t size_type;
1685 typedef ptrdiff_t difference_type;
1686 typedef const _Tp* pointer;
1687 typedef const _Tp* const_pointer;
1688 typedef const _Tp& reference;
1689 typedef const _Tp& const_reference;
1690 typedef _Tp value_type;
1691
1692 typedef true_type propagate_on_container_move_assignment;
1693
1694 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1695
1696 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1697 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1698 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1699 {return _VSTD::addressof(__x);}
1700 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1701 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
1702 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
1703 {::operator delete((void*)__p);}
1704 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1705 {return size_type(~0) / sizeof(_Tp);}
1706#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1707 template <class _Up, class... _Args>
1708 _LIBCPP_INLINE_VISIBILITY
1709 void
1710 construct(_Up* __p, _Args&&... __args)
1711 {
1712 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1713 }
1714#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1715 _LIBCPP_INLINE_VISIBILITY
1716 void
1717 construct(pointer __p)
1718 {
1719 ::new((void*)__p) _Tp();
1720 }
1721# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1722 template <class _A0>
1723 _LIBCPP_INLINE_VISIBILITY
1724 typename enable_if
1725 <
1726 !is_convertible<_A0, __rv<_A0> >::value,
1727 void
1728 >::type
1729 construct(pointer __p, _A0& __a0)
1730 {
1731 ::new((void*)__p) _Tp(__a0);
1732 }
1733 template <class _A0>
1734 _LIBCPP_INLINE_VISIBILITY
1735 typename enable_if
1736 <
1737 !is_convertible<_A0, __rv<_A0> >::value,
1738 void
1739 >::type
1740 construct(pointer __p, const _A0& __a0)
1741 {
1742 ::new((void*)__p) _Tp(__a0);
1743 }
1744 template <class _A0>
1745 _LIBCPP_INLINE_VISIBILITY
1746 typename enable_if
1747 <
1748 is_convertible<_A0, __rv<_A0> >::value,
1749 void
1750 >::type
1751 construct(pointer __p, _A0 __a0)
1752 {
1753 ::new((void*)__p) _Tp(_VSTD::move(__a0));
1754 }
1755# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1756 template <class _A0, class _A1>
1757 _LIBCPP_INLINE_VISIBILITY
1758 void
1759 construct(pointer __p, _A0& __a0, _A1& __a1)
1760 {
1761 ::new((void*)__p) _Tp(__a0, __a1);
1762 }
1763 template <class _A0, class _A1>
1764 _LIBCPP_INLINE_VISIBILITY
1765 void
1766 construct(pointer __p, const _A0& __a0, _A1& __a1)
1767 {
1768 ::new((void*)__p) _Tp(__a0, __a1);
1769 }
1770 template <class _A0, class _A1>
1771 _LIBCPP_INLINE_VISIBILITY
1772 void
1773 construct(pointer __p, _A0& __a0, const _A1& __a1)
1774 {
1775 ::new((void*)__p) _Tp(__a0, __a1);
1776 }
1777 template <class _A0, class _A1>
1778 _LIBCPP_INLINE_VISIBILITY
1779 void
1780 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1781 {
1782 ::new((void*)__p) _Tp(__a0, __a1);
1783 }
1784#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1785 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1786};
1787
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001788template <class _Tp, class _Up>
1789inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001790bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001791
1792template <class _Tp, class _Up>
1793inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001794bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001795
1796template <class _OutputIterator, class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001797class _LIBCPP_VISIBLE raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001798 : public iterator<output_iterator_tag,
1799 _Tp, // purposefully not C++03
1800 ptrdiff_t, // purposefully not C++03
1801 _Tp*, // purposefully not C++03
1802 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1803{
1804private:
1805 _OutputIterator __x_;
1806public:
1807 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1808 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1809 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1810 {::new(&*__x_) _Tp(__element); return *this;}
1811 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1812 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1813 {raw_storage_iterator __t(*this); ++__x_; return __t;}
1814};
1815
1816template <class _Tp>
1817pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00001818get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001819{
1820 pair<_Tp*, ptrdiff_t> __r(0, 0);
1821 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1822 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1823 / sizeof(_Tp);
1824 if (__n > __m)
1825 __n = __m;
1826 while (__n > 0)
1827 {
1828 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1829 if (__r.first)
1830 {
1831 __r.second = __n;
1832 break;
1833 }
1834 __n /= 2;
1835 }
1836 return __r;
1837}
1838
1839template <class _Tp>
1840inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001841void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001842
1843template <class _Tp>
1844struct auto_ptr_ref
1845{
1846 _Tp* __ptr_;
1847};
1848
1849template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001850class _LIBCPP_VISIBLE auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001851{
1852private:
1853 _Tp* __ptr_;
1854public:
1855 typedef _Tp element_type;
1856
1857 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1858 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1859 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1860 : __ptr_(__p.release()) {}
1861 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1862 {reset(__p.release()); return *this;}
1863 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1864 {reset(__p.release()); return *this;}
1865 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1866 {reset(__p.__ptr_); return *this;}
1867 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1868
1869 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1870 {return *__ptr_;}
1871 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1872 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1873 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1874 {
1875 _Tp* __t = __ptr_;
1876 __ptr_ = 0;
1877 return __t;
1878 }
1879 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1880 {
1881 if (__ptr_ != __p)
1882 delete __ptr_;
1883 __ptr_ = __p;
1884 }
1885
1886 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1887 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1888 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1889 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1890 {return auto_ptr<_Up>(release());}
1891};
1892
1893template <>
Howard Hinnant82894812010-09-22 16:48:34 +00001894class _LIBCPP_VISIBLE auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001895{
1896public:
1897 typedef void element_type;
1898};
1899
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001900template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1901 typename remove_cv<_T2>::type>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00001902 bool = is_empty<_T1>::value
1903#if __has_feature(is_final)
1904 && !__is_final(_T1)
1905#endif
1906 ,
1907 bool = is_empty<_T2>::value
1908#if __has_feature(is_final)
1909 && !__is_final(_T2)
1910#endif
1911 >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001912struct __libcpp_compressed_pair_switch;
1913
1914template <class _T1, class _T2, bool IsSame>
1915struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1916
1917template <class _T1, class _T2, bool IsSame>
1918struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1919
1920template <class _T1, class _T2, bool IsSame>
1921struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1922
1923template <class _T1, class _T2>
1924struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
1925
1926template <class _T1, class _T2>
1927struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
1928
1929template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
1930class __libcpp_compressed_pair_imp;
1931
1932template <class _T1, class _T2>
1933class __libcpp_compressed_pair_imp<_T1, _T2, 0>
1934{
1935private:
1936 _T1 __first_;
1937 _T2 __second_;
1938public:
1939 typedef _T1 _T1_param;
1940 typedef _T2 _T2_param;
1941
1942 typedef typename remove_reference<_T1>::type& _T1_reference;
1943 typedef typename remove_reference<_T2>::type& _T2_reference;
1944
1945 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1946 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1947
1948 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant57199402012-01-02 17:56:02 +00001949 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001950 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00001951 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001952 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001953 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001954 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001955
Howard Hinnant61aa6012011-07-01 19:24:36 +00001956#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
1957
1958 _LIBCPP_INLINE_VISIBILITY
1959 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
1960 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
1961 is_nothrow_copy_constructible<_T2>::value)
1962 : __first_(__p.first()),
1963 __second_(__p.second()) {}
1964
1965 _LIBCPP_INLINE_VISIBILITY
1966 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
1967 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
1968 is_nothrow_copy_assignable<_T2>::value)
1969 {
1970 __first_ = __p.first();
1971 __second_ = __p.second();
1972 return *this;
1973 }
1974
Howard Hinnant73d21a42010-09-04 23:28:19 +00001975#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00001976
1977 _LIBCPP_INLINE_VISIBILITY
1978 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00001979 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
1980 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant61aa6012011-07-01 19:24:36 +00001981 : __first_(_VSTD::forward<_T1>(__p.first())),
1982 __second_(_VSTD::forward<_T2>(__p.second())) {}
1983
1984 _LIBCPP_INLINE_VISIBILITY
1985 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
1986 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
1987 is_nothrow_move_assignable<_T2>::value)
1988 {
1989 __first_ = _VSTD::forward<_T1>(__p.first());
1990 __second_ = _VSTD::forward<_T2>(__p.second());
1991 return *this;
1992 }
1993
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00001994#ifndef _LIBCPP_HAS_NO_VARIADICS
1995
1996 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
1997 _LIBCPP_INLINE_VISIBILITY
1998 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
1999 tuple<_Args1...> __first_args,
2000 tuple<_Args2...> __second_args,
2001 __tuple_indices<_I1...>,
2002 __tuple_indices<_I2...>)
2003 : __first_(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
2004 __second_(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
2005 {}
2006
2007#endif // _LIBCPP_HAS_NO_VARIADICS
2008
Howard Hinnant73d21a42010-09-04 23:28:19 +00002009#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002010
Howard Hinnant61aa6012011-07-01 19:24:36 +00002011#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2012
Howard Hinnant1694d232011-05-28 14:41:13 +00002013 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2014 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002015
Howard Hinnant1694d232011-05-28 14:41:13 +00002016 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2017 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002018
2019 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002020 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2021 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002022 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002023 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002024 swap(__first_, __x.__first_);
2025 swap(__second_, __x.__second_);
2026 }
2027};
2028
2029template <class _T1, class _T2>
2030class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2031 : private _T1
2032{
2033private:
2034 _T2 __second_;
2035public:
2036 typedef _T1 _T1_param;
2037 typedef _T2 _T2_param;
2038
2039 typedef _T1& _T1_reference;
2040 typedef typename remove_reference<_T2>::type& _T2_reference;
2041
2042 typedef const _T1& _T1_const_reference;
2043 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2044
2045 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002046 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002047 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002048 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002049 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002050 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002051 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002052
Howard Hinnant61aa6012011-07-01 19:24:36 +00002053#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2054
2055 _LIBCPP_INLINE_VISIBILITY
2056 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2057 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2058 is_nothrow_copy_constructible<_T2>::value)
2059 : _T1(__p.first()), __second_(__p.second()) {}
2060
2061 _LIBCPP_INLINE_VISIBILITY
2062 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2063 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2064 is_nothrow_copy_assignable<_T2>::value)
2065 {
2066 _T1::operator=(__p.first());
2067 __second_ = __p.second();
2068 return *this;
2069 }
2070
Howard Hinnant73d21a42010-09-04 23:28:19 +00002071#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002072
2073 _LIBCPP_INLINE_VISIBILITY
2074 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002075 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2076 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002077 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002078
2079 _LIBCPP_INLINE_VISIBILITY
2080 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2081 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2082 is_nothrow_move_assignable<_T2>::value)
2083 {
2084 _T1::operator=(_VSTD::move(__p.first()));
2085 __second_ = _VSTD::forward<_T2>(__p.second());
2086 return *this;
2087 }
2088
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002089#ifndef _LIBCPP_HAS_NO_VARIADICS
2090
2091 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2092 _LIBCPP_INLINE_VISIBILITY
2093 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2094 tuple<_Args1...> __first_args,
2095 tuple<_Args2...> __second_args,
2096 __tuple_indices<_I1...>,
2097 __tuple_indices<_I2...>)
2098 : _T1(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
2099 __second_(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
2100 {}
2101
2102#endif // _LIBCPP_HAS_NO_VARIADICS
2103
Howard Hinnant73d21a42010-09-04 23:28:19 +00002104#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002105
Howard Hinnant61aa6012011-07-01 19:24:36 +00002106#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2107
Howard Hinnant1694d232011-05-28 14:41:13 +00002108 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2109 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002110
Howard Hinnant1694d232011-05-28 14:41:13 +00002111 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2112 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002113
2114 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002115 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2116 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002117 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002118 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002119 swap(__second_, __x.__second_);
2120 }
2121};
2122
2123template <class _T1, class _T2>
2124class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2125 : private _T2
2126{
2127private:
2128 _T1 __first_;
2129public:
2130 typedef _T1 _T1_param;
2131 typedef _T2 _T2_param;
2132
2133 typedef typename remove_reference<_T1>::type& _T1_reference;
2134 typedef _T2& _T2_reference;
2135
2136 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2137 typedef const _T2& _T2_const_reference;
2138
2139 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2140 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002141 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002142 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002143 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002144 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00002145 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2146 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002147 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002148
Howard Hinnant61aa6012011-07-01 19:24:36 +00002149#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2150
2151 _LIBCPP_INLINE_VISIBILITY
2152 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2153 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2154 is_nothrow_copy_constructible<_T2>::value)
2155 : _T2(__p.second()), __first_(__p.first()) {}
2156
2157 _LIBCPP_INLINE_VISIBILITY
2158 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2159 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2160 is_nothrow_copy_assignable<_T2>::value)
2161 {
2162 _T2::operator=(__p.second());
2163 __first_ = __p.first();
2164 return *this;
2165 }
2166
Howard Hinnant73d21a42010-09-04 23:28:19 +00002167#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002168
2169 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002170 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002171 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2172 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002173 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002174
2175 _LIBCPP_INLINE_VISIBILITY
2176 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2177 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2178 is_nothrow_move_assignable<_T2>::value)
2179 {
2180 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2181 __first_ = _VSTD::move(__p.first());
2182 return *this;
2183 }
2184
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002185#ifndef _LIBCPP_HAS_NO_VARIADICS
2186
2187 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2188 _LIBCPP_INLINE_VISIBILITY
2189 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2190 tuple<_Args1...> __first_args,
2191 tuple<_Args2...> __second_args,
2192 __tuple_indices<_I1...>,
2193 __tuple_indices<_I2...>)
2194 : _T2(_VSTD::forward<_Args2>(get<_I2>(__second_args))...),
2195 __first_(_VSTD::forward<_Args1>(get<_I1>(__first_args))...)
2196
2197 {}
2198
2199#endif // _LIBCPP_HAS_NO_VARIADICS
2200
Howard Hinnant73d21a42010-09-04 23:28:19 +00002201#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002202
Howard Hinnant61aa6012011-07-01 19:24:36 +00002203#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2204
Howard Hinnant1694d232011-05-28 14:41:13 +00002205 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2206 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002207
Howard Hinnant1694d232011-05-28 14:41:13 +00002208 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2209 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002210
2211 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002212 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2213 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002214 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002215 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002216 swap(__first_, __x.__first_);
2217 }
2218};
2219
2220template <class _T1, class _T2>
2221class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2222 : private _T1,
2223 private _T2
2224{
2225public:
2226 typedef _T1 _T1_param;
2227 typedef _T2 _T2_param;
2228
2229 typedef _T1& _T1_reference;
2230 typedef _T2& _T2_reference;
2231
2232 typedef const _T1& _T1_const_reference;
2233 typedef const _T2& _T2_const_reference;
2234
2235 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2236 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002237 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002238 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002239 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002240 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002241 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002242
Howard Hinnant61aa6012011-07-01 19:24:36 +00002243#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2244
2245 _LIBCPP_INLINE_VISIBILITY
2246 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2247 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2248 is_nothrow_copy_constructible<_T2>::value)
2249 : _T1(__p.first()), _T2(__p.second()) {}
2250
2251 _LIBCPP_INLINE_VISIBILITY
2252 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2253 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2254 is_nothrow_copy_assignable<_T2>::value)
2255 {
2256 _T1::operator=(__p.first());
2257 _T2::operator=(__p.second());
2258 return *this;
2259 }
2260
Howard Hinnant73d21a42010-09-04 23:28:19 +00002261#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002262
2263 _LIBCPP_INLINE_VISIBILITY
2264 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002265 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2266 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002267 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002268
2269 _LIBCPP_INLINE_VISIBILITY
2270 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2271 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2272 is_nothrow_move_assignable<_T2>::value)
2273 {
2274 _T1::operator=(_VSTD::move(__p.first()));
2275 _T2::operator=(_VSTD::move(__p.second()));
2276 return *this;
2277 }
2278
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002279#ifndef _LIBCPP_HAS_NO_VARIADICS
2280
2281 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2282 _LIBCPP_INLINE_VISIBILITY
2283 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2284 tuple<_Args1...> __first_args,
2285 tuple<_Args2...> __second_args,
2286 __tuple_indices<_I1...>,
2287 __tuple_indices<_I2...>)
2288 : _T1(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
2289 _T2(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
2290 {}
2291
2292#endif // _LIBCPP_HAS_NO_VARIADICS
2293
Howard Hinnant73d21a42010-09-04 23:28:19 +00002294#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002295
Howard Hinnant61aa6012011-07-01 19:24:36 +00002296#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2297
Howard Hinnant1694d232011-05-28 14:41:13 +00002298 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2299 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002300
Howard Hinnant1694d232011-05-28 14:41:13 +00002301 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2302 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002303
Howard Hinnantec3773c2011-12-01 20:21:04 +00002304 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant1694d232011-05-28 14:41:13 +00002305 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2306 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002307 {
2308 }
2309};
2310
2311template <class _T1, class _T2>
2312class __compressed_pair
2313 : private __libcpp_compressed_pair_imp<_T1, _T2>
2314{
2315 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2316public:
2317 typedef typename base::_T1_param _T1_param;
2318 typedef typename base::_T2_param _T2_param;
2319
2320 typedef typename base::_T1_reference _T1_reference;
2321 typedef typename base::_T2_reference _T2_reference;
2322
2323 typedef typename base::_T1_const_reference _T1_const_reference;
2324 typedef typename base::_T2_const_reference _T2_const_reference;
2325
2326 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002327 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002328 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002329 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002330 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002331 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002332 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002333
Howard Hinnant61aa6012011-07-01 19:24:36 +00002334#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2335
2336 _LIBCPP_INLINE_VISIBILITY
2337 __compressed_pair(const __compressed_pair& __p)
2338 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2339 is_nothrow_copy_constructible<_T2>::value)
2340 : base(__p) {}
2341
2342 _LIBCPP_INLINE_VISIBILITY
2343 __compressed_pair& operator=(const __compressed_pair& __p)
2344 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2345 is_nothrow_copy_assignable<_T2>::value)
2346 {
2347 base::operator=(__p);
2348 return *this;
2349 }
2350
Howard Hinnant73d21a42010-09-04 23:28:19 +00002351#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002352 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002353 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002354 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2355 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002356 : base(_VSTD::move(__p)) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002357
2358 _LIBCPP_INLINE_VISIBILITY
2359 __compressed_pair& operator=(__compressed_pair&& __p)
2360 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2361 is_nothrow_move_assignable<_T2>::value)
2362 {
2363 base::operator=(_VSTD::move(__p));
2364 return *this;
2365 }
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002366
2367#ifndef _LIBCPP_HAS_NO_VARIADICS
2368
2369 template <class... _Args1, class... _Args2>
2370 _LIBCPP_INLINE_VISIBILITY
2371 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2372 tuple<_Args2...> __second_args)
Howard Hinnant57199402012-01-02 17:56:02 +00002373 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002374 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2375 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2376 {}
2377
2378#endif // _LIBCPP_HAS_NO_VARIADICS
2379
Howard Hinnant73d21a42010-09-04 23:28:19 +00002380#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002381
Howard Hinnant61aa6012011-07-01 19:24:36 +00002382#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2383
Howard Hinnant1694d232011-05-28 14:41:13 +00002384 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2385 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002386
Howard Hinnant1694d232011-05-28 14:41:13 +00002387 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2388 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002389
Howard Hinnant1694d232011-05-28 14:41:13 +00002390 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2391 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2392 __is_nothrow_swappable<_T1>::value)
2393 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002394};
2395
2396template <class _T1, class _T2>
2397inline _LIBCPP_INLINE_VISIBILITY
2398void
2399swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002400 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2401 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002402 {__x.swap(__y);}
2403
Howard Hinnant57199402012-01-02 17:56:02 +00002404// __same_or_less_cv_qualified
2405
2406template <class _Ptr1, class _Ptr2,
2407 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2408 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2409 >::value
2410 >
2411struct __same_or_less_cv_qualified_imp
2412 : is_convertible<_Ptr1, _Ptr2> {};
2413
2414template <class _Ptr1, class _Ptr2>
2415struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2416 : false_type {};
2417
2418template <class _Ptr1, class _Ptr2, bool = is_scalar<_Ptr1>::value &&
2419 !is_pointer<_Ptr1>::value>
2420struct __same_or_less_cv_qualified
2421 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2422
2423template <class _Ptr1, class _Ptr2>
2424struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, true>
2425 : false_type {};
2426
2427// default_delete
2428
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002429template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002430struct _LIBCPP_VISIBLE default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002431{
Howard Hinnant1694d232011-05-28 14:41:13 +00002432 _LIBCPP_INLINE_VISIBILITY default_delete() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002433 template <class _Up>
2434 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002435 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2436 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002437 {
2438 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2439 delete __ptr;
2440 }
2441};
2442
2443template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002444struct _LIBCPP_VISIBLE default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002445{
Howard Hinnant8e843502011-12-18 21:19:44 +00002446public:
2447 _LIBCPP_INLINE_VISIBILITY default_delete() _NOEXCEPT {}
2448 template <class _Up>
2449 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant57199402012-01-02 17:56:02 +00002450 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant8e843502011-12-18 21:19:44 +00002451 template <class _Up>
2452 _LIBCPP_INLINE_VISIBILITY
2453 void operator() (_Up* __ptr,
Howard Hinnant57199402012-01-02 17:56:02 +00002454 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002455 {
2456 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2457 delete [] __ptr;
2458 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002459};
2460
2461template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant82894812010-09-22 16:48:34 +00002462class _LIBCPP_VISIBLE unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002463{
2464public:
2465 typedef _Tp element_type;
2466 typedef _Dp deleter_type;
2467 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2468private:
2469 __compressed_pair<pointer, deleter_type> __ptr_;
2470
Howard Hinnant57199402012-01-02 17:56:02 +00002471#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002472 unique_ptr(unique_ptr&);
2473 template <class _Up, class _Ep>
2474 unique_ptr(unique_ptr<_Up, _Ep>&);
2475 unique_ptr& operator=(unique_ptr&);
2476 template <class _Up, class _Ep>
2477 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002478#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002479
2480 struct __nat {int __for_bool_;};
2481
2482 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2483 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2484public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002485 _LIBCPP_INLINE_VISIBILITY unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002486 : __ptr_(pointer())
2487 {
2488 static_assert(!is_pointer<deleter_type>::value,
2489 "unique_ptr constructed with null function pointer deleter");
2490 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002491 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002492 : __ptr_(pointer())
2493 {
2494 static_assert(!is_pointer<deleter_type>::value,
2495 "unique_ptr constructed with null function pointer deleter");
2496 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002497 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002498 : __ptr_(_VSTD::move(__p))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002499 {
2500 static_assert(!is_pointer<deleter_type>::value,
2501 "unique_ptr constructed with null function pointer deleter");
2502 }
2503
Howard Hinnant73d21a42010-09-04 23:28:19 +00002504#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002505 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2506 is_reference<deleter_type>::value,
2507 deleter_type,
2508 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002509 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002510 : __ptr_(__p, __d) {}
2511
2512 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002513 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002514 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002515 {
2516 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2517 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002518 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002519 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002520 template <class _Up, class _Ep>
2521 _LIBCPP_INLINE_VISIBILITY
2522 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2523 typename enable_if
2524 <
2525 !is_array<_Up>::value &&
2526 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2527 is_convertible<_Ep, deleter_type>::value &&
2528 (
2529 !is_reference<deleter_type>::value ||
2530 is_same<deleter_type, _Ep>::value
2531 ),
2532 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002533 >::type = __nat()) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002534 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002535
2536 template <class _Up>
2537 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2538 typename enable_if<
2539 is_convertible<_Up*, _Tp*>::value &&
2540 is_same<_Dp, default_delete<_Tp> >::value,
2541 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002542 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002543 : __ptr_(__p.release())
2544 {
2545 }
2546
Howard Hinnant1694d232011-05-28 14:41:13 +00002547 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002548 {
2549 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002550 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002551 return *this;
2552 }
2553
2554 template <class _Up, class _Ep>
2555 _LIBCPP_INLINE_VISIBILITY
2556 typename enable_if
2557 <
Howard Hinnant57199402012-01-02 17:56:02 +00002558 !is_array<_Up>::value &&
2559 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2560 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002561 unique_ptr&
2562 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002563 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002564 {
2565 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002566 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002567 return *this;
2568 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002569#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002570
2571 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2572 {
2573 return __rv<unique_ptr>(*this);
2574 }
2575
2576 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002577 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002578
2579 template <class _Up, class _Ep>
2580 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2581 {
2582 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002583 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002584 return *this;
2585 }
2586
2587 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002588 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002589
2590 template <class _Up>
2591 _LIBCPP_INLINE_VISIBILITY
2592 typename enable_if<
2593 is_convertible<_Up*, _Tp*>::value &&
2594 is_same<_Dp, default_delete<_Tp> >::value,
2595 unique_ptr&
2596 >::type
2597 operator=(auto_ptr<_Up> __p)
2598 {reset(__p.release()); return *this;}
2599
Howard Hinnant73d21a42010-09-04 23:28:19 +00002600#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002601 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2602
Howard Hinnant1694d232011-05-28 14:41:13 +00002603 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002604 {
2605 reset();
2606 return *this;
2607 }
2608
2609 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2610 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002611 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2612 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2613 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2614 {return __ptr_.second();}
2615 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2616 {return __ptr_.second();}
2617 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const
2618 _NOEXCEPT
2619 {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002620
Howard Hinnant1694d232011-05-28 14:41:13 +00002621 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002622 {
2623 pointer __t = __ptr_.first();
2624 __ptr_.first() = pointer();
2625 return __t;
2626 }
2627
Howard Hinnant1694d232011-05-28 14:41:13 +00002628 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002629 {
2630 pointer __tmp = __ptr_.first();
2631 __ptr_.first() = __p;
2632 if (__tmp)
2633 __ptr_.second()(__tmp);
2634 }
2635
Howard Hinnant1694d232011-05-28 14:41:13 +00002636 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2637 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002638};
2639
2640template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002641class _LIBCPP_VISIBLE unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002642{
2643public:
2644 typedef _Tp element_type;
2645 typedef _Dp deleter_type;
2646 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2647private:
2648 __compressed_pair<pointer, deleter_type> __ptr_;
2649
Howard Hinnant57199402012-01-02 17:56:02 +00002650#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002651 unique_ptr(unique_ptr&);
2652 template <class _Up>
2653 unique_ptr(unique_ptr<_Up>&);
2654 unique_ptr& operator=(unique_ptr&);
2655 template <class _Up>
2656 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002657#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002658
2659 struct __nat {int __for_bool_;};
2660
2661 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2662 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2663public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002664 _LIBCPP_INLINE_VISIBILITY unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002665 : __ptr_(pointer())
2666 {
2667 static_assert(!is_pointer<deleter_type>::value,
2668 "unique_ptr constructed with null function pointer deleter");
2669 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002670 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002671 : __ptr_(pointer())
2672 {
2673 static_assert(!is_pointer<deleter_type>::value,
2674 "unique_ptr constructed with null function pointer deleter");
2675 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002676#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002677 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002678 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002679 >
Howard Hinnant99968442011-11-29 18:15:50 +00002680 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002681 : __ptr_(__p)
2682 {
2683 static_assert(!is_pointer<deleter_type>::value,
2684 "unique_ptr constructed with null function pointer deleter");
2685 }
2686
Howard Hinnant99968442011-11-29 18:15:50 +00002687 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002688 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002689 >
Howard Hinnant99968442011-11-29 18:15:50 +00002690 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002691 is_reference<deleter_type>::value,
2692 deleter_type,
2693 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002694 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002695 : __ptr_(__p, __d) {}
2696
2697 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2698 is_reference<deleter_type>::value,
2699 deleter_type,
2700 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002701 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002702 : __ptr_(pointer(), __d) {}
2703
Howard Hinnant99968442011-11-29 18:15:50 +00002704 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002705 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002706 >
Howard Hinnant99968442011-11-29 18:15:50 +00002707 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002708 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002709 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002710 {
2711 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2712 }
2713
2714 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002715 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002716 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002717 {
2718 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2719 }
2720
Howard Hinnant1694d232011-05-28 14:41:13 +00002721 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002722 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002723
Howard Hinnant1694d232011-05-28 14:41:13 +00002724 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002725 {
2726 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002727 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002728 return *this;
2729 }
Howard Hinnant8e843502011-12-18 21:19:44 +00002730
2731 template <class _Up, class _Ep>
2732 _LIBCPP_INLINE_VISIBILITY
2733 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2734 typename enable_if
2735 <
2736 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002737 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant8e843502011-12-18 21:19:44 +00002738 && is_convertible<_Ep, deleter_type>::value &&
2739 (
2740 !is_reference<deleter_type>::value ||
2741 is_same<deleter_type, _Ep>::value
2742 ),
2743 __nat
2744 >::type = __nat()
2745 ) _NOEXCEPT
2746 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2747
2748
2749 template <class _Up, class _Ep>
2750 _LIBCPP_INLINE_VISIBILITY
2751 typename enable_if
2752 <
2753 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002754 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2755 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant8e843502011-12-18 21:19:44 +00002756 unique_ptr&
2757 >::type
2758 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2759 {
2760 reset(__u.release());
2761 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2762 return *this;
2763 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002764#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002765
2766 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2767 : __ptr_(__p)
2768 {
2769 static_assert(!is_pointer<deleter_type>::value,
2770 "unique_ptr constructed with null function pointer deleter");
2771 }
2772
2773 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002774 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002775
2776 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002777 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002778
2779 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2780 {
2781 return __rv<unique_ptr>(*this);
2782 }
2783
2784 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002785 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002786
2787 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2788 {
2789 reset(__u->release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002790 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002791 return *this;
2792 }
2793
Howard Hinnant73d21a42010-09-04 23:28:19 +00002794#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002795 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2796
Howard Hinnant1694d232011-05-28 14:41:13 +00002797 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002798 {
2799 reset();
2800 return *this;
2801 }
2802
2803 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2804 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002805 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2806 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2807 {return __ptr_.second();}
2808 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2809 {return __ptr_.second();}
2810 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const _NOEXCEPT
2811 {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002812
Howard Hinnant1694d232011-05-28 14:41:13 +00002813 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002814 {
2815 pointer __t = __ptr_.first();
2816 __ptr_.first() = pointer();
2817 return __t;
2818 }
2819
Howard Hinnant73d21a42010-09-04 23:28:19 +00002820#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002821 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002822 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002823 >
Howard Hinnant99968442011-11-29 18:15:50 +00002824 _LIBCPP_INLINE_VISIBILITY void reset(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002825 {
2826 pointer __tmp = __ptr_.first();
2827 __ptr_.first() = __p;
2828 if (__tmp)
2829 __ptr_.second()(__tmp);
2830 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002831 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002832 {
2833 pointer __tmp = __ptr_.first();
2834 __ptr_.first() = nullptr;
2835 if (__tmp)
2836 __ptr_.second()(__tmp);
2837 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002838 _LIBCPP_INLINE_VISIBILITY void reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002839 {
2840 pointer __tmp = __ptr_.first();
2841 __ptr_.first() = nullptr;
2842 if (__tmp)
2843 __ptr_.second()(__tmp);
2844 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002845#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002846 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2847 {
2848 pointer __tmp = __ptr_.first();
2849 __ptr_.first() = __p;
2850 if (__tmp)
2851 __ptr_.second()(__tmp);
2852 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002853#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002854
2855 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2856private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002857
Howard Hinnant73d21a42010-09-04 23:28:19 +00002858#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002859 template <class _Up>
2860 explicit unique_ptr(_Up);
2861 template <class _Up>
2862 unique_ptr(_Up __u,
2863 typename conditional<
2864 is_reference<deleter_type>::value,
2865 deleter_type,
2866 typename add_lvalue_reference<const deleter_type>::type>::type,
2867 typename enable_if
2868 <
2869 is_convertible<_Up, pointer>::value,
2870 __nat
2871 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002872#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002873};
2874
2875template <class _Tp, class _Dp>
2876inline _LIBCPP_INLINE_VISIBILITY
2877void
Howard Hinnant1694d232011-05-28 14:41:13 +00002878swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002879
2880template <class _T1, class _D1, class _T2, class _D2>
2881inline _LIBCPP_INLINE_VISIBILITY
2882bool
2883operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2884
2885template <class _T1, class _D1, class _T2, class _D2>
2886inline _LIBCPP_INLINE_VISIBILITY
2887bool
2888operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2889
2890template <class _T1, class _D1, class _T2, class _D2>
2891inline _LIBCPP_INLINE_VISIBILITY
2892bool
2893operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() < __y.get();}
2894
2895template <class _T1, class _D1, class _T2, class _D2>
2896inline _LIBCPP_INLINE_VISIBILITY
2897bool
2898operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2899
2900template <class _T1, class _D1, class _T2, class _D2>
2901inline _LIBCPP_INLINE_VISIBILITY
2902bool
2903operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2904
2905template <class _T1, class _D1, class _T2, class _D2>
2906inline _LIBCPP_INLINE_VISIBILITY
2907bool
2908operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2909
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00002910template <class _Tp> struct hash;
Howard Hinnant21aefc32010-06-03 16:42:57 +00002911
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002912// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
2913// is 64 bits. This is because cityhash64 uses 64bit x 64bit
2914// multiplication, which can be very slow on 32-bit systems.
Howard Hinnant40c13d32011-12-05 00:08:45 +00002915template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002916struct __murmur2_or_cityhash;
Howard Hinnant40c13d32011-12-05 00:08:45 +00002917
2918template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002919struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnant40c13d32011-12-05 00:08:45 +00002920{
2921 _Size operator()(const void* __key, _Size __len);
2922};
2923
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002924// murmur2
Howard Hinnant40c13d32011-12-05 00:08:45 +00002925template <class _Size>
2926_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002927__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00002928{
2929 const _Size __m = 0x5bd1e995;
2930 const _Size __r = 24;
2931 _Size __h = __len;
2932 const unsigned char* __data = static_cast<const unsigned char*>(__key);
2933 for (; __len >= 4; __data += 4, __len -= 4)
2934 {
2935 _Size __k = *(const _Size*)__data;
2936 __k *= __m;
2937 __k ^= __k >> __r;
2938 __k *= __m;
2939 __h *= __m;
2940 __h ^= __k;
2941 }
2942 switch (__len)
2943 {
2944 case 3:
2945 __h ^= __data[2] << 16;
2946 case 2:
2947 __h ^= __data[1] << 8;
2948 case 1:
2949 __h ^= __data[0];
2950 __h *= __m;
2951 }
2952 __h ^= __h >> 13;
2953 __h *= __m;
2954 __h ^= __h >> 15;
2955 return __h;
2956}
2957
2958template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002959struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnant40c13d32011-12-05 00:08:45 +00002960{
2961 _Size operator()(const void* __key, _Size __len);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00002962
2963 private:
2964 // Some primes between 2^63 and 2^64.
2965 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
2966 static const _Size __k1 = 0xb492b66fbe98f273ULL;
2967 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
2968 static const _Size __k3 = 0xc949d7c7509e6557ULL;
2969
2970 static _Size __rotate(_Size __val, int __shift) {
2971 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
2972 }
2973
2974 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
2975 return (__val >> __shift) | (__val << (64 - __shift));
2976 }
2977
2978 static _Size __shift_mix(_Size __val) {
2979 return __val ^ (__val >> 47);
2980 }
2981
2982 static _Size __hash_len_16(_Size __u, _Size __v) {
2983 const _Size __mul = 0x9ddfea08eb382d69ULL;
2984 _Size __a = (__u ^ __v) * __mul;
2985 __a ^= (__a >> 47);
2986 _Size __b = (__v ^ __a) * __mul;
2987 __b ^= (__b >> 47);
2988 __b *= __mul;
2989 return __b;
2990 }
2991
2992 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
2993 if (__len > 8) {
2994 const _Size __a = *(const _Size*)__s;
2995 const _Size __b = *(const _Size*)(__s + __len - 8);
2996 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
2997 }
2998 if (__len >= 4) {
2999 const uint32_t __a = *(const uint32_t*)(__s);
3000 const uint32_t __b = *(const uint32_t*)(__s + __len - 4);
3001 return __hash_len_16(__len + (__a << 3), __b);
3002 }
3003 if (__len > 0) {
3004 const unsigned char __a = __s[0];
3005 const unsigned char __b = __s[__len >> 1];
3006 const unsigned char __c = __s[__len - 1];
3007 const uint32_t __y = static_cast<uint32_t>(__a) +
3008 (static_cast<uint32_t>(__b) << 8);
3009 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3010 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3011 }
3012 return __k2;
3013 }
3014
3015 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
3016 const _Size __a = *(const _Size*)(__s) * __k1;
3017 const _Size __b = *(const _Size*)(__s + 8);
3018 const _Size __c = *(const _Size*)(__s + __len - 8) * __k2;
3019 const _Size __d = *(const _Size*)(__s + __len - 16) * __k0;
3020 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3021 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3022 }
3023
3024 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3025 // Callers do best to use "random-looking" values for a and b.
3026 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3027 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3028 __a += __w;
3029 __b = __rotate(__b + __a + __z, 21);
3030 const _Size __c = __a;
3031 __a += __x;
3032 __a += __y;
3033 __b += __rotate(__a, 44);
3034 return pair<_Size, _Size>(__a + __z, __b + __c);
3035 }
3036
3037 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3038 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3039 const char* __s, _Size __a, _Size __b) {
3040 return __weak_hash_len_32_with_seeds(*(const _Size*)(__s),
3041 *(const _Size*)(__s + 8),
3042 *(const _Size*)(__s + 16),
3043 *(const _Size*)(__s + 24),
3044 __a,
3045 __b);
3046 }
3047
3048 // Return an 8-byte hash for 33 to 64 bytes.
3049 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
3050 _Size __z = *(const _Size*)(__s + 24);
3051 _Size __a = *(const _Size*)(__s) +
3052 (__len + *(const _Size*)(__s + __len - 16)) * __k0;
3053 _Size __b = __rotate(__a + __z, 52);
3054 _Size __c = __rotate(__a, 37);
3055 __a += *(const _Size*)(__s + 8);
3056 __c += __rotate(__a, 7);
3057 __a += *(const _Size*)(__s + 16);
3058 _Size __vf = __a + __z;
3059 _Size __vs = __b + __rotate(__a, 31) + __c;
3060 __a = *(const _Size*)(__s + 16) + *(const _Size*)(__s + __len - 32);
3061 __z += *(const _Size*)(__s + __len - 8);
3062 __b = __rotate(__a + __z, 52);
3063 __c = __rotate(__a, 37);
3064 __a += *(const _Size*)(__s + __len - 24);
3065 __c += __rotate(__a, 7);
3066 __a += *(const _Size*)(__s + __len - 16);
3067 _Size __wf = __a + __z;
3068 _Size __ws = __b + __rotate(__a, 31) + __c;
3069 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3070 return __shift_mix(__r * __k0 + __vs) * __k2;
3071 }
Howard Hinnant40c13d32011-12-05 00:08:45 +00003072};
3073
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003074// cityhash64
Howard Hinnant40c13d32011-12-05 00:08:45 +00003075template <class _Size>
3076_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003077__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003078{
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003079 const char* __s = static_cast<const char*>(__key);
3080 if (__len <= 32) {
3081 if (__len <= 16) {
3082 return __hash_len_0_to_16(__s, __len);
3083 } else {
3084 return __hash_len_17_to_32(__s, __len);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003085 }
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003086 } else if (__len <= 64) {
3087 return __hash_len_33_to_64(__s, __len);
3088 }
3089
3090 // For strings over 64 bytes we hash the end first, and then as we
3091 // loop we keep 56 bytes of state: v, w, x, y, and z.
3092 _Size __x = *(const _Size*)(__s + __len - 40);
3093 _Size __y = *(const _Size*)(__s + __len - 16) +
3094 *(const _Size*)(__s + __len - 56);
3095 _Size __z = __hash_len_16(*(const _Size*)(__s + __len - 48) + __len,
3096 *(const _Size*)(__s + __len - 24));
3097 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3098 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
3099 __x = __x * __k1 + *(const _Size*)(__s);
3100
3101 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3102 __len = (__len - 1) & ~static_cast<_Size>(63);
3103 do {
3104 __x = __rotate(__x + __y + __v.first + *(const _Size*)(__s + 8), 37) * __k1;
3105 __y = __rotate(__y + __v.second + *(const _Size*)(__s + 48), 42) * __k1;
3106 __x ^= __w.second;
3107 __y += __v.first + *(const _Size*)(__s + 40);
3108 __z = __rotate(__z + __w.first, 33) * __k1;
3109 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3110 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
3111 __y + *(const _Size*)(__s + 16));
3112 std::swap(__z, __x);
3113 __s += 64;
3114 __len -= 64;
3115 } while (__len != 0);
3116 return __hash_len_16(
3117 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3118 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003119}
3120
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003121template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3122struct __scalar_hash;
3123
3124template <class _Tp>
3125struct __scalar_hash<_Tp, 0>
3126 : public unary_function<_Tp, size_t>
Howard Hinnant21aefc32010-06-03 16:42:57 +00003127{
Howard Hinnant82894812010-09-22 16:48:34 +00003128 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003129 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003130 {
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003131 union
3132 {
3133 _Tp __t;
3134 size_t __a;
3135 } __u;
3136 __u.__a = 0;
3137 __u.__t = __v;
3138 return __u.__a;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003139 }
3140};
3141
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003142template <class _Tp>
3143struct __scalar_hash<_Tp, 1>
3144 : public unary_function<_Tp, size_t>
3145{
3146 _LIBCPP_INLINE_VISIBILITY
3147 size_t operator()(_Tp __v) const _NOEXCEPT
3148 {
3149 union
3150 {
3151 _Tp __t;
3152 size_t __a;
3153 } __u;
3154 __u.__t = __v;
3155 return __u.__a;
3156 }
3157};
3158
3159template <class _Tp>
3160struct __scalar_hash<_Tp, 2>
3161 : public unary_function<_Tp, size_t>
3162{
3163 _LIBCPP_INLINE_VISIBILITY
3164 size_t operator()(_Tp __v) const _NOEXCEPT
3165 {
3166 union
3167 {
3168 _Tp __t;
3169 struct
3170 {
3171 size_t __a;
3172 size_t __b;
3173 };
3174 } __u;
3175 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003176 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003177 }
3178};
3179
3180template <class _Tp>
3181struct __scalar_hash<_Tp, 3>
3182 : public unary_function<_Tp, size_t>
3183{
3184 _LIBCPP_INLINE_VISIBILITY
3185 size_t operator()(_Tp __v) const _NOEXCEPT
3186 {
3187 union
3188 {
3189 _Tp __t;
3190 struct
3191 {
3192 size_t __a;
3193 size_t __b;
3194 size_t __c;
3195 };
3196 } __u;
3197 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003198 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003199 }
3200};
3201
3202template <class _Tp>
3203struct __scalar_hash<_Tp, 4>
3204 : public unary_function<_Tp, size_t>
3205{
3206 _LIBCPP_INLINE_VISIBILITY
3207 size_t operator()(_Tp __v) const _NOEXCEPT
3208 {
3209 union
3210 {
3211 _Tp __t;
3212 struct
3213 {
3214 size_t __a;
3215 size_t __b;
3216 size_t __c;
3217 size_t __d;
3218 };
3219 } __u;
3220 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003221 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003222 }
3223};
3224
3225template<class _Tp>
3226struct _LIBCPP_VISIBLE hash<_Tp*>
3227 : public __scalar_hash<_Tp*>
3228{
3229};
3230
Howard Hinnant21aefc32010-06-03 16:42:57 +00003231template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00003232struct _LIBCPP_VISIBLE hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003233{
3234 typedef unique_ptr<_Tp, _Dp> argument_type;
3235 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003237 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003238 {
3239 typedef typename argument_type::pointer pointer;
3240 return hash<pointer>()(__ptr.get());
3241 }
3242};
3243
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003244struct __destruct_n
3245{
3246private:
3247 size_t size;
3248
3249 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003250 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003251 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3252
3253 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003254 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003255 {}
3256
Howard Hinnant1694d232011-05-28 14:41:13 +00003257 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003258 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003259 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003260 {}
3261
Howard Hinnant1694d232011-05-28 14:41:13 +00003262 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003263 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003264 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003265 {}
3266public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003267 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3268 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003269
3270 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003271 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003272 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003273
3274 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003275 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003276 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003277
3278 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003279 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003280 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003281};
3282
3283template <class _Alloc>
3284class __allocator_destructor
3285{
3286 typedef allocator_traits<_Alloc> __alloc_traits;
3287public:
3288 typedef typename __alloc_traits::pointer pointer;
3289 typedef typename __alloc_traits::size_type size_type;
3290private:
3291 _Alloc& __alloc_;
3292 size_type __s_;
3293public:
3294 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00003295 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003296 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003297 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003298 void operator()(pointer __p) _NOEXCEPT
3299 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003300};
3301
3302template <class _InputIterator, class _ForwardIterator>
3303_ForwardIterator
3304uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3305{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003306 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003307#ifndef _LIBCPP_NO_EXCEPTIONS
3308 _ForwardIterator __s = __r;
3309 try
3310 {
3311#endif
3312 for (; __f != __l; ++__f, ++__r)
3313 ::new(&*__r) value_type(*__f);
3314#ifndef _LIBCPP_NO_EXCEPTIONS
3315 }
3316 catch (...)
3317 {
3318 for (; __s != __r; ++__s)
3319 __s->~value_type();
3320 throw;
3321 }
3322#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003323 return __r;
3324}
3325
3326template <class _InputIterator, class _Size, class _ForwardIterator>
3327_ForwardIterator
3328uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3329{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003330 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003331#ifndef _LIBCPP_NO_EXCEPTIONS
3332 _ForwardIterator __s = __r;
3333 try
3334 {
3335#endif
3336 for (; __n > 0; ++__f, ++__r, --__n)
3337 ::new(&*__r) value_type(*__f);
3338#ifndef _LIBCPP_NO_EXCEPTIONS
3339 }
3340 catch (...)
3341 {
3342 for (; __s != __r; ++__s)
3343 __s->~value_type();
3344 throw;
3345 }
3346#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003347 return __r;
3348}
3349
3350template <class _ForwardIterator, class _Tp>
3351void
3352uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3353{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003354 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003355#ifndef _LIBCPP_NO_EXCEPTIONS
3356 _ForwardIterator __s = __f;
3357 try
3358 {
3359#endif
3360 for (; __f != __l; ++__f)
3361 ::new(&*__f) value_type(__x);
3362#ifndef _LIBCPP_NO_EXCEPTIONS
3363 }
3364 catch (...)
3365 {
3366 for (; __s != __f; ++__s)
3367 __s->~value_type();
3368 throw;
3369 }
3370#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003371}
3372
3373template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003374_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003375uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3376{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003377 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003378#ifndef _LIBCPP_NO_EXCEPTIONS
3379 _ForwardIterator __s = __f;
3380 try
3381 {
3382#endif
3383 for (; __n > 0; ++__f, --__n)
3384 ::new(&*__f) value_type(__x);
3385#ifndef _LIBCPP_NO_EXCEPTIONS
3386 }
3387 catch (...)
3388 {
3389 for (; __s != __f; ++__s)
3390 __s->~value_type();
3391 throw;
3392 }
3393#endif
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003394 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003395}
3396
Howard Hinnant82894812010-09-22 16:48:34 +00003397class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003398 : public std::exception
3399{
3400public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003401 virtual ~bad_weak_ptr() _NOEXCEPT;
3402 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003403};
3404
3405template<class _Tp> class weak_ptr;
3406
3407class __shared_count
3408{
3409 __shared_count(const __shared_count&);
3410 __shared_count& operator=(const __shared_count&);
3411
3412protected:
3413 long __shared_owners_;
3414 virtual ~__shared_count();
3415private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003416 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003417
3418public:
Howard Hinnant82894812010-09-22 16:48:34 +00003419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003420 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003421 : __shared_owners_(__refs) {}
3422
Howard Hinnant1694d232011-05-28 14:41:13 +00003423 void __add_shared() _NOEXCEPT;
3424 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003426 long use_count() const _NOEXCEPT {return __shared_owners_ + 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003427};
3428
3429class __shared_weak_count
3430 : private __shared_count
3431{
3432 long __shared_weak_owners_;
3433
3434public:
Howard Hinnant82894812010-09-22 16:48:34 +00003435 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003436 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003437 : __shared_count(__refs),
3438 __shared_weak_owners_(__refs) {}
3439protected:
3440 virtual ~__shared_weak_count();
3441
3442public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003443 void __add_shared() _NOEXCEPT;
3444 void __add_weak() _NOEXCEPT;
3445 void __release_shared() _NOEXCEPT;
3446 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003447 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003448 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3449 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003450
Howard Hinnant1694d232011-05-28 14:41:13 +00003451 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003452private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003453 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003454};
3455
3456template <class _Tp, class _Dp, class _Alloc>
3457class __shared_ptr_pointer
3458 : public __shared_weak_count
3459{
3460 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3461public:
Howard Hinnant82894812010-09-22 16:48:34 +00003462 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003463 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003464 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003465
Howard Hinnantd4444702010-08-11 17:04:31 +00003466#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00003467 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00003468#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003469
3470private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003471 virtual void __on_zero_shared() _NOEXCEPT;
3472 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003473};
3474
Howard Hinnantd4444702010-08-11 17:04:31 +00003475#ifndef _LIBCPP_NO_RTTI
3476
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003477template <class _Tp, class _Dp, class _Alloc>
3478const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00003479__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003480{
3481 return __t == typeid(_Dp) ? &__data_.first().second() : 0;
3482}
3483
Howard Hinnant324bb032010-08-22 00:02:43 +00003484#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003485
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003486template <class _Tp, class _Dp, class _Alloc>
3487void
Howard Hinnant1694d232011-05-28 14:41:13 +00003488__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003489{
3490 __data_.first().second()(__data_.first().first());
3491 __data_.first().second().~_Dp();
3492}
3493
3494template <class _Tp, class _Dp, class _Alloc>
3495void
Howard Hinnant1694d232011-05-28 14:41:13 +00003496__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003497{
3498 typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
3499 __data_.second().~_Alloc();
3500 __a.deallocate(this, 1);
3501}
3502
3503template <class _Tp, class _Alloc>
3504class __shared_ptr_emplace
3505 : public __shared_weak_count
3506{
3507 __compressed_pair<_Alloc, _Tp> __data_;
3508public:
3509#ifndef _LIBCPP_HAS_NO_VARIADICS
3510
Howard Hinnant82894812010-09-22 16:48:34 +00003511 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003512 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003513 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003514
3515 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00003516 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003517 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00003518 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3519 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003520
3521#else // _LIBCPP_HAS_NO_VARIADICS
3522
Howard Hinnant82894812010-09-22 16:48:34 +00003523 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003524 __shared_ptr_emplace(_Alloc __a)
3525 : __data_(__a) {}
3526
3527 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00003528 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003529 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3530 : __data_(__a, _Tp(__a0)) {}
3531
3532 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00003533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003534 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3535 : __data_(__a, _Tp(__a0, __a1)) {}
3536
3537 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00003538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003539 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3540 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3541
3542#endif // _LIBCPP_HAS_NO_VARIADICS
3543
3544private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003545 virtual void __on_zero_shared() _NOEXCEPT;
3546 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003547public:
Howard Hinnant82894812010-09-22 16:48:34 +00003548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003549 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003550};
3551
3552template <class _Tp, class _Alloc>
3553void
Howard Hinnant1694d232011-05-28 14:41:13 +00003554__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003555{
3556 __data_.second().~_Tp();
3557}
3558
3559template <class _Tp, class _Alloc>
3560void
Howard Hinnant1694d232011-05-28 14:41:13 +00003561__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003562{
3563 typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
3564 __data_.first().~_Alloc();
3565 __a.deallocate(this, 1);
3566}
3567
3568template<class _Tp> class enable_shared_from_this;
3569
3570template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003571class _LIBCPP_VISIBLE shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003572{
Howard Hinnant324bb032010-08-22 00:02:43 +00003573public:
3574 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003575private:
3576 element_type* __ptr_;
3577 __shared_weak_count* __cntrl_;
3578
3579 struct __nat {int __for_bool_;};
3580public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003581 shared_ptr() _NOEXCEPT;
3582 shared_ptr(nullptr_t) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003583 template<class _Yp,
3584 class = typename enable_if
3585 <
3586 is_convertible<_Yp*, element_type*>::value
3587 >::type
3588 >
3589 explicit shared_ptr(_Yp* __p);
3590 template<class _Yp, class _Dp,
3591 class = typename enable_if
3592 <
3593 is_convertible<_Yp*, element_type*>::value
3594 >::type
3595 >
3596 shared_ptr(_Yp* __p, _Dp __d);
3597 template<class _Yp, class _Dp, class _Alloc,
3598 class = typename enable_if
3599 <
3600 is_convertible<_Yp*, element_type*>::value
3601 >::type
3602 >
3603 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003604 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3605 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant1694d232011-05-28 14:41:13 +00003606 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3607 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003608 template<class _Yp>
3609 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003610 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3611 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003612#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003613 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003614 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003615 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3616 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003617#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003618 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003619 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003620#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003621 template<class _Yp,
3622 class = typename enable_if
3623 <
3624 is_convertible<_Yp*, element_type*>::value
3625 >::type
3626 >
3627 shared_ptr(auto_ptr<_Yp>&& __r);
Howard Hinnant324bb032010-08-22 00:02:43 +00003628#else
Howard Hinnant57199402012-01-02 17:56:02 +00003629 template<class _Yp,
3630 class = typename enable_if
3631 <
3632 is_convertible<_Yp*, element_type*>::value
3633 >::type
3634 >
3635 shared_ptr(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003636#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003637#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003638 template <class _Yp, class _Dp,
3639 class = typename enable_if
3640 <
3641 !is_array<_Yp>::value &&
3642 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3643 >::type
3644 >
3645 shared_ptr(unique_ptr<_Yp, _Dp>&&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003646 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant57199402012-01-02 17:56:02 +00003647 template <class _Yp, class _Dp,
3648 class = typename enable_if
3649 <
3650 !is_array<_Yp>::value &&
3651 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3652 >::type
3653 >
3654 shared_ptr(unique_ptr<_Yp, _Dp>&&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003655 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003656#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003657 template <class _Yp, class _Dp,
3658 class = typename enable_if
3659 <
3660 !is_array<_Yp>::value &&
3661 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3662 >::type
3663 > shared_ptr(unique_ptr<_Yp, _Dp>,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003664 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant57199402012-01-02 17:56:02 +00003665 template <class _Yp, class _Dp,
3666 class = typename enable_if
3667 <
3668 !is_array<_Yp>::value &&
3669 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3670 >::type
3671 >
3672 shared_ptr(unique_ptr<_Yp, _Dp>,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003673 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003674#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003675
3676 ~shared_ptr();
3677
Howard Hinnant1694d232011-05-28 14:41:13 +00003678 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003679 template<class _Yp>
3680 typename enable_if
3681 <
3682 is_convertible<_Yp*, element_type*>::value,
3683 shared_ptr&
3684 >::type
3685 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003686#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003687 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003688 template<class _Yp>
3689 typename enable_if
3690 <
3691 is_convertible<_Yp*, element_type*>::value,
3692 shared_ptr<_Tp>&
3693 >::type
3694 operator=(shared_ptr<_Yp>&& __r);
3695 template<class _Yp>
3696 typename enable_if
3697 <
3698 !is_array<_Yp>::value &&
3699 is_convertible<_Yp*, element_type*>::value,
3700 shared_ptr&
3701 >::type
3702 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003703#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003704 template<class _Yp>
3705 typename enable_if
3706 <
3707 !is_array<_Yp>::value &&
3708 is_convertible<_Yp*, element_type*>::value,
3709 shared_ptr&
3710 >::type
3711 operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003712#endif
Howard Hinnant57199402012-01-02 17:56:02 +00003713 template <class _Yp, class _Dp>
3714 typename enable_if
3715 <
3716 !is_array<_Yp>::value &&
3717 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3718 shared_ptr&
3719 >::type
Howard Hinnant73d21a42010-09-04 23:28:19 +00003720#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003721 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003722#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003723 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003724#endif
3725
Howard Hinnant1694d232011-05-28 14:41:13 +00003726 void swap(shared_ptr& __r) _NOEXCEPT;
3727 void reset() _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003728 template<class _Yp>
3729 typename enable_if
3730 <
3731 is_convertible<_Yp*, element_type*>::value,
3732 void
3733 >::type
3734 reset(_Yp* __p);
3735 template<class _Yp, class _Dp>
3736 typename enable_if
3737 <
3738 is_convertible<_Yp*, element_type*>::value,
3739 void
3740 >::type
3741 reset(_Yp* __p, _Dp __d);
3742 template<class _Yp, class _Dp, class _Alloc>
3743 typename enable_if
3744 <
3745 is_convertible<_Yp*, element_type*>::value,
3746 void
3747 >::type
3748 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003749
Howard Hinnant82894812010-09-22 16:48:34 +00003750 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003751 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003752 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003753 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3754 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003756 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003758 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003759 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003760 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00003761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003762 /*explicit*/ operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant99968442011-11-29 18:15:50 +00003763 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00003764 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003765 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003766 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant99968442011-11-29 18:15:50 +00003767 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00003768 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003769 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003770 {return __cntrl_ < __p.__cntrl_;}
3771
Howard Hinnantd4444702010-08-11 17:04:31 +00003772#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003773 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00003774 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003775 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003776 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00003777#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003778
3779#ifndef _LIBCPP_HAS_NO_VARIADICS
3780
3781 template<class ..._Args>
3782 static
3783 shared_ptr<_Tp>
3784 make_shared(_Args&& ...__args);
3785
3786 template<class _Alloc, class ..._Args>
3787 static
3788 shared_ptr<_Tp>
3789 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3790
3791#else // _LIBCPP_HAS_NO_VARIADICS
3792
3793 static shared_ptr<_Tp> make_shared();
3794
3795 template<class _A0>
3796 static shared_ptr<_Tp> make_shared(_A0&);
3797
3798 template<class _A0, class _A1>
3799 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3800
3801 template<class _A0, class _A1, class _A2>
3802 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3803
3804 template<class _Alloc>
3805 static shared_ptr<_Tp>
3806 allocate_shared(const _Alloc& __a);
3807
3808 template<class _Alloc, class _A0>
3809 static shared_ptr<_Tp>
3810 allocate_shared(const _Alloc& __a, _A0& __a0);
3811
3812 template<class _Alloc, class _A0, class _A1>
3813 static shared_ptr<_Tp>
3814 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3815
3816 template<class _Alloc, class _A0, class _A1, class _A2>
3817 static shared_ptr<_Tp>
3818 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3819
3820#endif // _LIBCPP_HAS_NO_VARIADICS
3821
3822private:
3823
3824 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00003825 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003826 void
Howard Hinnant1694d232011-05-28 14:41:13 +00003827 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003828 {
3829 if (__e)
3830 __e->__weak_this_ = *this;
3831 }
3832
Howard Hinnant82894812010-09-22 16:48:34 +00003833 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003834 void __enable_weak_this(const void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003835
Howard Hinnant82894812010-09-22 16:48:34 +00003836 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
3837 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003838};
3839
3840template<class _Tp>
3841inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003842shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003843 : __ptr_(0),
3844 __cntrl_(0)
3845{
3846}
3847
3848template<class _Tp>
3849inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003850shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003851 : __ptr_(0),
3852 __cntrl_(0)
3853{
3854}
3855
3856template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00003857template<class _Yp, class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003858shared_ptr<_Tp>::shared_ptr(_Yp* __p)
3859 : __ptr_(__p)
3860{
3861 unique_ptr<_Yp> __hold(__p);
3862 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3863 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
3864 __hold.release();
3865 __enable_weak_this(__p);
3866}
3867
3868template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00003869template<class _Yp, class _Dp, class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003870shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
3871 : __ptr_(__p)
3872{
3873#ifndef _LIBCPP_NO_EXCEPTIONS
3874 try
3875 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003876#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003877 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3878 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
3879 __enable_weak_this(__p);
3880#ifndef _LIBCPP_NO_EXCEPTIONS
3881 }
3882 catch (...)
3883 {
3884 __d(__p);
3885 throw;
3886 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003887#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003888}
3889
3890template<class _Tp>
3891template<class _Dp>
3892shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3893 : __ptr_(0)
3894{
3895#ifndef _LIBCPP_NO_EXCEPTIONS
3896 try
3897 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003898#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003899 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
3900 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
3901#ifndef _LIBCPP_NO_EXCEPTIONS
3902 }
3903 catch (...)
3904 {
3905 __d(__p);
3906 throw;
3907 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003908#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003909}
3910
3911template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00003912template<class _Yp, class _Dp, class _Alloc, class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003913shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
3914 : __ptr_(__p)
3915{
3916#ifndef _LIBCPP_NO_EXCEPTIONS
3917 try
3918 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003919#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003920 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
3921 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3922 typedef __allocator_destructor<_A2> _D2;
3923 _A2 __a2(__a);
3924 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3925 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
3926 __cntrl_ = __hold2.release();
3927 __enable_weak_this(__p);
3928#ifndef _LIBCPP_NO_EXCEPTIONS
3929 }
3930 catch (...)
3931 {
3932 __d(__p);
3933 throw;
3934 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003935#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003936}
3937
3938template<class _Tp>
3939template<class _Dp, class _Alloc>
3940shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
3941 : __ptr_(0)
3942{
3943#ifndef _LIBCPP_NO_EXCEPTIONS
3944 try
3945 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003946#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003947 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
3948 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3949 typedef __allocator_destructor<_A2> _D2;
3950 _A2 __a2(__a);
3951 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3952 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
3953 __cntrl_ = __hold2.release();
3954#ifndef _LIBCPP_NO_EXCEPTIONS
3955 }
3956 catch (...)
3957 {
3958 __d(__p);
3959 throw;
3960 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003961#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003962}
3963
3964template<class _Tp>
3965template<class _Yp>
3966inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003967shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003968 : __ptr_(__p),
3969 __cntrl_(__r.__cntrl_)
3970{
3971 if (__cntrl_)
3972 __cntrl_->__add_shared();
3973}
3974
3975template<class _Tp>
3976inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003977shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003978 : __ptr_(__r.__ptr_),
3979 __cntrl_(__r.__cntrl_)
3980{
3981 if (__cntrl_)
3982 __cntrl_->__add_shared();
3983}
3984
3985template<class _Tp>
3986template<class _Yp>
3987inline _LIBCPP_INLINE_VISIBILITY
3988shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
3989 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003990 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003991 : __ptr_(__r.__ptr_),
3992 __cntrl_(__r.__cntrl_)
3993{
3994 if (__cntrl_)
3995 __cntrl_->__add_shared();
3996}
3997
Howard Hinnant73d21a42010-09-04 23:28:19 +00003998#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003999
4000template<class _Tp>
4001inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004002shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004003 : __ptr_(__r.__ptr_),
4004 __cntrl_(__r.__cntrl_)
4005{
4006 __r.__ptr_ = 0;
4007 __r.__cntrl_ = 0;
4008}
4009
4010template<class _Tp>
4011template<class _Yp>
4012inline _LIBCPP_INLINE_VISIBILITY
4013shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4014 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004015 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004016 : __ptr_(__r.__ptr_),
4017 __cntrl_(__r.__cntrl_)
4018{
4019 __r.__ptr_ = 0;
4020 __r.__cntrl_ = 0;
4021}
4022
Howard Hinnant73d21a42010-09-04 23:28:19 +00004023#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004024
4025template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004026template<class _Yp, class>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004027#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004028shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
4029#else
Howard Hinnant92172b82010-08-21 21:14:53 +00004030shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004031#endif
4032 : __ptr_(__r.get())
4033{
4034 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4035 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
4036 __enable_weak_this(__r.get());
4037 __r.release();
4038}
4039
4040template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004041template <class _Yp, class _Dp, class>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004042#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004043shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4044#else
4045shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4046#endif
4047 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
4048 : __ptr_(__r.get())
4049{
4050 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4051 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
4052 __enable_weak_this(__r.get());
4053 __r.release();
4054}
4055
4056template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004057template <class _Yp, class _Dp, class>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004058#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004059shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4060#else
4061shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4062#endif
4063 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
4064 : __ptr_(__r.get())
4065{
4066 typedef __shared_ptr_pointer<_Yp*,
4067 reference_wrapper<typename remove_reference<_Dp>::type>,
4068 allocator<_Yp> > _CntrlBlk;
4069 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
4070 __enable_weak_this(__r.get());
4071 __r.release();
4072}
4073
4074#ifndef _LIBCPP_HAS_NO_VARIADICS
4075
4076template<class _Tp>
4077template<class ..._Args>
4078shared_ptr<_Tp>
4079shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4080{
4081 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4082 typedef allocator<_CntrlBlk> _A2;
4083 typedef __allocator_destructor<_A2> _D2;
4084 _A2 __a2;
4085 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004086 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004087 shared_ptr<_Tp> __r;
4088 __r.__ptr_ = __hold2.get()->get();
4089 __r.__cntrl_ = __hold2.release();
4090 __r.__enable_weak_this(__r.__ptr_);
4091 return __r;
4092}
4093
4094template<class _Tp>
4095template<class _Alloc, class ..._Args>
4096shared_ptr<_Tp>
4097shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4098{
4099 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4100 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
4101 typedef __allocator_destructor<_A2> _D2;
4102 _A2 __a2(__a);
4103 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004104 ::new(__hold2.get()) _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004105 shared_ptr<_Tp> __r;
4106 __r.__ptr_ = __hold2.get()->get();
4107 __r.__cntrl_ = __hold2.release();
4108 __r.__enable_weak_this(__r.__ptr_);
4109 return __r;
4110}
4111
4112#else // _LIBCPP_HAS_NO_VARIADICS
4113
4114template<class _Tp>
4115shared_ptr<_Tp>
4116shared_ptr<_Tp>::make_shared()
4117{
4118 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4119 typedef allocator<_CntrlBlk> _Alloc2;
4120 typedef __allocator_destructor<_Alloc2> _D2;
4121 _Alloc2 __alloc2;
4122 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4123 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4124 shared_ptr<_Tp> __r;
4125 __r.__ptr_ = __hold2.get()->get();
4126 __r.__cntrl_ = __hold2.release();
4127 __r.__enable_weak_this(__r.__ptr_);
4128 return __r;
4129}
4130
4131template<class _Tp>
4132template<class _A0>
4133shared_ptr<_Tp>
4134shared_ptr<_Tp>::make_shared(_A0& __a0)
4135{
4136 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4137 typedef allocator<_CntrlBlk> _Alloc2;
4138 typedef __allocator_destructor<_Alloc2> _D2;
4139 _Alloc2 __alloc2;
4140 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4141 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4142 shared_ptr<_Tp> __r;
4143 __r.__ptr_ = __hold2.get()->get();
4144 __r.__cntrl_ = __hold2.release();
4145 __r.__enable_weak_this(__r.__ptr_);
4146 return __r;
4147}
4148
4149template<class _Tp>
4150template<class _A0, class _A1>
4151shared_ptr<_Tp>
4152shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4153{
4154 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4155 typedef allocator<_CntrlBlk> _Alloc2;
4156 typedef __allocator_destructor<_Alloc2> _D2;
4157 _Alloc2 __alloc2;
4158 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4159 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4160 shared_ptr<_Tp> __r;
4161 __r.__ptr_ = __hold2.get()->get();
4162 __r.__cntrl_ = __hold2.release();
4163 __r.__enable_weak_this(__r.__ptr_);
4164 return __r;
4165}
4166
4167template<class _Tp>
4168template<class _A0, class _A1, class _A2>
4169shared_ptr<_Tp>
4170shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4171{
4172 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4173 typedef allocator<_CntrlBlk> _Alloc2;
4174 typedef __allocator_destructor<_Alloc2> _D2;
4175 _Alloc2 __alloc2;
4176 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4177 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4178 shared_ptr<_Tp> __r;
4179 __r.__ptr_ = __hold2.get()->get();
4180 __r.__cntrl_ = __hold2.release();
4181 __r.__enable_weak_this(__r.__ptr_);
4182 return __r;
4183}
4184
4185template<class _Tp>
4186template<class _Alloc>
4187shared_ptr<_Tp>
4188shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4189{
4190 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4191 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4192 typedef __allocator_destructor<_Alloc2> _D2;
4193 _Alloc2 __alloc2(__a);
4194 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4195 ::new(__hold2.get()) _CntrlBlk(__a);
4196 shared_ptr<_Tp> __r;
4197 __r.__ptr_ = __hold2.get()->get();
4198 __r.__cntrl_ = __hold2.release();
4199 __r.__enable_weak_this(__r.__ptr_);
4200 return __r;
4201}
4202
4203template<class _Tp>
4204template<class _Alloc, class _A0>
4205shared_ptr<_Tp>
4206shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4207{
4208 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4209 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4210 typedef __allocator_destructor<_Alloc2> _D2;
4211 _Alloc2 __alloc2(__a);
4212 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4213 ::new(__hold2.get()) _CntrlBlk(__a, __a0);
4214 shared_ptr<_Tp> __r;
4215 __r.__ptr_ = __hold2.get()->get();
4216 __r.__cntrl_ = __hold2.release();
4217 __r.__enable_weak_this(__r.__ptr_);
4218 return __r;
4219}
4220
4221template<class _Tp>
4222template<class _Alloc, class _A0, class _A1>
4223shared_ptr<_Tp>
4224shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4225{
4226 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4227 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4228 typedef __allocator_destructor<_Alloc2> _D2;
4229 _Alloc2 __alloc2(__a);
4230 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4231 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
4232 shared_ptr<_Tp> __r;
4233 __r.__ptr_ = __hold2.get()->get();
4234 __r.__cntrl_ = __hold2.release();
4235 __r.__enable_weak_this(__r.__ptr_);
4236 return __r;
4237}
4238
4239template<class _Tp>
4240template<class _Alloc, class _A0, class _A1, class _A2>
4241shared_ptr<_Tp>
4242shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4243{
4244 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4245 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4246 typedef __allocator_destructor<_Alloc2> _D2;
4247 _Alloc2 __alloc2(__a);
4248 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4249 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
4250 shared_ptr<_Tp> __r;
4251 __r.__ptr_ = __hold2.get()->get();
4252 __r.__cntrl_ = __hold2.release();
4253 __r.__enable_weak_this(__r.__ptr_);
4254 return __r;
4255}
4256
4257#endif // _LIBCPP_HAS_NO_VARIADICS
4258
4259template<class _Tp>
4260shared_ptr<_Tp>::~shared_ptr()
4261{
4262 if (__cntrl_)
4263 __cntrl_->__release_shared();
4264}
4265
4266template<class _Tp>
4267inline _LIBCPP_INLINE_VISIBILITY
4268shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004269shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004270{
4271 shared_ptr(__r).swap(*this);
4272 return *this;
4273}
4274
4275template<class _Tp>
4276template<class _Yp>
4277inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004278typename enable_if
4279<
4280 is_convertible<_Yp*, _Tp*>::value,
4281 shared_ptr<_Tp>&
4282>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004283shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004284{
4285 shared_ptr(__r).swap(*this);
4286 return *this;
4287}
4288
Howard Hinnant73d21a42010-09-04 23:28:19 +00004289#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004290
4291template<class _Tp>
4292inline _LIBCPP_INLINE_VISIBILITY
4293shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004294shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004295{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004296 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004297 return *this;
4298}
4299
4300template<class _Tp>
4301template<class _Yp>
4302inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004303typename enable_if
4304<
4305 is_convertible<_Yp*, _Tp*>::value,
4306 shared_ptr<_Tp>&
4307>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004308shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4309{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004310 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004311 return *this;
4312}
4313
4314template<class _Tp>
4315template<class _Yp>
4316inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004317typename enable_if
4318<
4319 !is_array<_Yp>::value &&
4320 is_convertible<_Yp*, _Tp*>::value,
4321 shared_ptr<_Tp>&
4322>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004323shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4324{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004325 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004326 return *this;
4327}
4328
4329template<class _Tp>
4330template <class _Yp, class _Dp>
4331inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004332typename enable_if
4333<
4334 !is_array<_Yp>::value &&
4335 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4336 shared_ptr<_Tp>&
4337>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004338shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4339{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004340 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004341 return *this;
4342}
4343
Howard Hinnant73d21a42010-09-04 23:28:19 +00004344#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004345
4346template<class _Tp>
4347template<class _Yp>
4348inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004349typename enable_if
4350<
4351 !is_array<_Yp>::value &&
4352 is_convertible<_Yp*, _Tp*>::value,
4353 shared_ptr<_Tp>&
4354>::type
Howard Hinnant324bb032010-08-22 00:02:43 +00004355shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004356{
4357 shared_ptr(__r).swap(*this);
4358 return *this;
4359}
4360
4361template<class _Tp>
4362template <class _Yp, class _Dp>
4363inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004364typename enable_if
4365<
4366 !is_array<_Yp>::value &&
4367 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4368 shared_ptr<_Tp>&
4369>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004370shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4371{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004372 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004373 return *this;
4374}
4375
Howard Hinnant73d21a42010-09-04 23:28:19 +00004376#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004377
4378template<class _Tp>
4379inline _LIBCPP_INLINE_VISIBILITY
4380void
Howard Hinnant1694d232011-05-28 14:41:13 +00004381shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004382{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004383 _VSTD::swap(__ptr_, __r.__ptr_);
4384 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004385}
4386
4387template<class _Tp>
4388inline _LIBCPP_INLINE_VISIBILITY
4389void
Howard Hinnant1694d232011-05-28 14:41:13 +00004390shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004391{
4392 shared_ptr().swap(*this);
4393}
4394
4395template<class _Tp>
4396template<class _Yp>
4397inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004398typename enable_if
4399<
4400 is_convertible<_Yp*, _Tp*>::value,
4401 void
4402>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004403shared_ptr<_Tp>::reset(_Yp* __p)
4404{
4405 shared_ptr(__p).swap(*this);
4406}
4407
4408template<class _Tp>
4409template<class _Yp, class _Dp>
4410inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004411typename enable_if
4412<
4413 is_convertible<_Yp*, _Tp*>::value,
4414 void
4415>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004416shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4417{
4418 shared_ptr(__p, __d).swap(*this);
4419}
4420
4421template<class _Tp>
4422template<class _Yp, class _Dp, class _Alloc>
4423inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004424typename enable_if
4425<
4426 is_convertible<_Yp*, _Tp*>::value,
4427 void
4428>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004429shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4430{
4431 shared_ptr(__p, __d, __a).swap(*this);
4432}
4433
4434#ifndef _LIBCPP_HAS_NO_VARIADICS
4435
Howard Hinnant324bb032010-08-22 00:02:43 +00004436template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004437inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004438typename enable_if
4439<
4440 !is_array<_Tp>::value,
4441 shared_ptr<_Tp>
4442>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004443make_shared(_Args&& ...__args)
4444{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004445 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004446}
4447
Howard Hinnant324bb032010-08-22 00:02:43 +00004448template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004449inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004450typename enable_if
4451<
4452 !is_array<_Tp>::value,
4453 shared_ptr<_Tp>
4454>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004455allocate_shared(const _Alloc& __a, _Args&& ...__args)
4456{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004457 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004458}
4459
4460#else // _LIBCPP_HAS_NO_VARIADICS
4461
4462template<class _Tp>
4463inline _LIBCPP_INLINE_VISIBILITY
4464shared_ptr<_Tp>
4465make_shared()
4466{
4467 return shared_ptr<_Tp>::make_shared();
4468}
4469
4470template<class _Tp, class _A0>
4471inline _LIBCPP_INLINE_VISIBILITY
4472shared_ptr<_Tp>
4473make_shared(_A0& __a0)
4474{
4475 return shared_ptr<_Tp>::make_shared(__a0);
4476}
4477
4478template<class _Tp, class _A0, class _A1>
4479inline _LIBCPP_INLINE_VISIBILITY
4480shared_ptr<_Tp>
4481make_shared(_A0& __a0, _A1& __a1)
4482{
4483 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4484}
4485
Howard Hinnant324bb032010-08-22 00:02:43 +00004486template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004487inline _LIBCPP_INLINE_VISIBILITY
4488shared_ptr<_Tp>
4489make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4490{
4491 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4492}
4493
4494template<class _Tp, class _Alloc>
4495inline _LIBCPP_INLINE_VISIBILITY
4496shared_ptr<_Tp>
4497allocate_shared(const _Alloc& __a)
4498{
4499 return shared_ptr<_Tp>::allocate_shared(__a);
4500}
4501
4502template<class _Tp, class _Alloc, class _A0>
4503inline _LIBCPP_INLINE_VISIBILITY
4504shared_ptr<_Tp>
4505allocate_shared(const _Alloc& __a, _A0& __a0)
4506{
4507 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4508}
4509
4510template<class _Tp, class _Alloc, class _A0, class _A1>
4511inline _LIBCPP_INLINE_VISIBILITY
4512shared_ptr<_Tp>
4513allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4514{
4515 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4516}
4517
4518template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4519inline _LIBCPP_INLINE_VISIBILITY
4520shared_ptr<_Tp>
4521allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4522{
4523 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4524}
4525
4526#endif // _LIBCPP_HAS_NO_VARIADICS
4527
4528template<class _Tp, class _Up>
4529inline _LIBCPP_INLINE_VISIBILITY
4530bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004531operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004532{
4533 return __x.get() == __y.get();
4534}
4535
4536template<class _Tp, class _Up>
4537inline _LIBCPP_INLINE_VISIBILITY
4538bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004539operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004540{
4541 return !(__x == __y);
4542}
4543
4544template<class _Tp, class _Up>
4545inline _LIBCPP_INLINE_VISIBILITY
4546bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004547operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004548{
4549 return __x.get() < __y.get();
4550}
4551
4552template<class _Tp>
4553inline _LIBCPP_INLINE_VISIBILITY
4554void
Howard Hinnant1694d232011-05-28 14:41:13 +00004555swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004556{
4557 __x.swap(__y);
4558}
4559
4560template<class _Tp, class _Up>
4561inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004562typename enable_if
4563<
4564 !is_array<_Tp>::value && !is_array<_Up>::value,
4565 shared_ptr<_Tp>
4566>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004567static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004568{
4569 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4570}
4571
4572template<class _Tp, class _Up>
4573inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004574typename enable_if
4575<
4576 !is_array<_Tp>::value && !is_array<_Up>::value,
4577 shared_ptr<_Tp>
4578>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004579dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004580{
4581 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4582 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4583}
4584
4585template<class _Tp, class _Up>
Howard Hinnant57199402012-01-02 17:56:02 +00004586typename enable_if
4587<
4588 is_array<_Tp>::value == is_array<_Up>::value,
4589 shared_ptr<_Tp>
4590>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004591const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004592{
Howard Hinnant57199402012-01-02 17:56:02 +00004593 typedef typename remove_extent<_Tp>::type _RTp;
4594 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004595}
4596
Howard Hinnantd4444702010-08-11 17:04:31 +00004597#ifndef _LIBCPP_NO_RTTI
4598
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004599template<class _Dp, class _Tp>
4600inline _LIBCPP_INLINE_VISIBILITY
4601_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00004602get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004603{
4604 return __p.template __get_deleter<_Dp>();
4605}
4606
Howard Hinnant324bb032010-08-22 00:02:43 +00004607#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00004608
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004609template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004610class _LIBCPP_VISIBLE weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004611{
Howard Hinnant324bb032010-08-22 00:02:43 +00004612public:
4613 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004614private:
4615 element_type* __ptr_;
4616 __shared_weak_count* __cntrl_;
4617
Howard Hinnant324bb032010-08-22 00:02:43 +00004618public:
Howard Hinnant1694d232011-05-28 14:41:13 +00004619 weak_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004620 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00004621 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4622 _NOEXCEPT;
4623 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004624 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00004625 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4626 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004627
Howard Hinnant57199402012-01-02 17:56:02 +00004628#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4629 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
4630 template<class _Yp> weak_ptr(weak_ptr<_Yp>&& __r,
4631 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4632 _NOEXCEPT;
4633#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00004634 ~weak_ptr();
4635
Howard Hinnant1694d232011-05-28 14:41:13 +00004636 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00004637 template<class _Yp>
4638 typename enable_if
4639 <
4640 is_convertible<_Yp*, element_type*>::value,
4641 weak_ptr&
4642 >::type
4643 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4644
4645#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4646
4647 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4648 template<class _Yp>
4649 typename enable_if
4650 <
4651 is_convertible<_Yp*, element_type*>::value,
4652 weak_ptr&
4653 >::type
4654 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4655
4656#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4657
4658 template<class _Yp>
4659 typename enable_if
4660 <
4661 is_convertible<_Yp*, element_type*>::value,
4662 weak_ptr&
4663 >::type
4664 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004665
Howard Hinnant1694d232011-05-28 14:41:13 +00004666 void swap(weak_ptr& __r) _NOEXCEPT;
4667 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004668
Howard Hinnant82894812010-09-22 16:48:34 +00004669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004670 long use_count() const _NOEXCEPT
4671 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00004672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004673 bool expired() const _NOEXCEPT
4674 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4675 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00004676 template<class _Up>
4677 _LIBCPP_INLINE_VISIBILITY
4678 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004679 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004680 template<class _Up>
4681 _LIBCPP_INLINE_VISIBILITY
4682 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004683 {return __cntrl_ < __r.__cntrl_;}
4684
Howard Hinnant82894812010-09-22 16:48:34 +00004685 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
4686 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004687};
4688
4689template<class _Tp>
4690inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004691weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004692 : __ptr_(0),
4693 __cntrl_(0)
4694{
4695}
4696
4697template<class _Tp>
4698inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004699weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004700 : __ptr_(__r.__ptr_),
4701 __cntrl_(__r.__cntrl_)
4702{
4703 if (__cntrl_)
4704 __cntrl_->__add_weak();
4705}
4706
4707template<class _Tp>
4708template<class _Yp>
4709inline _LIBCPP_INLINE_VISIBILITY
4710weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00004711 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004712 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004713 : __ptr_(__r.__ptr_),
4714 __cntrl_(__r.__cntrl_)
4715{
4716 if (__cntrl_)
4717 __cntrl_->__add_weak();
4718}
4719
4720template<class _Tp>
4721template<class _Yp>
4722inline _LIBCPP_INLINE_VISIBILITY
4723weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00004724 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004725 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004726 : __ptr_(__r.__ptr_),
4727 __cntrl_(__r.__cntrl_)
4728{
4729 if (__cntrl_)
4730 __cntrl_->__add_weak();
4731}
4732
Howard Hinnant57199402012-01-02 17:56:02 +00004733#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4734
4735template<class _Tp>
4736inline _LIBCPP_INLINE_VISIBILITY
4737weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
4738 : __ptr_(__r.__ptr_),
4739 __cntrl_(__r.__cntrl_)
4740{
4741 __r.__ptr_ = 0;
4742 __r.__cntrl_ = 0;
4743}
4744
4745template<class _Tp>
4746template<class _Yp>
4747inline _LIBCPP_INLINE_VISIBILITY
4748weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
4749 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
4750 _NOEXCEPT
4751 : __ptr_(__r.__ptr_),
4752 __cntrl_(__r.__cntrl_)
4753{
4754 __r.__ptr_ = 0;
4755 __r.__cntrl_ = 0;
4756}
4757
4758#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4759
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004760template<class _Tp>
4761weak_ptr<_Tp>::~weak_ptr()
4762{
4763 if (__cntrl_)
4764 __cntrl_->__release_weak();
4765}
4766
4767template<class _Tp>
4768inline _LIBCPP_INLINE_VISIBILITY
4769weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004770weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004771{
4772 weak_ptr(__r).swap(*this);
4773 return *this;
4774}
4775
4776template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00004777template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004778inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004779typename enable_if
4780<
4781 is_convertible<_Yp*, _Tp*>::value,
4782 weak_ptr<_Tp>&
4783>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004784weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004785{
4786 weak_ptr(__r).swap(*this);
4787 return *this;
4788}
4789
Howard Hinnant57199402012-01-02 17:56:02 +00004790#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4791
4792template<class _Tp>
4793inline _LIBCPP_INLINE_VISIBILITY
4794weak_ptr<_Tp>&
4795weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
4796{
4797 weak_ptr(_VSTD::move(__r)).swap(*this);
4798 return *this;
4799}
4800
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004801template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00004802template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004803inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004804typename enable_if
4805<
4806 is_convertible<_Yp*, _Tp*>::value,
4807 weak_ptr<_Tp>&
4808>::type
4809weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
4810{
4811 weak_ptr(_VSTD::move(__r)).swap(*this);
4812 return *this;
4813}
4814
4815#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4816
4817template<class _Tp>
4818template<class _Yp>
4819inline _LIBCPP_INLINE_VISIBILITY
4820typename enable_if
4821<
4822 is_convertible<_Yp*, _Tp*>::value,
4823 weak_ptr<_Tp>&
4824>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004825weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004826{
4827 weak_ptr(__r).swap(*this);
4828 return *this;
4829}
4830
4831template<class _Tp>
4832inline _LIBCPP_INLINE_VISIBILITY
4833void
Howard Hinnant1694d232011-05-28 14:41:13 +00004834weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004835{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004836 _VSTD::swap(__ptr_, __r.__ptr_);
4837 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004838}
4839
4840template<class _Tp>
4841inline _LIBCPP_INLINE_VISIBILITY
4842void
Howard Hinnant1694d232011-05-28 14:41:13 +00004843swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004844{
4845 __x.swap(__y);
4846}
4847
4848template<class _Tp>
4849inline _LIBCPP_INLINE_VISIBILITY
4850void
Howard Hinnant1694d232011-05-28 14:41:13 +00004851weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004852{
4853 weak_ptr().swap(*this);
4854}
4855
4856template<class _Tp>
4857template<class _Yp>
4858shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
4859 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
4860 : __ptr_(__r.__ptr_),
4861 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4862{
4863 if (__cntrl_ == 0)
4864#ifndef _LIBCPP_NO_EXCEPTIONS
4865 throw bad_weak_ptr();
4866#else
4867 assert(!"bad_weak_ptr");
4868#endif
4869}
4870
4871template<class _Tp>
4872shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00004873weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004874{
4875 shared_ptr<_Tp> __r;
4876 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4877 if (__r.__cntrl_)
4878 __r.__ptr_ = __ptr_;
4879 return __r;
4880}
4881
Howard Hinnant324bb032010-08-22 00:02:43 +00004882template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004883
4884template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004885struct _LIBCPP_VISIBLE owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004886 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00004887{
4888 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00004889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004890 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
4891 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004892 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004893 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
4894 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004895 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004896 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
4897 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00004898};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004899
4900template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004901struct _LIBCPP_VISIBLE owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004902 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4903{
Howard Hinnant324bb032010-08-22 00:02:43 +00004904 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00004905 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004906 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
4907 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004908 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004909 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
4910 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004911 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004912 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
4913 {return __x.owner_before(__y);}
4914};
4915
4916template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004917class _LIBCPP_VISIBLE enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004918{
4919 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00004920protected:
Howard Hinnant82894812010-09-22 16:48:34 +00004921 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004922 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00004923 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004924 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00004925 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004926 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
4927 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00004928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004929 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00004930public:
Howard Hinnant82894812010-09-22 16:48:34 +00004931 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004932 shared_ptr<_Tp> shared_from_this()
4933 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00004934 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004935 shared_ptr<_Tp const> shared_from_this() const
4936 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004937
4938 template <class _Up> friend class shared_ptr;
4939};
4940
Howard Hinnant21aefc32010-06-03 16:42:57 +00004941template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004942struct _LIBCPP_VISIBLE hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00004943{
4944 typedef shared_ptr<_Tp> argument_type;
4945 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00004946 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004947 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00004948 {
4949 return hash<_Tp*>()(__ptr.get());
4950 }
4951};
4952
Howard Hinnant99968442011-11-29 18:15:50 +00004953template<class _CharT, class _Traits, class _Yp>
Howard Hinnant464aa5c2011-07-18 15:51:59 +00004954inline _LIBCPP_INLINE_VISIBILITY
4955basic_ostream<_CharT, _Traits>&
Howard Hinnant99968442011-11-29 18:15:50 +00004956operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnant464aa5c2011-07-18 15:51:59 +00004957
Howard Hinnant324bb032010-08-22 00:02:43 +00004958//enum class
Howard Hinnant82894812010-09-22 16:48:34 +00004959struct _LIBCPP_VISIBLE pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004960{
4961 enum _
4962 {
4963 relaxed,
4964 preferred,
4965 strict
4966 };
4967
4968 _ __v_;
4969
Howard Hinnant82894812010-09-22 16:48:34 +00004970 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004971 pointer_safety(_ __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00004972 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004973 operator int() const {return __v_;}
4974};
4975
4976void declare_reachable(void* __p);
4977void declare_no_pointers(char* __p, size_t __n);
4978void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnant1694d232011-05-28 14:41:13 +00004979pointer_safety get_pointer_safety() _NOEXCEPT;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00004980void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004981
4982template <class _Tp>
4983inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00004984_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004985undeclare_reachable(_Tp* __p)
4986{
4987 return static_cast<_Tp*>(__undeclare_reachable(__p));
4988}
4989
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00004990void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004991
4992_LIBCPP_END_NAMESPACE_STD
4993
4994#endif // _LIBCPP_MEMORY