blob: 67f9937cbf38dd036a67dbfb4b79d3cc0ae7dd28 [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>
598#if defined(_LIBCPP_NO_EXCEPTIONS)
599 #include <cassert>
600#endif
601
602#pragma GCC system_header
603
604_LIBCPP_BEGIN_NAMESPACE_STD
605
606// allocator_arg_t
607
Howard Hinnant82894812010-09-22 16:48:34 +0000608struct _LIBCPP_VISIBLE allocator_arg_t { };
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000609
610extern const allocator_arg_t allocator_arg;
611
612// addressof
613
614template <class _Tp>
615inline _LIBCPP_INLINE_VISIBILITY
616_Tp*
Howard Hinnant1694d232011-05-28 14:41:13 +0000617addressof(_Tp& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000618{
619 return (_Tp*)&(char&)__x;
620}
621
622template <class _Tp> class allocator;
623
624template <>
Howard Hinnant82894812010-09-22 16:48:34 +0000625class _LIBCPP_VISIBLE allocator<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000626{
627public:
628 typedef void* pointer;
629 typedef const void* const_pointer;
630 typedef void value_type;
631
632 template <class _Up> struct rebind {typedef allocator<_Up> other;};
633};
634
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000635// pointer_traits
636
637template <class _Tp>
638struct __has_element_type
639{
640private:
641 struct __two {char _; char __;};
642 template <class _Up> static __two __test(...);
643 template <class _Up> static char __test(typename _Up::element_type* = 0);
644public:
645 static const bool value = sizeof(__test<_Tp>(0)) == 1;
646};
647
648template <class _Ptr, bool = __has_element_type<_Ptr>::value>
649struct __pointer_traits_element_type;
650
651template <class _Ptr>
652struct __pointer_traits_element_type<_Ptr, true>
653{
654 typedef typename _Ptr::element_type type;
655};
656
657#ifndef _LIBCPP_HAS_NO_VARIADICS
658
659template <template <class, class...> class _Sp, class _Tp, class ..._Args>
660struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
661{
662 typedef typename _Sp<_Tp, _Args...>::element_type type;
663};
664
665template <template <class, class...> class _Sp, class _Tp, class ..._Args>
666struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
667{
668 typedef _Tp type;
669};
670
Howard Hinnant324bb032010-08-22 00:02:43 +0000671#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000672
673template <template <class> class _Sp, class _Tp>
674struct __pointer_traits_element_type<_Sp<_Tp>, true>
675{
676 typedef typename _Sp<_Tp>::element_type type;
677};
678
679template <template <class> class _Sp, class _Tp>
680struct __pointer_traits_element_type<_Sp<_Tp>, false>
681{
682 typedef _Tp type;
683};
684
685template <template <class, class> class _Sp, class _Tp, class _A0>
686struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
687{
688 typedef typename _Sp<_Tp, _A0>::element_type type;
689};
690
691template <template <class, class> class _Sp, class _Tp, class _A0>
692struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
693{
694 typedef _Tp type;
695};
696
697template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
698struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
699{
700 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
701};
702
703template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
704struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
705{
706 typedef _Tp type;
707};
708
709template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
710 class _A1, class _A2>
711struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
712{
713 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
714};
715
716template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
717 class _A1, class _A2>
718struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
719{
720 typedef _Tp type;
721};
722
Howard Hinnant324bb032010-08-22 00:02:43 +0000723#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000724
725template <class _Tp>
726struct __has_difference_type
727{
728private:
729 struct __two {char _; char __;};
730 template <class _Up> static __two __test(...);
731 template <class _Up> static char __test(typename _Up::difference_type* = 0);
732public:
733 static const bool value = sizeof(__test<_Tp>(0)) == 1;
734};
735
736template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
737struct __pointer_traits_difference_type
738{
739 typedef ptrdiff_t type;
740};
741
742template <class _Ptr>
743struct __pointer_traits_difference_type<_Ptr, true>
744{
745 typedef typename _Ptr::difference_type type;
746};
747
748template <class _Tp, class _Up>
749struct __has_rebind
750{
751private:
752 struct __two {char _; char __;};
753 template <class _Xp> static __two __test(...);
754 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
755public:
756 static const bool value = sizeof(__test<_Tp>(0)) == 1;
757};
758
759template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
760struct __pointer_traits_rebind
761{
762#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
763 typedef typename _Tp::template rebind<_Up> type;
764#else
765 typedef typename _Tp::template rebind<_Up>::other type;
766#endif
767};
768
769#ifndef _LIBCPP_HAS_NO_VARIADICS
770
771template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
772struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
773{
774#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
775 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
776#else
777 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
778#endif
779};
780
781template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
782struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
783{
784 typedef _Sp<_Up, _Args...> type;
785};
786
Howard Hinnant324bb032010-08-22 00:02:43 +0000787#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000788
789template <template <class> class _Sp, class _Tp, class _Up>
790struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
791{
792#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
793 typedef typename _Sp<_Tp>::template rebind<_Up> type;
794#else
795 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
796#endif
797};
798
799template <template <class> class _Sp, class _Tp, class _Up>
800struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
801{
802 typedef _Sp<_Up> type;
803};
804
805template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
806struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
807{
808#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
809 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
810#else
811 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
812#endif
813};
814
815template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
816struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
817{
818 typedef _Sp<_Up, _A0> type;
819};
820
821template <template <class, class, class> class _Sp, class _Tp, class _A0,
822 class _A1, class _Up>
823struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
824{
825#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
826 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
827#else
828 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
829#endif
830};
831
832template <template <class, class, class> class _Sp, class _Tp, class _A0,
833 class _A1, class _Up>
834struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
835{
836 typedef _Sp<_Up, _A0, _A1> type;
837};
838
839template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
840 class _A1, class _A2, class _Up>
841struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
842{
843#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
844 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
845#else
846 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
847#endif
848};
849
850template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
851 class _A1, class _A2, class _Up>
852struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
853{
854 typedef _Sp<_Up, _A0, _A1, _A2> type;
855};
856
Howard Hinnant324bb032010-08-22 00:02:43 +0000857#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000858
859template <class _Ptr>
Howard Hinnant82894812010-09-22 16:48:34 +0000860struct _LIBCPP_VISIBLE pointer_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000861{
862 typedef _Ptr pointer;
863 typedef typename __pointer_traits_element_type<pointer>::type element_type;
864 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
865
866#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant6b41c602011-05-11 20:21:19 +0000867 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000868#else
869 template <class _Up> struct rebind
870 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant324bb032010-08-22 00:02:43 +0000871#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000872
873private:
874 struct __nat {};
875public:
Howard Hinnant82894812010-09-22 16:48:34 +0000876 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000877 static pointer pointer_to(typename conditional<is_void<element_type>::value,
878 __nat, element_type>::type& __r)
879 {return pointer::pointer_to(__r);}
880};
881
882template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +0000883struct _LIBCPP_VISIBLE pointer_traits<_Tp*>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000884{
885 typedef _Tp* pointer;
886 typedef _Tp element_type;
887 typedef ptrdiff_t difference_type;
888
889#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
890 template <class _Up> using rebind = _Up*;
891#else
892 template <class _Up> struct rebind {typedef _Up* other;};
893#endif
894
895private:
896 struct __nat {};
897public:
Howard Hinnant82894812010-09-22 16:48:34 +0000898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000899 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant1694d232011-05-28 14:41:13 +0000900 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000901 {return _STD::addressof(__r);}
902};
903
904// allocator_traits
905
906namespace __has_pointer_type_imp
907{
908 template <class _Up> static __two test(...);
909 template <class _Up> static char test(typename _Up::pointer* = 0);
910}
911
912template <class _Tp>
913struct __has_pointer_type
914 : public integral_constant<bool, sizeof(__has_pointer_type_imp::test<_Tp>(0)) == 1>
915{
916};
917
918namespace __pointer_type_imp
919{
920
921template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
922struct __pointer_type
923{
924 typedef typename _Dp::pointer type;
925};
926
927template <class _Tp, class _Dp>
928struct __pointer_type<_Tp, _Dp, false>
929{
930 typedef _Tp* type;
931};
932
Howard Hinnant47761072010-11-18 01:40:00 +0000933} // __pointer_type_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000934
935template <class _Tp, class _Dp>
936struct __pointer_type
937{
938 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
939};
940
941template <class _Tp>
942struct __has_const_pointer
943{
944private:
945 struct __two {char _; char __;};
946 template <class _Up> static __two __test(...);
947 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
948public:
949 static const bool value = sizeof(__test<_Tp>(0)) == 1;
950};
951
952template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
953struct __const_pointer
954{
955 typedef typename _Alloc::const_pointer type;
956};
957
958template <class _Tp, class _Ptr, class _Alloc>
959struct __const_pointer<_Tp, _Ptr, _Alloc, false>
960{
961#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
962 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
963#else
964 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
965#endif
966};
967
968template <class _Tp>
969struct __has_void_pointer
970{
971private:
972 struct __two {char _; char __;};
973 template <class _Up> static __two __test(...);
974 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
975public:
976 static const bool value = sizeof(__test<_Tp>(0)) == 1;
977};
978
979template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
980struct __void_pointer
981{
982 typedef typename _Alloc::void_pointer type;
983};
984
985template <class _Ptr, class _Alloc>
986struct __void_pointer<_Ptr, _Alloc, false>
987{
988#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
989 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
990#else
991 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
992#endif
993};
994
995template <class _Tp>
996struct __has_const_void_pointer
997{
998private:
999 struct __two {char _; char __;};
1000 template <class _Up> static __two __test(...);
1001 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1002public:
1003 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1004};
1005
1006template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1007struct __const_void_pointer
1008{
1009 typedef typename _Alloc::const_void_pointer type;
1010};
1011
1012template <class _Ptr, class _Alloc>
1013struct __const_void_pointer<_Ptr, _Alloc, false>
1014{
1015#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1016 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1017#else
1018 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1019#endif
1020};
1021
1022template <class _T>
1023inline _LIBCPP_INLINE_VISIBILITY
1024_T*
Howard Hinnant1694d232011-05-28 14:41:13 +00001025__to_raw_pointer(_T* __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001026{
1027 return __p;
1028}
1029
1030template <class _Pointer>
1031inline _LIBCPP_INLINE_VISIBILITY
1032typename pointer_traits<_Pointer>::element_type*
Howard Hinnant1694d232011-05-28 14:41:13 +00001033__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001034{
1035 return _STD::__to_raw_pointer(__p.operator->());
1036}
1037
1038template <class _Tp>
1039struct __has_size_type
1040{
1041private:
1042 struct __two {char _; char __;};
1043 template <class _Up> static __two __test(...);
1044 template <class _Up> static char __test(typename _Up::size_type* = 0);
1045public:
1046 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1047};
1048
Howard Hinnant47761072010-11-18 01:40:00 +00001049template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001050struct __size_type
1051{
Howard Hinnant47761072010-11-18 01:40:00 +00001052 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001053};
1054
Howard Hinnant47761072010-11-18 01:40:00 +00001055template <class _Alloc, class _DiffType>
1056struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001057{
1058 typedef typename _Alloc::size_type type;
1059};
1060
1061template <class _Tp>
1062struct __has_propagate_on_container_copy_assignment
1063{
1064private:
1065 struct __two {char _; char __;};
1066 template <class _Up> static __two __test(...);
1067 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1068public:
1069 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1070};
1071
1072template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1073struct __propagate_on_container_copy_assignment
1074{
1075 typedef false_type type;
1076};
1077
1078template <class _Alloc>
1079struct __propagate_on_container_copy_assignment<_Alloc, true>
1080{
1081 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1082};
1083
1084template <class _Tp>
1085struct __has_propagate_on_container_move_assignment
1086{
1087private:
1088 struct __two {char _; char __;};
1089 template <class _Up> static __two __test(...);
1090 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1091public:
1092 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1093};
1094
1095template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1096struct __propagate_on_container_move_assignment
1097{
1098 typedef false_type type;
1099};
1100
1101template <class _Alloc>
1102struct __propagate_on_container_move_assignment<_Alloc, true>
1103{
1104 typedef typename _Alloc::propagate_on_container_move_assignment type;
1105};
1106
1107template <class _Tp>
1108struct __has_propagate_on_container_swap
1109{
1110private:
1111 struct __two {char _; char __;};
1112 template <class _Up> static __two __test(...);
1113 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1114public:
1115 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1116};
1117
1118template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1119struct __propagate_on_container_swap
1120{
1121 typedef false_type type;
1122};
1123
1124template <class _Alloc>
1125struct __propagate_on_container_swap<_Alloc, true>
1126{
1127 typedef typename _Alloc::propagate_on_container_swap type;
1128};
1129
1130template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1131struct __has_rebind_other
1132{
1133private:
1134 struct __two {char _; char __;};
1135 template <class _Xp> static __two __test(...);
1136 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1137public:
1138 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1139};
1140
1141template <class _Tp, class _Up>
1142struct __has_rebind_other<_Tp, _Up, false>
1143{
1144 static const bool value = false;
1145};
1146
1147template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1148struct __allocator_traits_rebind
1149{
1150 typedef typename _Tp::template rebind<_Up>::other type;
1151};
1152
1153#ifndef _LIBCPP_HAS_NO_VARIADICS
1154
1155template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1156struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1157{
1158 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1159};
1160
1161template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1162struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1163{
1164 typedef _Alloc<_Up, _Args...> type;
1165};
1166
Howard Hinnant324bb032010-08-22 00:02:43 +00001167#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001168
1169template <template <class> class _Alloc, class _Tp, class _Up>
1170struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1171{
1172 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1173};
1174
1175template <template <class> class _Alloc, class _Tp, class _Up>
1176struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1177{
1178 typedef _Alloc<_Up> type;
1179};
1180
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001181template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1182struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1183{
1184 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1185};
1186
1187template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1188struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1189{
1190 typedef _Alloc<_Up, _A0> type;
1191};
1192
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001193template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1194 class _A1, class _Up>
1195struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1196{
1197 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1198};
1199
1200template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1201 class _A1, class _Up>
1202struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1203{
1204 typedef _Alloc<_Up, _A0, _A1> type;
1205};
1206
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001207template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1208 class _A1, class _A2, class _Up>
1209struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1210{
1211 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1212};
1213
1214template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1215 class _A1, class _A2, class _Up>
1216struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1217{
1218 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1219};
1220
Howard Hinnant324bb032010-08-22 00:02:43 +00001221#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001222
1223#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1224
1225template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1226auto
1227__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1228 -> decltype(__a.allocate(__sz, __p), true_type());
1229
1230template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1231auto
1232__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1233 -> false_type;
1234
1235template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1236struct __has_allocate_hint
1237 : integral_constant<bool,
1238 is_same<
1239 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1240 declval<_SizeType>(),
1241 declval<_ConstVoidPtr>())),
1242 true_type>::value>
1243{
1244};
1245
Howard Hinnant324bb032010-08-22 00:02:43 +00001246#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001247
1248template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1249struct __has_allocate_hint
1250 : true_type
1251{
1252};
1253
Howard Hinnant324bb032010-08-22 00:02:43 +00001254#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001255
1256#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1257
1258template <class _Alloc, class _Tp, class ..._Args>
1259decltype(_STD::declval<_Alloc>().construct(_STD::declval<_Tp*>(),
1260 _STD::declval<_Args>()...),
1261 true_type())
1262__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1263
1264template <class _Alloc, class _Pointer, class ..._Args>
1265false_type
1266__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1267
1268template <class _Alloc, class _Pointer, class ..._Args>
1269struct __has_construct
1270 : integral_constant<bool,
1271 is_same<
1272 decltype(__has_construct_test(declval<_Alloc>(),
1273 declval<_Pointer>(),
1274 declval<_Args>()...)),
1275 true_type>::value>
1276{
1277};
1278
1279template <class _Alloc, class _Pointer>
1280auto
1281__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1282 -> decltype(__a.destroy(__p), true_type());
1283
1284template <class _Alloc, class _Pointer>
1285auto
1286__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1287 -> false_type;
1288
1289template <class _Alloc, class _Pointer>
1290struct __has_destroy
1291 : integral_constant<bool,
1292 is_same<
1293 decltype(__has_destroy_test(declval<_Alloc>(),
1294 declval<_Pointer>())),
1295 true_type>::value>
1296{
1297};
1298
1299template <class _Alloc>
1300auto
1301__has_max_size_test(_Alloc&& __a)
1302 -> decltype(__a.max_size(), true_type());
1303
1304template <class _Alloc>
1305auto
1306__has_max_size_test(const volatile _Alloc& __a)
1307 -> false_type;
1308
1309template <class _Alloc>
1310struct __has_max_size
1311 : integral_constant<bool,
1312 is_same<
1313 decltype(__has_max_size_test(declval<_Alloc&>())),
1314 true_type>::value>
1315{
1316};
1317
1318template <class _Alloc>
1319auto
1320__has_select_on_container_copy_construction_test(_Alloc&& __a)
1321 -> decltype(__a.select_on_container_copy_construction(), true_type());
1322
1323template <class _Alloc>
1324auto
1325__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1326 -> false_type;
1327
1328template <class _Alloc>
1329struct __has_select_on_container_copy_construction
1330 : integral_constant<bool,
1331 is_same<
1332 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1333 true_type>::value>
1334{
1335};
1336
Howard Hinnant324bb032010-08-22 00:02:43 +00001337#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001338
1339#ifndef _LIBCPP_HAS_NO_VARIADICS
1340
1341template <class _Alloc, class _Pointer, class ..._Args>
1342struct __has_construct
1343 : false_type
1344{
1345};
1346
Howard Hinnant324bb032010-08-22 00:02:43 +00001347#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001348
1349template <class _Alloc, class _Pointer>
1350struct __has_destroy
1351 : false_type
1352{
1353};
1354
1355template <class _Alloc>
1356struct __has_max_size
1357 : true_type
1358{
1359};
1360
1361template <class _Alloc>
1362struct __has_select_on_container_copy_construction
1363 : false_type
1364{
1365};
1366
Howard Hinnant324bb032010-08-22 00:02:43 +00001367#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001368
Howard Hinnant47761072010-11-18 01:40:00 +00001369template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1370struct __alloc_traits_difference_type
1371{
1372 typedef typename pointer_traits<_Ptr>::difference_type type;
1373};
1374
1375template <class _Alloc, class _Ptr>
1376struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1377{
1378 typedef typename _Alloc::difference_type type;
1379};
1380
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001381template <class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001382struct _LIBCPP_VISIBLE allocator_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001383{
1384 typedef _Alloc allocator_type;
1385 typedef typename allocator_type::value_type value_type;
1386
1387 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1388 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1389 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1390 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1391
Howard Hinnant47761072010-11-18 01:40:00 +00001392 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1393 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001394
1395 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1396 propagate_on_container_copy_assignment;
1397 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1398 propagate_on_container_move_assignment;
1399 typedef typename __propagate_on_container_swap<allocator_type>::type
1400 propagate_on_container_swap;
1401
1402#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1403 template <class _Tp> using rebind_alloc =
Howard Hinnant6b41c602011-05-11 20:21:19 +00001404 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001405 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant324bb032010-08-22 00:02:43 +00001406#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001407 template <class _Tp> struct rebind_alloc
1408 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1409 template <class _Tp> struct rebind_traits
1410 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant324bb032010-08-22 00:02:43 +00001411#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001412
Howard Hinnant82894812010-09-22 16:48:34 +00001413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001414 static pointer allocate(allocator_type& __a, size_type __n)
1415 {return __a.allocate(__n);}
Howard Hinnant82894812010-09-22 16:48:34 +00001416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001417 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1418 {return allocate(__a, __n, __hint,
1419 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1420
Howard Hinnant82894812010-09-22 16:48:34 +00001421 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001422 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001423 {__a.deallocate(__p, __n);}
1424
1425#ifndef _LIBCPP_HAS_NO_VARIADICS
1426 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001427 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001428 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
1429 {__construct(__has_construct<allocator_type, pointer, _Args...>(),
1430 __a, __p, _STD::forward<_Args>(__args)...);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001431#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001432 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001433 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001434 static void construct(allocator_type& __a, _Tp* __p)
1435 {
1436 ::new ((void*)__p) _Tp();
1437 }
1438 template <class _Tp, class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00001439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001440 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1441 {
1442 ::new ((void*)__p) _Tp(__a0);
1443 }
1444 template <class _Tp, class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00001445 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001446 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1447 const _A1& __a1)
1448 {
1449 ::new ((void*)__p) _Tp(__a0, __a1);
1450 }
1451 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00001452 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001453 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1454 const _A1& __a1, const _A2& __a2)
1455 {
1456 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1457 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001458#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001459
1460 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001461 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001462 static void destroy(allocator_type& __a, _Tp* __p)
1463 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1464
Howard Hinnant82894812010-09-22 16:48:34 +00001465 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001466 static size_type max_size(const allocator_type& __a)
1467 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1468
Howard Hinnant82894812010-09-22 16:48:34 +00001469 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001470 static allocator_type
1471 select_on_container_copy_construction(const allocator_type& __a)
1472 {return select_on_container_copy_construction(
1473 __has_select_on_container_copy_construction<const allocator_type>(),
1474 __a);}
1475
1476private:
1477
Howard Hinnant82894812010-09-22 16:48:34 +00001478 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001479 static pointer allocate(allocator_type& __a, size_type __n,
1480 const_void_pointer __hint, true_type)
1481 {return __a.allocate(__n, __hint);}
Howard Hinnant82894812010-09-22 16:48:34 +00001482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001483 static pointer allocate(allocator_type& __a, size_type __n,
1484 const_void_pointer __hint, false_type)
1485 {return __a.allocate(__n);}
1486
1487#ifndef _LIBCPP_HAS_NO_VARIADICS
1488 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001490 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
1491 {__a.construct(__p, _STD::forward<_Args>(__args)...);}
1492 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001493 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001494 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1495 {
1496 ::new ((void*)__p) _Tp(_STD::forward<_Args>(__args)...);
1497 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001498#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001499
1500 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001501 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001502 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1503 {__a.destroy(__p);}
1504 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001505 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001506 static void __destroy(false_type, allocator_type&, _Tp* __p)
1507 {
1508 __p->~_Tp();
1509 }
1510
Howard Hinnant82894812010-09-22 16:48:34 +00001511 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001512 static size_type __max_size(true_type, const allocator_type& __a)
1513 {return __a.max_size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001514 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001515 static size_type __max_size(false_type, const allocator_type&)
1516 {return numeric_limits<size_type>::max();}
1517
Howard Hinnant82894812010-09-22 16:48:34 +00001518 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001519 static allocator_type
1520 select_on_container_copy_construction(true_type, const allocator_type& __a)
1521 {return __a.select_on_container_copy_construction();}
Howard Hinnant82894812010-09-22 16:48:34 +00001522 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001523 static allocator_type
1524 select_on_container_copy_construction(false_type, const allocator_type& __a)
1525 {return __a;}
1526};
1527
1528// uses_allocator
1529
1530template <class _Tp>
1531struct __has_allocator_type
1532{
1533private:
1534 struct __two {char _; char __;};
1535 template <class _Up> static __two __test(...);
1536 template <class _Up> static char __test(typename _Up::allocator_type* = 0);
1537public:
1538 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1539};
1540
1541template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
1542struct __uses_allocator
1543 : public integral_constant<bool,
1544 is_convertible<_Alloc, typename _Tp::allocator_type>::value>
1545{
1546};
1547
1548template <class _Tp, class _Alloc>
1549struct __uses_allocator<_Tp, _Alloc, false>
1550 : public false_type
1551{
1552};
1553
1554template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001555struct _LIBCPP_VISIBLE uses_allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001556 : public __uses_allocator<_Tp, _Alloc>
1557{
1558};
1559
Howard Hinnantac38bae2011-01-11 20:02:45 +00001560#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001561
1562// uses-allocator construction
1563
1564template <class _Tp, class _Alloc, class ..._Args>
1565struct __uses_alloc_ctor_imp
1566{
1567 static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
1568 static const bool __ic =
1569 is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
1570 static const int value = __ua ? 2 - __ic : 0;
1571};
1572
1573template <class _Tp, class _Alloc, class ..._Args>
1574struct __uses_alloc_ctor
1575 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
1576 {};
1577
Howard Hinnantac38bae2011-01-11 20:02:45 +00001578#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001579
1580// allocator
1581
1582template <class _Tp>
Howard Hinnant36cdf022010-09-10 16:42:26 +00001583class _LIBCPP_VISIBLE allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001584{
1585public:
1586 typedef size_t size_type;
1587 typedef ptrdiff_t difference_type;
1588 typedef _Tp* pointer;
1589 typedef const _Tp* const_pointer;
1590 typedef _Tp& reference;
1591 typedef const _Tp& const_reference;
1592 typedef _Tp value_type;
1593
Howard Hinnant18884f42011-06-02 21:38:57 +00001594 typedef true_type propagate_on_container_move_assignment;
1595
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001596 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1597
Howard Hinnant1694d232011-05-28 14:41:13 +00001598 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1599 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1600 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
1601 {return _STD::addressof(__x);}
1602 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1603 {return _STD::addressof(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001604 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1605 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
Howard Hinnant1694d232011-05-28 14:41:13 +00001606 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
1607 {::operator delete((void*)__p);}
1608 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1609 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001610#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611 template <class _Up, class... _Args>
1612 _LIBCPP_INLINE_VISIBILITY
1613 void
1614 construct(_Up* __p, _Args&&... __args)
1615 {
1616 ::new((void*)__p) _Up(_STD::forward<_Args>(__args)...);
1617 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001618#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001619 _LIBCPP_INLINE_VISIBILITY
1620 void
1621 construct(pointer __p)
1622 {
1623 ::new((void*)__p) _Tp();
1624 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001625# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001626 template <class _A0>
1627 _LIBCPP_INLINE_VISIBILITY
1628 typename enable_if
1629 <
1630 !is_convertible<_A0, __rv<_A0> >::value,
1631 void
1632 >::type
1633 construct(pointer __p, _A0& __a0)
1634 {
1635 ::new((void*)__p) _Tp(__a0);
1636 }
1637 template <class _A0>
1638 _LIBCPP_INLINE_VISIBILITY
1639 typename enable_if
1640 <
1641 !is_convertible<_A0, __rv<_A0> >::value,
1642 void
1643 >::type
1644 construct(pointer __p, const _A0& __a0)
1645 {
1646 ::new((void*)__p) _Tp(__a0);
1647 }
1648 template <class _A0>
1649 _LIBCPP_INLINE_VISIBILITY
1650 typename enable_if
1651 <
1652 is_convertible<_A0, __rv<_A0> >::value,
1653 void
1654 >::type
1655 construct(pointer __p, _A0 __a0)
1656 {
1657 ::new((void*)__p) _Tp(_STD::move(__a0));
1658 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001659# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001660 template <class _A0, class _A1>
1661 _LIBCPP_INLINE_VISIBILITY
1662 void
1663 construct(pointer __p, _A0& __a0, _A1& __a1)
1664 {
1665 ::new((void*)__p) _Tp(__a0, __a1);
1666 }
1667 template <class _A0, class _A1>
1668 _LIBCPP_INLINE_VISIBILITY
1669 void
1670 construct(pointer __p, const _A0& __a0, _A1& __a1)
1671 {
1672 ::new((void*)__p) _Tp(__a0, __a1);
1673 }
1674 template <class _A0, class _A1>
1675 _LIBCPP_INLINE_VISIBILITY
1676 void
1677 construct(pointer __p, _A0& __a0, const _A1& __a1)
1678 {
1679 ::new((void*)__p) _Tp(__a0, __a1);
1680 }
1681 template <class _A0, class _A1>
1682 _LIBCPP_INLINE_VISIBILITY
1683 void
1684 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1685 {
1686 ::new((void*)__p) _Tp(__a0, __a1);
1687 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001688#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001689 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1690};
1691
1692template <class _Tp, class _Up>
1693inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001694bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001695
1696template <class _Tp, class _Up>
1697inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001698bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001699
1700template <class _OutputIterator, class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001701class _LIBCPP_VISIBLE raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001702 : public iterator<output_iterator_tag,
1703 _Tp, // purposefully not C++03
1704 ptrdiff_t, // purposefully not C++03
1705 _Tp*, // purposefully not C++03
1706 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1707{
1708private:
1709 _OutputIterator __x_;
1710public:
1711 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1712 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1713 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1714 {::new(&*__x_) _Tp(__element); return *this;}
1715 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1716 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1717 {raw_storage_iterator __t(*this); ++__x_; return __t;}
1718};
1719
1720template <class _Tp>
1721pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00001722get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001723{
1724 pair<_Tp*, ptrdiff_t> __r(0, 0);
1725 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1726 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1727 / sizeof(_Tp);
1728 if (__n > __m)
1729 __n = __m;
1730 while (__n > 0)
1731 {
1732 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1733 if (__r.first)
1734 {
1735 __r.second = __n;
1736 break;
1737 }
1738 __n /= 2;
1739 }
1740 return __r;
1741}
1742
1743template <class _Tp>
1744inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001745void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001746
1747template <class _Tp>
1748struct auto_ptr_ref
1749{
1750 _Tp* __ptr_;
1751};
1752
1753template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001754class _LIBCPP_VISIBLE auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001755{
1756private:
1757 _Tp* __ptr_;
1758public:
1759 typedef _Tp element_type;
1760
1761 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1762 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1763 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1764 : __ptr_(__p.release()) {}
1765 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1766 {reset(__p.release()); return *this;}
1767 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1768 {reset(__p.release()); return *this;}
1769 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1770 {reset(__p.__ptr_); return *this;}
1771 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1772
1773 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1774 {return *__ptr_;}
1775 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1776 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1777 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1778 {
1779 _Tp* __t = __ptr_;
1780 __ptr_ = 0;
1781 return __t;
1782 }
1783 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1784 {
1785 if (__ptr_ != __p)
1786 delete __ptr_;
1787 __ptr_ = __p;
1788 }
1789
1790 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1791 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1792 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1793 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1794 {return auto_ptr<_Up>(release());}
1795};
1796
1797template <>
Howard Hinnant82894812010-09-22 16:48:34 +00001798class _LIBCPP_VISIBLE auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001799{
1800public:
1801 typedef void element_type;
1802};
1803
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001804template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1805 typename remove_cv<_T2>::type>::value,
1806 bool = is_empty<_T1>::value,
1807 bool = is_empty<_T2>::value>
1808struct __libcpp_compressed_pair_switch;
1809
1810template <class _T1, class _T2, bool IsSame>
1811struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1812
1813template <class _T1, class _T2, bool IsSame>
1814struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1815
1816template <class _T1, class _T2, bool IsSame>
1817struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1818
1819template <class _T1, class _T2>
1820struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
1821
1822template <class _T1, class _T2>
1823struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
1824
1825template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
1826class __libcpp_compressed_pair_imp;
1827
1828template <class _T1, class _T2>
1829class __libcpp_compressed_pair_imp<_T1, _T2, 0>
1830{
1831private:
1832 _T1 __first_;
1833 _T2 __second_;
1834public:
1835 typedef _T1 _T1_param;
1836 typedef _T2 _T2_param;
1837
1838 typedef typename remove_reference<_T1>::type& _T1_reference;
1839 typedef typename remove_reference<_T2>::type& _T2_reference;
1840
1841 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1842 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1843
1844 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1845 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
1846 : __first_(_STD::forward<_T1_param>(__t1)) {}
1847 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
1848 : __second_(_STD::forward<_T2_param>(__t2)) {}
1849 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1850 : __first_(_STD::forward<_T1_param>(__t1)), __second_(_STD::forward<_T2_param>(__t2)) {}
1851
Howard Hinnant73d21a42010-09-04 23:28:19 +00001852#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001853 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00001854 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
1855 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001856 : __first_(_STD::forward<_T1>(__p.first())), __second_(_STD::forward<_T2>(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001857#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001858
Howard Hinnant1694d232011-05-28 14:41:13 +00001859 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
1860 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001861
Howard Hinnant1694d232011-05-28 14:41:13 +00001862 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
1863 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001864
1865 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00001866 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1867 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001868 {
1869 using _STD::swap;
1870 swap(__first_, __x.__first_);
1871 swap(__second_, __x.__second_);
1872 }
1873};
1874
1875template <class _T1, class _T2>
1876class __libcpp_compressed_pair_imp<_T1, _T2, 1>
1877 : private _T1
1878{
1879private:
1880 _T2 __second_;
1881public:
1882 typedef _T1 _T1_param;
1883 typedef _T2 _T2_param;
1884
1885 typedef _T1& _T1_reference;
1886 typedef typename remove_reference<_T2>::type& _T2_reference;
1887
1888 typedef const _T1& _T1_const_reference;
1889 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1890
1891 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1892 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
1893 : _T1(_STD::forward<_T1_param>(__t1)) {}
1894 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
1895 : __second_(_STD::forward<_T2_param>(__t2)) {}
1896 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1897 : _T1(_STD::forward<_T1_param>(__t1)), __second_(_STD::forward<_T2_param>(__t2)) {}
1898
Howard Hinnant73d21a42010-09-04 23:28:19 +00001899#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001900 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00001901 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
1902 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001903 : _T1(_STD::move(__p.first())), __second_(_STD::forward<_T2>(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001904#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001905
Howard Hinnant1694d232011-05-28 14:41:13 +00001906 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
1907 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001908
Howard Hinnant1694d232011-05-28 14:41:13 +00001909 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
1910 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001911
1912 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00001913 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1914 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001915 {
1916 using _STD::swap;
1917 swap(__second_, __x.__second_);
1918 }
1919};
1920
1921template <class _T1, class _T2>
1922class __libcpp_compressed_pair_imp<_T1, _T2, 2>
1923 : private _T2
1924{
1925private:
1926 _T1 __first_;
1927public:
1928 typedef _T1 _T1_param;
1929 typedef _T2 _T2_param;
1930
1931 typedef typename remove_reference<_T1>::type& _T1_reference;
1932 typedef _T2& _T2_reference;
1933
1934 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1935 typedef const _T2& _T2_const_reference;
1936
1937 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1938 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
1939 : __first_(_STD::forward<_T1_param>(__t1)) {}
1940 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
1941 : _T2(_STD::forward<_T2_param>(__t2)) {}
1942 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00001943 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
1944 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001945 : _T2(_STD::forward<_T2_param>(__t2)), __first_(_STD::forward<_T1_param>(__t1)) {}
1946
Howard Hinnant73d21a42010-09-04 23:28:19 +00001947#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001948 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1949 : _T2(_STD::forward<_T2>(__p.second())), __first_(_STD::move(__p.first())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001950#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001951
Howard Hinnant1694d232011-05-28 14:41:13 +00001952 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
1953 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001954
Howard Hinnant1694d232011-05-28 14:41:13 +00001955 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
1956 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001957
1958 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00001959 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1960 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001961 {
1962 using _STD::swap;
1963 swap(__first_, __x.__first_);
1964 }
1965};
1966
1967template <class _T1, class _T2>
1968class __libcpp_compressed_pair_imp<_T1, _T2, 3>
1969 : private _T1,
1970 private _T2
1971{
1972public:
1973 typedef _T1 _T1_param;
1974 typedef _T2 _T2_param;
1975
1976 typedef _T1& _T1_reference;
1977 typedef _T2& _T2_reference;
1978
1979 typedef const _T1& _T1_const_reference;
1980 typedef const _T2& _T2_const_reference;
1981
1982 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1983 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
1984 : _T1(_STD::forward<_T1_param>(__t1)) {}
1985 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
1986 : _T2(_STD::forward<_T2_param>(__t2)) {}
1987 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1988 : _T1(_STD::forward<_T1_param>(__t1)), _T2(_STD::forward<_T2_param>(__t2)) {}
1989
Howard Hinnant73d21a42010-09-04 23:28:19 +00001990#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001991 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00001992 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
1993 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001994 : _T1(_STD::move(__p.first())), _T2(_STD::move(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001995#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001996
Howard Hinnant1694d232011-05-28 14:41:13 +00001997 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
1998 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001999
Howard Hinnant1694d232011-05-28 14:41:13 +00002000 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2001 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002002
2003 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002004 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2005 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002006 {
2007 }
2008};
2009
2010template <class _T1, class _T2>
2011class __compressed_pair
2012 : private __libcpp_compressed_pair_imp<_T1, _T2>
2013{
2014 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2015public:
2016 typedef typename base::_T1_param _T1_param;
2017 typedef typename base::_T2_param _T2_param;
2018
2019 typedef typename base::_T1_reference _T1_reference;
2020 typedef typename base::_T2_reference _T2_reference;
2021
2022 typedef typename base::_T1_const_reference _T1_const_reference;
2023 typedef typename base::_T2_const_reference _T2_const_reference;
2024
2025 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
2026 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1, int = 0)
2027 : base(_STD::forward<_T1_param>(__t1)) {}
2028 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2, int* = 0)
2029 : base(_STD::forward<_T2_param>(__t2)) {}
2030 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
2031 : base(_STD::forward<_T1_param>(__t1), _STD::forward<_T2_param>(__t2)) {}
2032
Howard Hinnant73d21a42010-09-04 23:28:19 +00002033#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002034 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002035 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2036 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002037 : base(_STD::move(__p)) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002038#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002039
Howard Hinnant1694d232011-05-28 14:41:13 +00002040 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2041 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002042
Howard Hinnant1694d232011-05-28 14:41:13 +00002043 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2044 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002045
Howard Hinnant1694d232011-05-28 14:41:13 +00002046 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2047 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2048 __is_nothrow_swappable<_T1>::value)
2049 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002050};
2051
2052template <class _T1, class _T2>
2053inline _LIBCPP_INLINE_VISIBILITY
2054void
2055swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002056 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2057 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002058 {__x.swap(__y);}
2059
2060template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002061struct _LIBCPP_VISIBLE default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002062{
Howard Hinnant1694d232011-05-28 14:41:13 +00002063 _LIBCPP_INLINE_VISIBILITY default_delete() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002064 template <class _Up>
2065 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002066 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2067 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002068 {
2069 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2070 delete __ptr;
2071 }
2072};
2073
2074template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002075struct _LIBCPP_VISIBLE default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002076{
Howard Hinnant1694d232011-05-28 14:41:13 +00002077 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002078 {
2079 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2080 delete [] __ptr;
2081 }
2082private:
2083 template <class _Up> void operator() (_Up*) const;
2084};
2085
2086template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant82894812010-09-22 16:48:34 +00002087class _LIBCPP_VISIBLE unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002088{
2089public:
2090 typedef _Tp element_type;
2091 typedef _Dp deleter_type;
2092 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2093private:
2094 __compressed_pair<pointer, deleter_type> __ptr_;
2095
Howard Hinnant73d21a42010-09-04 23:28:19 +00002096#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002097 unique_ptr(const unique_ptr&);
2098 unique_ptr& operator=(const unique_ptr&);
2099 template <class _Up, class _Ep>
2100 unique_ptr(const unique_ptr<_Up, _Ep>&);
2101 template <class _Up, class _Ep>
2102 unique_ptr& operator=(const unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002103#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002104 unique_ptr(unique_ptr&);
2105 template <class _Up, class _Ep>
2106 unique_ptr(unique_ptr<_Up, _Ep>&);
2107 unique_ptr& operator=(unique_ptr&);
2108 template <class _Up, class _Ep>
2109 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002110#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002111
2112 struct __nat {int __for_bool_;};
2113
2114 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2115 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2116public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002117 _LIBCPP_INLINE_VISIBILITY unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002118 : __ptr_(pointer())
2119 {
2120 static_assert(!is_pointer<deleter_type>::value,
2121 "unique_ptr constructed with null function pointer deleter");
2122 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002123 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002124 : __ptr_(pointer())
2125 {
2126 static_assert(!is_pointer<deleter_type>::value,
2127 "unique_ptr constructed with null function pointer deleter");
2128 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002129 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002130 : __ptr_(_STD::move(__p))
2131 {
2132 static_assert(!is_pointer<deleter_type>::value,
2133 "unique_ptr constructed with null function pointer deleter");
2134 }
2135
Howard Hinnant73d21a42010-09-04 23:28:19 +00002136#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002137 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2138 is_reference<deleter_type>::value,
2139 deleter_type,
2140 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002141 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002142 : __ptr_(__p, __d) {}
2143
2144 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002145 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002146 : __ptr_(__p, _STD::move(__d))
2147 {
2148 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2149 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002150 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002151 : __ptr_(__u.release(), _STD::forward<deleter_type>(__u.get_deleter())) {}
2152 template <class _Up, class _Ep>
2153 _LIBCPP_INLINE_VISIBILITY
2154 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2155 typename enable_if
2156 <
2157 !is_array<_Up>::value &&
2158 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2159 is_convertible<_Ep, deleter_type>::value &&
2160 (
2161 !is_reference<deleter_type>::value ||
2162 is_same<deleter_type, _Ep>::value
2163 ),
2164 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002165 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002166 : __ptr_(__u.release(), _STD::forward<_Ep>(__u.get_deleter())) {}
2167
2168 template <class _Up>
2169 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2170 typename enable_if<
2171 is_convertible<_Up*, _Tp*>::value &&
2172 is_same<_Dp, default_delete<_Tp> >::value,
2173 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002174 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002175 : __ptr_(__p.release())
2176 {
2177 }
2178
Howard Hinnant1694d232011-05-28 14:41:13 +00002179 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002180 {
2181 reset(__u.release());
2182 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2183 return *this;
2184 }
2185
2186 template <class _Up, class _Ep>
2187 _LIBCPP_INLINE_VISIBILITY
2188 typename enable_if
2189 <
2190 !is_array<_Up>::value,
2191 unique_ptr&
2192 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002193 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002194 {
2195 reset(__u.release());
2196 __ptr_.second() = _STD::forward<_Ep>(__u.get_deleter());
2197 return *this;
2198 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002199#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002200
2201 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2202 {
2203 return __rv<unique_ptr>(*this);
2204 }
2205
2206 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
2207 : __ptr_(__u->release(), _STD::forward<deleter_type>(__u->get_deleter())) {}
2208
2209 template <class _Up, class _Ep>
2210 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2211 {
2212 reset(__u.release());
2213 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2214 return *this;
2215 }
2216
2217 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
2218 : __ptr_(_STD::move(__p), _STD::move(__d)) {}
2219
2220 template <class _Up>
2221 _LIBCPP_INLINE_VISIBILITY
2222 typename enable_if<
2223 is_convertible<_Up*, _Tp*>::value &&
2224 is_same<_Dp, default_delete<_Tp> >::value,
2225 unique_ptr&
2226 >::type
2227 operator=(auto_ptr<_Up> __p)
2228 {reset(__p.release()); return *this;}
2229
Howard Hinnant73d21a42010-09-04 23:28:19 +00002230#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002231 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2232
Howard Hinnant1694d232011-05-28 14:41:13 +00002233 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002234 {
2235 reset();
2236 return *this;
2237 }
2238
2239 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2240 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002241 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2242 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2243 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2244 {return __ptr_.second();}
2245 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2246 {return __ptr_.second();}
2247 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const
2248 _NOEXCEPT
2249 {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002250
Howard Hinnant1694d232011-05-28 14:41:13 +00002251 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002252 {
2253 pointer __t = __ptr_.first();
2254 __ptr_.first() = pointer();
2255 return __t;
2256 }
2257
Howard Hinnant1694d232011-05-28 14:41:13 +00002258 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002259 {
2260 pointer __tmp = __ptr_.first();
2261 __ptr_.first() = __p;
2262 if (__tmp)
2263 __ptr_.second()(__tmp);
2264 }
2265
Howard Hinnant1694d232011-05-28 14:41:13 +00002266 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2267 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002268};
2269
2270template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002271class _LIBCPP_VISIBLE unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002272{
2273public:
2274 typedef _Tp element_type;
2275 typedef _Dp deleter_type;
2276 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2277private:
2278 __compressed_pair<pointer, deleter_type> __ptr_;
2279
Howard Hinnant73d21a42010-09-04 23:28:19 +00002280#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002281 unique_ptr(const unique_ptr&);
2282 unique_ptr& operator=(const unique_ptr&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002283#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002284 unique_ptr(unique_ptr&);
2285 template <class _Up>
2286 unique_ptr(unique_ptr<_Up>&);
2287 unique_ptr& operator=(unique_ptr&);
2288 template <class _Up>
2289 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002290#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002291
2292 struct __nat {int __for_bool_;};
2293
2294 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2295 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2296public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002297 _LIBCPP_INLINE_VISIBILITY unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002298 : __ptr_(pointer())
2299 {
2300 static_assert(!is_pointer<deleter_type>::value,
2301 "unique_ptr constructed with null function pointer deleter");
2302 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002303 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002304 : __ptr_(pointer())
2305 {
2306 static_assert(!is_pointer<deleter_type>::value,
2307 "unique_ptr constructed with null function pointer deleter");
2308 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002309#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002310 template <class _P,
2311 class = typename enable_if<is_same<_P, pointer>::value>::type
2312 >
Howard Hinnant1694d232011-05-28 14:41:13 +00002313 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_P __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002314 : __ptr_(__p)
2315 {
2316 static_assert(!is_pointer<deleter_type>::value,
2317 "unique_ptr constructed with null function pointer deleter");
2318 }
2319
2320 template <class _P,
2321 class = typename enable_if<is_same<_P, pointer>::value>::type
2322 >
2323 _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename conditional<
2324 is_reference<deleter_type>::value,
2325 deleter_type,
2326 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002327 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002328 : __ptr_(__p, __d) {}
2329
2330 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2331 is_reference<deleter_type>::value,
2332 deleter_type,
2333 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002334 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002335 : __ptr_(pointer(), __d) {}
2336
2337 template <class _P,
2338 class = typename enable_if<is_same<_P, pointer>::value ||
2339 is_same<_P, nullptr_t>::value>::type
2340 >
2341 _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002342 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002343 : __ptr_(__p, _STD::move(__d))
2344 {
2345 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2346 }
2347
2348 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002349 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002350 : __ptr_(pointer(), _STD::move(__d))
2351 {
2352 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2353 }
2354
Howard Hinnant1694d232011-05-28 14:41:13 +00002355 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002356 : __ptr_(__u.release(), _STD::forward<deleter_type>(__u.get_deleter())) {}
2357
Howard Hinnant1694d232011-05-28 14:41:13 +00002358 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002359 {
2360 reset(__u.release());
2361 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2362 return *this;
2363 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002364#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002365
2366 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2367 : __ptr_(__p)
2368 {
2369 static_assert(!is_pointer<deleter_type>::value,
2370 "unique_ptr constructed with null function pointer deleter");
2371 }
2372
2373 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
2374 : __ptr_(__p, _STD::forward<deleter_type>(__d)) {}
2375
2376 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
2377 : __ptr_(pointer(), _STD::forward<deleter_type>(__d)) {}
2378
2379 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2380 {
2381 return __rv<unique_ptr>(*this);
2382 }
2383
2384 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
2385 : __ptr_(__u->release(), _STD::forward<deleter_type>(__u->get_deleter())) {}
2386
2387 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2388 {
2389 reset(__u->release());
2390 __ptr_.second() = _STD::forward<deleter_type>(__u->get_deleter());
2391 return *this;
2392 }
2393
Howard Hinnant73d21a42010-09-04 23:28:19 +00002394#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002395 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2396
Howard Hinnant1694d232011-05-28 14:41:13 +00002397 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002398 {
2399 reset();
2400 return *this;
2401 }
2402
2403 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2404 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002405 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2406 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2407 {return __ptr_.second();}
2408 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2409 {return __ptr_.second();}
2410 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const _NOEXCEPT
2411 {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002412
Howard Hinnant1694d232011-05-28 14:41:13 +00002413 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002414 {
2415 pointer __t = __ptr_.first();
2416 __ptr_.first() = pointer();
2417 return __t;
2418 }
2419
Howard Hinnant73d21a42010-09-04 23:28:19 +00002420#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002421 template <class _P,
2422 class = typename enable_if<is_same<_P, pointer>::value>::type
2423 >
Howard Hinnant1694d232011-05-28 14:41:13 +00002424 _LIBCPP_INLINE_VISIBILITY void reset(_P __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002425 {
2426 pointer __tmp = __ptr_.first();
2427 __ptr_.first() = __p;
2428 if (__tmp)
2429 __ptr_.second()(__tmp);
2430 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002431 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002432 {
2433 pointer __tmp = __ptr_.first();
2434 __ptr_.first() = nullptr;
2435 if (__tmp)
2436 __ptr_.second()(__tmp);
2437 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002438 _LIBCPP_INLINE_VISIBILITY void reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002439 {
2440 pointer __tmp = __ptr_.first();
2441 __ptr_.first() = nullptr;
2442 if (__tmp)
2443 __ptr_.second()(__tmp);
2444 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002445#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002446 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2447 {
2448 pointer __tmp = __ptr_.first();
2449 __ptr_.first() = __p;
2450 if (__tmp)
2451 __ptr_.second()(__tmp);
2452 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002453#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002454
2455 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2456private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002457
Howard Hinnant73d21a42010-09-04 23:28:19 +00002458#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002459 template <class _Up>
2460 explicit unique_ptr(_Up);
2461 template <class _Up>
2462 unique_ptr(_Up __u,
2463 typename conditional<
2464 is_reference<deleter_type>::value,
2465 deleter_type,
2466 typename add_lvalue_reference<const deleter_type>::type>::type,
2467 typename enable_if
2468 <
2469 is_convertible<_Up, pointer>::value,
2470 __nat
2471 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002472#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002473};
2474
2475template <class _Tp, class _Dp>
2476inline _LIBCPP_INLINE_VISIBILITY
2477void
Howard Hinnant1694d232011-05-28 14:41:13 +00002478swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002479
2480template <class _T1, class _D1, class _T2, class _D2>
2481inline _LIBCPP_INLINE_VISIBILITY
2482bool
2483operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2484
2485template <class _T1, class _D1, class _T2, class _D2>
2486inline _LIBCPP_INLINE_VISIBILITY
2487bool
2488operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2489
2490template <class _T1, class _D1, class _T2, class _D2>
2491inline _LIBCPP_INLINE_VISIBILITY
2492bool
2493operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() < __y.get();}
2494
2495template <class _T1, class _D1, class _T2, class _D2>
2496inline _LIBCPP_INLINE_VISIBILITY
2497bool
2498operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2499
2500template <class _T1, class _D1, class _T2, class _D2>
2501inline _LIBCPP_INLINE_VISIBILITY
2502bool
2503operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2504
2505template <class _T1, class _D1, class _T2, class _D2>
2506inline _LIBCPP_INLINE_VISIBILITY
2507bool
2508operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2509
Howard Hinnant21aefc32010-06-03 16:42:57 +00002510template <class> struct hash;
2511
2512template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002513struct _LIBCPP_VISIBLE hash<_Tp*>
Howard Hinnant21aefc32010-06-03 16:42:57 +00002514 : public unary_function<_Tp*, size_t>
2515{
Howard Hinnant82894812010-09-22 16:48:34 +00002516 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002517 size_t operator()(_Tp* __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00002518 {
2519 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
2520 return *__p;
2521 }
2522};
2523
2524template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002525struct _LIBCPP_VISIBLE hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00002526{
2527 typedef unique_ptr<_Tp, _Dp> argument_type;
2528 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00002529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002530 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00002531 {
2532 typedef typename argument_type::pointer pointer;
2533 return hash<pointer>()(__ptr.get());
2534 }
2535};
2536
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002537struct __destruct_n
2538{
2539private:
2540 size_t size;
2541
2542 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002543 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002544 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
2545
2546 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002547 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002548 {}
2549
Howard Hinnant1694d232011-05-28 14:41:13 +00002550 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002551 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00002552 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002553 {}
2554
Howard Hinnant1694d232011-05-28 14:41:13 +00002555 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002556 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00002557 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002558 {}
2559public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002560 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
2561 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002562
2563 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002564 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00002565 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002566
2567 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002568 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00002569 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002570
2571 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002572 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00002573 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002574};
2575
2576template <class _Alloc>
2577class __allocator_destructor
2578{
2579 typedef allocator_traits<_Alloc> __alloc_traits;
2580public:
2581 typedef typename __alloc_traits::pointer pointer;
2582 typedef typename __alloc_traits::size_type size_type;
2583private:
2584 _Alloc& __alloc_;
2585 size_type __s_;
2586public:
2587 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00002588 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002589 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00002590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002591 void operator()(pointer __p) _NOEXCEPT
2592 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002593};
2594
2595template <class _InputIterator, class _ForwardIterator>
2596_ForwardIterator
2597uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2598{
2599 __destruct_n __d(0);
2600 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2601 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2602 for (; __f != __l; ++__f, ++__r, __d.__incr((value_type*)0))
2603 ::new(&*__r) value_type(*__f);
2604 __h.release();
2605 return __r;
2606}
2607
2608template <class _InputIterator, class _Size, class _ForwardIterator>
2609_ForwardIterator
2610uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2611{
2612 __destruct_n __d(0);
2613 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2614 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2615 for (; __n > 0; ++__f, ++__r, __d.__incr((value_type*)0), --__n)
2616 ::new(&*__r) value_type(*__f);
2617 __h.release();
2618 return __r;
2619}
2620
2621template <class _ForwardIterator, class _Tp>
2622void
2623uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2624{
2625 __destruct_n __d(0);
2626 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2627 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2628 for (; __f != __l; ++__f, __d.__incr((value_type*)0))
2629 ::new(&*__f) value_type(__x);
2630 __h.release();
2631}
2632
2633template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00002634_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002635uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2636{
2637 __destruct_n __d(0);
2638 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2639 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2640 for (; __n > 0; ++__f, --__n, __d.__incr((value_type*)0))
2641 ::new(&*__f) value_type(__x);
2642 __h.release();
Howard Hinnant2f6a6272010-11-18 16:13:03 +00002643 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002644}
2645
Howard Hinnant82894812010-09-22 16:48:34 +00002646class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002647 : public std::exception
2648{
2649public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002650 virtual ~bad_weak_ptr() _NOEXCEPT;
2651 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002652};
2653
2654template<class _Tp> class weak_ptr;
2655
2656class __shared_count
2657{
2658 __shared_count(const __shared_count&);
2659 __shared_count& operator=(const __shared_count&);
2660
2661protected:
2662 long __shared_owners_;
2663 virtual ~__shared_count();
2664private:
Howard Hinnant1694d232011-05-28 14:41:13 +00002665 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002666
2667public:
Howard Hinnant82894812010-09-22 16:48:34 +00002668 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002669 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002670 : __shared_owners_(__refs) {}
2671
Howard Hinnant1694d232011-05-28 14:41:13 +00002672 void __add_shared() _NOEXCEPT;
2673 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00002674 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002675 long use_count() const _NOEXCEPT {return __shared_owners_ + 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002676};
2677
2678class __shared_weak_count
2679 : private __shared_count
2680{
2681 long __shared_weak_owners_;
2682
2683public:
Howard Hinnant82894812010-09-22 16:48:34 +00002684 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002685 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002686 : __shared_count(__refs),
2687 __shared_weak_owners_(__refs) {}
2688protected:
2689 virtual ~__shared_weak_count();
2690
2691public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002692 void __add_shared() _NOEXCEPT;
2693 void __add_weak() _NOEXCEPT;
2694 void __release_shared() _NOEXCEPT;
2695 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00002696 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002697 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
2698 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002699
Howard Hinnantd4444702010-08-11 17:04:31 +00002700#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00002701 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00002702#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002703private:
Howard Hinnant1694d232011-05-28 14:41:13 +00002704 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002705};
2706
2707template <class _Tp, class _Dp, class _Alloc>
2708class __shared_ptr_pointer
2709 : public __shared_weak_count
2710{
2711 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2712public:
Howard Hinnant82894812010-09-22 16:48:34 +00002713 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002714 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
2715 : __data_(__compressed_pair<_Tp, _Dp>(__p, _STD::move(__d)), _STD::move(__a)) {}
2716
Howard Hinnantd4444702010-08-11 17:04:31 +00002717#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00002718 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00002719#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002720
2721private:
Howard Hinnant1694d232011-05-28 14:41:13 +00002722 virtual void __on_zero_shared() _NOEXCEPT;
2723 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002724};
2725
Howard Hinnantd4444702010-08-11 17:04:31 +00002726#ifndef _LIBCPP_NO_RTTI
2727
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002728template <class _Tp, class _Dp, class _Alloc>
2729const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00002730__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002731{
2732 return __t == typeid(_Dp) ? &__data_.first().second() : 0;
2733}
2734
Howard Hinnant324bb032010-08-22 00:02:43 +00002735#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00002736
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002737template <class _Tp, class _Dp, class _Alloc>
2738void
Howard Hinnant1694d232011-05-28 14:41:13 +00002739__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002740{
2741 __data_.first().second()(__data_.first().first());
2742 __data_.first().second().~_Dp();
2743}
2744
2745template <class _Tp, class _Dp, class _Alloc>
2746void
Howard Hinnant1694d232011-05-28 14:41:13 +00002747__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002748{
2749 typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
2750 __data_.second().~_Alloc();
2751 __a.deallocate(this, 1);
2752}
2753
2754template <class _Tp, class _Alloc>
2755class __shared_ptr_emplace
2756 : public __shared_weak_count
2757{
2758 __compressed_pair<_Alloc, _Tp> __data_;
2759public:
2760#ifndef _LIBCPP_HAS_NO_VARIADICS
2761
Howard Hinnant82894812010-09-22 16:48:34 +00002762 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002763 __shared_ptr_emplace(_Alloc __a)
2764 : __data_(_STD::move(__a)) {}
2765
2766 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00002767 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002768 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
2769 : __data_(_STD::move(__a), _Tp(_STD::forward<_Args>(__args)...)) {}
2770
2771#else // _LIBCPP_HAS_NO_VARIADICS
2772
Howard Hinnant82894812010-09-22 16:48:34 +00002773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002774 __shared_ptr_emplace(_Alloc __a)
2775 : __data_(__a) {}
2776
2777 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00002778 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002779 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
2780 : __data_(__a, _Tp(__a0)) {}
2781
2782 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00002783 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002784 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
2785 : __data_(__a, _Tp(__a0, __a1)) {}
2786
2787 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00002788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002789 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
2790 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
2791
2792#endif // _LIBCPP_HAS_NO_VARIADICS
2793
2794private:
Howard Hinnant1694d232011-05-28 14:41:13 +00002795 virtual void __on_zero_shared() _NOEXCEPT;
2796 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002797public:
Howard Hinnant82894812010-09-22 16:48:34 +00002798 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002799 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002800};
2801
2802template <class _Tp, class _Alloc>
2803void
Howard Hinnant1694d232011-05-28 14:41:13 +00002804__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002805{
2806 __data_.second().~_Tp();
2807}
2808
2809template <class _Tp, class _Alloc>
2810void
Howard Hinnant1694d232011-05-28 14:41:13 +00002811__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002812{
2813 typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
2814 __data_.first().~_Alloc();
2815 __a.deallocate(this, 1);
2816}
2817
2818template<class _Tp> class enable_shared_from_this;
2819
2820template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002821class _LIBCPP_VISIBLE shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002822{
Howard Hinnant324bb032010-08-22 00:02:43 +00002823public:
2824 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002825private:
2826 element_type* __ptr_;
2827 __shared_weak_count* __cntrl_;
2828
2829 struct __nat {int __for_bool_;};
2830public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002831 shared_ptr() _NOEXCEPT;
2832 shared_ptr(nullptr_t) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002833 template<class _Yp> explicit shared_ptr(_Yp* __p);
Howard Hinnant324bb032010-08-22 00:02:43 +00002834 template<class _Yp, class _Dp> shared_ptr(_Yp* __p, _Dp __d);
2835 template<class _Yp, class _Dp, class _Alloc> shared_ptr(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002836 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2837 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant1694d232011-05-28 14:41:13 +00002838 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
2839 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002840 template<class _Yp>
2841 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00002842 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
2843 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00002844#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00002845 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002846 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00002847 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
2848 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00002849#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002850 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00002851 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002852#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002853 template<class _Yp> shared_ptr(auto_ptr<_Yp>&& __r);
2854#else
2855 template<class _Yp> shared_ptr(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002856#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00002857#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002858private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002859 template <class _Yp, class _Dp> shared_ptr(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002860public:
2861 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
2862 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2863 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
2864 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002865#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002866 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
2867 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2868 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
2869 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002870#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002871
2872 ~shared_ptr();
2873
Howard Hinnant1694d232011-05-28 14:41:13 +00002874 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
2875 template<class _Yp> shared_ptr& operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00002876#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00002877 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00002878 template<class _Yp> shared_ptr& operator=(shared_ptr<_Yp>&& __r);
2879 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002880#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002881 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002882#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00002883#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002884private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002885 template <class _Yp, class _Dp> shared_ptr& operator=(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002886public:
Howard Hinnant324bb032010-08-22 00:02:43 +00002887 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002888#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002889 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002890#endif
2891
Howard Hinnant1694d232011-05-28 14:41:13 +00002892 void swap(shared_ptr& __r) _NOEXCEPT;
2893 void reset() _NOEXCEPT;
2894 template<class _Yp> void reset(_Yp* __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002895 template<class _Yp, class _Dp> void reset(_Yp* __p, _Dp __d);
2896 template<class _Yp, class _Dp, class _Alloc> void reset(_Yp* __p, _Dp __d, _Alloc __a);
2897
Howard Hinnant82894812010-09-22 16:48:34 +00002898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002899 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002900 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002901 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
2902 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002903 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002904 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002905 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002906 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00002907 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002908 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00002909 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002910 /*explicit*/ operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00002911 template <class _U>
2912 _LIBCPP_INLINE_VISIBILITY
2913 bool owner_before(shared_ptr<_U> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002914 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002915 template <class _U>
2916 _LIBCPP_INLINE_VISIBILITY
2917 bool owner_before(weak_ptr<_U> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002918 {return __cntrl_ < __p.__cntrl_;}
2919
Howard Hinnantd4444702010-08-11 17:04:31 +00002920#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002921 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002922 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002923 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002924 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00002925#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002926
2927#ifndef _LIBCPP_HAS_NO_VARIADICS
2928
2929 template<class ..._Args>
2930 static
2931 shared_ptr<_Tp>
2932 make_shared(_Args&& ...__args);
2933
2934 template<class _Alloc, class ..._Args>
2935 static
2936 shared_ptr<_Tp>
2937 allocate_shared(const _Alloc& __a, _Args&& ...__args);
2938
2939#else // _LIBCPP_HAS_NO_VARIADICS
2940
2941 static shared_ptr<_Tp> make_shared();
2942
2943 template<class _A0>
2944 static shared_ptr<_Tp> make_shared(_A0&);
2945
2946 template<class _A0, class _A1>
2947 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
2948
2949 template<class _A0, class _A1, class _A2>
2950 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
2951
2952 template<class _Alloc>
2953 static shared_ptr<_Tp>
2954 allocate_shared(const _Alloc& __a);
2955
2956 template<class _Alloc, class _A0>
2957 static shared_ptr<_Tp>
2958 allocate_shared(const _Alloc& __a, _A0& __a0);
2959
2960 template<class _Alloc, class _A0, class _A1>
2961 static shared_ptr<_Tp>
2962 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
2963
2964 template<class _Alloc, class _A0, class _A1, class _A2>
2965 static shared_ptr<_Tp>
2966 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
2967
2968#endif // _LIBCPP_HAS_NO_VARIADICS
2969
2970private:
2971
2972 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00002973 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002974 void
Howard Hinnant1694d232011-05-28 14:41:13 +00002975 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002976 {
2977 if (__e)
2978 __e->__weak_this_ = *this;
2979 }
2980
Howard Hinnant82894812010-09-22 16:48:34 +00002981 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002982 void __enable_weak_this(const void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002983
Howard Hinnant82894812010-09-22 16:48:34 +00002984 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
2985 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002986};
2987
2988template<class _Tp>
2989inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002990shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002991 : __ptr_(0),
2992 __cntrl_(0)
2993{
2994}
2995
2996template<class _Tp>
2997inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002998shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002999 : __ptr_(0),
3000 __cntrl_(0)
3001{
3002}
3003
3004template<class _Tp>
3005template<class _Yp>
3006shared_ptr<_Tp>::shared_ptr(_Yp* __p)
3007 : __ptr_(__p)
3008{
3009 unique_ptr<_Yp> __hold(__p);
3010 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3011 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
3012 __hold.release();
3013 __enable_weak_this(__p);
3014}
3015
3016template<class _Tp>
3017template<class _Yp, class _Dp>
3018shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
3019 : __ptr_(__p)
3020{
3021#ifndef _LIBCPP_NO_EXCEPTIONS
3022 try
3023 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003024#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003025 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3026 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
3027 __enable_weak_this(__p);
3028#ifndef _LIBCPP_NO_EXCEPTIONS
3029 }
3030 catch (...)
3031 {
3032 __d(__p);
3033 throw;
3034 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003035#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003036}
3037
3038template<class _Tp>
3039template<class _Dp>
3040shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3041 : __ptr_(0)
3042{
3043#ifndef _LIBCPP_NO_EXCEPTIONS
3044 try
3045 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003046#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003047 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
3048 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
3049#ifndef _LIBCPP_NO_EXCEPTIONS
3050 }
3051 catch (...)
3052 {
3053 __d(__p);
3054 throw;
3055 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003056#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003057}
3058
3059template<class _Tp>
3060template<class _Yp, class _Dp, class _Alloc>
3061shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
3062 : __ptr_(__p)
3063{
3064#ifndef _LIBCPP_NO_EXCEPTIONS
3065 try
3066 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003067#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003068 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
3069 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3070 typedef __allocator_destructor<_A2> _D2;
3071 _A2 __a2(__a);
3072 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3073 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
3074 __cntrl_ = __hold2.release();
3075 __enable_weak_this(__p);
3076#ifndef _LIBCPP_NO_EXCEPTIONS
3077 }
3078 catch (...)
3079 {
3080 __d(__p);
3081 throw;
3082 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003083#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003084}
3085
3086template<class _Tp>
3087template<class _Dp, class _Alloc>
3088shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
3089 : __ptr_(0)
3090{
3091#ifndef _LIBCPP_NO_EXCEPTIONS
3092 try
3093 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003094#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003095 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
3096 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3097 typedef __allocator_destructor<_A2> _D2;
3098 _A2 __a2(__a);
3099 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3100 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
3101 __cntrl_ = __hold2.release();
3102#ifndef _LIBCPP_NO_EXCEPTIONS
3103 }
3104 catch (...)
3105 {
3106 __d(__p);
3107 throw;
3108 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003109#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003110}
3111
3112template<class _Tp>
3113template<class _Yp>
3114inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003115shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003116 : __ptr_(__p),
3117 __cntrl_(__r.__cntrl_)
3118{
3119 if (__cntrl_)
3120 __cntrl_->__add_shared();
3121}
3122
3123template<class _Tp>
3124inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003125shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003126 : __ptr_(__r.__ptr_),
3127 __cntrl_(__r.__cntrl_)
3128{
3129 if (__cntrl_)
3130 __cntrl_->__add_shared();
3131}
3132
3133template<class _Tp>
3134template<class _Yp>
3135inline _LIBCPP_INLINE_VISIBILITY
3136shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
3137 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003138 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003139 : __ptr_(__r.__ptr_),
3140 __cntrl_(__r.__cntrl_)
3141{
3142 if (__cntrl_)
3143 __cntrl_->__add_shared();
3144}
3145
Howard Hinnant73d21a42010-09-04 23:28:19 +00003146#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003147
3148template<class _Tp>
3149inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003150shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003151 : __ptr_(__r.__ptr_),
3152 __cntrl_(__r.__cntrl_)
3153{
3154 __r.__ptr_ = 0;
3155 __r.__cntrl_ = 0;
3156}
3157
3158template<class _Tp>
3159template<class _Yp>
3160inline _LIBCPP_INLINE_VISIBILITY
3161shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
3162 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003163 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003164 : __ptr_(__r.__ptr_),
3165 __cntrl_(__r.__cntrl_)
3166{
3167 __r.__ptr_ = 0;
3168 __r.__cntrl_ = 0;
3169}
3170
Howard Hinnant73d21a42010-09-04 23:28:19 +00003171#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003172
3173template<class _Tp>
3174template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003175#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003176shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
3177#else
Howard Hinnant92172b82010-08-21 21:14:53 +00003178shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003179#endif
3180 : __ptr_(__r.get())
3181{
3182 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3183 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
3184 __enable_weak_this(__r.get());
3185 __r.release();
3186}
3187
3188template<class _Tp>
3189template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003190#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003191shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3192#else
3193shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3194#endif
3195 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
3196 : __ptr_(__r.get())
3197{
3198 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3199 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
3200 __enable_weak_this(__r.get());
3201 __r.release();
3202}
3203
3204template<class _Tp>
3205template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003206#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003207shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3208#else
3209shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3210#endif
3211 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
3212 : __ptr_(__r.get())
3213{
3214 typedef __shared_ptr_pointer<_Yp*,
3215 reference_wrapper<typename remove_reference<_Dp>::type>,
3216 allocator<_Yp> > _CntrlBlk;
3217 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
3218 __enable_weak_this(__r.get());
3219 __r.release();
3220}
3221
3222#ifndef _LIBCPP_HAS_NO_VARIADICS
3223
3224template<class _Tp>
3225template<class ..._Args>
3226shared_ptr<_Tp>
3227shared_ptr<_Tp>::make_shared(_Args&& ...__args)
3228{
3229 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3230 typedef allocator<_CntrlBlk> _A2;
3231 typedef __allocator_destructor<_A2> _D2;
3232 _A2 __a2;
3233 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3234 ::new(__hold2.get()) _CntrlBlk(__a2, _STD::forward<_Args>(__args)...);
3235 shared_ptr<_Tp> __r;
3236 __r.__ptr_ = __hold2.get()->get();
3237 __r.__cntrl_ = __hold2.release();
3238 __r.__enable_weak_this(__r.__ptr_);
3239 return __r;
3240}
3241
3242template<class _Tp>
3243template<class _Alloc, class ..._Args>
3244shared_ptr<_Tp>
3245shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
3246{
3247 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3248 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3249 typedef __allocator_destructor<_A2> _D2;
3250 _A2 __a2(__a);
3251 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3252 ::new(__hold2.get()) _CntrlBlk(__a, _STD::forward<_Args>(__args)...);
3253 shared_ptr<_Tp> __r;
3254 __r.__ptr_ = __hold2.get()->get();
3255 __r.__cntrl_ = __hold2.release();
3256 __r.__enable_weak_this(__r.__ptr_);
3257 return __r;
3258}
3259
3260#else // _LIBCPP_HAS_NO_VARIADICS
3261
3262template<class _Tp>
3263shared_ptr<_Tp>
3264shared_ptr<_Tp>::make_shared()
3265{
3266 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3267 typedef allocator<_CntrlBlk> _Alloc2;
3268 typedef __allocator_destructor<_Alloc2> _D2;
3269 _Alloc2 __alloc2;
3270 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3271 ::new(__hold2.get()) _CntrlBlk(__alloc2);
3272 shared_ptr<_Tp> __r;
3273 __r.__ptr_ = __hold2.get()->get();
3274 __r.__cntrl_ = __hold2.release();
3275 __r.__enable_weak_this(__r.__ptr_);
3276 return __r;
3277}
3278
3279template<class _Tp>
3280template<class _A0>
3281shared_ptr<_Tp>
3282shared_ptr<_Tp>::make_shared(_A0& __a0)
3283{
3284 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3285 typedef allocator<_CntrlBlk> _Alloc2;
3286 typedef __allocator_destructor<_Alloc2> _D2;
3287 _Alloc2 __alloc2;
3288 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3289 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
3290 shared_ptr<_Tp> __r;
3291 __r.__ptr_ = __hold2.get()->get();
3292 __r.__cntrl_ = __hold2.release();
3293 __r.__enable_weak_this(__r.__ptr_);
3294 return __r;
3295}
3296
3297template<class _Tp>
3298template<class _A0, class _A1>
3299shared_ptr<_Tp>
3300shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
3301{
3302 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3303 typedef allocator<_CntrlBlk> _Alloc2;
3304 typedef __allocator_destructor<_Alloc2> _D2;
3305 _Alloc2 __alloc2;
3306 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3307 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
3308 shared_ptr<_Tp> __r;
3309 __r.__ptr_ = __hold2.get()->get();
3310 __r.__cntrl_ = __hold2.release();
3311 __r.__enable_weak_this(__r.__ptr_);
3312 return __r;
3313}
3314
3315template<class _Tp>
3316template<class _A0, class _A1, class _A2>
3317shared_ptr<_Tp>
3318shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3319{
3320 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3321 typedef allocator<_CntrlBlk> _Alloc2;
3322 typedef __allocator_destructor<_Alloc2> _D2;
3323 _Alloc2 __alloc2;
3324 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3325 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
3326 shared_ptr<_Tp> __r;
3327 __r.__ptr_ = __hold2.get()->get();
3328 __r.__cntrl_ = __hold2.release();
3329 __r.__enable_weak_this(__r.__ptr_);
3330 return __r;
3331}
3332
3333template<class _Tp>
3334template<class _Alloc>
3335shared_ptr<_Tp>
3336shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
3337{
3338 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3339 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3340 typedef __allocator_destructor<_Alloc2> _D2;
3341 _Alloc2 __alloc2(__a);
3342 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3343 ::new(__hold2.get()) _CntrlBlk(__a);
3344 shared_ptr<_Tp> __r;
3345 __r.__ptr_ = __hold2.get()->get();
3346 __r.__cntrl_ = __hold2.release();
3347 __r.__enable_weak_this(__r.__ptr_);
3348 return __r;
3349}
3350
3351template<class _Tp>
3352template<class _Alloc, class _A0>
3353shared_ptr<_Tp>
3354shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
3355{
3356 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3357 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3358 typedef __allocator_destructor<_Alloc2> _D2;
3359 _Alloc2 __alloc2(__a);
3360 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3361 ::new(__hold2.get()) _CntrlBlk(__a, __a0);
3362 shared_ptr<_Tp> __r;
3363 __r.__ptr_ = __hold2.get()->get();
3364 __r.__cntrl_ = __hold2.release();
3365 __r.__enable_weak_this(__r.__ptr_);
3366 return __r;
3367}
3368
3369template<class _Tp>
3370template<class _Alloc, class _A0, class _A1>
3371shared_ptr<_Tp>
3372shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3373{
3374 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3375 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3376 typedef __allocator_destructor<_Alloc2> _D2;
3377 _Alloc2 __alloc2(__a);
3378 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3379 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
3380 shared_ptr<_Tp> __r;
3381 __r.__ptr_ = __hold2.get()->get();
3382 __r.__cntrl_ = __hold2.release();
3383 __r.__enable_weak_this(__r.__ptr_);
3384 return __r;
3385}
3386
3387template<class _Tp>
3388template<class _Alloc, class _A0, class _A1, class _A2>
3389shared_ptr<_Tp>
3390shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3391{
3392 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3393 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3394 typedef __allocator_destructor<_Alloc2> _D2;
3395 _Alloc2 __alloc2(__a);
3396 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3397 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
3398 shared_ptr<_Tp> __r;
3399 __r.__ptr_ = __hold2.get()->get();
3400 __r.__cntrl_ = __hold2.release();
3401 __r.__enable_weak_this(__r.__ptr_);
3402 return __r;
3403}
3404
3405#endif // _LIBCPP_HAS_NO_VARIADICS
3406
3407template<class _Tp>
3408shared_ptr<_Tp>::~shared_ptr()
3409{
3410 if (__cntrl_)
3411 __cntrl_->__release_shared();
3412}
3413
3414template<class _Tp>
3415inline _LIBCPP_INLINE_VISIBILITY
3416shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003417shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003418{
3419 shared_ptr(__r).swap(*this);
3420 return *this;
3421}
3422
3423template<class _Tp>
3424template<class _Yp>
3425inline _LIBCPP_INLINE_VISIBILITY
3426shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003427shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003428{
3429 shared_ptr(__r).swap(*this);
3430 return *this;
3431}
3432
Howard Hinnant73d21a42010-09-04 23:28:19 +00003433#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003434
3435template<class _Tp>
3436inline _LIBCPP_INLINE_VISIBILITY
3437shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003438shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003439{
3440 shared_ptr(_STD::move(__r)).swap(*this);
3441 return *this;
3442}
3443
3444template<class _Tp>
3445template<class _Yp>
3446inline _LIBCPP_INLINE_VISIBILITY
3447shared_ptr<_Tp>&
3448shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3449{
3450 shared_ptr(_STD::move(__r)).swap(*this);
3451 return *this;
3452}
3453
3454template<class _Tp>
3455template<class _Yp>
3456inline _LIBCPP_INLINE_VISIBILITY
3457shared_ptr<_Tp>&
3458shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3459{
Howard Hinnant6b41c602011-05-11 20:21:19 +00003460 shared_ptr(_STD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003461 return *this;
3462}
3463
3464template<class _Tp>
3465template <class _Yp, class _Dp>
3466inline _LIBCPP_INLINE_VISIBILITY
3467shared_ptr<_Tp>&
3468shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3469{
3470 shared_ptr(_STD::move(__r)).swap(*this);
3471 return *this;
3472}
3473
Howard Hinnant73d21a42010-09-04 23:28:19 +00003474#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003475
3476template<class _Tp>
3477template<class _Yp>
3478inline _LIBCPP_INLINE_VISIBILITY
3479shared_ptr<_Tp>&
Howard Hinnant324bb032010-08-22 00:02:43 +00003480shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003481{
3482 shared_ptr(__r).swap(*this);
3483 return *this;
3484}
3485
3486template<class _Tp>
3487template <class _Yp, class _Dp>
3488inline _LIBCPP_INLINE_VISIBILITY
3489shared_ptr<_Tp>&
3490shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
3491{
3492 shared_ptr(_STD::move(__r)).swap(*this);
3493 return *this;
3494}
3495
Howard Hinnant73d21a42010-09-04 23:28:19 +00003496#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003497
3498template<class _Tp>
3499inline _LIBCPP_INLINE_VISIBILITY
3500void
Howard Hinnant1694d232011-05-28 14:41:13 +00003501shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003502{
3503 _STD::swap(__ptr_, __r.__ptr_);
3504 _STD::swap(__cntrl_, __r.__cntrl_);
3505}
3506
3507template<class _Tp>
3508inline _LIBCPP_INLINE_VISIBILITY
3509void
Howard Hinnant1694d232011-05-28 14:41:13 +00003510shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003511{
3512 shared_ptr().swap(*this);
3513}
3514
3515template<class _Tp>
3516template<class _Yp>
3517inline _LIBCPP_INLINE_VISIBILITY
3518void
3519shared_ptr<_Tp>::reset(_Yp* __p)
3520{
3521 shared_ptr(__p).swap(*this);
3522}
3523
3524template<class _Tp>
3525template<class _Yp, class _Dp>
3526inline _LIBCPP_INLINE_VISIBILITY
3527void
3528shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3529{
3530 shared_ptr(__p, __d).swap(*this);
3531}
3532
3533template<class _Tp>
3534template<class _Yp, class _Dp, class _Alloc>
3535inline _LIBCPP_INLINE_VISIBILITY
3536void
3537shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3538{
3539 shared_ptr(__p, __d, __a).swap(*this);
3540}
3541
3542#ifndef _LIBCPP_HAS_NO_VARIADICS
3543
Howard Hinnant324bb032010-08-22 00:02:43 +00003544template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003545inline _LIBCPP_INLINE_VISIBILITY
3546shared_ptr<_Tp>
3547make_shared(_Args&& ...__args)
3548{
3549 return shared_ptr<_Tp>::make_shared(_STD::forward<_Args>(__args)...);
3550}
3551
Howard Hinnant324bb032010-08-22 00:02:43 +00003552template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003553inline _LIBCPP_INLINE_VISIBILITY
3554shared_ptr<_Tp>
3555allocate_shared(const _Alloc& __a, _Args&& ...__args)
3556{
3557 return shared_ptr<_Tp>::allocate_shared(__a, _STD::forward<_Args>(__args)...);
3558}
3559
3560#else // _LIBCPP_HAS_NO_VARIADICS
3561
3562template<class _Tp>
3563inline _LIBCPP_INLINE_VISIBILITY
3564shared_ptr<_Tp>
3565make_shared()
3566{
3567 return shared_ptr<_Tp>::make_shared();
3568}
3569
3570template<class _Tp, class _A0>
3571inline _LIBCPP_INLINE_VISIBILITY
3572shared_ptr<_Tp>
3573make_shared(_A0& __a0)
3574{
3575 return shared_ptr<_Tp>::make_shared(__a0);
3576}
3577
3578template<class _Tp, class _A0, class _A1>
3579inline _LIBCPP_INLINE_VISIBILITY
3580shared_ptr<_Tp>
3581make_shared(_A0& __a0, _A1& __a1)
3582{
3583 return shared_ptr<_Tp>::make_shared(__a0, __a1);
3584}
3585
Howard Hinnant324bb032010-08-22 00:02:43 +00003586template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003587inline _LIBCPP_INLINE_VISIBILITY
3588shared_ptr<_Tp>
3589make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3590{
3591 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
3592}
3593
3594template<class _Tp, class _Alloc>
3595inline _LIBCPP_INLINE_VISIBILITY
3596shared_ptr<_Tp>
3597allocate_shared(const _Alloc& __a)
3598{
3599 return shared_ptr<_Tp>::allocate_shared(__a);
3600}
3601
3602template<class _Tp, class _Alloc, class _A0>
3603inline _LIBCPP_INLINE_VISIBILITY
3604shared_ptr<_Tp>
3605allocate_shared(const _Alloc& __a, _A0& __a0)
3606{
3607 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
3608}
3609
3610template<class _Tp, class _Alloc, class _A0, class _A1>
3611inline _LIBCPP_INLINE_VISIBILITY
3612shared_ptr<_Tp>
3613allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3614{
3615 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
3616}
3617
3618template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
3619inline _LIBCPP_INLINE_VISIBILITY
3620shared_ptr<_Tp>
3621allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3622{
3623 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
3624}
3625
3626#endif // _LIBCPP_HAS_NO_VARIADICS
3627
3628template<class _Tp, class _Up>
3629inline _LIBCPP_INLINE_VISIBILITY
3630bool
Howard Hinnant1694d232011-05-28 14:41:13 +00003631operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003632{
3633 return __x.get() == __y.get();
3634}
3635
3636template<class _Tp, class _Up>
3637inline _LIBCPP_INLINE_VISIBILITY
3638bool
Howard Hinnant1694d232011-05-28 14:41:13 +00003639operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003640{
3641 return !(__x == __y);
3642}
3643
3644template<class _Tp, class _Up>
3645inline _LIBCPP_INLINE_VISIBILITY
3646bool
Howard Hinnant1694d232011-05-28 14:41:13 +00003647operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003648{
3649 return __x.get() < __y.get();
3650}
3651
3652template<class _Tp>
3653inline _LIBCPP_INLINE_VISIBILITY
3654void
Howard Hinnant1694d232011-05-28 14:41:13 +00003655swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003656{
3657 __x.swap(__y);
3658}
3659
3660template<class _Tp, class _Up>
3661inline _LIBCPP_INLINE_VISIBILITY
3662shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003663static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003664{
3665 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
3666}
3667
3668template<class _Tp, class _Up>
3669inline _LIBCPP_INLINE_VISIBILITY
3670shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003671dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003672{
3673 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
3674 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3675}
3676
3677template<class _Tp, class _Up>
3678shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003679const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003680{
3681 return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get()));
3682}
3683
Howard Hinnantd4444702010-08-11 17:04:31 +00003684#ifndef _LIBCPP_NO_RTTI
3685
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003686template<class _Dp, class _Tp>
3687inline _LIBCPP_INLINE_VISIBILITY
3688_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00003689get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003690{
3691 return __p.template __get_deleter<_Dp>();
3692}
3693
Howard Hinnant324bb032010-08-22 00:02:43 +00003694#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003695
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003696template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003697class _LIBCPP_VISIBLE weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003698{
Howard Hinnant324bb032010-08-22 00:02:43 +00003699public:
3700 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003701private:
3702 element_type* __ptr_;
3703 __shared_weak_count* __cntrl_;
3704
Howard Hinnant324bb032010-08-22 00:02:43 +00003705public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003706 weak_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003707 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003708 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3709 _NOEXCEPT;
3710 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003711 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003712 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3713 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00003714
3715 ~weak_ptr();
3716
Howard Hinnant1694d232011-05-28 14:41:13 +00003717 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
3718 template<class _Yp> weak_ptr& operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
3719 template<class _Yp> weak_ptr& operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00003720
Howard Hinnant1694d232011-05-28 14:41:13 +00003721 void swap(weak_ptr& __r) _NOEXCEPT;
3722 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00003723
Howard Hinnant82894812010-09-22 16:48:34 +00003724 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003725 long use_count() const _NOEXCEPT
3726 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003728 bool expired() const _NOEXCEPT
3729 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
3730 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003731 template<class _Up>
3732 _LIBCPP_INLINE_VISIBILITY
3733 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003734 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003735 template<class _Up>
3736 _LIBCPP_INLINE_VISIBILITY
3737 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003738 {return __cntrl_ < __r.__cntrl_;}
3739
Howard Hinnant82894812010-09-22 16:48:34 +00003740 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
3741 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003742};
3743
3744template<class _Tp>
3745inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003746weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003747 : __ptr_(0),
3748 __cntrl_(0)
3749{
3750}
3751
3752template<class _Tp>
3753inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003754weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003755 : __ptr_(__r.__ptr_),
3756 __cntrl_(__r.__cntrl_)
3757{
3758 if (__cntrl_)
3759 __cntrl_->__add_weak();
3760}
3761
3762template<class _Tp>
3763template<class _Yp>
3764inline _LIBCPP_INLINE_VISIBILITY
3765weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00003766 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003767 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003768 : __ptr_(__r.__ptr_),
3769 __cntrl_(__r.__cntrl_)
3770{
3771 if (__cntrl_)
3772 __cntrl_->__add_weak();
3773}
3774
3775template<class _Tp>
3776template<class _Yp>
3777inline _LIBCPP_INLINE_VISIBILITY
3778weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00003779 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003780 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003781 : __ptr_(__r.__ptr_),
3782 __cntrl_(__r.__cntrl_)
3783{
3784 if (__cntrl_)
3785 __cntrl_->__add_weak();
3786}
3787
3788template<class _Tp>
3789weak_ptr<_Tp>::~weak_ptr()
3790{
3791 if (__cntrl_)
3792 __cntrl_->__release_weak();
3793}
3794
3795template<class _Tp>
3796inline _LIBCPP_INLINE_VISIBILITY
3797weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003798weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003799{
3800 weak_ptr(__r).swap(*this);
3801 return *this;
3802}
3803
3804template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00003805template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003806inline _LIBCPP_INLINE_VISIBILITY
3807weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003808weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003809{
3810 weak_ptr(__r).swap(*this);
3811 return *this;
3812}
3813
3814template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00003815template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003816inline _LIBCPP_INLINE_VISIBILITY
3817weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003818weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003819{
3820 weak_ptr(__r).swap(*this);
3821 return *this;
3822}
3823
3824template<class _Tp>
3825inline _LIBCPP_INLINE_VISIBILITY
3826void
Howard Hinnant1694d232011-05-28 14:41:13 +00003827weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003828{
3829 _STD::swap(__ptr_, __r.__ptr_);
3830 _STD::swap(__cntrl_, __r.__cntrl_);
3831}
3832
3833template<class _Tp>
3834inline _LIBCPP_INLINE_VISIBILITY
3835void
Howard Hinnant1694d232011-05-28 14:41:13 +00003836swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003837{
3838 __x.swap(__y);
3839}
3840
3841template<class _Tp>
3842inline _LIBCPP_INLINE_VISIBILITY
3843void
Howard Hinnant1694d232011-05-28 14:41:13 +00003844weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003845{
3846 weak_ptr().swap(*this);
3847}
3848
3849template<class _Tp>
3850template<class _Yp>
3851shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
3852 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3853 : __ptr_(__r.__ptr_),
3854 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3855{
3856 if (__cntrl_ == 0)
3857#ifndef _LIBCPP_NO_EXCEPTIONS
3858 throw bad_weak_ptr();
3859#else
3860 assert(!"bad_weak_ptr");
3861#endif
3862}
3863
3864template<class _Tp>
3865shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003866weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003867{
3868 shared_ptr<_Tp> __r;
3869 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3870 if (__r.__cntrl_)
3871 __r.__ptr_ = __ptr_;
3872 return __r;
3873}
3874
Howard Hinnant324bb032010-08-22 00:02:43 +00003875template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003876
3877template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003878struct _LIBCPP_VISIBLE owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003879 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00003880{
3881 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003882 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003883 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3884 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003885 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003886 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3887 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003888 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003889 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3890 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00003891};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003892
3893template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003894struct _LIBCPP_VISIBLE owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003895 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3896{
Howard Hinnant324bb032010-08-22 00:02:43 +00003897 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003899 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3900 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003902 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3903 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003904 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003905 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3906 {return __x.owner_before(__y);}
3907};
3908
3909template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003910class _LIBCPP_VISIBLE enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003911{
3912 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00003913protected:
Howard Hinnant82894812010-09-22 16:48:34 +00003914 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003915 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00003916 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003917 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00003918 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003919 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
3920 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00003921 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003922 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00003923public:
Howard Hinnant82894812010-09-22 16:48:34 +00003924 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003925 shared_ptr<_Tp> shared_from_this()
3926 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00003927 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003928 shared_ptr<_Tp const> shared_from_this() const
3929 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003930
3931 template <class _Up> friend class shared_ptr;
3932};
3933
Howard Hinnant21aefc32010-06-03 16:42:57 +00003934template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003935struct _LIBCPP_VISIBLE hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003936{
3937 typedef shared_ptr<_Tp> argument_type;
3938 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003940 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003941 {
3942 return hash<_Tp*>()(__ptr.get());
3943 }
3944};
3945
Howard Hinnant324bb032010-08-22 00:02:43 +00003946//enum class
Howard Hinnant82894812010-09-22 16:48:34 +00003947struct _LIBCPP_VISIBLE pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003948{
3949 enum _
3950 {
3951 relaxed,
3952 preferred,
3953 strict
3954 };
3955
3956 _ __v_;
3957
Howard Hinnant82894812010-09-22 16:48:34 +00003958 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003959 pointer_safety(_ __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003960 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003961 operator int() const {return __v_;}
3962};
3963
3964void declare_reachable(void* __p);
3965void declare_no_pointers(char* __p, size_t __n);
3966void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnant1694d232011-05-28 14:41:13 +00003967pointer_safety get_pointer_safety() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003968void* __undeclare_reachable(void*);
3969
3970template <class _Tp>
3971inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00003972_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003973undeclare_reachable(_Tp* __p)
3974{
3975 return static_cast<_Tp*>(__undeclare_reachable(__p));
3976}
3977
3978void* align(size_t, size_t, void*&, size_t&);
3979
3980_LIBCPP_END_NAMESPACE_STD
3981
3982#endif // _LIBCPP_MEMORY