blob: 1972d06f4d9feab559f002d9d972410548381e3e [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 Hinnantbc8d3f92010-05-11 19:42:16 +0000599#if defined(_LIBCPP_NO_EXCEPTIONS)
600 #include <cassert>
601#endif
602
603#pragma GCC system_header
604
605_LIBCPP_BEGIN_NAMESPACE_STD
606
607// allocator_arg_t
608
Howard Hinnant82894812010-09-22 16:48:34 +0000609struct _LIBCPP_VISIBLE allocator_arg_t { };
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000610
611extern const allocator_arg_t allocator_arg;
612
613// addressof
614
615template <class _Tp>
616inline _LIBCPP_INLINE_VISIBILITY
617_Tp*
Howard Hinnant1694d232011-05-28 14:41:13 +0000618addressof(_Tp& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000619{
620 return (_Tp*)&(char&)__x;
621}
622
Douglas Gregor35d2fcf2011-06-22 22:17:44 +0000623#if defined(_LIBCPP_HAS_OBJC_ARC) && !defined(_LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF)
624// Objective-C++ Automatic Reference Counting uses qualified pointers
625// that require special addressof() signatures. When
626// _LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF is defined, the compiler
627// itself is providing these definitions. Otherwise, we provide them.
628template <class _Tp>
629inline _LIBCPP_INLINE_VISIBILITY
630__strong _Tp*
631addressof(__strong _Tp& __x) _NOEXCEPT
632{
633 return &__x;
634}
635
636#ifdef _LIBCPP_HAS_OBJC_ARC_WEAK
637template <class _Tp>
638inline _LIBCPP_INLINE_VISIBILITY
639__weak _Tp*
640addressof(__weak _Tp& __x) _NOEXCEPT
641{
642 return &__x;
643}
644#endif
645
646template <class _Tp>
647inline _LIBCPP_INLINE_VISIBILITY
648__autoreleasing _Tp*
649addressof(__autoreleasing _Tp& __x) _NOEXCEPT
650{
651 return &__x;
652}
653
654template <class _Tp>
655inline _LIBCPP_INLINE_VISIBILITY
656__unsafe_unretained _Tp*
657addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT
658{
659 return &__x;
660}
661#endif
662
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000663template <class _Tp> class allocator;
664
665template <>
Howard Hinnant82894812010-09-22 16:48:34 +0000666class _LIBCPP_VISIBLE allocator<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000667{
668public:
669 typedef void* pointer;
670 typedef const void* const_pointer;
671 typedef void value_type;
672
673 template <class _Up> struct rebind {typedef allocator<_Up> other;};
674};
675
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000676// pointer_traits
677
678template <class _Tp>
679struct __has_element_type
680{
681private:
682 struct __two {char _; char __;};
683 template <class _Up> static __two __test(...);
684 template <class _Up> static char __test(typename _Up::element_type* = 0);
685public:
686 static const bool value = sizeof(__test<_Tp>(0)) == 1;
687};
688
689template <class _Ptr, bool = __has_element_type<_Ptr>::value>
690struct __pointer_traits_element_type;
691
692template <class _Ptr>
693struct __pointer_traits_element_type<_Ptr, true>
694{
695 typedef typename _Ptr::element_type type;
696};
697
698#ifndef _LIBCPP_HAS_NO_VARIADICS
699
700template <template <class, class...> class _Sp, class _Tp, class ..._Args>
701struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
702{
703 typedef typename _Sp<_Tp, _Args...>::element_type type;
704};
705
706template <template <class, class...> class _Sp, class _Tp, class ..._Args>
707struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
708{
709 typedef _Tp type;
710};
711
Howard Hinnant324bb032010-08-22 00:02:43 +0000712#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000713
714template <template <class> class _Sp, class _Tp>
715struct __pointer_traits_element_type<_Sp<_Tp>, true>
716{
717 typedef typename _Sp<_Tp>::element_type type;
718};
719
720template <template <class> class _Sp, class _Tp>
721struct __pointer_traits_element_type<_Sp<_Tp>, false>
722{
723 typedef _Tp type;
724};
725
726template <template <class, class> class _Sp, class _Tp, class _A0>
727struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
728{
729 typedef typename _Sp<_Tp, _A0>::element_type type;
730};
731
732template <template <class, class> class _Sp, class _Tp, class _A0>
733struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
734{
735 typedef _Tp type;
736};
737
738template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
739struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
740{
741 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
742};
743
744template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
745struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
746{
747 typedef _Tp type;
748};
749
750template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
751 class _A1, class _A2>
752struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
753{
754 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
755};
756
757template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
758 class _A1, class _A2>
759struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
760{
761 typedef _Tp type;
762};
763
Howard Hinnant324bb032010-08-22 00:02:43 +0000764#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000765
766template <class _Tp>
767struct __has_difference_type
768{
769private:
770 struct __two {char _; char __;};
771 template <class _Up> static __two __test(...);
772 template <class _Up> static char __test(typename _Up::difference_type* = 0);
773public:
774 static const bool value = sizeof(__test<_Tp>(0)) == 1;
775};
776
777template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
778struct __pointer_traits_difference_type
779{
780 typedef ptrdiff_t type;
781};
782
783template <class _Ptr>
784struct __pointer_traits_difference_type<_Ptr, true>
785{
786 typedef typename _Ptr::difference_type type;
787};
788
789template <class _Tp, class _Up>
790struct __has_rebind
791{
792private:
793 struct __two {char _; char __;};
794 template <class _Xp> static __two __test(...);
795 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
796public:
797 static const bool value = sizeof(__test<_Tp>(0)) == 1;
798};
799
800template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
801struct __pointer_traits_rebind
802{
803#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
804 typedef typename _Tp::template rebind<_Up> type;
805#else
806 typedef typename _Tp::template rebind<_Up>::other type;
807#endif
808};
809
810#ifndef _LIBCPP_HAS_NO_VARIADICS
811
812template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
813struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
814{
815#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
816 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
817#else
818 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
819#endif
820};
821
822template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
823struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
824{
825 typedef _Sp<_Up, _Args...> type;
826};
827
Howard Hinnant324bb032010-08-22 00:02:43 +0000828#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000829
830template <template <class> class _Sp, class _Tp, class _Up>
831struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
832{
833#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
834 typedef typename _Sp<_Tp>::template rebind<_Up> type;
835#else
836 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
837#endif
838};
839
840template <template <class> class _Sp, class _Tp, class _Up>
841struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
842{
843 typedef _Sp<_Up> type;
844};
845
846template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
847struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
848{
849#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
850 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
851#else
852 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
853#endif
854};
855
856template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
857struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
858{
859 typedef _Sp<_Up, _A0> type;
860};
861
862template <template <class, class, class> class _Sp, class _Tp, class _A0,
863 class _A1, class _Up>
864struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
865{
866#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
867 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
868#else
869 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
870#endif
871};
872
873template <template <class, class, class> class _Sp, class _Tp, class _A0,
874 class _A1, class _Up>
875struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
876{
877 typedef _Sp<_Up, _A0, _A1> type;
878};
879
880template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
881 class _A1, class _A2, class _Up>
882struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
883{
884#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
885 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
886#else
887 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
888#endif
889};
890
891template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
892 class _A1, class _A2, class _Up>
893struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
894{
895 typedef _Sp<_Up, _A0, _A1, _A2> type;
896};
897
Howard Hinnant324bb032010-08-22 00:02:43 +0000898#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000899
900template <class _Ptr>
Howard Hinnant82894812010-09-22 16:48:34 +0000901struct _LIBCPP_VISIBLE pointer_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000902{
903 typedef _Ptr pointer;
904 typedef typename __pointer_traits_element_type<pointer>::type element_type;
905 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
906
907#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant6b41c602011-05-11 20:21:19 +0000908 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000909#else
910 template <class _Up> struct rebind
911 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant324bb032010-08-22 00:02:43 +0000912#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000913
914private:
915 struct __nat {};
916public:
Howard Hinnant82894812010-09-22 16:48:34 +0000917 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000918 static pointer pointer_to(typename conditional<is_void<element_type>::value,
919 __nat, element_type>::type& __r)
920 {return pointer::pointer_to(__r);}
921};
922
923template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +0000924struct _LIBCPP_VISIBLE pointer_traits<_Tp*>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000925{
926 typedef _Tp* pointer;
927 typedef _Tp element_type;
928 typedef ptrdiff_t difference_type;
929
930#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
931 template <class _Up> using rebind = _Up*;
932#else
933 template <class _Up> struct rebind {typedef _Up* other;};
934#endif
935
936private:
937 struct __nat {};
938public:
Howard Hinnant82894812010-09-22 16:48:34 +0000939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000940 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant1694d232011-05-28 14:41:13 +0000941 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000942 {return _VSTD::addressof(__r);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000943};
944
945// allocator_traits
946
947namespace __has_pointer_type_imp
948{
949 template <class _Up> static __two test(...);
950 template <class _Up> static char test(typename _Up::pointer* = 0);
951}
952
953template <class _Tp>
954struct __has_pointer_type
955 : public integral_constant<bool, sizeof(__has_pointer_type_imp::test<_Tp>(0)) == 1>
956{
957};
958
959namespace __pointer_type_imp
960{
961
962template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
963struct __pointer_type
964{
965 typedef typename _Dp::pointer type;
966};
967
968template <class _Tp, class _Dp>
969struct __pointer_type<_Tp, _Dp, false>
970{
971 typedef _Tp* type;
972};
973
Howard Hinnant47761072010-11-18 01:40:00 +0000974} // __pointer_type_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000975
976template <class _Tp, class _Dp>
977struct __pointer_type
978{
979 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
980};
981
982template <class _Tp>
983struct __has_const_pointer
984{
985private:
986 struct __two {char _; char __;};
987 template <class _Up> static __two __test(...);
988 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
989public:
990 static const bool value = sizeof(__test<_Tp>(0)) == 1;
991};
992
993template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
994struct __const_pointer
995{
996 typedef typename _Alloc::const_pointer type;
997};
998
999template <class _Tp, class _Ptr, class _Alloc>
1000struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1001{
1002#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1003 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1004#else
1005 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1006#endif
1007};
1008
1009template <class _Tp>
1010struct __has_void_pointer
1011{
1012private:
1013 struct __two {char _; char __;};
1014 template <class _Up> static __two __test(...);
1015 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1016public:
1017 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1018};
1019
1020template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1021struct __void_pointer
1022{
1023 typedef typename _Alloc::void_pointer type;
1024};
1025
1026template <class _Ptr, class _Alloc>
1027struct __void_pointer<_Ptr, _Alloc, false>
1028{
1029#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1030 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1031#else
1032 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1033#endif
1034};
1035
1036template <class _Tp>
1037struct __has_const_void_pointer
1038{
1039private:
1040 struct __two {char _; char __;};
1041 template <class _Up> static __two __test(...);
1042 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1043public:
1044 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1045};
1046
1047template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1048struct __const_void_pointer
1049{
1050 typedef typename _Alloc::const_void_pointer type;
1051};
1052
1053template <class _Ptr, class _Alloc>
1054struct __const_void_pointer<_Ptr, _Alloc, false>
1055{
1056#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1057 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1058#else
1059 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1060#endif
1061};
1062
1063template <class _T>
1064inline _LIBCPP_INLINE_VISIBILITY
1065_T*
Howard Hinnant1694d232011-05-28 14:41:13 +00001066__to_raw_pointer(_T* __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001067{
1068 return __p;
1069}
1070
1071template <class _Pointer>
1072inline _LIBCPP_INLINE_VISIBILITY
1073typename pointer_traits<_Pointer>::element_type*
Howard Hinnant1694d232011-05-28 14:41:13 +00001074__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001075{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001076 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001077}
1078
1079template <class _Tp>
1080struct __has_size_type
1081{
1082private:
1083 struct __two {char _; char __;};
1084 template <class _Up> static __two __test(...);
1085 template <class _Up> static char __test(typename _Up::size_type* = 0);
1086public:
1087 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1088};
1089
Howard Hinnant47761072010-11-18 01:40:00 +00001090template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001091struct __size_type
1092{
Howard Hinnant47761072010-11-18 01:40:00 +00001093 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001094};
1095
Howard Hinnant47761072010-11-18 01:40:00 +00001096template <class _Alloc, class _DiffType>
1097struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001098{
1099 typedef typename _Alloc::size_type type;
1100};
1101
1102template <class _Tp>
1103struct __has_propagate_on_container_copy_assignment
1104{
1105private:
1106 struct __two {char _; char __;};
1107 template <class _Up> static __two __test(...);
1108 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1109public:
1110 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1111};
1112
1113template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1114struct __propagate_on_container_copy_assignment
1115{
1116 typedef false_type type;
1117};
1118
1119template <class _Alloc>
1120struct __propagate_on_container_copy_assignment<_Alloc, true>
1121{
1122 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1123};
1124
1125template <class _Tp>
1126struct __has_propagate_on_container_move_assignment
1127{
1128private:
1129 struct __two {char _; char __;};
1130 template <class _Up> static __two __test(...);
1131 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1132public:
1133 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1134};
1135
1136template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1137struct __propagate_on_container_move_assignment
1138{
1139 typedef false_type type;
1140};
1141
1142template <class _Alloc>
1143struct __propagate_on_container_move_assignment<_Alloc, true>
1144{
1145 typedef typename _Alloc::propagate_on_container_move_assignment type;
1146};
1147
1148template <class _Tp>
1149struct __has_propagate_on_container_swap
1150{
1151private:
1152 struct __two {char _; char __;};
1153 template <class _Up> static __two __test(...);
1154 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1155public:
1156 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1157};
1158
1159template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1160struct __propagate_on_container_swap
1161{
1162 typedef false_type type;
1163};
1164
1165template <class _Alloc>
1166struct __propagate_on_container_swap<_Alloc, true>
1167{
1168 typedef typename _Alloc::propagate_on_container_swap type;
1169};
1170
1171template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1172struct __has_rebind_other
1173{
1174private:
1175 struct __two {char _; char __;};
1176 template <class _Xp> static __two __test(...);
1177 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1178public:
1179 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1180};
1181
1182template <class _Tp, class _Up>
1183struct __has_rebind_other<_Tp, _Up, false>
1184{
1185 static const bool value = false;
1186};
1187
1188template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1189struct __allocator_traits_rebind
1190{
1191 typedef typename _Tp::template rebind<_Up>::other type;
1192};
1193
1194#ifndef _LIBCPP_HAS_NO_VARIADICS
1195
1196template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1197struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1198{
1199 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1200};
1201
1202template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1203struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1204{
1205 typedef _Alloc<_Up, _Args...> type;
1206};
1207
Howard Hinnant324bb032010-08-22 00:02:43 +00001208#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001209
1210template <template <class> class _Alloc, class _Tp, class _Up>
1211struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1212{
1213 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1214};
1215
1216template <template <class> class _Alloc, class _Tp, class _Up>
1217struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1218{
1219 typedef _Alloc<_Up> type;
1220};
1221
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001222template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1223struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1224{
1225 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1226};
1227
1228template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1229struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1230{
1231 typedef _Alloc<_Up, _A0> type;
1232};
1233
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001234template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1235 class _A1, class _Up>
1236struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1237{
1238 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1239};
1240
1241template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1242 class _A1, class _Up>
1243struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1244{
1245 typedef _Alloc<_Up, _A0, _A1> type;
1246};
1247
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001248template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1249 class _A1, class _A2, class _Up>
1250struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1251{
1252 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1253};
1254
1255template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1256 class _A1, class _A2, class _Up>
1257struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1258{
1259 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1260};
1261
Howard Hinnant324bb032010-08-22 00:02:43 +00001262#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001263
1264#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1265
1266template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1267auto
1268__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1269 -> decltype(__a.allocate(__sz, __p), true_type());
1270
1271template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1272auto
1273__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1274 -> false_type;
1275
1276template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1277struct __has_allocate_hint
1278 : integral_constant<bool,
1279 is_same<
1280 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1281 declval<_SizeType>(),
1282 declval<_ConstVoidPtr>())),
1283 true_type>::value>
1284{
1285};
1286
Howard Hinnant324bb032010-08-22 00:02:43 +00001287#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001288
1289template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1290struct __has_allocate_hint
1291 : true_type
1292{
1293};
1294
Howard Hinnant324bb032010-08-22 00:02:43 +00001295#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001296
1297#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1298
1299template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnant0949eed2011-06-30 21:18:19 +00001300decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1301 _VSTD::declval<_Args>()...),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001302 true_type())
1303__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1304
1305template <class _Alloc, class _Pointer, class ..._Args>
1306false_type
1307__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1308
1309template <class _Alloc, class _Pointer, class ..._Args>
1310struct __has_construct
1311 : integral_constant<bool,
1312 is_same<
1313 decltype(__has_construct_test(declval<_Alloc>(),
1314 declval<_Pointer>(),
1315 declval<_Args>()...)),
1316 true_type>::value>
1317{
1318};
1319
1320template <class _Alloc, class _Pointer>
1321auto
1322__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1323 -> decltype(__a.destroy(__p), true_type());
1324
1325template <class _Alloc, class _Pointer>
1326auto
1327__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1328 -> false_type;
1329
1330template <class _Alloc, class _Pointer>
1331struct __has_destroy
1332 : integral_constant<bool,
1333 is_same<
1334 decltype(__has_destroy_test(declval<_Alloc>(),
1335 declval<_Pointer>())),
1336 true_type>::value>
1337{
1338};
1339
1340template <class _Alloc>
1341auto
1342__has_max_size_test(_Alloc&& __a)
1343 -> decltype(__a.max_size(), true_type());
1344
1345template <class _Alloc>
1346auto
1347__has_max_size_test(const volatile _Alloc& __a)
1348 -> false_type;
1349
1350template <class _Alloc>
1351struct __has_max_size
1352 : integral_constant<bool,
1353 is_same<
1354 decltype(__has_max_size_test(declval<_Alloc&>())),
1355 true_type>::value>
1356{
1357};
1358
1359template <class _Alloc>
1360auto
1361__has_select_on_container_copy_construction_test(_Alloc&& __a)
1362 -> decltype(__a.select_on_container_copy_construction(), true_type());
1363
1364template <class _Alloc>
1365auto
1366__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1367 -> false_type;
1368
1369template <class _Alloc>
1370struct __has_select_on_container_copy_construction
1371 : integral_constant<bool,
1372 is_same<
1373 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1374 true_type>::value>
1375{
1376};
1377
Howard Hinnant324bb032010-08-22 00:02:43 +00001378#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001379
1380#ifndef _LIBCPP_HAS_NO_VARIADICS
1381
1382template <class _Alloc, class _Pointer, class ..._Args>
1383struct __has_construct
1384 : false_type
1385{
1386};
1387
Howard Hinnant324bb032010-08-22 00:02:43 +00001388#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001389
1390template <class _Alloc, class _Pointer>
1391struct __has_destroy
1392 : false_type
1393{
1394};
1395
1396template <class _Alloc>
1397struct __has_max_size
1398 : true_type
1399{
1400};
1401
1402template <class _Alloc>
1403struct __has_select_on_container_copy_construction
1404 : false_type
1405{
1406};
1407
Howard Hinnant324bb032010-08-22 00:02:43 +00001408#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001409
Howard Hinnant47761072010-11-18 01:40:00 +00001410template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1411struct __alloc_traits_difference_type
1412{
1413 typedef typename pointer_traits<_Ptr>::difference_type type;
1414};
1415
1416template <class _Alloc, class _Ptr>
1417struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1418{
1419 typedef typename _Alloc::difference_type type;
1420};
1421
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001422template <class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001423struct _LIBCPP_VISIBLE allocator_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001424{
1425 typedef _Alloc allocator_type;
1426 typedef typename allocator_type::value_type value_type;
1427
1428 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1429 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1430 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1431 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1432
Howard Hinnant47761072010-11-18 01:40:00 +00001433 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1434 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001435
1436 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1437 propagate_on_container_copy_assignment;
1438 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1439 propagate_on_container_move_assignment;
1440 typedef typename __propagate_on_container_swap<allocator_type>::type
1441 propagate_on_container_swap;
1442
1443#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1444 template <class _Tp> using rebind_alloc =
Howard Hinnant6b41c602011-05-11 20:21:19 +00001445 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001446 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant324bb032010-08-22 00:02:43 +00001447#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001448 template <class _Tp> struct rebind_alloc
1449 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1450 template <class _Tp> struct rebind_traits
1451 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant324bb032010-08-22 00:02:43 +00001452#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001453
Howard Hinnant82894812010-09-22 16:48:34 +00001454 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001455 static pointer allocate(allocator_type& __a, size_type __n)
1456 {return __a.allocate(__n);}
Howard Hinnant82894812010-09-22 16:48:34 +00001457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001458 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1459 {return allocate(__a, __n, __hint,
1460 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1461
Howard Hinnant82894812010-09-22 16:48:34 +00001462 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001463 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001464 {__a.deallocate(__p, __n);}
1465
1466#ifndef _LIBCPP_HAS_NO_VARIADICS
1467 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001468 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001469 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
1470 {__construct(__has_construct<allocator_type, pointer, _Args...>(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001471 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001472#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001473 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001474 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001475 static void construct(allocator_type& __a, _Tp* __p)
1476 {
1477 ::new ((void*)__p) _Tp();
1478 }
1479 template <class _Tp, class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00001480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001481 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1482 {
1483 ::new ((void*)__p) _Tp(__a0);
1484 }
1485 template <class _Tp, class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00001486 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001487 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1488 const _A1& __a1)
1489 {
1490 ::new ((void*)__p) _Tp(__a0, __a1);
1491 }
1492 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00001493 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001494 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1495 const _A1& __a1, const _A2& __a2)
1496 {
1497 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1498 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001499#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001500
1501 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001502 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001503 static void destroy(allocator_type& __a, _Tp* __p)
1504 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1505
Howard Hinnant82894812010-09-22 16:48:34 +00001506 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001507 static size_type max_size(const allocator_type& __a)
1508 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1509
Howard Hinnant82894812010-09-22 16:48:34 +00001510 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001511 static allocator_type
1512 select_on_container_copy_construction(const allocator_type& __a)
1513 {return select_on_container_copy_construction(
1514 __has_select_on_container_copy_construction<const allocator_type>(),
1515 __a);}
1516
1517private:
1518
Howard Hinnant82894812010-09-22 16:48:34 +00001519 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001520 static pointer allocate(allocator_type& __a, size_type __n,
1521 const_void_pointer __hint, true_type)
1522 {return __a.allocate(__n, __hint);}
Howard Hinnant82894812010-09-22 16:48:34 +00001523 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001524 static pointer allocate(allocator_type& __a, size_type __n,
1525 const_void_pointer __hint, false_type)
1526 {return __a.allocate(__n);}
1527
1528#ifndef _LIBCPP_HAS_NO_VARIADICS
1529 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001530 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001531 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001532 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001533 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001534 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001535 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1536 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001537 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001538 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001539#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001540
1541 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001543 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1544 {__a.destroy(__p);}
1545 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001547 static void __destroy(false_type, allocator_type&, _Tp* __p)
1548 {
1549 __p->~_Tp();
1550 }
1551
Howard Hinnant82894812010-09-22 16:48:34 +00001552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001553 static size_type __max_size(true_type, const allocator_type& __a)
1554 {return __a.max_size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001556 static size_type __max_size(false_type, const allocator_type&)
1557 {return numeric_limits<size_type>::max();}
1558
Howard Hinnant82894812010-09-22 16:48:34 +00001559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001560 static allocator_type
1561 select_on_container_copy_construction(true_type, const allocator_type& __a)
1562 {return __a.select_on_container_copy_construction();}
Howard Hinnant82894812010-09-22 16:48:34 +00001563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001564 static allocator_type
1565 select_on_container_copy_construction(false_type, const allocator_type& __a)
1566 {return __a;}
1567};
1568
1569// uses_allocator
1570
1571template <class _Tp>
1572struct __has_allocator_type
1573{
1574private:
1575 struct __two {char _; char __;};
1576 template <class _Up> static __two __test(...);
1577 template <class _Up> static char __test(typename _Up::allocator_type* = 0);
1578public:
1579 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1580};
1581
1582template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
1583struct __uses_allocator
1584 : public integral_constant<bool,
1585 is_convertible<_Alloc, typename _Tp::allocator_type>::value>
1586{
1587};
1588
1589template <class _Tp, class _Alloc>
1590struct __uses_allocator<_Tp, _Alloc, false>
1591 : public false_type
1592{
1593};
1594
1595template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001596struct _LIBCPP_VISIBLE uses_allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001597 : public __uses_allocator<_Tp, _Alloc>
1598{
1599};
1600
Howard Hinnantac38bae2011-01-11 20:02:45 +00001601#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001602
1603// uses-allocator construction
1604
1605template <class _Tp, class _Alloc, class ..._Args>
1606struct __uses_alloc_ctor_imp
1607{
1608 static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
1609 static const bool __ic =
1610 is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
1611 static const int value = __ua ? 2 - __ic : 0;
1612};
1613
1614template <class _Tp, class _Alloc, class ..._Args>
1615struct __uses_alloc_ctor
1616 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
1617 {};
1618
Howard Hinnantac38bae2011-01-11 20:02:45 +00001619#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001620
1621// allocator
1622
1623template <class _Tp>
Howard Hinnant36cdf022010-09-10 16:42:26 +00001624class _LIBCPP_VISIBLE allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001625{
1626public:
1627 typedef size_t size_type;
1628 typedef ptrdiff_t difference_type;
1629 typedef _Tp* pointer;
1630 typedef const _Tp* const_pointer;
1631 typedef _Tp& reference;
1632 typedef const _Tp& const_reference;
1633 typedef _Tp value_type;
1634
Howard Hinnant18884f42011-06-02 21:38:57 +00001635 typedef true_type propagate_on_container_move_assignment;
1636
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001637 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1638
Howard Hinnant1694d232011-05-28 14:41:13 +00001639 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1640 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1641 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001642 {return _VSTD::addressof(__x);}
Howard Hinnant1694d232011-05-28 14:41:13 +00001643 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001644 {return _VSTD::addressof(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001645 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1646 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
Howard Hinnant1694d232011-05-28 14:41:13 +00001647 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
1648 {::operator delete((void*)__p);}
1649 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1650 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001651#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001652 template <class _Up, class... _Args>
1653 _LIBCPP_INLINE_VISIBILITY
1654 void
1655 construct(_Up* __p, _Args&&... __args)
1656 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001657 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001658 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001659#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001660 _LIBCPP_INLINE_VISIBILITY
1661 void
1662 construct(pointer __p)
1663 {
1664 ::new((void*)__p) _Tp();
1665 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001666# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001667 template <class _A0>
1668 _LIBCPP_INLINE_VISIBILITY
1669 typename enable_if
1670 <
1671 !is_convertible<_A0, __rv<_A0> >::value,
1672 void
1673 >::type
1674 construct(pointer __p, _A0& __a0)
1675 {
1676 ::new((void*)__p) _Tp(__a0);
1677 }
1678 template <class _A0>
1679 _LIBCPP_INLINE_VISIBILITY
1680 typename enable_if
1681 <
1682 !is_convertible<_A0, __rv<_A0> >::value,
1683 void
1684 >::type
1685 construct(pointer __p, const _A0& __a0)
1686 {
1687 ::new((void*)__p) _Tp(__a0);
1688 }
1689 template <class _A0>
1690 _LIBCPP_INLINE_VISIBILITY
1691 typename enable_if
1692 <
1693 is_convertible<_A0, __rv<_A0> >::value,
1694 void
1695 >::type
1696 construct(pointer __p, _A0 __a0)
1697 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001698 ::new((void*)__p) _Tp(_VSTD::move(__a0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001699 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001700# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001701 template <class _A0, class _A1>
1702 _LIBCPP_INLINE_VISIBILITY
1703 void
1704 construct(pointer __p, _A0& __a0, _A1& __a1)
1705 {
1706 ::new((void*)__p) _Tp(__a0, __a1);
1707 }
1708 template <class _A0, class _A1>
1709 _LIBCPP_INLINE_VISIBILITY
1710 void
1711 construct(pointer __p, const _A0& __a0, _A1& __a1)
1712 {
1713 ::new((void*)__p) _Tp(__a0, __a1);
1714 }
1715 template <class _A0, class _A1>
1716 _LIBCPP_INLINE_VISIBILITY
1717 void
1718 construct(pointer __p, _A0& __a0, const _A1& __a1)
1719 {
1720 ::new((void*)__p) _Tp(__a0, __a1);
1721 }
1722 template <class _A0, class _A1>
1723 _LIBCPP_INLINE_VISIBILITY
1724 void
1725 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1726 {
1727 ::new((void*)__p) _Tp(__a0, __a1);
1728 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001729#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001730 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1731};
1732
1733template <class _Tp, class _Up>
1734inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001735bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001736
1737template <class _Tp, class _Up>
1738inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001739bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001740
1741template <class _OutputIterator, class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001742class _LIBCPP_VISIBLE raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001743 : public iterator<output_iterator_tag,
1744 _Tp, // purposefully not C++03
1745 ptrdiff_t, // purposefully not C++03
1746 _Tp*, // purposefully not C++03
1747 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1748{
1749private:
1750 _OutputIterator __x_;
1751public:
1752 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1753 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1754 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1755 {::new(&*__x_) _Tp(__element); return *this;}
1756 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1757 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1758 {raw_storage_iterator __t(*this); ++__x_; return __t;}
1759};
1760
1761template <class _Tp>
1762pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00001763get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001764{
1765 pair<_Tp*, ptrdiff_t> __r(0, 0);
1766 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1767 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1768 / sizeof(_Tp);
1769 if (__n > __m)
1770 __n = __m;
1771 while (__n > 0)
1772 {
1773 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1774 if (__r.first)
1775 {
1776 __r.second = __n;
1777 break;
1778 }
1779 __n /= 2;
1780 }
1781 return __r;
1782}
1783
1784template <class _Tp>
1785inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001786void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001787
1788template <class _Tp>
1789struct auto_ptr_ref
1790{
1791 _Tp* __ptr_;
1792};
1793
1794template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001795class _LIBCPP_VISIBLE auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001796{
1797private:
1798 _Tp* __ptr_;
1799public:
1800 typedef _Tp element_type;
1801
1802 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1803 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1804 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1805 : __ptr_(__p.release()) {}
1806 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1807 {reset(__p.release()); return *this;}
1808 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1809 {reset(__p.release()); return *this;}
1810 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1811 {reset(__p.__ptr_); return *this;}
1812 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1813
1814 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1815 {return *__ptr_;}
1816 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1817 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1818 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1819 {
1820 _Tp* __t = __ptr_;
1821 __ptr_ = 0;
1822 return __t;
1823 }
1824 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1825 {
1826 if (__ptr_ != __p)
1827 delete __ptr_;
1828 __ptr_ = __p;
1829 }
1830
1831 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1832 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1833 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1834 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1835 {return auto_ptr<_Up>(release());}
1836};
1837
1838template <>
Howard Hinnant82894812010-09-22 16:48:34 +00001839class _LIBCPP_VISIBLE auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001840{
1841public:
1842 typedef void element_type;
1843};
1844
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001845template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1846 typename remove_cv<_T2>::type>::value,
1847 bool = is_empty<_T1>::value,
1848 bool = is_empty<_T2>::value>
1849struct __libcpp_compressed_pair_switch;
1850
1851template <class _T1, class _T2, bool IsSame>
1852struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1853
1854template <class _T1, class _T2, bool IsSame>
1855struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1856
1857template <class _T1, class _T2, bool IsSame>
1858struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1859
1860template <class _T1, class _T2>
1861struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
1862
1863template <class _T1, class _T2>
1864struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
1865
1866template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
1867class __libcpp_compressed_pair_imp;
1868
1869template <class _T1, class _T2>
1870class __libcpp_compressed_pair_imp<_T1, _T2, 0>
1871{
1872private:
1873 _T1 __first_;
1874 _T2 __second_;
1875public:
1876 typedef _T1 _T1_param;
1877 typedef _T2 _T2_param;
1878
1879 typedef typename remove_reference<_T1>::type& _T1_reference;
1880 typedef typename remove_reference<_T2>::type& _T2_reference;
1881
1882 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1883 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1884
1885 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1886 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001887 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001888 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001889 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001890 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001891 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001892
Howard Hinnant61aa6012011-07-01 19:24:36 +00001893#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
1894
1895 _LIBCPP_INLINE_VISIBILITY
1896 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
1897 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
1898 is_nothrow_copy_constructible<_T2>::value)
1899 : __first_(__p.first()),
1900 __second_(__p.second()) {}
1901
1902 _LIBCPP_INLINE_VISIBILITY
1903 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
1904 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
1905 is_nothrow_copy_assignable<_T2>::value)
1906 {
1907 __first_ = __p.first();
1908 __second_ = __p.second();
1909 return *this;
1910 }
1911
Howard Hinnant73d21a42010-09-04 23:28:19 +00001912#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00001913
1914 _LIBCPP_INLINE_VISIBILITY
1915 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00001916 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
1917 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant61aa6012011-07-01 19:24:36 +00001918 : __first_(_VSTD::forward<_T1>(__p.first())),
1919 __second_(_VSTD::forward<_T2>(__p.second())) {}
1920
1921 _LIBCPP_INLINE_VISIBILITY
1922 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
1923 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
1924 is_nothrow_move_assignable<_T2>::value)
1925 {
1926 __first_ = _VSTD::forward<_T1>(__p.first());
1927 __second_ = _VSTD::forward<_T2>(__p.second());
1928 return *this;
1929 }
1930
Howard Hinnant73d21a42010-09-04 23:28:19 +00001931#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001932
Howard Hinnant61aa6012011-07-01 19:24:36 +00001933#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
1934
Howard Hinnant1694d232011-05-28 14:41:13 +00001935 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
1936 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001937
Howard Hinnant1694d232011-05-28 14:41:13 +00001938 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
1939 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001940
1941 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00001942 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1943 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001944 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001945 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001946 swap(__first_, __x.__first_);
1947 swap(__second_, __x.__second_);
1948 }
1949};
1950
1951template <class _T1, class _T2>
1952class __libcpp_compressed_pair_imp<_T1, _T2, 1>
1953 : private _T1
1954{
1955private:
1956 _T2 __second_;
1957public:
1958 typedef _T1 _T1_param;
1959 typedef _T2 _T2_param;
1960
1961 typedef _T1& _T1_reference;
1962 typedef typename remove_reference<_T2>::type& _T2_reference;
1963
1964 typedef const _T1& _T1_const_reference;
1965 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1966
1967 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1968 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001969 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001970 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001971 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001972 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001973 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001974
Howard Hinnant61aa6012011-07-01 19:24:36 +00001975#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
1976
1977 _LIBCPP_INLINE_VISIBILITY
1978 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
1979 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
1980 is_nothrow_copy_constructible<_T2>::value)
1981 : _T1(__p.first()), __second_(__p.second()) {}
1982
1983 _LIBCPP_INLINE_VISIBILITY
1984 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
1985 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
1986 is_nothrow_copy_assignable<_T2>::value)
1987 {
1988 _T1::operator=(__p.first());
1989 __second_ = __p.second();
1990 return *this;
1991 }
1992
Howard Hinnant73d21a42010-09-04 23:28:19 +00001993#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00001994
1995 _LIBCPP_INLINE_VISIBILITY
1996 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00001997 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
1998 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001999 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002000
2001 _LIBCPP_INLINE_VISIBILITY
2002 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2003 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2004 is_nothrow_move_assignable<_T2>::value)
2005 {
2006 _T1::operator=(_VSTD::move(__p.first()));
2007 __second_ = _VSTD::forward<_T2>(__p.second());
2008 return *this;
2009 }
2010
Howard Hinnant73d21a42010-09-04 23:28:19 +00002011#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002012
Howard Hinnant61aa6012011-07-01 19:24:36 +00002013#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2014
Howard Hinnant1694d232011-05-28 14:41:13 +00002015 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2016 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002017
Howard Hinnant1694d232011-05-28 14:41:13 +00002018 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2019 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002020
2021 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002022 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2023 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002024 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002025 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002026 swap(__second_, __x.__second_);
2027 }
2028};
2029
2030template <class _T1, class _T2>
2031class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2032 : private _T2
2033{
2034private:
2035 _T1 __first_;
2036public:
2037 typedef _T1 _T1_param;
2038 typedef _T2 _T2_param;
2039
2040 typedef typename remove_reference<_T1>::type& _T1_reference;
2041 typedef _T2& _T2_reference;
2042
2043 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2044 typedef const _T2& _T2_const_reference;
2045
2046 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2047 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002048 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002049 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002050 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002051 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00002052 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2053 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002054 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002055
Howard Hinnant61aa6012011-07-01 19:24:36 +00002056#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2057
2058 _LIBCPP_INLINE_VISIBILITY
2059 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2060 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2061 is_nothrow_copy_constructible<_T2>::value)
2062 : _T2(__p.second()), __first_(__p.first()) {}
2063
2064 _LIBCPP_INLINE_VISIBILITY
2065 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2066 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2067 is_nothrow_copy_assignable<_T2>::value)
2068 {
2069 _T2::operator=(__p.second());
2070 __first_ = __p.first();
2071 return *this;
2072 }
2073
Howard Hinnant73d21a42010-09-04 23:28:19 +00002074#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002075
2076 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002077 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002078 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2079 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002080 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002081
2082 _LIBCPP_INLINE_VISIBILITY
2083 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2084 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2085 is_nothrow_move_assignable<_T2>::value)
2086 {
2087 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2088 __first_ = _VSTD::move(__p.first());
2089 return *this;
2090 }
2091
Howard Hinnant73d21a42010-09-04 23:28:19 +00002092#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002093
Howard Hinnant61aa6012011-07-01 19:24:36 +00002094#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2095
Howard Hinnant1694d232011-05-28 14:41:13 +00002096 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2097 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002098
Howard Hinnant1694d232011-05-28 14:41:13 +00002099 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2100 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002101
2102 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002103 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2104 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002105 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002106 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002107 swap(__first_, __x.__first_);
2108 }
2109};
2110
2111template <class _T1, class _T2>
2112class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2113 : private _T1,
2114 private _T2
2115{
2116public:
2117 typedef _T1 _T1_param;
2118 typedef _T2 _T2_param;
2119
2120 typedef _T1& _T1_reference;
2121 typedef _T2& _T2_reference;
2122
2123 typedef const _T1& _T1_const_reference;
2124 typedef const _T2& _T2_const_reference;
2125
2126 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2127 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002128 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002129 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002130 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002131 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002132 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002133
Howard Hinnant61aa6012011-07-01 19:24:36 +00002134#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2135
2136 _LIBCPP_INLINE_VISIBILITY
2137 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2138 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2139 is_nothrow_copy_constructible<_T2>::value)
2140 : _T1(__p.first()), _T2(__p.second()) {}
2141
2142 _LIBCPP_INLINE_VISIBILITY
2143 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2144 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2145 is_nothrow_copy_assignable<_T2>::value)
2146 {
2147 _T1::operator=(__p.first());
2148 _T2::operator=(__p.second());
2149 return *this;
2150 }
2151
Howard Hinnant73d21a42010-09-04 23:28:19 +00002152#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002153
2154 _LIBCPP_INLINE_VISIBILITY
2155 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002156 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2157 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002158 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002159
2160 _LIBCPP_INLINE_VISIBILITY
2161 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2162 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2163 is_nothrow_move_assignable<_T2>::value)
2164 {
2165 _T1::operator=(_VSTD::move(__p.first()));
2166 _T2::operator=(_VSTD::move(__p.second()));
2167 return *this;
2168 }
2169
Howard Hinnant73d21a42010-09-04 23:28:19 +00002170#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002171
Howard Hinnant61aa6012011-07-01 19:24:36 +00002172#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2173
Howard Hinnant1694d232011-05-28 14:41:13 +00002174 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2175 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002176
Howard Hinnant1694d232011-05-28 14:41:13 +00002177 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2178 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002179
2180 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002181 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2182 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002183 {
2184 }
2185};
2186
2187template <class _T1, class _T2>
2188class __compressed_pair
2189 : private __libcpp_compressed_pair_imp<_T1, _T2>
2190{
2191 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2192public:
2193 typedef typename base::_T1_param _T1_param;
2194 typedef typename base::_T2_param _T2_param;
2195
2196 typedef typename base::_T1_reference _T1_reference;
2197 typedef typename base::_T2_reference _T2_reference;
2198
2199 typedef typename base::_T1_const_reference _T1_const_reference;
2200 typedef typename base::_T2_const_reference _T2_const_reference;
2201
2202 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
2203 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1, int = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002204 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002205 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2, int* = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002206 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002207 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002208 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002209
Howard Hinnant61aa6012011-07-01 19:24:36 +00002210#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2211
2212 _LIBCPP_INLINE_VISIBILITY
2213 __compressed_pair(const __compressed_pair& __p)
2214 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2215 is_nothrow_copy_constructible<_T2>::value)
2216 : base(__p) {}
2217
2218 _LIBCPP_INLINE_VISIBILITY
2219 __compressed_pair& operator=(const __compressed_pair& __p)
2220 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2221 is_nothrow_copy_assignable<_T2>::value)
2222 {
2223 base::operator=(__p);
2224 return *this;
2225 }
2226
Howard Hinnant73d21a42010-09-04 23:28:19 +00002227#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002228 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002229 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002230 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2231 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002232 : base(_VSTD::move(__p)) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002233
2234 _LIBCPP_INLINE_VISIBILITY
2235 __compressed_pair& operator=(__compressed_pair&& __p)
2236 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2237 is_nothrow_move_assignable<_T2>::value)
2238 {
2239 base::operator=(_VSTD::move(__p));
2240 return *this;
2241 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002242#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002243
Howard Hinnant61aa6012011-07-01 19:24:36 +00002244#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2245
Howard Hinnant1694d232011-05-28 14:41:13 +00002246 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2247 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002248
Howard Hinnant1694d232011-05-28 14:41:13 +00002249 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2250 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002251
Howard Hinnant1694d232011-05-28 14:41:13 +00002252 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2253 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2254 __is_nothrow_swappable<_T1>::value)
2255 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002256};
2257
2258template <class _T1, class _T2>
2259inline _LIBCPP_INLINE_VISIBILITY
2260void
2261swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002262 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2263 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002264 {__x.swap(__y);}
2265
2266template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002267struct _LIBCPP_VISIBLE default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002268{
Howard Hinnant1694d232011-05-28 14:41:13 +00002269 _LIBCPP_INLINE_VISIBILITY default_delete() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002270 template <class _Up>
2271 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002272 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2273 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002274 {
2275 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2276 delete __ptr;
2277 }
2278};
2279
2280template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002281struct _LIBCPP_VISIBLE default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002282{
Howard Hinnant1694d232011-05-28 14:41:13 +00002283 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002284 {
2285 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2286 delete [] __ptr;
2287 }
2288private:
2289 template <class _Up> void operator() (_Up*) const;
2290};
2291
2292template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant82894812010-09-22 16:48:34 +00002293class _LIBCPP_VISIBLE unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002294{
2295public:
2296 typedef _Tp element_type;
2297 typedef _Dp deleter_type;
2298 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2299private:
2300 __compressed_pair<pointer, deleter_type> __ptr_;
2301
Howard Hinnant73d21a42010-09-04 23:28:19 +00002302#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002303 unique_ptr(const unique_ptr&);
2304 unique_ptr& operator=(const unique_ptr&);
2305 template <class _Up, class _Ep>
2306 unique_ptr(const unique_ptr<_Up, _Ep>&);
2307 template <class _Up, class _Ep>
2308 unique_ptr& operator=(const unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002309#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002310 unique_ptr(unique_ptr&);
2311 template <class _Up, class _Ep>
2312 unique_ptr(unique_ptr<_Up, _Ep>&);
2313 unique_ptr& operator=(unique_ptr&);
2314 template <class _Up, class _Ep>
2315 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002316#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002317
2318 struct __nat {int __for_bool_;};
2319
2320 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2321 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2322public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002323 _LIBCPP_INLINE_VISIBILITY unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002324 : __ptr_(pointer())
2325 {
2326 static_assert(!is_pointer<deleter_type>::value,
2327 "unique_ptr constructed with null function pointer deleter");
2328 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002329 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002330 : __ptr_(pointer())
2331 {
2332 static_assert(!is_pointer<deleter_type>::value,
2333 "unique_ptr constructed with null function pointer deleter");
2334 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002335 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002336 : __ptr_(_VSTD::move(__p))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002337 {
2338 static_assert(!is_pointer<deleter_type>::value,
2339 "unique_ptr constructed with null function pointer deleter");
2340 }
2341
Howard Hinnant73d21a42010-09-04 23:28:19 +00002342#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002343 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2344 is_reference<deleter_type>::value,
2345 deleter_type,
2346 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002347 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002348 : __ptr_(__p, __d) {}
2349
2350 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002351 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002352 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002353 {
2354 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2355 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002356 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002357 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002358 template <class _Up, class _Ep>
2359 _LIBCPP_INLINE_VISIBILITY
2360 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2361 typename enable_if
2362 <
2363 !is_array<_Up>::value &&
2364 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2365 is_convertible<_Ep, deleter_type>::value &&
2366 (
2367 !is_reference<deleter_type>::value ||
2368 is_same<deleter_type, _Ep>::value
2369 ),
2370 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002371 >::type = __nat()) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002372 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002373
2374 template <class _Up>
2375 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2376 typename enable_if<
2377 is_convertible<_Up*, _Tp*>::value &&
2378 is_same<_Dp, default_delete<_Tp> >::value,
2379 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002380 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002381 : __ptr_(__p.release())
2382 {
2383 }
2384
Howard Hinnant1694d232011-05-28 14:41:13 +00002385 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002386 {
2387 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002388 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002389 return *this;
2390 }
2391
2392 template <class _Up, class _Ep>
2393 _LIBCPP_INLINE_VISIBILITY
2394 typename enable_if
2395 <
2396 !is_array<_Up>::value,
2397 unique_ptr&
2398 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002399 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002400 {
2401 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002402 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002403 return *this;
2404 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002405#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002406
2407 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2408 {
2409 return __rv<unique_ptr>(*this);
2410 }
2411
2412 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002413 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002414
2415 template <class _Up, class _Ep>
2416 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2417 {
2418 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002419 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002420 return *this;
2421 }
2422
2423 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002424 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002425
2426 template <class _Up>
2427 _LIBCPP_INLINE_VISIBILITY
2428 typename enable_if<
2429 is_convertible<_Up*, _Tp*>::value &&
2430 is_same<_Dp, default_delete<_Tp> >::value,
2431 unique_ptr&
2432 >::type
2433 operator=(auto_ptr<_Up> __p)
2434 {reset(__p.release()); return *this;}
2435
Howard Hinnant73d21a42010-09-04 23:28:19 +00002436#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002437 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2438
Howard Hinnant1694d232011-05-28 14:41:13 +00002439 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002440 {
2441 reset();
2442 return *this;
2443 }
2444
2445 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2446 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002447 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2448 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2449 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2450 {return __ptr_.second();}
2451 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2452 {return __ptr_.second();}
2453 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const
2454 _NOEXCEPT
2455 {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002456
Howard Hinnant1694d232011-05-28 14:41:13 +00002457 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002458 {
2459 pointer __t = __ptr_.first();
2460 __ptr_.first() = pointer();
2461 return __t;
2462 }
2463
Howard Hinnant1694d232011-05-28 14:41:13 +00002464 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002465 {
2466 pointer __tmp = __ptr_.first();
2467 __ptr_.first() = __p;
2468 if (__tmp)
2469 __ptr_.second()(__tmp);
2470 }
2471
Howard Hinnant1694d232011-05-28 14:41:13 +00002472 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2473 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002474};
2475
2476template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002477class _LIBCPP_VISIBLE unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002478{
2479public:
2480 typedef _Tp element_type;
2481 typedef _Dp deleter_type;
2482 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2483private:
2484 __compressed_pair<pointer, deleter_type> __ptr_;
2485
Howard Hinnant73d21a42010-09-04 23:28:19 +00002486#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002487 unique_ptr(const unique_ptr&);
2488 unique_ptr& operator=(const unique_ptr&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002489#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002490 unique_ptr(unique_ptr&);
2491 template <class _Up>
2492 unique_ptr(unique_ptr<_Up>&);
2493 unique_ptr& operator=(unique_ptr&);
2494 template <class _Up>
2495 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002496#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002497
2498 struct __nat {int __for_bool_;};
2499
2500 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2501 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2502public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002503 _LIBCPP_INLINE_VISIBILITY unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002504 : __ptr_(pointer())
2505 {
2506 static_assert(!is_pointer<deleter_type>::value,
2507 "unique_ptr constructed with null function pointer deleter");
2508 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002509 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002510 : __ptr_(pointer())
2511 {
2512 static_assert(!is_pointer<deleter_type>::value,
2513 "unique_ptr constructed with null function pointer deleter");
2514 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002515#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002516 template <class _P,
2517 class = typename enable_if<is_same<_P, pointer>::value>::type
2518 >
Howard Hinnant1694d232011-05-28 14:41:13 +00002519 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_P __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002520 : __ptr_(__p)
2521 {
2522 static_assert(!is_pointer<deleter_type>::value,
2523 "unique_ptr constructed with null function pointer deleter");
2524 }
2525
2526 template <class _P,
2527 class = typename enable_if<is_same<_P, pointer>::value>::type
2528 >
2529 _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename conditional<
2530 is_reference<deleter_type>::value,
2531 deleter_type,
2532 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002533 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002534 : __ptr_(__p, __d) {}
2535
2536 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2537 is_reference<deleter_type>::value,
2538 deleter_type,
2539 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002540 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002541 : __ptr_(pointer(), __d) {}
2542
2543 template <class _P,
2544 class = typename enable_if<is_same<_P, pointer>::value ||
2545 is_same<_P, nullptr_t>::value>::type
2546 >
2547 _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002548 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002549 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002550 {
2551 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2552 }
2553
2554 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002555 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002556 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002557 {
2558 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2559 }
2560
Howard Hinnant1694d232011-05-28 14:41:13 +00002561 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002562 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002563
Howard Hinnant1694d232011-05-28 14:41:13 +00002564 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002565 {
2566 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002567 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002568 return *this;
2569 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002570#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002571
2572 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2573 : __ptr_(__p)
2574 {
2575 static_assert(!is_pointer<deleter_type>::value,
2576 "unique_ptr constructed with null function pointer deleter");
2577 }
2578
2579 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002580 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002581
2582 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002583 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002584
2585 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2586 {
2587 return __rv<unique_ptr>(*this);
2588 }
2589
2590 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002591 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002592
2593 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2594 {
2595 reset(__u->release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002596 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002597 return *this;
2598 }
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[](size_t __i) const
2610 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002611 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2612 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2613 {return __ptr_.second();}
2614 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2615 {return __ptr_.second();}
2616 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const _NOEXCEPT
2617 {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002618
Howard Hinnant1694d232011-05-28 14:41:13 +00002619 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002620 {
2621 pointer __t = __ptr_.first();
2622 __ptr_.first() = pointer();
2623 return __t;
2624 }
2625
Howard Hinnant73d21a42010-09-04 23:28:19 +00002626#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002627 template <class _P,
2628 class = typename enable_if<is_same<_P, pointer>::value>::type
2629 >
Howard Hinnant1694d232011-05-28 14:41:13 +00002630 _LIBCPP_INLINE_VISIBILITY void reset(_P __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002631 {
2632 pointer __tmp = __ptr_.first();
2633 __ptr_.first() = __p;
2634 if (__tmp)
2635 __ptr_.second()(__tmp);
2636 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002637 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002638 {
2639 pointer __tmp = __ptr_.first();
2640 __ptr_.first() = nullptr;
2641 if (__tmp)
2642 __ptr_.second()(__tmp);
2643 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002644 _LIBCPP_INLINE_VISIBILITY void reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002645 {
2646 pointer __tmp = __ptr_.first();
2647 __ptr_.first() = nullptr;
2648 if (__tmp)
2649 __ptr_.second()(__tmp);
2650 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002651#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002652 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2653 {
2654 pointer __tmp = __ptr_.first();
2655 __ptr_.first() = __p;
2656 if (__tmp)
2657 __ptr_.second()(__tmp);
2658 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002659#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002660
2661 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2662private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002663
Howard Hinnant73d21a42010-09-04 23:28:19 +00002664#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002665 template <class _Up>
2666 explicit unique_ptr(_Up);
2667 template <class _Up>
2668 unique_ptr(_Up __u,
2669 typename conditional<
2670 is_reference<deleter_type>::value,
2671 deleter_type,
2672 typename add_lvalue_reference<const deleter_type>::type>::type,
2673 typename enable_if
2674 <
2675 is_convertible<_Up, pointer>::value,
2676 __nat
2677 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002678#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002679};
2680
2681template <class _Tp, class _Dp>
2682inline _LIBCPP_INLINE_VISIBILITY
2683void
Howard Hinnant1694d232011-05-28 14:41:13 +00002684swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002685
2686template <class _T1, class _D1, class _T2, class _D2>
2687inline _LIBCPP_INLINE_VISIBILITY
2688bool
2689operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2690
2691template <class _T1, class _D1, class _T2, class _D2>
2692inline _LIBCPP_INLINE_VISIBILITY
2693bool
2694operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2695
2696template <class _T1, class _D1, class _T2, class _D2>
2697inline _LIBCPP_INLINE_VISIBILITY
2698bool
2699operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() < __y.get();}
2700
2701template <class _T1, class _D1, class _T2, class _D2>
2702inline _LIBCPP_INLINE_VISIBILITY
2703bool
2704operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2705
2706template <class _T1, class _D1, class _T2, class _D2>
2707inline _LIBCPP_INLINE_VISIBILITY
2708bool
2709operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2710
2711template <class _T1, class _D1, class _T2, class _D2>
2712inline _LIBCPP_INLINE_VISIBILITY
2713bool
2714operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2715
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00002716template <class _Tp> struct hash;
Howard Hinnant21aefc32010-06-03 16:42:57 +00002717
2718template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002719struct _LIBCPP_VISIBLE hash<_Tp*>
Howard Hinnant21aefc32010-06-03 16:42:57 +00002720 : public unary_function<_Tp*, size_t>
2721{
Howard Hinnant82894812010-09-22 16:48:34 +00002722 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002723 size_t operator()(_Tp* __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00002724 {
2725 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
2726 return *__p;
2727 }
2728};
2729
2730template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002731struct _LIBCPP_VISIBLE hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00002732{
2733 typedef unique_ptr<_Tp, _Dp> argument_type;
2734 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00002735 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002736 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00002737 {
2738 typedef typename argument_type::pointer pointer;
2739 return hash<pointer>()(__ptr.get());
2740 }
2741};
2742
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002743struct __destruct_n
2744{
2745private:
2746 size_t size;
2747
2748 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002749 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002750 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
2751
2752 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002753 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002754 {}
2755
Howard Hinnant1694d232011-05-28 14:41:13 +00002756 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002757 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00002758 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002759 {}
2760
Howard Hinnant1694d232011-05-28 14:41:13 +00002761 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002762 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00002763 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002764 {}
2765public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002766 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
2767 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002768
2769 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002770 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00002771 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002772
2773 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002774 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00002775 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002776
2777 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002778 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00002779 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002780};
2781
2782template <class _Alloc>
2783class __allocator_destructor
2784{
2785 typedef allocator_traits<_Alloc> __alloc_traits;
2786public:
2787 typedef typename __alloc_traits::pointer pointer;
2788 typedef typename __alloc_traits::size_type size_type;
2789private:
2790 _Alloc& __alloc_;
2791 size_type __s_;
2792public:
2793 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00002794 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002795 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00002796 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002797 void operator()(pointer __p) _NOEXCEPT
2798 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002799};
2800
2801template <class _InputIterator, class _ForwardIterator>
2802_ForwardIterator
2803uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2804{
2805 __destruct_n __d(0);
2806 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2807 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2808 for (; __f != __l; ++__f, ++__r, __d.__incr((value_type*)0))
2809 ::new(&*__r) value_type(*__f);
2810 __h.release();
2811 return __r;
2812}
2813
2814template <class _InputIterator, class _Size, class _ForwardIterator>
2815_ForwardIterator
2816uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2817{
2818 __destruct_n __d(0);
2819 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2820 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2821 for (; __n > 0; ++__f, ++__r, __d.__incr((value_type*)0), --__n)
2822 ::new(&*__r) value_type(*__f);
2823 __h.release();
2824 return __r;
2825}
2826
2827template <class _ForwardIterator, class _Tp>
2828void
2829uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2830{
2831 __destruct_n __d(0);
2832 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2833 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2834 for (; __f != __l; ++__f, __d.__incr((value_type*)0))
2835 ::new(&*__f) value_type(__x);
2836 __h.release();
2837}
2838
2839template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00002840_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002841uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2842{
2843 __destruct_n __d(0);
2844 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2845 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2846 for (; __n > 0; ++__f, --__n, __d.__incr((value_type*)0))
2847 ::new(&*__f) value_type(__x);
2848 __h.release();
Howard Hinnant2f6a6272010-11-18 16:13:03 +00002849 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002850}
2851
Howard Hinnant82894812010-09-22 16:48:34 +00002852class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002853 : public std::exception
2854{
2855public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002856 virtual ~bad_weak_ptr() _NOEXCEPT;
2857 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002858};
2859
2860template<class _Tp> class weak_ptr;
2861
2862class __shared_count
2863{
2864 __shared_count(const __shared_count&);
2865 __shared_count& operator=(const __shared_count&);
2866
2867protected:
2868 long __shared_owners_;
2869 virtual ~__shared_count();
2870private:
Howard Hinnant1694d232011-05-28 14:41:13 +00002871 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002872
2873public:
Howard Hinnant82894812010-09-22 16:48:34 +00002874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002875 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002876 : __shared_owners_(__refs) {}
2877
Howard Hinnant1694d232011-05-28 14:41:13 +00002878 void __add_shared() _NOEXCEPT;
2879 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00002880 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002881 long use_count() const _NOEXCEPT {return __shared_owners_ + 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002882};
2883
2884class __shared_weak_count
2885 : private __shared_count
2886{
2887 long __shared_weak_owners_;
2888
2889public:
Howard Hinnant82894812010-09-22 16:48:34 +00002890 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002891 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002892 : __shared_count(__refs),
2893 __shared_weak_owners_(__refs) {}
2894protected:
2895 virtual ~__shared_weak_count();
2896
2897public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002898 void __add_shared() _NOEXCEPT;
2899 void __add_weak() _NOEXCEPT;
2900 void __release_shared() _NOEXCEPT;
2901 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00002902 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002903 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
2904 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002905
Howard Hinnantd4444702010-08-11 17:04:31 +00002906#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00002907 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00002908#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002909private:
Howard Hinnant1694d232011-05-28 14:41:13 +00002910 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002911};
2912
2913template <class _Tp, class _Dp, class _Alloc>
2914class __shared_ptr_pointer
2915 : public __shared_weak_count
2916{
2917 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2918public:
Howard Hinnant82894812010-09-22 16:48:34 +00002919 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002920 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002921 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002922
Howard Hinnantd4444702010-08-11 17:04:31 +00002923#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00002924 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00002925#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002926
2927private:
Howard Hinnant1694d232011-05-28 14:41:13 +00002928 virtual void __on_zero_shared() _NOEXCEPT;
2929 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002930};
2931
Howard Hinnantd4444702010-08-11 17:04:31 +00002932#ifndef _LIBCPP_NO_RTTI
2933
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002934template <class _Tp, class _Dp, class _Alloc>
2935const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00002936__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002937{
2938 return __t == typeid(_Dp) ? &__data_.first().second() : 0;
2939}
2940
Howard Hinnant324bb032010-08-22 00:02:43 +00002941#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00002942
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002943template <class _Tp, class _Dp, class _Alloc>
2944void
Howard Hinnant1694d232011-05-28 14:41:13 +00002945__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002946{
2947 __data_.first().second()(__data_.first().first());
2948 __data_.first().second().~_Dp();
2949}
2950
2951template <class _Tp, class _Dp, class _Alloc>
2952void
Howard Hinnant1694d232011-05-28 14:41:13 +00002953__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002954{
2955 typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
2956 __data_.second().~_Alloc();
2957 __a.deallocate(this, 1);
2958}
2959
2960template <class _Tp, class _Alloc>
2961class __shared_ptr_emplace
2962 : public __shared_weak_count
2963{
2964 __compressed_pair<_Alloc, _Tp> __data_;
2965public:
2966#ifndef _LIBCPP_HAS_NO_VARIADICS
2967
Howard Hinnant82894812010-09-22 16:48:34 +00002968 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002969 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002970 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002971
2972 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00002973 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002974 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002975 : __data_(_VSTD::move(__a), _Tp(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002976
2977#else // _LIBCPP_HAS_NO_VARIADICS
2978
Howard Hinnant82894812010-09-22 16:48:34 +00002979 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002980 __shared_ptr_emplace(_Alloc __a)
2981 : __data_(__a) {}
2982
2983 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00002984 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002985 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
2986 : __data_(__a, _Tp(__a0)) {}
2987
2988 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00002989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002990 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
2991 : __data_(__a, _Tp(__a0, __a1)) {}
2992
2993 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00002994 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002995 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
2996 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
2997
2998#endif // _LIBCPP_HAS_NO_VARIADICS
2999
3000private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003001 virtual void __on_zero_shared() _NOEXCEPT;
3002 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003003public:
Howard Hinnant82894812010-09-22 16:48:34 +00003004 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003005 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003006};
3007
3008template <class _Tp, class _Alloc>
3009void
Howard Hinnant1694d232011-05-28 14:41:13 +00003010__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003011{
3012 __data_.second().~_Tp();
3013}
3014
3015template <class _Tp, class _Alloc>
3016void
Howard Hinnant1694d232011-05-28 14:41:13 +00003017__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003018{
3019 typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
3020 __data_.first().~_Alloc();
3021 __a.deallocate(this, 1);
3022}
3023
3024template<class _Tp> class enable_shared_from_this;
3025
3026template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003027class _LIBCPP_VISIBLE shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003028{
Howard Hinnant324bb032010-08-22 00:02:43 +00003029public:
3030 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003031private:
3032 element_type* __ptr_;
3033 __shared_weak_count* __cntrl_;
3034
3035 struct __nat {int __for_bool_;};
3036public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003037 shared_ptr() _NOEXCEPT;
3038 shared_ptr(nullptr_t) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003039 template<class _Yp> explicit shared_ptr(_Yp* __p);
Howard Hinnant324bb032010-08-22 00:02:43 +00003040 template<class _Yp, class _Dp> shared_ptr(_Yp* __p, _Dp __d);
3041 template<class _Yp, class _Dp, class _Alloc> shared_ptr(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003042 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3043 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant1694d232011-05-28 14:41:13 +00003044 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3045 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003046 template<class _Yp>
3047 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003048 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3049 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003050#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003051 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003052 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003053 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3054 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003055#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003056 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003057 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003058#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00003059 template<class _Yp> shared_ptr(auto_ptr<_Yp>&& __r);
3060#else
3061 template<class _Yp> shared_ptr(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003062#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003063#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003064private:
Howard Hinnant324bb032010-08-22 00:02:43 +00003065 template <class _Yp, class _Dp> shared_ptr(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003066public:
3067 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
3068 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
3069 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
3070 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003071#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003072 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
3073 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
3074 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
3075 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003076#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003077
3078 ~shared_ptr();
3079
Howard Hinnant1694d232011-05-28 14:41:13 +00003080 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
3081 template<class _Yp> shared_ptr& operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003082#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003083 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00003084 template<class _Yp> shared_ptr& operator=(shared_ptr<_Yp>&& __r);
3085 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003086#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00003087 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003088#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003089#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003090private:
Howard Hinnant324bb032010-08-22 00:02:43 +00003091 template <class _Yp, class _Dp> shared_ptr& operator=(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003092public:
Howard Hinnant324bb032010-08-22 00:02:43 +00003093 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003094#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00003095 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003096#endif
3097
Howard Hinnant1694d232011-05-28 14:41:13 +00003098 void swap(shared_ptr& __r) _NOEXCEPT;
3099 void reset() _NOEXCEPT;
3100 template<class _Yp> void reset(_Yp* __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003101 template<class _Yp, class _Dp> void reset(_Yp* __p, _Dp __d);
3102 template<class _Yp, class _Dp, class _Alloc> void reset(_Yp* __p, _Dp __d, _Alloc __a);
3103
Howard Hinnant82894812010-09-22 16:48:34 +00003104 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003105 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003106 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003107 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3108 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003109 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003110 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003111 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003112 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003113 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003114 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00003115 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003116 /*explicit*/ operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003117 template <class _U>
3118 _LIBCPP_INLINE_VISIBILITY
3119 bool owner_before(shared_ptr<_U> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003120 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003121 template <class _U>
3122 _LIBCPP_INLINE_VISIBILITY
3123 bool owner_before(weak_ptr<_U> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003124 {return __cntrl_ < __p.__cntrl_;}
3125
Howard Hinnantd4444702010-08-11 17:04:31 +00003126#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003127 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00003128 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003129 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003130 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00003131#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003132
3133#ifndef _LIBCPP_HAS_NO_VARIADICS
3134
3135 template<class ..._Args>
3136 static
3137 shared_ptr<_Tp>
3138 make_shared(_Args&& ...__args);
3139
3140 template<class _Alloc, class ..._Args>
3141 static
3142 shared_ptr<_Tp>
3143 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3144
3145#else // _LIBCPP_HAS_NO_VARIADICS
3146
3147 static shared_ptr<_Tp> make_shared();
3148
3149 template<class _A0>
3150 static shared_ptr<_Tp> make_shared(_A0&);
3151
3152 template<class _A0, class _A1>
3153 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3154
3155 template<class _A0, class _A1, class _A2>
3156 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3157
3158 template<class _Alloc>
3159 static shared_ptr<_Tp>
3160 allocate_shared(const _Alloc& __a);
3161
3162 template<class _Alloc, class _A0>
3163 static shared_ptr<_Tp>
3164 allocate_shared(const _Alloc& __a, _A0& __a0);
3165
3166 template<class _Alloc, class _A0, class _A1>
3167 static shared_ptr<_Tp>
3168 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3169
3170 template<class _Alloc, class _A0, class _A1, class _A2>
3171 static shared_ptr<_Tp>
3172 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3173
3174#endif // _LIBCPP_HAS_NO_VARIADICS
3175
3176private:
3177
3178 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00003179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003180 void
Howard Hinnant1694d232011-05-28 14:41:13 +00003181 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003182 {
3183 if (__e)
3184 __e->__weak_this_ = *this;
3185 }
3186
Howard Hinnant82894812010-09-22 16:48:34 +00003187 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003188 void __enable_weak_this(const void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003189
Howard Hinnant82894812010-09-22 16:48:34 +00003190 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
3191 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003192};
3193
3194template<class _Tp>
3195inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003196shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003197 : __ptr_(0),
3198 __cntrl_(0)
3199{
3200}
3201
3202template<class _Tp>
3203inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003204shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003205 : __ptr_(0),
3206 __cntrl_(0)
3207{
3208}
3209
3210template<class _Tp>
3211template<class _Yp>
3212shared_ptr<_Tp>::shared_ptr(_Yp* __p)
3213 : __ptr_(__p)
3214{
3215 unique_ptr<_Yp> __hold(__p);
3216 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3217 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
3218 __hold.release();
3219 __enable_weak_this(__p);
3220}
3221
3222template<class _Tp>
3223template<class _Yp, class _Dp>
3224shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
3225 : __ptr_(__p)
3226{
3227#ifndef _LIBCPP_NO_EXCEPTIONS
3228 try
3229 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003230#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003231 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3232 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
3233 __enable_weak_this(__p);
3234#ifndef _LIBCPP_NO_EXCEPTIONS
3235 }
3236 catch (...)
3237 {
3238 __d(__p);
3239 throw;
3240 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003241#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003242}
3243
3244template<class _Tp>
3245template<class _Dp>
3246shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3247 : __ptr_(0)
3248{
3249#ifndef _LIBCPP_NO_EXCEPTIONS
3250 try
3251 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003252#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003253 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
3254 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
3255#ifndef _LIBCPP_NO_EXCEPTIONS
3256 }
3257 catch (...)
3258 {
3259 __d(__p);
3260 throw;
3261 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003262#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003263}
3264
3265template<class _Tp>
3266template<class _Yp, class _Dp, class _Alloc>
3267shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
3268 : __ptr_(__p)
3269{
3270#ifndef _LIBCPP_NO_EXCEPTIONS
3271 try
3272 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003273#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003274 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
3275 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3276 typedef __allocator_destructor<_A2> _D2;
3277 _A2 __a2(__a);
3278 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3279 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
3280 __cntrl_ = __hold2.release();
3281 __enable_weak_this(__p);
3282#ifndef _LIBCPP_NO_EXCEPTIONS
3283 }
3284 catch (...)
3285 {
3286 __d(__p);
3287 throw;
3288 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003289#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003290}
3291
3292template<class _Tp>
3293template<class _Dp, class _Alloc>
3294shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
3295 : __ptr_(0)
3296{
3297#ifndef _LIBCPP_NO_EXCEPTIONS
3298 try
3299 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003300#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003301 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
3302 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3303 typedef __allocator_destructor<_A2> _D2;
3304 _A2 __a2(__a);
3305 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3306 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
3307 __cntrl_ = __hold2.release();
3308#ifndef _LIBCPP_NO_EXCEPTIONS
3309 }
3310 catch (...)
3311 {
3312 __d(__p);
3313 throw;
3314 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003315#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003316}
3317
3318template<class _Tp>
3319template<class _Yp>
3320inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003321shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003322 : __ptr_(__p),
3323 __cntrl_(__r.__cntrl_)
3324{
3325 if (__cntrl_)
3326 __cntrl_->__add_shared();
3327}
3328
3329template<class _Tp>
3330inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003331shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003332 : __ptr_(__r.__ptr_),
3333 __cntrl_(__r.__cntrl_)
3334{
3335 if (__cntrl_)
3336 __cntrl_->__add_shared();
3337}
3338
3339template<class _Tp>
3340template<class _Yp>
3341inline _LIBCPP_INLINE_VISIBILITY
3342shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
3343 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003344 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003345 : __ptr_(__r.__ptr_),
3346 __cntrl_(__r.__cntrl_)
3347{
3348 if (__cntrl_)
3349 __cntrl_->__add_shared();
3350}
3351
Howard Hinnant73d21a42010-09-04 23:28:19 +00003352#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003353
3354template<class _Tp>
3355inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003356shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003357 : __ptr_(__r.__ptr_),
3358 __cntrl_(__r.__cntrl_)
3359{
3360 __r.__ptr_ = 0;
3361 __r.__cntrl_ = 0;
3362}
3363
3364template<class _Tp>
3365template<class _Yp>
3366inline _LIBCPP_INLINE_VISIBILITY
3367shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
3368 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003369 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003370 : __ptr_(__r.__ptr_),
3371 __cntrl_(__r.__cntrl_)
3372{
3373 __r.__ptr_ = 0;
3374 __r.__cntrl_ = 0;
3375}
3376
Howard Hinnant73d21a42010-09-04 23:28:19 +00003377#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003378
3379template<class _Tp>
3380template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003381#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003382shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
3383#else
Howard Hinnant92172b82010-08-21 21:14:53 +00003384shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003385#endif
3386 : __ptr_(__r.get())
3387{
3388 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3389 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
3390 __enable_weak_this(__r.get());
3391 __r.release();
3392}
3393
3394template<class _Tp>
3395template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003396#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003397shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3398#else
3399shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3400#endif
3401 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
3402 : __ptr_(__r.get())
3403{
3404 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3405 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
3406 __enable_weak_this(__r.get());
3407 __r.release();
3408}
3409
3410template<class _Tp>
3411template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003412#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003413shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3414#else
3415shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3416#endif
3417 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
3418 : __ptr_(__r.get())
3419{
3420 typedef __shared_ptr_pointer<_Yp*,
3421 reference_wrapper<typename remove_reference<_Dp>::type>,
3422 allocator<_Yp> > _CntrlBlk;
3423 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
3424 __enable_weak_this(__r.get());
3425 __r.release();
3426}
3427
3428#ifndef _LIBCPP_HAS_NO_VARIADICS
3429
3430template<class _Tp>
3431template<class ..._Args>
3432shared_ptr<_Tp>
3433shared_ptr<_Tp>::make_shared(_Args&& ...__args)
3434{
3435 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3436 typedef allocator<_CntrlBlk> _A2;
3437 typedef __allocator_destructor<_A2> _D2;
3438 _A2 __a2;
3439 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003440 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003441 shared_ptr<_Tp> __r;
3442 __r.__ptr_ = __hold2.get()->get();
3443 __r.__cntrl_ = __hold2.release();
3444 __r.__enable_weak_this(__r.__ptr_);
3445 return __r;
3446}
3447
3448template<class _Tp>
3449template<class _Alloc, class ..._Args>
3450shared_ptr<_Tp>
3451shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
3452{
3453 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3454 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3455 typedef __allocator_destructor<_A2> _D2;
3456 _A2 __a2(__a);
3457 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003458 ::new(__hold2.get()) _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003459 shared_ptr<_Tp> __r;
3460 __r.__ptr_ = __hold2.get()->get();
3461 __r.__cntrl_ = __hold2.release();
3462 __r.__enable_weak_this(__r.__ptr_);
3463 return __r;
3464}
3465
3466#else // _LIBCPP_HAS_NO_VARIADICS
3467
3468template<class _Tp>
3469shared_ptr<_Tp>
3470shared_ptr<_Tp>::make_shared()
3471{
3472 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3473 typedef allocator<_CntrlBlk> _Alloc2;
3474 typedef __allocator_destructor<_Alloc2> _D2;
3475 _Alloc2 __alloc2;
3476 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3477 ::new(__hold2.get()) _CntrlBlk(__alloc2);
3478 shared_ptr<_Tp> __r;
3479 __r.__ptr_ = __hold2.get()->get();
3480 __r.__cntrl_ = __hold2.release();
3481 __r.__enable_weak_this(__r.__ptr_);
3482 return __r;
3483}
3484
3485template<class _Tp>
3486template<class _A0>
3487shared_ptr<_Tp>
3488shared_ptr<_Tp>::make_shared(_A0& __a0)
3489{
3490 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3491 typedef allocator<_CntrlBlk> _Alloc2;
3492 typedef __allocator_destructor<_Alloc2> _D2;
3493 _Alloc2 __alloc2;
3494 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3495 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
3496 shared_ptr<_Tp> __r;
3497 __r.__ptr_ = __hold2.get()->get();
3498 __r.__cntrl_ = __hold2.release();
3499 __r.__enable_weak_this(__r.__ptr_);
3500 return __r;
3501}
3502
3503template<class _Tp>
3504template<class _A0, class _A1>
3505shared_ptr<_Tp>
3506shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
3507{
3508 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3509 typedef allocator<_CntrlBlk> _Alloc2;
3510 typedef __allocator_destructor<_Alloc2> _D2;
3511 _Alloc2 __alloc2;
3512 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3513 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
3514 shared_ptr<_Tp> __r;
3515 __r.__ptr_ = __hold2.get()->get();
3516 __r.__cntrl_ = __hold2.release();
3517 __r.__enable_weak_this(__r.__ptr_);
3518 return __r;
3519}
3520
3521template<class _Tp>
3522template<class _A0, class _A1, class _A2>
3523shared_ptr<_Tp>
3524shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3525{
3526 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3527 typedef allocator<_CntrlBlk> _Alloc2;
3528 typedef __allocator_destructor<_Alloc2> _D2;
3529 _Alloc2 __alloc2;
3530 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3531 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
3532 shared_ptr<_Tp> __r;
3533 __r.__ptr_ = __hold2.get()->get();
3534 __r.__cntrl_ = __hold2.release();
3535 __r.__enable_weak_this(__r.__ptr_);
3536 return __r;
3537}
3538
3539template<class _Tp>
3540template<class _Alloc>
3541shared_ptr<_Tp>
3542shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
3543{
3544 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3545 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3546 typedef __allocator_destructor<_Alloc2> _D2;
3547 _Alloc2 __alloc2(__a);
3548 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3549 ::new(__hold2.get()) _CntrlBlk(__a);
3550 shared_ptr<_Tp> __r;
3551 __r.__ptr_ = __hold2.get()->get();
3552 __r.__cntrl_ = __hold2.release();
3553 __r.__enable_weak_this(__r.__ptr_);
3554 return __r;
3555}
3556
3557template<class _Tp>
3558template<class _Alloc, class _A0>
3559shared_ptr<_Tp>
3560shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
3561{
3562 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3563 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3564 typedef __allocator_destructor<_Alloc2> _D2;
3565 _Alloc2 __alloc2(__a);
3566 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3567 ::new(__hold2.get()) _CntrlBlk(__a, __a0);
3568 shared_ptr<_Tp> __r;
3569 __r.__ptr_ = __hold2.get()->get();
3570 __r.__cntrl_ = __hold2.release();
3571 __r.__enable_weak_this(__r.__ptr_);
3572 return __r;
3573}
3574
3575template<class _Tp>
3576template<class _Alloc, class _A0, class _A1>
3577shared_ptr<_Tp>
3578shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3579{
3580 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3581 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3582 typedef __allocator_destructor<_Alloc2> _D2;
3583 _Alloc2 __alloc2(__a);
3584 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3585 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
3586 shared_ptr<_Tp> __r;
3587 __r.__ptr_ = __hold2.get()->get();
3588 __r.__cntrl_ = __hold2.release();
3589 __r.__enable_weak_this(__r.__ptr_);
3590 return __r;
3591}
3592
3593template<class _Tp>
3594template<class _Alloc, class _A0, class _A1, class _A2>
3595shared_ptr<_Tp>
3596shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3597{
3598 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3599 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3600 typedef __allocator_destructor<_Alloc2> _D2;
3601 _Alloc2 __alloc2(__a);
3602 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3603 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
3604 shared_ptr<_Tp> __r;
3605 __r.__ptr_ = __hold2.get()->get();
3606 __r.__cntrl_ = __hold2.release();
3607 __r.__enable_weak_this(__r.__ptr_);
3608 return __r;
3609}
3610
3611#endif // _LIBCPP_HAS_NO_VARIADICS
3612
3613template<class _Tp>
3614shared_ptr<_Tp>::~shared_ptr()
3615{
3616 if (__cntrl_)
3617 __cntrl_->__release_shared();
3618}
3619
3620template<class _Tp>
3621inline _LIBCPP_INLINE_VISIBILITY
3622shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003623shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003624{
3625 shared_ptr(__r).swap(*this);
3626 return *this;
3627}
3628
3629template<class _Tp>
3630template<class _Yp>
3631inline _LIBCPP_INLINE_VISIBILITY
3632shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003633shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003634{
3635 shared_ptr(__r).swap(*this);
3636 return *this;
3637}
3638
Howard Hinnant73d21a42010-09-04 23:28:19 +00003639#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003640
3641template<class _Tp>
3642inline _LIBCPP_INLINE_VISIBILITY
3643shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003644shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003645{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003646 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003647 return *this;
3648}
3649
3650template<class _Tp>
3651template<class _Yp>
3652inline _LIBCPP_INLINE_VISIBILITY
3653shared_ptr<_Tp>&
3654shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3655{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003656 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003657 return *this;
3658}
3659
3660template<class _Tp>
3661template<class _Yp>
3662inline _LIBCPP_INLINE_VISIBILITY
3663shared_ptr<_Tp>&
3664shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3665{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003666 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003667 return *this;
3668}
3669
3670template<class _Tp>
3671template <class _Yp, class _Dp>
3672inline _LIBCPP_INLINE_VISIBILITY
3673shared_ptr<_Tp>&
3674shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3675{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003676 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003677 return *this;
3678}
3679
Howard Hinnant73d21a42010-09-04 23:28:19 +00003680#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003681
3682template<class _Tp>
3683template<class _Yp>
3684inline _LIBCPP_INLINE_VISIBILITY
3685shared_ptr<_Tp>&
Howard Hinnant324bb032010-08-22 00:02:43 +00003686shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003687{
3688 shared_ptr(__r).swap(*this);
3689 return *this;
3690}
3691
3692template<class _Tp>
3693template <class _Yp, class _Dp>
3694inline _LIBCPP_INLINE_VISIBILITY
3695shared_ptr<_Tp>&
3696shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
3697{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003698 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003699 return *this;
3700}
3701
Howard Hinnant73d21a42010-09-04 23:28:19 +00003702#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003703
3704template<class _Tp>
3705inline _LIBCPP_INLINE_VISIBILITY
3706void
Howard Hinnant1694d232011-05-28 14:41:13 +00003707shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003708{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003709 _VSTD::swap(__ptr_, __r.__ptr_);
3710 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003711}
3712
3713template<class _Tp>
3714inline _LIBCPP_INLINE_VISIBILITY
3715void
Howard Hinnant1694d232011-05-28 14:41:13 +00003716shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003717{
3718 shared_ptr().swap(*this);
3719}
3720
3721template<class _Tp>
3722template<class _Yp>
3723inline _LIBCPP_INLINE_VISIBILITY
3724void
3725shared_ptr<_Tp>::reset(_Yp* __p)
3726{
3727 shared_ptr(__p).swap(*this);
3728}
3729
3730template<class _Tp>
3731template<class _Yp, class _Dp>
3732inline _LIBCPP_INLINE_VISIBILITY
3733void
3734shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3735{
3736 shared_ptr(__p, __d).swap(*this);
3737}
3738
3739template<class _Tp>
3740template<class _Yp, class _Dp, class _Alloc>
3741inline _LIBCPP_INLINE_VISIBILITY
3742void
3743shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3744{
3745 shared_ptr(__p, __d, __a).swap(*this);
3746}
3747
3748#ifndef _LIBCPP_HAS_NO_VARIADICS
3749
Howard Hinnant324bb032010-08-22 00:02:43 +00003750template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003751inline _LIBCPP_INLINE_VISIBILITY
3752shared_ptr<_Tp>
3753make_shared(_Args&& ...__args)
3754{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003755 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003756}
3757
Howard Hinnant324bb032010-08-22 00:02:43 +00003758template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003759inline _LIBCPP_INLINE_VISIBILITY
3760shared_ptr<_Tp>
3761allocate_shared(const _Alloc& __a, _Args&& ...__args)
3762{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003763 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003764}
3765
3766#else // _LIBCPP_HAS_NO_VARIADICS
3767
3768template<class _Tp>
3769inline _LIBCPP_INLINE_VISIBILITY
3770shared_ptr<_Tp>
3771make_shared()
3772{
3773 return shared_ptr<_Tp>::make_shared();
3774}
3775
3776template<class _Tp, class _A0>
3777inline _LIBCPP_INLINE_VISIBILITY
3778shared_ptr<_Tp>
3779make_shared(_A0& __a0)
3780{
3781 return shared_ptr<_Tp>::make_shared(__a0);
3782}
3783
3784template<class _Tp, class _A0, class _A1>
3785inline _LIBCPP_INLINE_VISIBILITY
3786shared_ptr<_Tp>
3787make_shared(_A0& __a0, _A1& __a1)
3788{
3789 return shared_ptr<_Tp>::make_shared(__a0, __a1);
3790}
3791
Howard Hinnant324bb032010-08-22 00:02:43 +00003792template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003793inline _LIBCPP_INLINE_VISIBILITY
3794shared_ptr<_Tp>
3795make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3796{
3797 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
3798}
3799
3800template<class _Tp, class _Alloc>
3801inline _LIBCPP_INLINE_VISIBILITY
3802shared_ptr<_Tp>
3803allocate_shared(const _Alloc& __a)
3804{
3805 return shared_ptr<_Tp>::allocate_shared(__a);
3806}
3807
3808template<class _Tp, class _Alloc, class _A0>
3809inline _LIBCPP_INLINE_VISIBILITY
3810shared_ptr<_Tp>
3811allocate_shared(const _Alloc& __a, _A0& __a0)
3812{
3813 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
3814}
3815
3816template<class _Tp, class _Alloc, class _A0, class _A1>
3817inline _LIBCPP_INLINE_VISIBILITY
3818shared_ptr<_Tp>
3819allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3820{
3821 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
3822}
3823
3824template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
3825inline _LIBCPP_INLINE_VISIBILITY
3826shared_ptr<_Tp>
3827allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3828{
3829 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
3830}
3831
3832#endif // _LIBCPP_HAS_NO_VARIADICS
3833
3834template<class _Tp, class _Up>
3835inline _LIBCPP_INLINE_VISIBILITY
3836bool
Howard Hinnant1694d232011-05-28 14:41:13 +00003837operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003838{
3839 return __x.get() == __y.get();
3840}
3841
3842template<class _Tp, class _Up>
3843inline _LIBCPP_INLINE_VISIBILITY
3844bool
Howard Hinnant1694d232011-05-28 14:41:13 +00003845operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003846{
3847 return !(__x == __y);
3848}
3849
3850template<class _Tp, class _Up>
3851inline _LIBCPP_INLINE_VISIBILITY
3852bool
Howard Hinnant1694d232011-05-28 14:41:13 +00003853operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003854{
3855 return __x.get() < __y.get();
3856}
3857
3858template<class _Tp>
3859inline _LIBCPP_INLINE_VISIBILITY
3860void
Howard Hinnant1694d232011-05-28 14:41:13 +00003861swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003862{
3863 __x.swap(__y);
3864}
3865
3866template<class _Tp, class _Up>
3867inline _LIBCPP_INLINE_VISIBILITY
3868shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003869static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003870{
3871 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
3872}
3873
3874template<class _Tp, class _Up>
3875inline _LIBCPP_INLINE_VISIBILITY
3876shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003877dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003878{
3879 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
3880 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3881}
3882
3883template<class _Tp, class _Up>
3884shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003885const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003886{
3887 return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get()));
3888}
3889
Howard Hinnantd4444702010-08-11 17:04:31 +00003890#ifndef _LIBCPP_NO_RTTI
3891
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003892template<class _Dp, class _Tp>
3893inline _LIBCPP_INLINE_VISIBILITY
3894_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00003895get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003896{
3897 return __p.template __get_deleter<_Dp>();
3898}
3899
Howard Hinnant324bb032010-08-22 00:02:43 +00003900#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003901
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003902template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003903class _LIBCPP_VISIBLE weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003904{
Howard Hinnant324bb032010-08-22 00:02:43 +00003905public:
3906 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003907private:
3908 element_type* __ptr_;
3909 __shared_weak_count* __cntrl_;
3910
Howard Hinnant324bb032010-08-22 00:02:43 +00003911public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003912 weak_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003913 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003914 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3915 _NOEXCEPT;
3916 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003917 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003918 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3919 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00003920
3921 ~weak_ptr();
3922
Howard Hinnant1694d232011-05-28 14:41:13 +00003923 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
3924 template<class _Yp> weak_ptr& operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
3925 template<class _Yp> weak_ptr& operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00003926
Howard Hinnant1694d232011-05-28 14:41:13 +00003927 void swap(weak_ptr& __r) _NOEXCEPT;
3928 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00003929
Howard Hinnant82894812010-09-22 16:48:34 +00003930 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003931 long use_count() const _NOEXCEPT
3932 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003933 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003934 bool expired() const _NOEXCEPT
3935 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
3936 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003937 template<class _Up>
3938 _LIBCPP_INLINE_VISIBILITY
3939 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003940 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003941 template<class _Up>
3942 _LIBCPP_INLINE_VISIBILITY
3943 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003944 {return __cntrl_ < __r.__cntrl_;}
3945
Howard Hinnant82894812010-09-22 16:48:34 +00003946 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
3947 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003948};
3949
3950template<class _Tp>
3951inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003952weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003953 : __ptr_(0),
3954 __cntrl_(0)
3955{
3956}
3957
3958template<class _Tp>
3959inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003960weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003961 : __ptr_(__r.__ptr_),
3962 __cntrl_(__r.__cntrl_)
3963{
3964 if (__cntrl_)
3965 __cntrl_->__add_weak();
3966}
3967
3968template<class _Tp>
3969template<class _Yp>
3970inline _LIBCPP_INLINE_VISIBILITY
3971weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00003972 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003973 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003974 : __ptr_(__r.__ptr_),
3975 __cntrl_(__r.__cntrl_)
3976{
3977 if (__cntrl_)
3978 __cntrl_->__add_weak();
3979}
3980
3981template<class _Tp>
3982template<class _Yp>
3983inline _LIBCPP_INLINE_VISIBILITY
3984weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00003985 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003986 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003987 : __ptr_(__r.__ptr_),
3988 __cntrl_(__r.__cntrl_)
3989{
3990 if (__cntrl_)
3991 __cntrl_->__add_weak();
3992}
3993
3994template<class _Tp>
3995weak_ptr<_Tp>::~weak_ptr()
3996{
3997 if (__cntrl_)
3998 __cntrl_->__release_weak();
3999}
4000
4001template<class _Tp>
4002inline _LIBCPP_INLINE_VISIBILITY
4003weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004004weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004005{
4006 weak_ptr(__r).swap(*this);
4007 return *this;
4008}
4009
4010template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00004011template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004012inline _LIBCPP_INLINE_VISIBILITY
4013weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004014weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004015{
4016 weak_ptr(__r).swap(*this);
4017 return *this;
4018}
4019
4020template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00004021template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004022inline _LIBCPP_INLINE_VISIBILITY
4023weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004024weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004025{
4026 weak_ptr(__r).swap(*this);
4027 return *this;
4028}
4029
4030template<class _Tp>
4031inline _LIBCPP_INLINE_VISIBILITY
4032void
Howard Hinnant1694d232011-05-28 14:41:13 +00004033weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004034{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004035 _VSTD::swap(__ptr_, __r.__ptr_);
4036 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004037}
4038
4039template<class _Tp>
4040inline _LIBCPP_INLINE_VISIBILITY
4041void
Howard Hinnant1694d232011-05-28 14:41:13 +00004042swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004043{
4044 __x.swap(__y);
4045}
4046
4047template<class _Tp>
4048inline _LIBCPP_INLINE_VISIBILITY
4049void
Howard Hinnant1694d232011-05-28 14:41:13 +00004050weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004051{
4052 weak_ptr().swap(*this);
4053}
4054
4055template<class _Tp>
4056template<class _Yp>
4057shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
4058 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
4059 : __ptr_(__r.__ptr_),
4060 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4061{
4062 if (__cntrl_ == 0)
4063#ifndef _LIBCPP_NO_EXCEPTIONS
4064 throw bad_weak_ptr();
4065#else
4066 assert(!"bad_weak_ptr");
4067#endif
4068}
4069
4070template<class _Tp>
4071shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00004072weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004073{
4074 shared_ptr<_Tp> __r;
4075 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4076 if (__r.__cntrl_)
4077 __r.__ptr_ = __ptr_;
4078 return __r;
4079}
4080
Howard Hinnant324bb032010-08-22 00:02:43 +00004081template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004082
4083template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004084struct _LIBCPP_VISIBLE owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004085 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00004086{
4087 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00004088 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004089 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
4090 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004091 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004092 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
4093 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004094 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004095 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
4096 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00004097};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004098
4099template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004100struct _LIBCPP_VISIBLE owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004101 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4102{
Howard Hinnant324bb032010-08-22 00:02:43 +00004103 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00004104 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004105 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
4106 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004107 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004108 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
4109 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004110 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004111 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
4112 {return __x.owner_before(__y);}
4113};
4114
4115template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004116class _LIBCPP_VISIBLE enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004117{
4118 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00004119protected:
Howard Hinnant82894812010-09-22 16:48:34 +00004120 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004121 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00004122 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004123 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00004124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004125 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
4126 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00004127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004128 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00004129public:
Howard Hinnant82894812010-09-22 16:48:34 +00004130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004131 shared_ptr<_Tp> shared_from_this()
4132 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00004133 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004134 shared_ptr<_Tp const> shared_from_this() const
4135 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004136
4137 template <class _Up> friend class shared_ptr;
4138};
4139
Howard Hinnant21aefc32010-06-03 16:42:57 +00004140template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004141struct _LIBCPP_VISIBLE hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00004142{
4143 typedef shared_ptr<_Tp> argument_type;
4144 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00004145 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004146 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00004147 {
4148 return hash<_Tp*>()(__ptr.get());
4149 }
4150};
4151
Howard Hinnant464aa5c2011-07-18 15:51:59 +00004152template<class _CharT, class _Traits, class _Y>
4153inline _LIBCPP_INLINE_VISIBILITY
4154basic_ostream<_CharT, _Traits>&
4155operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Y> const& __p);
4156
Howard Hinnant324bb032010-08-22 00:02:43 +00004157//enum class
Howard Hinnant82894812010-09-22 16:48:34 +00004158struct _LIBCPP_VISIBLE pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004159{
4160 enum _
4161 {
4162 relaxed,
4163 preferred,
4164 strict
4165 };
4166
4167 _ __v_;
4168
Howard Hinnant82894812010-09-22 16:48:34 +00004169 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004170 pointer_safety(_ __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00004171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004172 operator int() const {return __v_;}
4173};
4174
4175void declare_reachable(void* __p);
4176void declare_no_pointers(char* __p, size_t __n);
4177void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnant1694d232011-05-28 14:41:13 +00004178pointer_safety get_pointer_safety() _NOEXCEPT;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00004179void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004180
4181template <class _Tp>
4182inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00004183_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004184undeclare_reachable(_Tp* __p)
4185{
4186 return static_cast<_Tp*>(__undeclare_reachable(__p));
4187}
4188
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00004189void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004190
4191_LIBCPP_END_NAMESPACE_STD
4192
4193#endif // _LIBCPP_MEMORY