blob: f2d6d716a7a996c27cdc737a89f81a9017192a94 [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
Douglas Gregor35d2fcf2011-06-22 22:17:44 +0000622#if defined(_LIBCPP_HAS_OBJC_ARC) && !defined(_LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF)
623// Objective-C++ Automatic Reference Counting uses qualified pointers
624// that require special addressof() signatures. When
625// _LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF is defined, the compiler
626// itself is providing these definitions. Otherwise, we provide them.
627template <class _Tp>
628inline _LIBCPP_INLINE_VISIBILITY
629__strong _Tp*
630addressof(__strong _Tp& __x) _NOEXCEPT
631{
632 return &__x;
633}
634
635#ifdef _LIBCPP_HAS_OBJC_ARC_WEAK
636template <class _Tp>
637inline _LIBCPP_INLINE_VISIBILITY
638__weak _Tp*
639addressof(__weak _Tp& __x) _NOEXCEPT
640{
641 return &__x;
642}
643#endif
644
645template <class _Tp>
646inline _LIBCPP_INLINE_VISIBILITY
647__autoreleasing _Tp*
648addressof(__autoreleasing _Tp& __x) _NOEXCEPT
649{
650 return &__x;
651}
652
653template <class _Tp>
654inline _LIBCPP_INLINE_VISIBILITY
655__unsafe_unretained _Tp*
656addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT
657{
658 return &__x;
659}
660#endif
661
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000662template <class _Tp> class allocator;
663
664template <>
Howard Hinnant82894812010-09-22 16:48:34 +0000665class _LIBCPP_VISIBLE allocator<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000666{
667public:
668 typedef void* pointer;
669 typedef const void* const_pointer;
670 typedef void value_type;
671
672 template <class _Up> struct rebind {typedef allocator<_Up> other;};
673};
674
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000675// pointer_traits
676
677template <class _Tp>
678struct __has_element_type
679{
680private:
681 struct __two {char _; char __;};
682 template <class _Up> static __two __test(...);
683 template <class _Up> static char __test(typename _Up::element_type* = 0);
684public:
685 static const bool value = sizeof(__test<_Tp>(0)) == 1;
686};
687
688template <class _Ptr, bool = __has_element_type<_Ptr>::value>
689struct __pointer_traits_element_type;
690
691template <class _Ptr>
692struct __pointer_traits_element_type<_Ptr, true>
693{
694 typedef typename _Ptr::element_type type;
695};
696
697#ifndef _LIBCPP_HAS_NO_VARIADICS
698
699template <template <class, class...> class _Sp, class _Tp, class ..._Args>
700struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
701{
702 typedef typename _Sp<_Tp, _Args...>::element_type type;
703};
704
705template <template <class, class...> class _Sp, class _Tp, class ..._Args>
706struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
707{
708 typedef _Tp type;
709};
710
Howard Hinnant324bb032010-08-22 00:02:43 +0000711#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000712
713template <template <class> class _Sp, class _Tp>
714struct __pointer_traits_element_type<_Sp<_Tp>, true>
715{
716 typedef typename _Sp<_Tp>::element_type type;
717};
718
719template <template <class> class _Sp, class _Tp>
720struct __pointer_traits_element_type<_Sp<_Tp>, false>
721{
722 typedef _Tp type;
723};
724
725template <template <class, class> class _Sp, class _Tp, class _A0>
726struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
727{
728 typedef typename _Sp<_Tp, _A0>::element_type type;
729};
730
731template <template <class, class> class _Sp, class _Tp, class _A0>
732struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
733{
734 typedef _Tp type;
735};
736
737template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
738struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
739{
740 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
741};
742
743template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
744struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
745{
746 typedef _Tp type;
747};
748
749template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
750 class _A1, class _A2>
751struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
752{
753 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
754};
755
756template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
757 class _A1, class _A2>
758struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
759{
760 typedef _Tp type;
761};
762
Howard Hinnant324bb032010-08-22 00:02:43 +0000763#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000764
765template <class _Tp>
766struct __has_difference_type
767{
768private:
769 struct __two {char _; char __;};
770 template <class _Up> static __two __test(...);
771 template <class _Up> static char __test(typename _Up::difference_type* = 0);
772public:
773 static const bool value = sizeof(__test<_Tp>(0)) == 1;
774};
775
776template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
777struct __pointer_traits_difference_type
778{
779 typedef ptrdiff_t type;
780};
781
782template <class _Ptr>
783struct __pointer_traits_difference_type<_Ptr, true>
784{
785 typedef typename _Ptr::difference_type type;
786};
787
788template <class _Tp, class _Up>
789struct __has_rebind
790{
791private:
792 struct __two {char _; char __;};
793 template <class _Xp> static __two __test(...);
794 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
795public:
796 static const bool value = sizeof(__test<_Tp>(0)) == 1;
797};
798
799template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
800struct __pointer_traits_rebind
801{
802#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
803 typedef typename _Tp::template rebind<_Up> type;
804#else
805 typedef typename _Tp::template rebind<_Up>::other type;
806#endif
807};
808
809#ifndef _LIBCPP_HAS_NO_VARIADICS
810
811template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
812struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
813{
814#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
815 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
816#else
817 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
818#endif
819};
820
821template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
822struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
823{
824 typedef _Sp<_Up, _Args...> type;
825};
826
Howard Hinnant324bb032010-08-22 00:02:43 +0000827#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000828
829template <template <class> class _Sp, class _Tp, class _Up>
830struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
831{
832#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
833 typedef typename _Sp<_Tp>::template rebind<_Up> type;
834#else
835 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
836#endif
837};
838
839template <template <class> class _Sp, class _Tp, class _Up>
840struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
841{
842 typedef _Sp<_Up> type;
843};
844
845template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
846struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
847{
848#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
849 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
850#else
851 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
852#endif
853};
854
855template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
856struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
857{
858 typedef _Sp<_Up, _A0> type;
859};
860
861template <template <class, class, class> class _Sp, class _Tp, class _A0,
862 class _A1, class _Up>
863struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
864{
865#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
866 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
867#else
868 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
869#endif
870};
871
872template <template <class, class, class> class _Sp, class _Tp, class _A0,
873 class _A1, class _Up>
874struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
875{
876 typedef _Sp<_Up, _A0, _A1> type;
877};
878
879template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
880 class _A1, class _A2, class _Up>
881struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
882{
883#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
884 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
885#else
886 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
887#endif
888};
889
890template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
891 class _A1, class _A2, class _Up>
892struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
893{
894 typedef _Sp<_Up, _A0, _A1, _A2> type;
895};
896
Howard Hinnant324bb032010-08-22 00:02:43 +0000897#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000898
899template <class _Ptr>
Howard Hinnant82894812010-09-22 16:48:34 +0000900struct _LIBCPP_VISIBLE pointer_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000901{
902 typedef _Ptr pointer;
903 typedef typename __pointer_traits_element_type<pointer>::type element_type;
904 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
905
906#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant6b41c602011-05-11 20:21:19 +0000907 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000908#else
909 template <class _Up> struct rebind
910 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant324bb032010-08-22 00:02:43 +0000911#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000912
913private:
914 struct __nat {};
915public:
Howard Hinnant82894812010-09-22 16:48:34 +0000916 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000917 static pointer pointer_to(typename conditional<is_void<element_type>::value,
918 __nat, element_type>::type& __r)
919 {return pointer::pointer_to(__r);}
920};
921
922template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +0000923struct _LIBCPP_VISIBLE pointer_traits<_Tp*>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000924{
925 typedef _Tp* pointer;
926 typedef _Tp element_type;
927 typedef ptrdiff_t difference_type;
928
929#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
930 template <class _Up> using rebind = _Up*;
931#else
932 template <class _Up> struct rebind {typedef _Up* other;};
933#endif
934
935private:
936 struct __nat {};
937public:
Howard Hinnant82894812010-09-22 16:48:34 +0000938 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000939 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant1694d232011-05-28 14:41:13 +0000940 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000941 {return _VSTD::addressof(__r);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000942};
943
944// allocator_traits
945
946namespace __has_pointer_type_imp
947{
948 template <class _Up> static __two test(...);
949 template <class _Up> static char test(typename _Up::pointer* = 0);
950}
951
952template <class _Tp>
953struct __has_pointer_type
954 : public integral_constant<bool, sizeof(__has_pointer_type_imp::test<_Tp>(0)) == 1>
955{
956};
957
958namespace __pointer_type_imp
959{
960
961template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
962struct __pointer_type
963{
964 typedef typename _Dp::pointer type;
965};
966
967template <class _Tp, class _Dp>
968struct __pointer_type<_Tp, _Dp, false>
969{
970 typedef _Tp* type;
971};
972
Howard Hinnant47761072010-11-18 01:40:00 +0000973} // __pointer_type_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000974
975template <class _Tp, class _Dp>
976struct __pointer_type
977{
978 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
979};
980
981template <class _Tp>
982struct __has_const_pointer
983{
984private:
985 struct __two {char _; char __;};
986 template <class _Up> static __two __test(...);
987 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
988public:
989 static const bool value = sizeof(__test<_Tp>(0)) == 1;
990};
991
992template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
993struct __const_pointer
994{
995 typedef typename _Alloc::const_pointer type;
996};
997
998template <class _Tp, class _Ptr, class _Alloc>
999struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1000{
1001#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1002 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1003#else
1004 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1005#endif
1006};
1007
1008template <class _Tp>
1009struct __has_void_pointer
1010{
1011private:
1012 struct __two {char _; char __;};
1013 template <class _Up> static __two __test(...);
1014 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1015public:
1016 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1017};
1018
1019template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1020struct __void_pointer
1021{
1022 typedef typename _Alloc::void_pointer type;
1023};
1024
1025template <class _Ptr, class _Alloc>
1026struct __void_pointer<_Ptr, _Alloc, false>
1027{
1028#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1029 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1030#else
1031 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1032#endif
1033};
1034
1035template <class _Tp>
1036struct __has_const_void_pointer
1037{
1038private:
1039 struct __two {char _; char __;};
1040 template <class _Up> static __two __test(...);
1041 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1042public:
1043 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1044};
1045
1046template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1047struct __const_void_pointer
1048{
1049 typedef typename _Alloc::const_void_pointer type;
1050};
1051
1052template <class _Ptr, class _Alloc>
1053struct __const_void_pointer<_Ptr, _Alloc, false>
1054{
1055#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1056 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1057#else
1058 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1059#endif
1060};
1061
1062template <class _T>
1063inline _LIBCPP_INLINE_VISIBILITY
1064_T*
Howard Hinnant1694d232011-05-28 14:41:13 +00001065__to_raw_pointer(_T* __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001066{
1067 return __p;
1068}
1069
1070template <class _Pointer>
1071inline _LIBCPP_INLINE_VISIBILITY
1072typename pointer_traits<_Pointer>::element_type*
Howard Hinnant1694d232011-05-28 14:41:13 +00001073__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001075 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001076}
1077
1078template <class _Tp>
1079struct __has_size_type
1080{
1081private:
1082 struct __two {char _; char __;};
1083 template <class _Up> static __two __test(...);
1084 template <class _Up> static char __test(typename _Up::size_type* = 0);
1085public:
1086 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1087};
1088
Howard Hinnant47761072010-11-18 01:40:00 +00001089template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001090struct __size_type
1091{
Howard Hinnant47761072010-11-18 01:40:00 +00001092 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001093};
1094
Howard Hinnant47761072010-11-18 01:40:00 +00001095template <class _Alloc, class _DiffType>
1096struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001097{
1098 typedef typename _Alloc::size_type type;
1099};
1100
1101template <class _Tp>
1102struct __has_propagate_on_container_copy_assignment
1103{
1104private:
1105 struct __two {char _; char __;};
1106 template <class _Up> static __two __test(...);
1107 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1108public:
1109 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1110};
1111
1112template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1113struct __propagate_on_container_copy_assignment
1114{
1115 typedef false_type type;
1116};
1117
1118template <class _Alloc>
1119struct __propagate_on_container_copy_assignment<_Alloc, true>
1120{
1121 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1122};
1123
1124template <class _Tp>
1125struct __has_propagate_on_container_move_assignment
1126{
1127private:
1128 struct __two {char _; char __;};
1129 template <class _Up> static __two __test(...);
1130 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1131public:
1132 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1133};
1134
1135template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1136struct __propagate_on_container_move_assignment
1137{
1138 typedef false_type type;
1139};
1140
1141template <class _Alloc>
1142struct __propagate_on_container_move_assignment<_Alloc, true>
1143{
1144 typedef typename _Alloc::propagate_on_container_move_assignment type;
1145};
1146
1147template <class _Tp>
1148struct __has_propagate_on_container_swap
1149{
1150private:
1151 struct __two {char _; char __;};
1152 template <class _Up> static __two __test(...);
1153 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1154public:
1155 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1156};
1157
1158template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1159struct __propagate_on_container_swap
1160{
1161 typedef false_type type;
1162};
1163
1164template <class _Alloc>
1165struct __propagate_on_container_swap<_Alloc, true>
1166{
1167 typedef typename _Alloc::propagate_on_container_swap type;
1168};
1169
1170template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1171struct __has_rebind_other
1172{
1173private:
1174 struct __two {char _; char __;};
1175 template <class _Xp> static __two __test(...);
1176 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1177public:
1178 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1179};
1180
1181template <class _Tp, class _Up>
1182struct __has_rebind_other<_Tp, _Up, false>
1183{
1184 static const bool value = false;
1185};
1186
1187template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1188struct __allocator_traits_rebind
1189{
1190 typedef typename _Tp::template rebind<_Up>::other type;
1191};
1192
1193#ifndef _LIBCPP_HAS_NO_VARIADICS
1194
1195template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1196struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1197{
1198 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1199};
1200
1201template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1202struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1203{
1204 typedef _Alloc<_Up, _Args...> type;
1205};
1206
Howard Hinnant324bb032010-08-22 00:02:43 +00001207#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001208
1209template <template <class> class _Alloc, class _Tp, class _Up>
1210struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1211{
1212 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1213};
1214
1215template <template <class> class _Alloc, class _Tp, class _Up>
1216struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1217{
1218 typedef _Alloc<_Up> type;
1219};
1220
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001221template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1222struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1223{
1224 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1225};
1226
1227template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1228struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1229{
1230 typedef _Alloc<_Up, _A0> type;
1231};
1232
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001233template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1234 class _A1, class _Up>
1235struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1236{
1237 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1238};
1239
1240template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1241 class _A1, class _Up>
1242struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1243{
1244 typedef _Alloc<_Up, _A0, _A1> type;
1245};
1246
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001247template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1248 class _A1, class _A2, class _Up>
1249struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1250{
1251 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1252};
1253
1254template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1255 class _A1, class _A2, class _Up>
1256struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1257{
1258 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1259};
1260
Howard Hinnant324bb032010-08-22 00:02:43 +00001261#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001262
1263#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1264
1265template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1266auto
1267__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1268 -> decltype(__a.allocate(__sz, __p), true_type());
1269
1270template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1271auto
1272__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1273 -> false_type;
1274
1275template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1276struct __has_allocate_hint
1277 : integral_constant<bool,
1278 is_same<
1279 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1280 declval<_SizeType>(),
1281 declval<_ConstVoidPtr>())),
1282 true_type>::value>
1283{
1284};
1285
Howard Hinnant324bb032010-08-22 00:02:43 +00001286#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001287
1288template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1289struct __has_allocate_hint
1290 : true_type
1291{
1292};
1293
Howard Hinnant324bb032010-08-22 00:02:43 +00001294#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001295
1296#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1297
1298template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnant0949eed2011-06-30 21:18:19 +00001299decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1300 _VSTD::declval<_Args>()...),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001301 true_type())
1302__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1303
1304template <class _Alloc, class _Pointer, class ..._Args>
1305false_type
1306__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1307
1308template <class _Alloc, class _Pointer, class ..._Args>
1309struct __has_construct
1310 : integral_constant<bool,
1311 is_same<
1312 decltype(__has_construct_test(declval<_Alloc>(),
1313 declval<_Pointer>(),
1314 declval<_Args>()...)),
1315 true_type>::value>
1316{
1317};
1318
1319template <class _Alloc, class _Pointer>
1320auto
1321__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1322 -> decltype(__a.destroy(__p), true_type());
1323
1324template <class _Alloc, class _Pointer>
1325auto
1326__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1327 -> false_type;
1328
1329template <class _Alloc, class _Pointer>
1330struct __has_destroy
1331 : integral_constant<bool,
1332 is_same<
1333 decltype(__has_destroy_test(declval<_Alloc>(),
1334 declval<_Pointer>())),
1335 true_type>::value>
1336{
1337};
1338
1339template <class _Alloc>
1340auto
1341__has_max_size_test(_Alloc&& __a)
1342 -> decltype(__a.max_size(), true_type());
1343
1344template <class _Alloc>
1345auto
1346__has_max_size_test(const volatile _Alloc& __a)
1347 -> false_type;
1348
1349template <class _Alloc>
1350struct __has_max_size
1351 : integral_constant<bool,
1352 is_same<
1353 decltype(__has_max_size_test(declval<_Alloc&>())),
1354 true_type>::value>
1355{
1356};
1357
1358template <class _Alloc>
1359auto
1360__has_select_on_container_copy_construction_test(_Alloc&& __a)
1361 -> decltype(__a.select_on_container_copy_construction(), true_type());
1362
1363template <class _Alloc>
1364auto
1365__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1366 -> false_type;
1367
1368template <class _Alloc>
1369struct __has_select_on_container_copy_construction
1370 : integral_constant<bool,
1371 is_same<
1372 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1373 true_type>::value>
1374{
1375};
1376
Howard Hinnant324bb032010-08-22 00:02:43 +00001377#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001378
1379#ifndef _LIBCPP_HAS_NO_VARIADICS
1380
1381template <class _Alloc, class _Pointer, class ..._Args>
1382struct __has_construct
1383 : false_type
1384{
1385};
1386
Howard Hinnant324bb032010-08-22 00:02:43 +00001387#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001388
1389template <class _Alloc, class _Pointer>
1390struct __has_destroy
1391 : false_type
1392{
1393};
1394
1395template <class _Alloc>
1396struct __has_max_size
1397 : true_type
1398{
1399};
1400
1401template <class _Alloc>
1402struct __has_select_on_container_copy_construction
1403 : false_type
1404{
1405};
1406
Howard Hinnant324bb032010-08-22 00:02:43 +00001407#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001408
Howard Hinnant47761072010-11-18 01:40:00 +00001409template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1410struct __alloc_traits_difference_type
1411{
1412 typedef typename pointer_traits<_Ptr>::difference_type type;
1413};
1414
1415template <class _Alloc, class _Ptr>
1416struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1417{
1418 typedef typename _Alloc::difference_type type;
1419};
1420
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001421template <class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001422struct _LIBCPP_VISIBLE allocator_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001423{
1424 typedef _Alloc allocator_type;
1425 typedef typename allocator_type::value_type value_type;
1426
1427 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1428 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1429 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1430 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1431
Howard Hinnant47761072010-11-18 01:40:00 +00001432 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1433 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001434
1435 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1436 propagate_on_container_copy_assignment;
1437 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1438 propagate_on_container_move_assignment;
1439 typedef typename __propagate_on_container_swap<allocator_type>::type
1440 propagate_on_container_swap;
1441
1442#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1443 template <class _Tp> using rebind_alloc =
Howard Hinnant6b41c602011-05-11 20:21:19 +00001444 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001445 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant324bb032010-08-22 00:02:43 +00001446#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001447 template <class _Tp> struct rebind_alloc
1448 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1449 template <class _Tp> struct rebind_traits
1450 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant324bb032010-08-22 00:02:43 +00001451#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001452
Howard Hinnant82894812010-09-22 16:48:34 +00001453 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001454 static pointer allocate(allocator_type& __a, size_type __n)
1455 {return __a.allocate(__n);}
Howard Hinnant82894812010-09-22 16:48:34 +00001456 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001457 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1458 {return allocate(__a, __n, __hint,
1459 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1460
Howard Hinnant82894812010-09-22 16:48:34 +00001461 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001462 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001463 {__a.deallocate(__p, __n);}
1464
1465#ifndef _LIBCPP_HAS_NO_VARIADICS
1466 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001468 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
1469 {__construct(__has_construct<allocator_type, pointer, _Args...>(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001470 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001471#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001472 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001473 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001474 static void construct(allocator_type& __a, _Tp* __p)
1475 {
1476 ::new ((void*)__p) _Tp();
1477 }
1478 template <class _Tp, class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00001479 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001480 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1481 {
1482 ::new ((void*)__p) _Tp(__a0);
1483 }
1484 template <class _Tp, class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00001485 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001486 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1487 const _A1& __a1)
1488 {
1489 ::new ((void*)__p) _Tp(__a0, __a1);
1490 }
1491 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00001492 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001493 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1494 const _A1& __a1, const _A2& __a2)
1495 {
1496 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1497 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001498#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001499
1500 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001501 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001502 static void destroy(allocator_type& __a, _Tp* __p)
1503 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1504
Howard Hinnant82894812010-09-22 16:48:34 +00001505 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001506 static size_type max_size(const allocator_type& __a)
1507 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1508
Howard Hinnant82894812010-09-22 16:48:34 +00001509 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001510 static allocator_type
1511 select_on_container_copy_construction(const allocator_type& __a)
1512 {return select_on_container_copy_construction(
1513 __has_select_on_container_copy_construction<const allocator_type>(),
1514 __a);}
1515
1516private:
1517
Howard Hinnant82894812010-09-22 16:48:34 +00001518 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001519 static pointer allocate(allocator_type& __a, size_type __n,
1520 const_void_pointer __hint, true_type)
1521 {return __a.allocate(__n, __hint);}
Howard Hinnant82894812010-09-22 16:48:34 +00001522 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001523 static pointer allocate(allocator_type& __a, size_type __n,
1524 const_void_pointer __hint, false_type)
1525 {return __a.allocate(__n);}
1526
1527#ifndef _LIBCPP_HAS_NO_VARIADICS
1528 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001531 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001532 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001534 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1535 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001536 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001537 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001538#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001539
1540 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001542 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1543 {__a.destroy(__p);}
1544 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001546 static void __destroy(false_type, allocator_type&, _Tp* __p)
1547 {
1548 __p->~_Tp();
1549 }
1550
Howard Hinnant82894812010-09-22 16:48:34 +00001551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001552 static size_type __max_size(true_type, const allocator_type& __a)
1553 {return __a.max_size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001555 static size_type __max_size(false_type, const allocator_type&)
1556 {return numeric_limits<size_type>::max();}
1557
Howard Hinnant82894812010-09-22 16:48:34 +00001558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001559 static allocator_type
1560 select_on_container_copy_construction(true_type, const allocator_type& __a)
1561 {return __a.select_on_container_copy_construction();}
Howard Hinnant82894812010-09-22 16:48:34 +00001562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001563 static allocator_type
1564 select_on_container_copy_construction(false_type, const allocator_type& __a)
1565 {return __a;}
1566};
1567
1568// uses_allocator
1569
1570template <class _Tp>
1571struct __has_allocator_type
1572{
1573private:
1574 struct __two {char _; char __;};
1575 template <class _Up> static __two __test(...);
1576 template <class _Up> static char __test(typename _Up::allocator_type* = 0);
1577public:
1578 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1579};
1580
1581template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
1582struct __uses_allocator
1583 : public integral_constant<bool,
1584 is_convertible<_Alloc, typename _Tp::allocator_type>::value>
1585{
1586};
1587
1588template <class _Tp, class _Alloc>
1589struct __uses_allocator<_Tp, _Alloc, false>
1590 : public false_type
1591{
1592};
1593
1594template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001595struct _LIBCPP_VISIBLE uses_allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001596 : public __uses_allocator<_Tp, _Alloc>
1597{
1598};
1599
Howard Hinnantac38bae2011-01-11 20:02:45 +00001600#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001601
1602// uses-allocator construction
1603
1604template <class _Tp, class _Alloc, class ..._Args>
1605struct __uses_alloc_ctor_imp
1606{
1607 static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
1608 static const bool __ic =
1609 is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
1610 static const int value = __ua ? 2 - __ic : 0;
1611};
1612
1613template <class _Tp, class _Alloc, class ..._Args>
1614struct __uses_alloc_ctor
1615 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
1616 {};
1617
Howard Hinnantac38bae2011-01-11 20:02:45 +00001618#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001619
1620// allocator
1621
1622template <class _Tp>
Howard Hinnant36cdf022010-09-10 16:42:26 +00001623class _LIBCPP_VISIBLE allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001624{
1625public:
1626 typedef size_t size_type;
1627 typedef ptrdiff_t difference_type;
1628 typedef _Tp* pointer;
1629 typedef const _Tp* const_pointer;
1630 typedef _Tp& reference;
1631 typedef const _Tp& const_reference;
1632 typedef _Tp value_type;
1633
Howard Hinnant18884f42011-06-02 21:38:57 +00001634 typedef true_type propagate_on_container_move_assignment;
1635
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001636 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1637
Howard Hinnant1694d232011-05-28 14:41:13 +00001638 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1639 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1640 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001641 {return _VSTD::addressof(__x);}
Howard Hinnant1694d232011-05-28 14:41:13 +00001642 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001643 {return _VSTD::addressof(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001644 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1645 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
Howard Hinnant1694d232011-05-28 14:41:13 +00001646 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
1647 {::operator delete((void*)__p);}
1648 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1649 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001650#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001651 template <class _Up, class... _Args>
1652 _LIBCPP_INLINE_VISIBILITY
1653 void
1654 construct(_Up* __p, _Args&&... __args)
1655 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001656 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001657 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001658#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001659 _LIBCPP_INLINE_VISIBILITY
1660 void
1661 construct(pointer __p)
1662 {
1663 ::new((void*)__p) _Tp();
1664 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001665# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001666 template <class _A0>
1667 _LIBCPP_INLINE_VISIBILITY
1668 typename enable_if
1669 <
1670 !is_convertible<_A0, __rv<_A0> >::value,
1671 void
1672 >::type
1673 construct(pointer __p, _A0& __a0)
1674 {
1675 ::new((void*)__p) _Tp(__a0);
1676 }
1677 template <class _A0>
1678 _LIBCPP_INLINE_VISIBILITY
1679 typename enable_if
1680 <
1681 !is_convertible<_A0, __rv<_A0> >::value,
1682 void
1683 >::type
1684 construct(pointer __p, const _A0& __a0)
1685 {
1686 ::new((void*)__p) _Tp(__a0);
1687 }
1688 template <class _A0>
1689 _LIBCPP_INLINE_VISIBILITY
1690 typename enable_if
1691 <
1692 is_convertible<_A0, __rv<_A0> >::value,
1693 void
1694 >::type
1695 construct(pointer __p, _A0 __a0)
1696 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001697 ::new((void*)__p) _Tp(_VSTD::move(__a0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001698 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001699# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001700 template <class _A0, class _A1>
1701 _LIBCPP_INLINE_VISIBILITY
1702 void
1703 construct(pointer __p, _A0& __a0, _A1& __a1)
1704 {
1705 ::new((void*)__p) _Tp(__a0, __a1);
1706 }
1707 template <class _A0, class _A1>
1708 _LIBCPP_INLINE_VISIBILITY
1709 void
1710 construct(pointer __p, const _A0& __a0, _A1& __a1)
1711 {
1712 ::new((void*)__p) _Tp(__a0, __a1);
1713 }
1714 template <class _A0, class _A1>
1715 _LIBCPP_INLINE_VISIBILITY
1716 void
1717 construct(pointer __p, _A0& __a0, const _A1& __a1)
1718 {
1719 ::new((void*)__p) _Tp(__a0, __a1);
1720 }
1721 template <class _A0, class _A1>
1722 _LIBCPP_INLINE_VISIBILITY
1723 void
1724 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1725 {
1726 ::new((void*)__p) _Tp(__a0, __a1);
1727 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001728#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001729 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1730};
1731
1732template <class _Tp, class _Up>
1733inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001734bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001735
1736template <class _Tp, class _Up>
1737inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001738bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001739
1740template <class _OutputIterator, class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001741class _LIBCPP_VISIBLE raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001742 : public iterator<output_iterator_tag,
1743 _Tp, // purposefully not C++03
1744 ptrdiff_t, // purposefully not C++03
1745 _Tp*, // purposefully not C++03
1746 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1747{
1748private:
1749 _OutputIterator __x_;
1750public:
1751 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1752 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1753 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1754 {::new(&*__x_) _Tp(__element); return *this;}
1755 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1756 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1757 {raw_storage_iterator __t(*this); ++__x_; return __t;}
1758};
1759
1760template <class _Tp>
1761pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00001762get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001763{
1764 pair<_Tp*, ptrdiff_t> __r(0, 0);
1765 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1766 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1767 / sizeof(_Tp);
1768 if (__n > __m)
1769 __n = __m;
1770 while (__n > 0)
1771 {
1772 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1773 if (__r.first)
1774 {
1775 __r.second = __n;
1776 break;
1777 }
1778 __n /= 2;
1779 }
1780 return __r;
1781}
1782
1783template <class _Tp>
1784inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001785void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001786
1787template <class _Tp>
1788struct auto_ptr_ref
1789{
1790 _Tp* __ptr_;
1791};
1792
1793template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001794class _LIBCPP_VISIBLE auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001795{
1796private:
1797 _Tp* __ptr_;
1798public:
1799 typedef _Tp element_type;
1800
1801 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1802 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1803 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1804 : __ptr_(__p.release()) {}
1805 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1806 {reset(__p.release()); return *this;}
1807 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1808 {reset(__p.release()); return *this;}
1809 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1810 {reset(__p.__ptr_); return *this;}
1811 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1812
1813 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1814 {return *__ptr_;}
1815 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1816 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1817 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1818 {
1819 _Tp* __t = __ptr_;
1820 __ptr_ = 0;
1821 return __t;
1822 }
1823 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1824 {
1825 if (__ptr_ != __p)
1826 delete __ptr_;
1827 __ptr_ = __p;
1828 }
1829
1830 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1831 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1832 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1833 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1834 {return auto_ptr<_Up>(release());}
1835};
1836
1837template <>
Howard Hinnant82894812010-09-22 16:48:34 +00001838class _LIBCPP_VISIBLE auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001839{
1840public:
1841 typedef void element_type;
1842};
1843
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001844template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1845 typename remove_cv<_T2>::type>::value,
1846 bool = is_empty<_T1>::value,
1847 bool = is_empty<_T2>::value>
1848struct __libcpp_compressed_pair_switch;
1849
1850template <class _T1, class _T2, bool IsSame>
1851struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1852
1853template <class _T1, class _T2, bool IsSame>
1854struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1855
1856template <class _T1, class _T2, bool IsSame>
1857struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1858
1859template <class _T1, class _T2>
1860struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
1861
1862template <class _T1, class _T2>
1863struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
1864
1865template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
1866class __libcpp_compressed_pair_imp;
1867
1868template <class _T1, class _T2>
1869class __libcpp_compressed_pair_imp<_T1, _T2, 0>
1870{
1871private:
1872 _T1 __first_;
1873 _T2 __second_;
1874public:
1875 typedef _T1 _T1_param;
1876 typedef _T2 _T2_param;
1877
1878 typedef typename remove_reference<_T1>::type& _T1_reference;
1879 typedef typename remove_reference<_T2>::type& _T2_reference;
1880
1881 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1882 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1883
1884 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1885 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001886 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001887 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001888 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001889 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001890 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001891
Howard Hinnant73d21a42010-09-04 23:28:19 +00001892#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001893 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00001894 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
1895 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001896 : __first_(_VSTD::forward<_T1>(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001897#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001898
Howard Hinnant1694d232011-05-28 14:41:13 +00001899 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
1900 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001901
Howard Hinnant1694d232011-05-28 14:41:13 +00001902 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
1903 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001904
1905 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00001906 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1907 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001908 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001909 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001910 swap(__first_, __x.__first_);
1911 swap(__second_, __x.__second_);
1912 }
1913};
1914
1915template <class _T1, class _T2>
1916class __libcpp_compressed_pair_imp<_T1, _T2, 1>
1917 : private _T1
1918{
1919private:
1920 _T2 __second_;
1921public:
1922 typedef _T1 _T1_param;
1923 typedef _T2 _T2_param;
1924
1925 typedef _T1& _T1_reference;
1926 typedef typename remove_reference<_T2>::type& _T2_reference;
1927
1928 typedef const _T1& _T1_const_reference;
1929 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1930
1931 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1932 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001933 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001934 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001935 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001936 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001937 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001938
Howard Hinnant73d21a42010-09-04 23:28:19 +00001939#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001940 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00001941 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
1942 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001943 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001944#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001945
Howard Hinnant1694d232011-05-28 14:41:13 +00001946 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
1947 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001948
Howard Hinnant1694d232011-05-28 14:41:13 +00001949 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
1950 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001951
1952 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00001953 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1954 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001955 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001956 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001957 swap(__second_, __x.__second_);
1958 }
1959};
1960
1961template <class _T1, class _T2>
1962class __libcpp_compressed_pair_imp<_T1, _T2, 2>
1963 : private _T2
1964{
1965private:
1966 _T1 __first_;
1967public:
1968 typedef _T1 _T1_param;
1969 typedef _T2 _T2_param;
1970
1971 typedef typename remove_reference<_T1>::type& _T1_reference;
1972 typedef _T2& _T2_reference;
1973
1974 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1975 typedef const _T2& _T2_const_reference;
1976
1977 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1978 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001979 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001980 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001981 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001982 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00001983 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
1984 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001985 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001986
Howard Hinnant73d21a42010-09-04 23:28:19 +00001987#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001988 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001989 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001990#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001991
Howard Hinnant1694d232011-05-28 14:41:13 +00001992 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
1993 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001994
Howard Hinnant1694d232011-05-28 14:41:13 +00001995 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
1996 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001997
1998 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00001999 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2000 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002001 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002002 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002003 swap(__first_, __x.__first_);
2004 }
2005};
2006
2007template <class _T1, class _T2>
2008class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2009 : private _T1,
2010 private _T2
2011{
2012public:
2013 typedef _T1 _T1_param;
2014 typedef _T2 _T2_param;
2015
2016 typedef _T1& _T1_reference;
2017 typedef _T2& _T2_reference;
2018
2019 typedef const _T1& _T1_const_reference;
2020 typedef const _T2& _T2_const_reference;
2021
2022 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2023 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002024 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002025 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002026 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002027 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002028 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002029
Howard Hinnant73d21a42010-09-04 23:28:19 +00002030#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002031 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002032 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2033 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002034 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002035#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002036
Howard Hinnant1694d232011-05-28 14:41:13 +00002037 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2038 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002039
Howard Hinnant1694d232011-05-28 14:41:13 +00002040 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2041 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002042
2043 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002044 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2045 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002046 {
2047 }
2048};
2049
2050template <class _T1, class _T2>
2051class __compressed_pair
2052 : private __libcpp_compressed_pair_imp<_T1, _T2>
2053{
2054 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2055public:
2056 typedef typename base::_T1_param _T1_param;
2057 typedef typename base::_T2_param _T2_param;
2058
2059 typedef typename base::_T1_reference _T1_reference;
2060 typedef typename base::_T2_reference _T2_reference;
2061
2062 typedef typename base::_T1_const_reference _T1_const_reference;
2063 typedef typename base::_T2_const_reference _T2_const_reference;
2064
2065 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
2066 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1, int = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002067 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002068 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2, int* = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002069 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002070 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002071 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002072
Howard Hinnant73d21a42010-09-04 23:28:19 +00002073#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002074 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002075 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2076 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002077 : base(_VSTD::move(__p)) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002078#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002079
Howard Hinnant1694d232011-05-28 14:41:13 +00002080 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2081 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002082
Howard Hinnant1694d232011-05-28 14:41:13 +00002083 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2084 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002085
Howard Hinnant1694d232011-05-28 14:41:13 +00002086 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2087 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2088 __is_nothrow_swappable<_T1>::value)
2089 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002090};
2091
2092template <class _T1, class _T2>
2093inline _LIBCPP_INLINE_VISIBILITY
2094void
2095swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002096 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2097 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002098 {__x.swap(__y);}
2099
2100template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002101struct _LIBCPP_VISIBLE default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002102{
Howard Hinnant1694d232011-05-28 14:41:13 +00002103 _LIBCPP_INLINE_VISIBILITY default_delete() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002104 template <class _Up>
2105 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002106 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2107 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002108 {
2109 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2110 delete __ptr;
2111 }
2112};
2113
2114template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002115struct _LIBCPP_VISIBLE default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002116{
Howard Hinnant1694d232011-05-28 14:41:13 +00002117 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002118 {
2119 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2120 delete [] __ptr;
2121 }
2122private:
2123 template <class _Up> void operator() (_Up*) const;
2124};
2125
2126template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant82894812010-09-22 16:48:34 +00002127class _LIBCPP_VISIBLE unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002128{
2129public:
2130 typedef _Tp element_type;
2131 typedef _Dp deleter_type;
2132 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2133private:
2134 __compressed_pair<pointer, deleter_type> __ptr_;
2135
Howard Hinnant73d21a42010-09-04 23:28:19 +00002136#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002137 unique_ptr(const unique_ptr&);
2138 unique_ptr& operator=(const unique_ptr&);
2139 template <class _Up, class _Ep>
2140 unique_ptr(const unique_ptr<_Up, _Ep>&);
2141 template <class _Up, class _Ep>
2142 unique_ptr& operator=(const unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002143#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002144 unique_ptr(unique_ptr&);
2145 template <class _Up, class _Ep>
2146 unique_ptr(unique_ptr<_Up, _Ep>&);
2147 unique_ptr& operator=(unique_ptr&);
2148 template <class _Up, class _Ep>
2149 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002150#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002151
2152 struct __nat {int __for_bool_;};
2153
2154 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2155 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2156public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002157 _LIBCPP_INLINE_VISIBILITY unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002158 : __ptr_(pointer())
2159 {
2160 static_assert(!is_pointer<deleter_type>::value,
2161 "unique_ptr constructed with null function pointer deleter");
2162 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002163 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002164 : __ptr_(pointer())
2165 {
2166 static_assert(!is_pointer<deleter_type>::value,
2167 "unique_ptr constructed with null function pointer deleter");
2168 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002169 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002170 : __ptr_(_VSTD::move(__p))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002171 {
2172 static_assert(!is_pointer<deleter_type>::value,
2173 "unique_ptr constructed with null function pointer deleter");
2174 }
2175
Howard Hinnant73d21a42010-09-04 23:28:19 +00002176#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002177 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2178 is_reference<deleter_type>::value,
2179 deleter_type,
2180 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002181 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002182 : __ptr_(__p, __d) {}
2183
2184 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002185 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002186 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002187 {
2188 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2189 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002190 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002191 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002192 template <class _Up, class _Ep>
2193 _LIBCPP_INLINE_VISIBILITY
2194 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2195 typename enable_if
2196 <
2197 !is_array<_Up>::value &&
2198 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2199 is_convertible<_Ep, deleter_type>::value &&
2200 (
2201 !is_reference<deleter_type>::value ||
2202 is_same<deleter_type, _Ep>::value
2203 ),
2204 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002205 >::type = __nat()) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002206 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002207
2208 template <class _Up>
2209 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2210 typename enable_if<
2211 is_convertible<_Up*, _Tp*>::value &&
2212 is_same<_Dp, default_delete<_Tp> >::value,
2213 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002214 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002215 : __ptr_(__p.release())
2216 {
2217 }
2218
Howard Hinnant1694d232011-05-28 14:41:13 +00002219 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002220 {
2221 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002222 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002223 return *this;
2224 }
2225
2226 template <class _Up, class _Ep>
2227 _LIBCPP_INLINE_VISIBILITY
2228 typename enable_if
2229 <
2230 !is_array<_Up>::value,
2231 unique_ptr&
2232 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002233 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002234 {
2235 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002236 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002237 return *this;
2238 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002239#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002240
2241 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2242 {
2243 return __rv<unique_ptr>(*this);
2244 }
2245
2246 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002247 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002248
2249 template <class _Up, class _Ep>
2250 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2251 {
2252 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002253 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002254 return *this;
2255 }
2256
2257 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002258 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002259
2260 template <class _Up>
2261 _LIBCPP_INLINE_VISIBILITY
2262 typename enable_if<
2263 is_convertible<_Up*, _Tp*>::value &&
2264 is_same<_Dp, default_delete<_Tp> >::value,
2265 unique_ptr&
2266 >::type
2267 operator=(auto_ptr<_Up> __p)
2268 {reset(__p.release()); return *this;}
2269
Howard Hinnant73d21a42010-09-04 23:28:19 +00002270#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002271 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2272
Howard Hinnant1694d232011-05-28 14:41:13 +00002273 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002274 {
2275 reset();
2276 return *this;
2277 }
2278
2279 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2280 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002281 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2282 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2283 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2284 {return __ptr_.second();}
2285 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2286 {return __ptr_.second();}
2287 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const
2288 _NOEXCEPT
2289 {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002290
Howard Hinnant1694d232011-05-28 14:41:13 +00002291 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002292 {
2293 pointer __t = __ptr_.first();
2294 __ptr_.first() = pointer();
2295 return __t;
2296 }
2297
Howard Hinnant1694d232011-05-28 14:41:13 +00002298 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002299 {
2300 pointer __tmp = __ptr_.first();
2301 __ptr_.first() = __p;
2302 if (__tmp)
2303 __ptr_.second()(__tmp);
2304 }
2305
Howard Hinnant1694d232011-05-28 14:41:13 +00002306 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2307 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002308};
2309
2310template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002311class _LIBCPP_VISIBLE unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002312{
2313public:
2314 typedef _Tp element_type;
2315 typedef _Dp deleter_type;
2316 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2317private:
2318 __compressed_pair<pointer, deleter_type> __ptr_;
2319
Howard Hinnant73d21a42010-09-04 23:28:19 +00002320#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002321 unique_ptr(const unique_ptr&);
2322 unique_ptr& operator=(const unique_ptr&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002323#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002324 unique_ptr(unique_ptr&);
2325 template <class _Up>
2326 unique_ptr(unique_ptr<_Up>&);
2327 unique_ptr& operator=(unique_ptr&);
2328 template <class _Up>
2329 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002330#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002331
2332 struct __nat {int __for_bool_;};
2333
2334 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2335 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2336public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002337 _LIBCPP_INLINE_VISIBILITY unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002338 : __ptr_(pointer())
2339 {
2340 static_assert(!is_pointer<deleter_type>::value,
2341 "unique_ptr constructed with null function pointer deleter");
2342 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002343 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002344 : __ptr_(pointer())
2345 {
2346 static_assert(!is_pointer<deleter_type>::value,
2347 "unique_ptr constructed with null function pointer deleter");
2348 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002349#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002350 template <class _P,
2351 class = typename enable_if<is_same<_P, pointer>::value>::type
2352 >
Howard Hinnant1694d232011-05-28 14:41:13 +00002353 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_P __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002354 : __ptr_(__p)
2355 {
2356 static_assert(!is_pointer<deleter_type>::value,
2357 "unique_ptr constructed with null function pointer deleter");
2358 }
2359
2360 template <class _P,
2361 class = typename enable_if<is_same<_P, pointer>::value>::type
2362 >
2363 _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename conditional<
2364 is_reference<deleter_type>::value,
2365 deleter_type,
2366 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002367 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002368 : __ptr_(__p, __d) {}
2369
2370 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2371 is_reference<deleter_type>::value,
2372 deleter_type,
2373 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002374 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002375 : __ptr_(pointer(), __d) {}
2376
2377 template <class _P,
2378 class = typename enable_if<is_same<_P, pointer>::value ||
2379 is_same<_P, nullptr_t>::value>::type
2380 >
2381 _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002382 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002383 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002384 {
2385 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2386 }
2387
2388 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002389 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002390 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002391 {
2392 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2393 }
2394
Howard Hinnant1694d232011-05-28 14:41:13 +00002395 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002396 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002397
Howard Hinnant1694d232011-05-28 14:41:13 +00002398 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002399 {
2400 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002401 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002402 return *this;
2403 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002404#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002405
2406 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2407 : __ptr_(__p)
2408 {
2409 static_assert(!is_pointer<deleter_type>::value,
2410 "unique_ptr constructed with null function pointer deleter");
2411 }
2412
2413 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002414 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002415
2416 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002417 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002418
2419 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2420 {
2421 return __rv<unique_ptr>(*this);
2422 }
2423
2424 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002425 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002426
2427 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2428 {
2429 reset(__u->release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002430 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002431 return *this;
2432 }
2433
Howard Hinnant73d21a42010-09-04 23:28:19 +00002434#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002435 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2436
Howard Hinnant1694d232011-05-28 14:41:13 +00002437 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002438 {
2439 reset();
2440 return *this;
2441 }
2442
2443 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2444 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002445 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2446 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2447 {return __ptr_.second();}
2448 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2449 {return __ptr_.second();}
2450 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const _NOEXCEPT
2451 {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002452
Howard Hinnant1694d232011-05-28 14:41:13 +00002453 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002454 {
2455 pointer __t = __ptr_.first();
2456 __ptr_.first() = pointer();
2457 return __t;
2458 }
2459
Howard Hinnant73d21a42010-09-04 23:28:19 +00002460#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002461 template <class _P,
2462 class = typename enable_if<is_same<_P, pointer>::value>::type
2463 >
Howard Hinnant1694d232011-05-28 14:41:13 +00002464 _LIBCPP_INLINE_VISIBILITY void reset(_P __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002465 {
2466 pointer __tmp = __ptr_.first();
2467 __ptr_.first() = __p;
2468 if (__tmp)
2469 __ptr_.second()(__tmp);
2470 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002471 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002472 {
2473 pointer __tmp = __ptr_.first();
2474 __ptr_.first() = nullptr;
2475 if (__tmp)
2476 __ptr_.second()(__tmp);
2477 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002478 _LIBCPP_INLINE_VISIBILITY void reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002479 {
2480 pointer __tmp = __ptr_.first();
2481 __ptr_.first() = nullptr;
2482 if (__tmp)
2483 __ptr_.second()(__tmp);
2484 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002485#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002486 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2487 {
2488 pointer __tmp = __ptr_.first();
2489 __ptr_.first() = __p;
2490 if (__tmp)
2491 __ptr_.second()(__tmp);
2492 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002493#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002494
2495 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2496private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002497
Howard Hinnant73d21a42010-09-04 23:28:19 +00002498#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002499 template <class _Up>
2500 explicit unique_ptr(_Up);
2501 template <class _Up>
2502 unique_ptr(_Up __u,
2503 typename conditional<
2504 is_reference<deleter_type>::value,
2505 deleter_type,
2506 typename add_lvalue_reference<const deleter_type>::type>::type,
2507 typename enable_if
2508 <
2509 is_convertible<_Up, pointer>::value,
2510 __nat
2511 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002512#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002513};
2514
2515template <class _Tp, class _Dp>
2516inline _LIBCPP_INLINE_VISIBILITY
2517void
Howard Hinnant1694d232011-05-28 14:41:13 +00002518swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002519
2520template <class _T1, class _D1, class _T2, class _D2>
2521inline _LIBCPP_INLINE_VISIBILITY
2522bool
2523operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2524
2525template <class _T1, class _D1, class _T2, class _D2>
2526inline _LIBCPP_INLINE_VISIBILITY
2527bool
2528operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2529
2530template <class _T1, class _D1, class _T2, class _D2>
2531inline _LIBCPP_INLINE_VISIBILITY
2532bool
2533operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() < __y.get();}
2534
2535template <class _T1, class _D1, class _T2, class _D2>
2536inline _LIBCPP_INLINE_VISIBILITY
2537bool
2538operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2539
2540template <class _T1, class _D1, class _T2, class _D2>
2541inline _LIBCPP_INLINE_VISIBILITY
2542bool
2543operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2544
2545template <class _T1, class _D1, class _T2, class _D2>
2546inline _LIBCPP_INLINE_VISIBILITY
2547bool
2548operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2549
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00002550template <class _Tp> struct hash;
Howard Hinnant21aefc32010-06-03 16:42:57 +00002551
2552template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002553struct _LIBCPP_VISIBLE hash<_Tp*>
Howard Hinnant21aefc32010-06-03 16:42:57 +00002554 : public unary_function<_Tp*, size_t>
2555{
Howard Hinnant82894812010-09-22 16:48:34 +00002556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002557 size_t operator()(_Tp* __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00002558 {
2559 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
2560 return *__p;
2561 }
2562};
2563
2564template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002565struct _LIBCPP_VISIBLE hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00002566{
2567 typedef unique_ptr<_Tp, _Dp> argument_type;
2568 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00002569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002570 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00002571 {
2572 typedef typename argument_type::pointer pointer;
2573 return hash<pointer>()(__ptr.get());
2574 }
2575};
2576
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002577struct __destruct_n
2578{
2579private:
2580 size_t size;
2581
2582 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002583 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002584 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
2585
2586 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002587 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002588 {}
2589
Howard Hinnant1694d232011-05-28 14:41:13 +00002590 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002591 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00002592 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002593 {}
2594
Howard Hinnant1694d232011-05-28 14:41:13 +00002595 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002596 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00002597 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002598 {}
2599public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002600 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
2601 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002602
2603 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002604 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00002605 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002606
2607 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002608 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00002609 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002610
2611 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002612 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00002613 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002614};
2615
2616template <class _Alloc>
2617class __allocator_destructor
2618{
2619 typedef allocator_traits<_Alloc> __alloc_traits;
2620public:
2621 typedef typename __alloc_traits::pointer pointer;
2622 typedef typename __alloc_traits::size_type size_type;
2623private:
2624 _Alloc& __alloc_;
2625 size_type __s_;
2626public:
2627 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00002628 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002629 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00002630 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002631 void operator()(pointer __p) _NOEXCEPT
2632 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002633};
2634
2635template <class _InputIterator, class _ForwardIterator>
2636_ForwardIterator
2637uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2638{
2639 __destruct_n __d(0);
2640 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2641 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2642 for (; __f != __l; ++__f, ++__r, __d.__incr((value_type*)0))
2643 ::new(&*__r) value_type(*__f);
2644 __h.release();
2645 return __r;
2646}
2647
2648template <class _InputIterator, class _Size, class _ForwardIterator>
2649_ForwardIterator
2650uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2651{
2652 __destruct_n __d(0);
2653 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2654 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2655 for (; __n > 0; ++__f, ++__r, __d.__incr((value_type*)0), --__n)
2656 ::new(&*__r) value_type(*__f);
2657 __h.release();
2658 return __r;
2659}
2660
2661template <class _ForwardIterator, class _Tp>
2662void
2663uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2664{
2665 __destruct_n __d(0);
2666 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2667 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2668 for (; __f != __l; ++__f, __d.__incr((value_type*)0))
2669 ::new(&*__f) value_type(__x);
2670 __h.release();
2671}
2672
2673template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00002674_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002675uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2676{
2677 __destruct_n __d(0);
2678 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2679 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2680 for (; __n > 0; ++__f, --__n, __d.__incr((value_type*)0))
2681 ::new(&*__f) value_type(__x);
2682 __h.release();
Howard Hinnant2f6a6272010-11-18 16:13:03 +00002683 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002684}
2685
Howard Hinnant82894812010-09-22 16:48:34 +00002686class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002687 : public std::exception
2688{
2689public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002690 virtual ~bad_weak_ptr() _NOEXCEPT;
2691 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002692};
2693
2694template<class _Tp> class weak_ptr;
2695
2696class __shared_count
2697{
2698 __shared_count(const __shared_count&);
2699 __shared_count& operator=(const __shared_count&);
2700
2701protected:
2702 long __shared_owners_;
2703 virtual ~__shared_count();
2704private:
Howard Hinnant1694d232011-05-28 14:41:13 +00002705 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002706
2707public:
Howard Hinnant82894812010-09-22 16:48:34 +00002708 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002709 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002710 : __shared_owners_(__refs) {}
2711
Howard Hinnant1694d232011-05-28 14:41:13 +00002712 void __add_shared() _NOEXCEPT;
2713 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00002714 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002715 long use_count() const _NOEXCEPT {return __shared_owners_ + 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002716};
2717
2718class __shared_weak_count
2719 : private __shared_count
2720{
2721 long __shared_weak_owners_;
2722
2723public:
Howard Hinnant82894812010-09-22 16:48:34 +00002724 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002725 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002726 : __shared_count(__refs),
2727 __shared_weak_owners_(__refs) {}
2728protected:
2729 virtual ~__shared_weak_count();
2730
2731public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002732 void __add_shared() _NOEXCEPT;
2733 void __add_weak() _NOEXCEPT;
2734 void __release_shared() _NOEXCEPT;
2735 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00002736 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002737 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
2738 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002739
Howard Hinnantd4444702010-08-11 17:04:31 +00002740#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00002741 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00002742#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002743private:
Howard Hinnant1694d232011-05-28 14:41:13 +00002744 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002745};
2746
2747template <class _Tp, class _Dp, class _Alloc>
2748class __shared_ptr_pointer
2749 : public __shared_weak_count
2750{
2751 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2752public:
Howard Hinnant82894812010-09-22 16:48:34 +00002753 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002754 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002755 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002756
Howard Hinnantd4444702010-08-11 17:04:31 +00002757#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00002758 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00002759#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002760
2761private:
Howard Hinnant1694d232011-05-28 14:41:13 +00002762 virtual void __on_zero_shared() _NOEXCEPT;
2763 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002764};
2765
Howard Hinnantd4444702010-08-11 17:04:31 +00002766#ifndef _LIBCPP_NO_RTTI
2767
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002768template <class _Tp, class _Dp, class _Alloc>
2769const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00002770__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002771{
2772 return __t == typeid(_Dp) ? &__data_.first().second() : 0;
2773}
2774
Howard Hinnant324bb032010-08-22 00:02:43 +00002775#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00002776
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002777template <class _Tp, class _Dp, class _Alloc>
2778void
Howard Hinnant1694d232011-05-28 14:41:13 +00002779__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002780{
2781 __data_.first().second()(__data_.first().first());
2782 __data_.first().second().~_Dp();
2783}
2784
2785template <class _Tp, class _Dp, class _Alloc>
2786void
Howard Hinnant1694d232011-05-28 14:41:13 +00002787__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002788{
2789 typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
2790 __data_.second().~_Alloc();
2791 __a.deallocate(this, 1);
2792}
2793
2794template <class _Tp, class _Alloc>
2795class __shared_ptr_emplace
2796 : public __shared_weak_count
2797{
2798 __compressed_pair<_Alloc, _Tp> __data_;
2799public:
2800#ifndef _LIBCPP_HAS_NO_VARIADICS
2801
Howard Hinnant82894812010-09-22 16:48:34 +00002802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002803 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002804 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002805
2806 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00002807 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002808 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002809 : __data_(_VSTD::move(__a), _Tp(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002810
2811#else // _LIBCPP_HAS_NO_VARIADICS
2812
Howard Hinnant82894812010-09-22 16:48:34 +00002813 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002814 __shared_ptr_emplace(_Alloc __a)
2815 : __data_(__a) {}
2816
2817 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00002818 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002819 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
2820 : __data_(__a, _Tp(__a0)) {}
2821
2822 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00002823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002824 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
2825 : __data_(__a, _Tp(__a0, __a1)) {}
2826
2827 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00002828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002829 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
2830 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
2831
2832#endif // _LIBCPP_HAS_NO_VARIADICS
2833
2834private:
Howard Hinnant1694d232011-05-28 14:41:13 +00002835 virtual void __on_zero_shared() _NOEXCEPT;
2836 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002837public:
Howard Hinnant82894812010-09-22 16:48:34 +00002838 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002839 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002840};
2841
2842template <class _Tp, class _Alloc>
2843void
Howard Hinnant1694d232011-05-28 14:41:13 +00002844__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002845{
2846 __data_.second().~_Tp();
2847}
2848
2849template <class _Tp, class _Alloc>
2850void
Howard Hinnant1694d232011-05-28 14:41:13 +00002851__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002852{
2853 typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
2854 __data_.first().~_Alloc();
2855 __a.deallocate(this, 1);
2856}
2857
2858template<class _Tp> class enable_shared_from_this;
2859
2860template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002861class _LIBCPP_VISIBLE shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002862{
Howard Hinnant324bb032010-08-22 00:02:43 +00002863public:
2864 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002865private:
2866 element_type* __ptr_;
2867 __shared_weak_count* __cntrl_;
2868
2869 struct __nat {int __for_bool_;};
2870public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002871 shared_ptr() _NOEXCEPT;
2872 shared_ptr(nullptr_t) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002873 template<class _Yp> explicit shared_ptr(_Yp* __p);
Howard Hinnant324bb032010-08-22 00:02:43 +00002874 template<class _Yp, class _Dp> shared_ptr(_Yp* __p, _Dp __d);
2875 template<class _Yp, class _Dp, class _Alloc> shared_ptr(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002876 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2877 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant1694d232011-05-28 14:41:13 +00002878 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
2879 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002880 template<class _Yp>
2881 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00002882 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
2883 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00002884#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00002885 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002886 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00002887 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
2888 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00002889#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002890 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00002891 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002892#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002893 template<class _Yp> shared_ptr(auto_ptr<_Yp>&& __r);
2894#else
2895 template<class _Yp> shared_ptr(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002896#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00002897#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002898private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002899 template <class _Yp, class _Dp> shared_ptr(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002900public:
2901 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
2902 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2903 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
2904 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002905#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002906 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
2907 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2908 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
2909 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002910#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002911
2912 ~shared_ptr();
2913
Howard Hinnant1694d232011-05-28 14:41:13 +00002914 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
2915 template<class _Yp> shared_ptr& operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00002916#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00002917 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00002918 template<class _Yp> shared_ptr& operator=(shared_ptr<_Yp>&& __r);
2919 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002920#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002921 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002922#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00002923#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002924private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002925 template <class _Yp, class _Dp> shared_ptr& operator=(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002926public:
Howard Hinnant324bb032010-08-22 00:02:43 +00002927 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002928#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002929 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002930#endif
2931
Howard Hinnant1694d232011-05-28 14:41:13 +00002932 void swap(shared_ptr& __r) _NOEXCEPT;
2933 void reset() _NOEXCEPT;
2934 template<class _Yp> void reset(_Yp* __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002935 template<class _Yp, class _Dp> void reset(_Yp* __p, _Dp __d);
2936 template<class _Yp, class _Dp, class _Alloc> void reset(_Yp* __p, _Dp __d, _Alloc __a);
2937
Howard Hinnant82894812010-09-22 16:48:34 +00002938 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002939 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002940 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002941 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
2942 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002943 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002944 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002945 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002946 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00002947 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002948 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00002949 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002950 /*explicit*/ operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00002951 template <class _U>
2952 _LIBCPP_INLINE_VISIBILITY
2953 bool owner_before(shared_ptr<_U> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002954 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002955 template <class _U>
2956 _LIBCPP_INLINE_VISIBILITY
2957 bool owner_before(weak_ptr<_U> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002958 {return __cntrl_ < __p.__cntrl_;}
2959
Howard Hinnantd4444702010-08-11 17:04:31 +00002960#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002961 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002962 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002963 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002964 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00002965#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002966
2967#ifndef _LIBCPP_HAS_NO_VARIADICS
2968
2969 template<class ..._Args>
2970 static
2971 shared_ptr<_Tp>
2972 make_shared(_Args&& ...__args);
2973
2974 template<class _Alloc, class ..._Args>
2975 static
2976 shared_ptr<_Tp>
2977 allocate_shared(const _Alloc& __a, _Args&& ...__args);
2978
2979#else // _LIBCPP_HAS_NO_VARIADICS
2980
2981 static shared_ptr<_Tp> make_shared();
2982
2983 template<class _A0>
2984 static shared_ptr<_Tp> make_shared(_A0&);
2985
2986 template<class _A0, class _A1>
2987 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
2988
2989 template<class _A0, class _A1, class _A2>
2990 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
2991
2992 template<class _Alloc>
2993 static shared_ptr<_Tp>
2994 allocate_shared(const _Alloc& __a);
2995
2996 template<class _Alloc, class _A0>
2997 static shared_ptr<_Tp>
2998 allocate_shared(const _Alloc& __a, _A0& __a0);
2999
3000 template<class _Alloc, class _A0, class _A1>
3001 static shared_ptr<_Tp>
3002 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3003
3004 template<class _Alloc, class _A0, class _A1, class _A2>
3005 static shared_ptr<_Tp>
3006 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3007
3008#endif // _LIBCPP_HAS_NO_VARIADICS
3009
3010private:
3011
3012 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00003013 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003014 void
Howard Hinnant1694d232011-05-28 14:41:13 +00003015 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003016 {
3017 if (__e)
3018 __e->__weak_this_ = *this;
3019 }
3020
Howard Hinnant82894812010-09-22 16:48:34 +00003021 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003022 void __enable_weak_this(const void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003023
Howard Hinnant82894812010-09-22 16:48:34 +00003024 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
3025 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003026};
3027
3028template<class _Tp>
3029inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003030shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003031 : __ptr_(0),
3032 __cntrl_(0)
3033{
3034}
3035
3036template<class _Tp>
3037inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003038shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003039 : __ptr_(0),
3040 __cntrl_(0)
3041{
3042}
3043
3044template<class _Tp>
3045template<class _Yp>
3046shared_ptr<_Tp>::shared_ptr(_Yp* __p)
3047 : __ptr_(__p)
3048{
3049 unique_ptr<_Yp> __hold(__p);
3050 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3051 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
3052 __hold.release();
3053 __enable_weak_this(__p);
3054}
3055
3056template<class _Tp>
3057template<class _Yp, class _Dp>
3058shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
3059 : __ptr_(__p)
3060{
3061#ifndef _LIBCPP_NO_EXCEPTIONS
3062 try
3063 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003064#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003065 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3066 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
3067 __enable_weak_this(__p);
3068#ifndef _LIBCPP_NO_EXCEPTIONS
3069 }
3070 catch (...)
3071 {
3072 __d(__p);
3073 throw;
3074 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003075#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003076}
3077
3078template<class _Tp>
3079template<class _Dp>
3080shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3081 : __ptr_(0)
3082{
3083#ifndef _LIBCPP_NO_EXCEPTIONS
3084 try
3085 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003086#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003087 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
3088 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
3089#ifndef _LIBCPP_NO_EXCEPTIONS
3090 }
3091 catch (...)
3092 {
3093 __d(__p);
3094 throw;
3095 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003096#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003097}
3098
3099template<class _Tp>
3100template<class _Yp, class _Dp, class _Alloc>
3101shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
3102 : __ptr_(__p)
3103{
3104#ifndef _LIBCPP_NO_EXCEPTIONS
3105 try
3106 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003107#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003108 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
3109 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3110 typedef __allocator_destructor<_A2> _D2;
3111 _A2 __a2(__a);
3112 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3113 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
3114 __cntrl_ = __hold2.release();
3115 __enable_weak_this(__p);
3116#ifndef _LIBCPP_NO_EXCEPTIONS
3117 }
3118 catch (...)
3119 {
3120 __d(__p);
3121 throw;
3122 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003123#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003124}
3125
3126template<class _Tp>
3127template<class _Dp, class _Alloc>
3128shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
3129 : __ptr_(0)
3130{
3131#ifndef _LIBCPP_NO_EXCEPTIONS
3132 try
3133 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003134#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003135 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
3136 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3137 typedef __allocator_destructor<_A2> _D2;
3138 _A2 __a2(__a);
3139 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3140 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
3141 __cntrl_ = __hold2.release();
3142#ifndef _LIBCPP_NO_EXCEPTIONS
3143 }
3144 catch (...)
3145 {
3146 __d(__p);
3147 throw;
3148 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003149#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003150}
3151
3152template<class _Tp>
3153template<class _Yp>
3154inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003155shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003156 : __ptr_(__p),
3157 __cntrl_(__r.__cntrl_)
3158{
3159 if (__cntrl_)
3160 __cntrl_->__add_shared();
3161}
3162
3163template<class _Tp>
3164inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003165shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003166 : __ptr_(__r.__ptr_),
3167 __cntrl_(__r.__cntrl_)
3168{
3169 if (__cntrl_)
3170 __cntrl_->__add_shared();
3171}
3172
3173template<class _Tp>
3174template<class _Yp>
3175inline _LIBCPP_INLINE_VISIBILITY
3176shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
3177 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003178 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003179 : __ptr_(__r.__ptr_),
3180 __cntrl_(__r.__cntrl_)
3181{
3182 if (__cntrl_)
3183 __cntrl_->__add_shared();
3184}
3185
Howard Hinnant73d21a42010-09-04 23:28:19 +00003186#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003187
3188template<class _Tp>
3189inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003190shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003191 : __ptr_(__r.__ptr_),
3192 __cntrl_(__r.__cntrl_)
3193{
3194 __r.__ptr_ = 0;
3195 __r.__cntrl_ = 0;
3196}
3197
3198template<class _Tp>
3199template<class _Yp>
3200inline _LIBCPP_INLINE_VISIBILITY
3201shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
3202 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003203 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003204 : __ptr_(__r.__ptr_),
3205 __cntrl_(__r.__cntrl_)
3206{
3207 __r.__ptr_ = 0;
3208 __r.__cntrl_ = 0;
3209}
3210
Howard Hinnant73d21a42010-09-04 23:28:19 +00003211#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003212
3213template<class _Tp>
3214template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003215#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003216shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
3217#else
Howard Hinnant92172b82010-08-21 21:14:53 +00003218shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003219#endif
3220 : __ptr_(__r.get())
3221{
3222 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3223 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
3224 __enable_weak_this(__r.get());
3225 __r.release();
3226}
3227
3228template<class _Tp>
3229template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003230#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003231shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3232#else
3233shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3234#endif
3235 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
3236 : __ptr_(__r.get())
3237{
3238 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3239 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
3240 __enable_weak_this(__r.get());
3241 __r.release();
3242}
3243
3244template<class _Tp>
3245template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003246#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003247shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3248#else
3249shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3250#endif
3251 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
3252 : __ptr_(__r.get())
3253{
3254 typedef __shared_ptr_pointer<_Yp*,
3255 reference_wrapper<typename remove_reference<_Dp>::type>,
3256 allocator<_Yp> > _CntrlBlk;
3257 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
3258 __enable_weak_this(__r.get());
3259 __r.release();
3260}
3261
3262#ifndef _LIBCPP_HAS_NO_VARIADICS
3263
3264template<class _Tp>
3265template<class ..._Args>
3266shared_ptr<_Tp>
3267shared_ptr<_Tp>::make_shared(_Args&& ...__args)
3268{
3269 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3270 typedef allocator<_CntrlBlk> _A2;
3271 typedef __allocator_destructor<_A2> _D2;
3272 _A2 __a2;
3273 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003274 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003275 shared_ptr<_Tp> __r;
3276 __r.__ptr_ = __hold2.get()->get();
3277 __r.__cntrl_ = __hold2.release();
3278 __r.__enable_weak_this(__r.__ptr_);
3279 return __r;
3280}
3281
3282template<class _Tp>
3283template<class _Alloc, class ..._Args>
3284shared_ptr<_Tp>
3285shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
3286{
3287 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3288 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3289 typedef __allocator_destructor<_A2> _D2;
3290 _A2 __a2(__a);
3291 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003292 ::new(__hold2.get()) _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003293 shared_ptr<_Tp> __r;
3294 __r.__ptr_ = __hold2.get()->get();
3295 __r.__cntrl_ = __hold2.release();
3296 __r.__enable_weak_this(__r.__ptr_);
3297 return __r;
3298}
3299
3300#else // _LIBCPP_HAS_NO_VARIADICS
3301
3302template<class _Tp>
3303shared_ptr<_Tp>
3304shared_ptr<_Tp>::make_shared()
3305{
3306 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3307 typedef allocator<_CntrlBlk> _Alloc2;
3308 typedef __allocator_destructor<_Alloc2> _D2;
3309 _Alloc2 __alloc2;
3310 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3311 ::new(__hold2.get()) _CntrlBlk(__alloc2);
3312 shared_ptr<_Tp> __r;
3313 __r.__ptr_ = __hold2.get()->get();
3314 __r.__cntrl_ = __hold2.release();
3315 __r.__enable_weak_this(__r.__ptr_);
3316 return __r;
3317}
3318
3319template<class _Tp>
3320template<class _A0>
3321shared_ptr<_Tp>
3322shared_ptr<_Tp>::make_shared(_A0& __a0)
3323{
3324 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3325 typedef allocator<_CntrlBlk> _Alloc2;
3326 typedef __allocator_destructor<_Alloc2> _D2;
3327 _Alloc2 __alloc2;
3328 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3329 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
3330 shared_ptr<_Tp> __r;
3331 __r.__ptr_ = __hold2.get()->get();
3332 __r.__cntrl_ = __hold2.release();
3333 __r.__enable_weak_this(__r.__ptr_);
3334 return __r;
3335}
3336
3337template<class _Tp>
3338template<class _A0, class _A1>
3339shared_ptr<_Tp>
3340shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
3341{
3342 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3343 typedef allocator<_CntrlBlk> _Alloc2;
3344 typedef __allocator_destructor<_Alloc2> _D2;
3345 _Alloc2 __alloc2;
3346 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3347 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
3348 shared_ptr<_Tp> __r;
3349 __r.__ptr_ = __hold2.get()->get();
3350 __r.__cntrl_ = __hold2.release();
3351 __r.__enable_weak_this(__r.__ptr_);
3352 return __r;
3353}
3354
3355template<class _Tp>
3356template<class _A0, class _A1, class _A2>
3357shared_ptr<_Tp>
3358shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3359{
3360 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3361 typedef allocator<_CntrlBlk> _Alloc2;
3362 typedef __allocator_destructor<_Alloc2> _D2;
3363 _Alloc2 __alloc2;
3364 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3365 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
3366 shared_ptr<_Tp> __r;
3367 __r.__ptr_ = __hold2.get()->get();
3368 __r.__cntrl_ = __hold2.release();
3369 __r.__enable_weak_this(__r.__ptr_);
3370 return __r;
3371}
3372
3373template<class _Tp>
3374template<class _Alloc>
3375shared_ptr<_Tp>
3376shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
3377{
3378 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3379 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3380 typedef __allocator_destructor<_Alloc2> _D2;
3381 _Alloc2 __alloc2(__a);
3382 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3383 ::new(__hold2.get()) _CntrlBlk(__a);
3384 shared_ptr<_Tp> __r;
3385 __r.__ptr_ = __hold2.get()->get();
3386 __r.__cntrl_ = __hold2.release();
3387 __r.__enable_weak_this(__r.__ptr_);
3388 return __r;
3389}
3390
3391template<class _Tp>
3392template<class _Alloc, class _A0>
3393shared_ptr<_Tp>
3394shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
3395{
3396 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3397 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3398 typedef __allocator_destructor<_Alloc2> _D2;
3399 _Alloc2 __alloc2(__a);
3400 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3401 ::new(__hold2.get()) _CntrlBlk(__a, __a0);
3402 shared_ptr<_Tp> __r;
3403 __r.__ptr_ = __hold2.get()->get();
3404 __r.__cntrl_ = __hold2.release();
3405 __r.__enable_weak_this(__r.__ptr_);
3406 return __r;
3407}
3408
3409template<class _Tp>
3410template<class _Alloc, class _A0, class _A1>
3411shared_ptr<_Tp>
3412shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3413{
3414 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3415 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3416 typedef __allocator_destructor<_Alloc2> _D2;
3417 _Alloc2 __alloc2(__a);
3418 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3419 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
3420 shared_ptr<_Tp> __r;
3421 __r.__ptr_ = __hold2.get()->get();
3422 __r.__cntrl_ = __hold2.release();
3423 __r.__enable_weak_this(__r.__ptr_);
3424 return __r;
3425}
3426
3427template<class _Tp>
3428template<class _Alloc, class _A0, class _A1, class _A2>
3429shared_ptr<_Tp>
3430shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3431{
3432 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3433 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3434 typedef __allocator_destructor<_Alloc2> _D2;
3435 _Alloc2 __alloc2(__a);
3436 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3437 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
3438 shared_ptr<_Tp> __r;
3439 __r.__ptr_ = __hold2.get()->get();
3440 __r.__cntrl_ = __hold2.release();
3441 __r.__enable_weak_this(__r.__ptr_);
3442 return __r;
3443}
3444
3445#endif // _LIBCPP_HAS_NO_VARIADICS
3446
3447template<class _Tp>
3448shared_ptr<_Tp>::~shared_ptr()
3449{
3450 if (__cntrl_)
3451 __cntrl_->__release_shared();
3452}
3453
3454template<class _Tp>
3455inline _LIBCPP_INLINE_VISIBILITY
3456shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003457shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003458{
3459 shared_ptr(__r).swap(*this);
3460 return *this;
3461}
3462
3463template<class _Tp>
3464template<class _Yp>
3465inline _LIBCPP_INLINE_VISIBILITY
3466shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003467shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003468{
3469 shared_ptr(__r).swap(*this);
3470 return *this;
3471}
3472
Howard Hinnant73d21a42010-09-04 23:28:19 +00003473#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003474
3475template<class _Tp>
3476inline _LIBCPP_INLINE_VISIBILITY
3477shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003478shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003479{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003480 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003481 return *this;
3482}
3483
3484template<class _Tp>
3485template<class _Yp>
3486inline _LIBCPP_INLINE_VISIBILITY
3487shared_ptr<_Tp>&
3488shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3489{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003490 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003491 return *this;
3492}
3493
3494template<class _Tp>
3495template<class _Yp>
3496inline _LIBCPP_INLINE_VISIBILITY
3497shared_ptr<_Tp>&
3498shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3499{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003500 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003501 return *this;
3502}
3503
3504template<class _Tp>
3505template <class _Yp, class _Dp>
3506inline _LIBCPP_INLINE_VISIBILITY
3507shared_ptr<_Tp>&
3508shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3509{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003510 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003511 return *this;
3512}
3513
Howard Hinnant73d21a42010-09-04 23:28:19 +00003514#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003515
3516template<class _Tp>
3517template<class _Yp>
3518inline _LIBCPP_INLINE_VISIBILITY
3519shared_ptr<_Tp>&
Howard Hinnant324bb032010-08-22 00:02:43 +00003520shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003521{
3522 shared_ptr(__r).swap(*this);
3523 return *this;
3524}
3525
3526template<class _Tp>
3527template <class _Yp, class _Dp>
3528inline _LIBCPP_INLINE_VISIBILITY
3529shared_ptr<_Tp>&
3530shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
3531{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003532 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003533 return *this;
3534}
3535
Howard Hinnant73d21a42010-09-04 23:28:19 +00003536#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003537
3538template<class _Tp>
3539inline _LIBCPP_INLINE_VISIBILITY
3540void
Howard Hinnant1694d232011-05-28 14:41:13 +00003541shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003542{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003543 _VSTD::swap(__ptr_, __r.__ptr_);
3544 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003545}
3546
3547template<class _Tp>
3548inline _LIBCPP_INLINE_VISIBILITY
3549void
Howard Hinnant1694d232011-05-28 14:41:13 +00003550shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003551{
3552 shared_ptr().swap(*this);
3553}
3554
3555template<class _Tp>
3556template<class _Yp>
3557inline _LIBCPP_INLINE_VISIBILITY
3558void
3559shared_ptr<_Tp>::reset(_Yp* __p)
3560{
3561 shared_ptr(__p).swap(*this);
3562}
3563
3564template<class _Tp>
3565template<class _Yp, class _Dp>
3566inline _LIBCPP_INLINE_VISIBILITY
3567void
3568shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3569{
3570 shared_ptr(__p, __d).swap(*this);
3571}
3572
3573template<class _Tp>
3574template<class _Yp, class _Dp, class _Alloc>
3575inline _LIBCPP_INLINE_VISIBILITY
3576void
3577shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3578{
3579 shared_ptr(__p, __d, __a).swap(*this);
3580}
3581
3582#ifndef _LIBCPP_HAS_NO_VARIADICS
3583
Howard Hinnant324bb032010-08-22 00:02:43 +00003584template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003585inline _LIBCPP_INLINE_VISIBILITY
3586shared_ptr<_Tp>
3587make_shared(_Args&& ...__args)
3588{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003589 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003590}
3591
Howard Hinnant324bb032010-08-22 00:02:43 +00003592template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003593inline _LIBCPP_INLINE_VISIBILITY
3594shared_ptr<_Tp>
3595allocate_shared(const _Alloc& __a, _Args&& ...__args)
3596{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003597 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003598}
3599
3600#else // _LIBCPP_HAS_NO_VARIADICS
3601
3602template<class _Tp>
3603inline _LIBCPP_INLINE_VISIBILITY
3604shared_ptr<_Tp>
3605make_shared()
3606{
3607 return shared_ptr<_Tp>::make_shared();
3608}
3609
3610template<class _Tp, class _A0>
3611inline _LIBCPP_INLINE_VISIBILITY
3612shared_ptr<_Tp>
3613make_shared(_A0& __a0)
3614{
3615 return shared_ptr<_Tp>::make_shared(__a0);
3616}
3617
3618template<class _Tp, class _A0, class _A1>
3619inline _LIBCPP_INLINE_VISIBILITY
3620shared_ptr<_Tp>
3621make_shared(_A0& __a0, _A1& __a1)
3622{
3623 return shared_ptr<_Tp>::make_shared(__a0, __a1);
3624}
3625
Howard Hinnant324bb032010-08-22 00:02:43 +00003626template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003627inline _LIBCPP_INLINE_VISIBILITY
3628shared_ptr<_Tp>
3629make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3630{
3631 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
3632}
3633
3634template<class _Tp, class _Alloc>
3635inline _LIBCPP_INLINE_VISIBILITY
3636shared_ptr<_Tp>
3637allocate_shared(const _Alloc& __a)
3638{
3639 return shared_ptr<_Tp>::allocate_shared(__a);
3640}
3641
3642template<class _Tp, class _Alloc, class _A0>
3643inline _LIBCPP_INLINE_VISIBILITY
3644shared_ptr<_Tp>
3645allocate_shared(const _Alloc& __a, _A0& __a0)
3646{
3647 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
3648}
3649
3650template<class _Tp, class _Alloc, class _A0, class _A1>
3651inline _LIBCPP_INLINE_VISIBILITY
3652shared_ptr<_Tp>
3653allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3654{
3655 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
3656}
3657
3658template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
3659inline _LIBCPP_INLINE_VISIBILITY
3660shared_ptr<_Tp>
3661allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3662{
3663 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
3664}
3665
3666#endif // _LIBCPP_HAS_NO_VARIADICS
3667
3668template<class _Tp, class _Up>
3669inline _LIBCPP_INLINE_VISIBILITY
3670bool
Howard Hinnant1694d232011-05-28 14:41:13 +00003671operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003672{
3673 return __x.get() == __y.get();
3674}
3675
3676template<class _Tp, class _Up>
3677inline _LIBCPP_INLINE_VISIBILITY
3678bool
Howard Hinnant1694d232011-05-28 14:41:13 +00003679operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003680{
3681 return !(__x == __y);
3682}
3683
3684template<class _Tp, class _Up>
3685inline _LIBCPP_INLINE_VISIBILITY
3686bool
Howard Hinnant1694d232011-05-28 14:41:13 +00003687operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003688{
3689 return __x.get() < __y.get();
3690}
3691
3692template<class _Tp>
3693inline _LIBCPP_INLINE_VISIBILITY
3694void
Howard Hinnant1694d232011-05-28 14:41:13 +00003695swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003696{
3697 __x.swap(__y);
3698}
3699
3700template<class _Tp, class _Up>
3701inline _LIBCPP_INLINE_VISIBILITY
3702shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003703static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003704{
3705 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
3706}
3707
3708template<class _Tp, class _Up>
3709inline _LIBCPP_INLINE_VISIBILITY
3710shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003711dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003712{
3713 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
3714 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3715}
3716
3717template<class _Tp, class _Up>
3718shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003719const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003720{
3721 return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get()));
3722}
3723
Howard Hinnantd4444702010-08-11 17:04:31 +00003724#ifndef _LIBCPP_NO_RTTI
3725
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003726template<class _Dp, class _Tp>
3727inline _LIBCPP_INLINE_VISIBILITY
3728_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00003729get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003730{
3731 return __p.template __get_deleter<_Dp>();
3732}
3733
Howard Hinnant324bb032010-08-22 00:02:43 +00003734#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003735
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003736template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003737class _LIBCPP_VISIBLE weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003738{
Howard Hinnant324bb032010-08-22 00:02:43 +00003739public:
3740 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003741private:
3742 element_type* __ptr_;
3743 __shared_weak_count* __cntrl_;
3744
Howard Hinnant324bb032010-08-22 00:02:43 +00003745public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003746 weak_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003747 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003748 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3749 _NOEXCEPT;
3750 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003751 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003752 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3753 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00003754
3755 ~weak_ptr();
3756
Howard Hinnant1694d232011-05-28 14:41:13 +00003757 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
3758 template<class _Yp> weak_ptr& operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
3759 template<class _Yp> weak_ptr& operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00003760
Howard Hinnant1694d232011-05-28 14:41:13 +00003761 void swap(weak_ptr& __r) _NOEXCEPT;
3762 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00003763
Howard Hinnant82894812010-09-22 16:48:34 +00003764 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003765 long use_count() const _NOEXCEPT
3766 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003767 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003768 bool expired() const _NOEXCEPT
3769 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
3770 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003771 template<class _Up>
3772 _LIBCPP_INLINE_VISIBILITY
3773 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003774 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003775 template<class _Up>
3776 _LIBCPP_INLINE_VISIBILITY
3777 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003778 {return __cntrl_ < __r.__cntrl_;}
3779
Howard Hinnant82894812010-09-22 16:48:34 +00003780 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
3781 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003782};
3783
3784template<class _Tp>
3785inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003786weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003787 : __ptr_(0),
3788 __cntrl_(0)
3789{
3790}
3791
3792template<class _Tp>
3793inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003794weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003795 : __ptr_(__r.__ptr_),
3796 __cntrl_(__r.__cntrl_)
3797{
3798 if (__cntrl_)
3799 __cntrl_->__add_weak();
3800}
3801
3802template<class _Tp>
3803template<class _Yp>
3804inline _LIBCPP_INLINE_VISIBILITY
3805weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00003806 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003807 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003808 : __ptr_(__r.__ptr_),
3809 __cntrl_(__r.__cntrl_)
3810{
3811 if (__cntrl_)
3812 __cntrl_->__add_weak();
3813}
3814
3815template<class _Tp>
3816template<class _Yp>
3817inline _LIBCPP_INLINE_VISIBILITY
3818weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00003819 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003820 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003821 : __ptr_(__r.__ptr_),
3822 __cntrl_(__r.__cntrl_)
3823{
3824 if (__cntrl_)
3825 __cntrl_->__add_weak();
3826}
3827
3828template<class _Tp>
3829weak_ptr<_Tp>::~weak_ptr()
3830{
3831 if (__cntrl_)
3832 __cntrl_->__release_weak();
3833}
3834
3835template<class _Tp>
3836inline _LIBCPP_INLINE_VISIBILITY
3837weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003838weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003839{
3840 weak_ptr(__r).swap(*this);
3841 return *this;
3842}
3843
3844template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00003845template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003846inline _LIBCPP_INLINE_VISIBILITY
3847weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003848weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003849{
3850 weak_ptr(__r).swap(*this);
3851 return *this;
3852}
3853
3854template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00003855template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003856inline _LIBCPP_INLINE_VISIBILITY
3857weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003858weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003859{
3860 weak_ptr(__r).swap(*this);
3861 return *this;
3862}
3863
3864template<class _Tp>
3865inline _LIBCPP_INLINE_VISIBILITY
3866void
Howard Hinnant1694d232011-05-28 14:41:13 +00003867weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003868{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003869 _VSTD::swap(__ptr_, __r.__ptr_);
3870 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003871}
3872
3873template<class _Tp>
3874inline _LIBCPP_INLINE_VISIBILITY
3875void
Howard Hinnant1694d232011-05-28 14:41:13 +00003876swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003877{
3878 __x.swap(__y);
3879}
3880
3881template<class _Tp>
3882inline _LIBCPP_INLINE_VISIBILITY
3883void
Howard Hinnant1694d232011-05-28 14:41:13 +00003884weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003885{
3886 weak_ptr().swap(*this);
3887}
3888
3889template<class _Tp>
3890template<class _Yp>
3891shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
3892 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3893 : __ptr_(__r.__ptr_),
3894 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3895{
3896 if (__cntrl_ == 0)
3897#ifndef _LIBCPP_NO_EXCEPTIONS
3898 throw bad_weak_ptr();
3899#else
3900 assert(!"bad_weak_ptr");
3901#endif
3902}
3903
3904template<class _Tp>
3905shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003906weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003907{
3908 shared_ptr<_Tp> __r;
3909 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3910 if (__r.__cntrl_)
3911 __r.__ptr_ = __ptr_;
3912 return __r;
3913}
3914
Howard Hinnant324bb032010-08-22 00:02:43 +00003915template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003916
3917template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003918struct _LIBCPP_VISIBLE owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003919 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00003920{
3921 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003922 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003923 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3924 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003925 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003926 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3927 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003929 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3930 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00003931};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003932
3933template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003934struct _LIBCPP_VISIBLE owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003935 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3936{
Howard Hinnant324bb032010-08-22 00:02:43 +00003937 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003938 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003939 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3940 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003941 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003942 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3943 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003944 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003945 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3946 {return __x.owner_before(__y);}
3947};
3948
3949template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003950class _LIBCPP_VISIBLE enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003951{
3952 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00003953protected:
Howard Hinnant82894812010-09-22 16:48:34 +00003954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003955 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00003956 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003957 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00003958 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003959 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
3960 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00003961 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003962 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00003963public:
Howard Hinnant82894812010-09-22 16:48:34 +00003964 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003965 shared_ptr<_Tp> shared_from_this()
3966 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00003967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003968 shared_ptr<_Tp const> shared_from_this() const
3969 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003970
3971 template <class _Up> friend class shared_ptr;
3972};
3973
Howard Hinnant21aefc32010-06-03 16:42:57 +00003974template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003975struct _LIBCPP_VISIBLE hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003976{
3977 typedef shared_ptr<_Tp> argument_type;
3978 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003979 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003980 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003981 {
3982 return hash<_Tp*>()(__ptr.get());
3983 }
3984};
3985
Howard Hinnant324bb032010-08-22 00:02:43 +00003986//enum class
Howard Hinnant82894812010-09-22 16:48:34 +00003987struct _LIBCPP_VISIBLE pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003988{
3989 enum _
3990 {
3991 relaxed,
3992 preferred,
3993 strict
3994 };
3995
3996 _ __v_;
3997
Howard Hinnant82894812010-09-22 16:48:34 +00003998 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003999 pointer_safety(_ __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00004000 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004001 operator int() const {return __v_;}
4002};
4003
4004void declare_reachable(void* __p);
4005void declare_no_pointers(char* __p, size_t __n);
4006void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnant1694d232011-05-28 14:41:13 +00004007pointer_safety get_pointer_safety() _NOEXCEPT;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00004008void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004009
4010template <class _Tp>
4011inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00004012_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004013undeclare_reachable(_Tp* __p)
4014{
4015 return static_cast<_Tp*>(__undeclare_reachable(__p));
4016}
4017
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00004018void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004019
4020_LIBCPP_END_NAMESPACE_STD
4021
4022#endif // _LIBCPP_MEMORY