blob: 37700995c3748afcbbb1cdf3953589ccbf8f97c1 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_MEMORY
12#define _LIBCPP_MEMORY
13
14/*
15 memory synopsis
16
17namespace std
18{
19
20struct allocator_arg_t { };
21constexpr allocator_arg_t allocator_arg = allocator_arg_t();
22
23template <class T, class Alloc> struct uses_allocator;
24
25template <class Ptr>
26struct pointer_traits
27{
28 typedef Ptr pointer;
29 typedef <details> element_type;
30 typedef <details> difference_type;
Howard Hinnant324bb032010-08-22 00:02:43 +000031
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000032 template <class U> using rebind = <details>;
Howard Hinnant324bb032010-08-22 00:02:43 +000033
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000034 static pointer pointer_to(<details>);
35};
36
Howard Hinnant1694d232011-05-28 14:41:13 +000037template <class T>
38struct pointer_traits<T*>
39{
40 typedef T* pointer;
41 typedef T element_type;
42 typedef ptrdiff_t difference_type;
43
44 template <class U> using rebind = U*;
45
46 static pointer pointer_to(<details>) noexcept;
47};
48
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000049template <class Alloc>
50struct allocator_traits
51{
52 typedef Alloc allocator_type;
53 typedef typename allocator_type::value_type
54 value_type;
55
56 typedef Alloc::pointer | value_type* pointer;
57 typedef Alloc::const_pointer
58 | pointer_traits<pointer>::rebind<const value_type>
59 const_pointer;
60 typedef Alloc::void_pointer
61 | pointer_traits<pointer>::rebind<void>
62 void_pointer;
63 typedef Alloc::const_void_pointer
64 | pointer_traits<pointer>::rebind<const void>
65 const_void_pointer;
66 typedef Alloc::difference_type
Howard Hinnant47761072010-11-18 01:40:00 +000067 | pointer_traits<pointer>::difference_type
68 difference_type;
69 typedef Alloc::size_type
70 | make_unsigned<difference_type>::type
71 size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000072 typedef Alloc::propagate_on_container_copy_assignment
73 | false_type propagate_on_container_copy_assignment;
74 typedef Alloc::propagate_on_container_move_assignment
75 | false_type propagate_on_container_move_assignment;
76 typedef Alloc::propagate_on_container_swap
77 | false_type propagate_on_container_swap;
78
79 template <class T> using rebind_alloc = Alloc::rebind<U>::other | Alloc<T, Args...>;
80 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
81
82 static pointer allocate(allocator_type& a, size_type n);
83 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint);
84
Howard Hinnant1694d232011-05-28 14:41:13 +000085 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000086
87 template <class T, class... Args>
88 static void construct(allocator_type& a, T* p, Args&&... args);
89
90 template <class T>
91 static void destroy(allocator_type& a, T* p);
92
93 static size_type max_size(const allocator_type& a);
94
95 static allocator_type
96 select_on_container_copy_construction(const allocator_type& a);
97};
98
99template <>
100class allocator<void>
101{
102public:
103 typedef void* pointer;
104 typedef const void* const_pointer;
105 typedef void value_type;
106
107 template <class _Up> struct rebind {typedef allocator<_Up> other;};
108};
109
110template <class T>
111class allocator
112{
113public:
114 typedef size_t size_type;
115 typedef ptrdiff_t difference_type;
116 typedef T* pointer;
117 typedef const T* const_pointer;
118 typedef typename add_lvalue_reference<T>::type reference;
119 typedef typename add_lvalue_reference<const T>::type const_reference;
120 typedef T value_type;
121
122 template <class U> struct rebind {typedef allocator<U> other;};
123
Howard Hinnant1694d232011-05-28 14:41:13 +0000124 allocator() noexcept;
125 allocator(const allocator&) noexcept;
126 template <class U> allocator(const allocator<U>&) noexcept;
127 ~allocator();
128 pointer address(reference x) const noexcept;
129 const_pointer address(const_reference x) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000130 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
Howard Hinnant1694d232011-05-28 14:41:13 +0000131 void deallocate(pointer p, size_type n) noexcept;
132 size_type max_size() const noexcept;
133 template<class U, class... Args>
134 void construct(U* p, Args&&... args);
135 template <class U>
136 void destroy(U* p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000137};
138
139template <class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000140bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141
142template <class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000143bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000144
145template <class OutputIterator, class T>
146class raw_storage_iterator
147 : public iterator<output_iterator_tag,
148 T, // purposefully not C++03
149 ptrdiff_t, // purposefully not C++03
150 T*, // purposefully not C++03
151 raw_storage_iterator&> // purposefully not C++03
152{
153public:
154 explicit raw_storage_iterator(OutputIterator x);
155 raw_storage_iterator& operator*();
156 raw_storage_iterator& operator=(const T& element);
157 raw_storage_iterator& operator++();
158 raw_storage_iterator operator++(int);
159};
160
Howard Hinnant1694d232011-05-28 14:41:13 +0000161template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
162template <class T> void return_temporary_buffer(T* p) noexcept;
163
164template <class T> T* addressof(T& r) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000165
166template <class InputIterator, class ForwardIterator>
167ForwardIterator
168uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
169
Howard Hinnant1694d232011-05-28 14:41:13 +0000170template <class InputIterator, class Size, class ForwardIterator>
171ForwardIterator
172uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
173
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000174template <class ForwardIterator, class T>
175void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
176
177template <class ForwardIterator, class Size, class T>
Howard Hinnant2f6a6272010-11-18 16:13:03 +0000178ForwardIterator
179uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000180
181template <class Y> struct auto_ptr_ref {};
182
183template<class X>
184class auto_ptr
185{
186public:
187 typedef X element_type;
188
189 explicit auto_ptr(X* p =0) throw();
190 auto_ptr(auto_ptr&) throw();
191 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
192 auto_ptr& operator=(auto_ptr&) throw();
193 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
194 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
195 ~auto_ptr() throw();
196
197 typename add_lvalue_reference<X>::type operator*() const throw();
198 X* operator->() const throw();
199 X* get() const throw();
200 X* release() throw();
201 void reset(X* p =0) throw();
202
203 auto_ptr(auto_ptr_ref<X>) throw();
204 template<class Y> operator auto_ptr_ref<Y>() throw();
205 template<class Y> operator auto_ptr<Y>() throw();
206};
207
Howard Hinnante92c3d72010-08-19 18:39:17 +0000208template <class T>
209struct default_delete
210{
Howard Hinnant1694d232011-05-28 14:41:13 +0000211 constexpr default_delete() noexcept = default;
212 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000213
Howard Hinnant1694d232011-05-28 14:41:13 +0000214 void operator()(T*) const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000215};
216
217template <class T>
218struct default_delete<T[]>
219{
Howard Hinnant1694d232011-05-28 14:41:13 +0000220 constexpr default_delete() noexcept = default;
221 void operator()(T*) const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000222 template <class U> void operator()(U*) const = delete;
223};
224
Howard Hinnante92c3d72010-08-19 18:39:17 +0000225template <class T, class D = default_delete<T>>
226class unique_ptr
227{
228public:
229 typedef see below pointer;
230 typedef T element_type;
231 typedef D deleter_type;
232
233 // constructors
Howard Hinnant1694d232011-05-28 14:41:13 +0000234 constexpr unique_ptr() noexcept;
235 explicit unique_ptr(pointer p) noexcept;
236 unique_ptr(pointer p, see below d1) noexcept;
237 unique_ptr(pointer p, see below d2) noexcept;
238 unique_ptr(unique_ptr&& u) noexcept;
239 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnante92c3d72010-08-19 18:39:17 +0000240 template <class U, class E>
Howard Hinnant1694d232011-05-28 14:41:13 +0000241 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000242 template <class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000243 unique_ptr(auto_ptr<U>&& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000244
245 // destructor
246 ~unique_ptr();
247
248 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000249 unique_ptr& operator=(unique_ptr&& u) noexcept;
250 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
251 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000252
253 // observers
254 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant1694d232011-05-28 14:41:13 +0000255 pointer operator->() const noexcept;
256 pointer get() const noexcept;
257 deleter_type& get_deleter() noexcept;
258 const deleter_type& get_deleter() const noexcept;
259 explicit operator bool() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000260
261 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000262 pointer release() noexcept;
263 void reset(pointer p = pointer()) noexcept;
264 void swap(unique_ptr& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000265};
266
267template <class T, class D>
268class unique_ptr<T[], D>
269{
270public:
271 typedef implementation-defined pointer;
272 typedef T element_type;
273 typedef D deleter_type;
274
275 // constructors
Howard Hinnant1694d232011-05-28 14:41:13 +0000276 constexpr unique_ptr() noexcept;
277 explicit unique_ptr(pointer p) noexcept;
278 unique_ptr(pointer p, see below d) noexcept;
279 unique_ptr(pointer p, see below d) noexcept;
280 unique_ptr(unique_ptr&& u) noexcept;
281 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnante92c3d72010-08-19 18:39:17 +0000282
283 // destructor
Howard Hinnant324bb032010-08-22 00:02:43 +0000284 ~unique_ptr();
Howard Hinnante92c3d72010-08-19 18:39:17 +0000285
286 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000287 unique_ptr& operator=(unique_ptr&& u) noexcept;
288 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000289
290 // observers
291 T& operator[](size_t i) const;
Howard Hinnant1694d232011-05-28 14:41:13 +0000292 pointer get() const noexcept;
293 deleter_type& get_deleter() noexcept;
294 const deleter_type& get_deleter() const noexcept;
295 explicit operator bool() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000296
297 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000298 pointer release() noexcept;
299 void reset(pointer p = pointer()) noexcept;
300 void reset(nullptr_t) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000301 template <class U> void reset(U) = delete;
Howard Hinnant1694d232011-05-28 14:41:13 +0000302 void swap(unique_ptr& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000303};
304
305template <class T, class D>
Howard Hinnant1694d232011-05-28 14:41:13 +0000306 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000307
308template <class T1, class D1, class T2, class D2>
309 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
310template <class T1, class D1, class T2, class D2>
311 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
312template <class T1, class D1, class T2, class D2>
313 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
314template <class T1, class D1, class T2, class D2>
315 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
316template <class T1, class D1, class T2, class D2>
317 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
318template <class T1, class D1, class T2, class D2>
319 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
320
Howard Hinnant1694d232011-05-28 14:41:13 +0000321template <class T, class D>
322 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
323template <class T, class D>
324 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
325template <class T, class D>
326 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
327template <class T, class D>
328 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
329
330template <class T, class D>
331 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
332template <class T, class D>
333 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
334template <class T, class D>
335 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
336template <class T, class D>
337 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
338template <class T, class D>
339 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
340template <class T, class D>
341 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
342template <class T, class D>
343 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
344template <class T, class D>
345 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
346
Howard Hinnante92c3d72010-08-19 18:39:17 +0000347class bad_weak_ptr
348 : public std::exception
349{
Howard Hinnant1694d232011-05-28 14:41:13 +0000350 bad_weak_ptr() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000351};
352
353template<class T>
354class shared_ptr
355{
356public:
357 typedef T element_type;
358
359 // constructors:
Howard Hinnant1694d232011-05-28 14:41:13 +0000360 constexpr shared_ptr() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000361 template<class Y> explicit shared_ptr(Y* p);
362 template<class Y, class D> shared_ptr(Y* p, D d);
363 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
364 template <class D> shared_ptr(nullptr_t p, D d);
365 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant1694d232011-05-28 14:41:13 +0000366 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
367 shared_ptr(const shared_ptr& r) noexcept;
368 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
369 shared_ptr(shared_ptr&& r) noexcept;
370 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000371 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
372 template<class Y> shared_ptr(auto_ptr<Y>&& r);
373 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
374 shared_ptr(nullptr_t) : shared_ptr() { }
375
376 // destructor:
377 ~shared_ptr();
378
379 // assignment:
Howard Hinnant1694d232011-05-28 14:41:13 +0000380 shared_ptr& operator=(const shared_ptr& r) noexcept;
381 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
382 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000383 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
384 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r);
385 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
386
387 // modifiers:
Howard Hinnant1694d232011-05-28 14:41:13 +0000388 void swap(shared_ptr& r) noexcept;
389 void reset() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000390 template<class Y> void reset(Y* p);
391 template<class Y, class D> void reset(Y* p, D d);
392 template<class Y, class D, class A> void reset(Y* p, D d, A a);
393
Howard Hinnant1694d232011-05-28 14:41:13 +0000394 // observers:
395 T* get() const noexcept;
396 T& operator*() const noexcept;
397 T* operator->() const noexcept;
398 long use_count() const noexcept;
399 bool unique() const noexcept;
400 explicit operator bool() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000401 template<class U> bool owner_before(shared_ptr<U> const& b) const;
402 template<class U> bool owner_before(weak_ptr<U> const& b) const;
403};
404
405// shared_ptr comparisons:
406template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000407 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000408template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000409 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000410template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000411 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000412template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000413 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000414template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000415 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000416template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000417 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
418
419template <class T>
420 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
421template <class T>
422 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
423template <class T>
424 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
425template <class T>
426 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
427template <class T>
428 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
429template <class T>
430bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
431template <class T>
432 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
433template <class T>
434 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
435template <class T>
436 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
437template <class T>
438 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
439template <class T>
440 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
441template <class T>
442 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000443
444// shared_ptr specialized algorithms:
Howard Hinnant1694d232011-05-28 14:41:13 +0000445template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000446
447// shared_ptr casts:
448template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000449 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000450template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000451 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000452template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000453 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000454
455// shared_ptr I/O:
456template<class E, class T, class Y>
457 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
458
459// shared_ptr get_deleter:
Howard Hinnant1694d232011-05-28 14:41:13 +0000460template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000461
462template<class T, class... Args>
463 shared_ptr<T> make_shared(Args&&... args);
464template<class T, class A, class... Args>
465 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
466
467template<class T>
468class weak_ptr
469{
470public:
471 typedef T element_type;
472
473 // constructors
Howard Hinnant1694d232011-05-28 14:41:13 +0000474 constexpr weak_ptr() noexcept;
475 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
476 weak_ptr(weak_ptr const& r) noexcept;
477 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000478
479 // destructor
480 ~weak_ptr();
481
482 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000483 weak_ptr& operator=(weak_ptr const& r) noexcept;
484 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
485 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000486
487 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000488 void swap(weak_ptr& r) noexcept;
489 void reset() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000490
491 // observers
Howard Hinnant1694d232011-05-28 14:41:13 +0000492 long use_count() const noexcept;
493 bool expired() const noexcept;
494 shared_ptr<T> lock() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000495 template<class U> bool owner_before(shared_ptr<U> const& b);
496 template<class U> bool owner_before(weak_ptr<U> const& b);
497};
498
499// weak_ptr specialized algorithms:
Howard Hinnant1694d232011-05-28 14:41:13 +0000500template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000501
502// class owner_less:
503template<class T> struct owner_less;
504
505template<class T>
506struct owner_less<shared_ptr<T>>
507 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
508{
509 typedef bool result_type;
510 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const;
511 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
512 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
513};
514
515template<class T>
516struct owner_less<weak_ptr<T>>
517 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
518{
519 typedef bool result_type;
520 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const;
521 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
522 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
523};
524
525template<class T>
526class enable_shared_from_this
527{
528protected:
Howard Hinnant1694d232011-05-28 14:41:13 +0000529 constexpr enable_shared_from_this() noexcept;
530 enable_shared_from_this(enable_shared_from_this const&) noexcept;
531 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000532 ~enable_shared_from_this();
533public:
534 shared_ptr<T> shared_from_this();
535 shared_ptr<T const> shared_from_this() const;
536};
537
538template<class T>
539 bool atomic_is_lock_free(const shared_ptr<T>* p);
540template<class T>
541 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
542template<class T>
543 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
544template<class T>
545 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
546template<class T>
547 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
548template<class T>
549 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
550template<class T>
551 shared_ptr<T>
552 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
553template<class T>
554 bool
555 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
556template<class T>
557 bool
558 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
559template<class T>
560 bool
561 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
562 shared_ptr<T> w, memory_order success,
563 memory_order failure);
564template<class T>
565 bool
566 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
567 shared_ptr<T> w, memory_order success,
568 memory_order failure);
569// Hash support
570template <class T> struct hash;
571template <class T, class D> struct hash<unique_ptr<T, D> >;
572template <class T> struct hash<shared_ptr<T> >;
573
574// Pointer safety
575enum class pointer_safety { relaxed, preferred, strict };
576void declare_reachable(void *p);
577template <class T> T *undeclare_reachable(T *p);
578void declare_no_pointers(char *p, size_t n);
579void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant1694d232011-05-28 14:41:13 +0000580pointer_safety get_pointer_safety() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000581
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000582void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
583
584} // std
585
586*/
587
588#include <__config>
589#include <type_traits>
590#include <typeinfo>
591#include <cstddef>
592#include <cstdint>
593#include <new>
594#include <utility>
595#include <limits>
596#include <iterator>
597#include <__functional_base>
598#if defined(_LIBCPP_NO_EXCEPTIONS)
599 #include <cassert>
600#endif
601
602#pragma GCC system_header
603
604_LIBCPP_BEGIN_NAMESPACE_STD
605
606// allocator_arg_t
607
Howard Hinnant82894812010-09-22 16:48:34 +0000608struct _LIBCPP_VISIBLE allocator_arg_t { };
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000609
610extern const allocator_arg_t allocator_arg;
611
612// addressof
613
614template <class _Tp>
615inline _LIBCPP_INLINE_VISIBILITY
616_Tp*
Howard Hinnant1694d232011-05-28 14:41:13 +0000617addressof(_Tp& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000618{
619 return (_Tp*)&(char&)__x;
620}
621
622template <class _Tp> class allocator;
623
624template <>
Howard Hinnant82894812010-09-22 16:48:34 +0000625class _LIBCPP_VISIBLE allocator<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000626{
627public:
628 typedef void* pointer;
629 typedef const void* const_pointer;
630 typedef void value_type;
631
632 template <class _Up> struct rebind {typedef allocator<_Up> other;};
633};
634
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000635// pointer_traits
636
637template <class _Tp>
638struct __has_element_type
639{
640private:
641 struct __two {char _; char __;};
642 template <class _Up> static __two __test(...);
643 template <class _Up> static char __test(typename _Up::element_type* = 0);
644public:
645 static const bool value = sizeof(__test<_Tp>(0)) == 1;
646};
647
648template <class _Ptr, bool = __has_element_type<_Ptr>::value>
649struct __pointer_traits_element_type;
650
651template <class _Ptr>
652struct __pointer_traits_element_type<_Ptr, true>
653{
654 typedef typename _Ptr::element_type type;
655};
656
657#ifndef _LIBCPP_HAS_NO_VARIADICS
658
659template <template <class, class...> class _Sp, class _Tp, class ..._Args>
660struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
661{
662 typedef typename _Sp<_Tp, _Args...>::element_type type;
663};
664
665template <template <class, class...> class _Sp, class _Tp, class ..._Args>
666struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
667{
668 typedef _Tp type;
669};
670
Howard Hinnant324bb032010-08-22 00:02:43 +0000671#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000672
673template <template <class> class _Sp, class _Tp>
674struct __pointer_traits_element_type<_Sp<_Tp>, true>
675{
676 typedef typename _Sp<_Tp>::element_type type;
677};
678
679template <template <class> class _Sp, class _Tp>
680struct __pointer_traits_element_type<_Sp<_Tp>, false>
681{
682 typedef _Tp type;
683};
684
685template <template <class, class> class _Sp, class _Tp, class _A0>
686struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
687{
688 typedef typename _Sp<_Tp, _A0>::element_type type;
689};
690
691template <template <class, class> class _Sp, class _Tp, class _A0>
692struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
693{
694 typedef _Tp type;
695};
696
697template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
698struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
699{
700 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
701};
702
703template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
704struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
705{
706 typedef _Tp type;
707};
708
709template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
710 class _A1, class _A2>
711struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
712{
713 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
714};
715
716template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
717 class _A1, class _A2>
718struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
719{
720 typedef _Tp type;
721};
722
Howard Hinnant324bb032010-08-22 00:02:43 +0000723#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000724
725template <class _Tp>
726struct __has_difference_type
727{
728private:
729 struct __two {char _; char __;};
730 template <class _Up> static __two __test(...);
731 template <class _Up> static char __test(typename _Up::difference_type* = 0);
732public:
733 static const bool value = sizeof(__test<_Tp>(0)) == 1;
734};
735
736template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
737struct __pointer_traits_difference_type
738{
739 typedef ptrdiff_t type;
740};
741
742template <class _Ptr>
743struct __pointer_traits_difference_type<_Ptr, true>
744{
745 typedef typename _Ptr::difference_type type;
746};
747
748template <class _Tp, class _Up>
749struct __has_rebind
750{
751private:
752 struct __two {char _; char __;};
753 template <class _Xp> static __two __test(...);
754 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
755public:
756 static const bool value = sizeof(__test<_Tp>(0)) == 1;
757};
758
759template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
760struct __pointer_traits_rebind
761{
762#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
763 typedef typename _Tp::template rebind<_Up> type;
764#else
765 typedef typename _Tp::template rebind<_Up>::other type;
766#endif
767};
768
769#ifndef _LIBCPP_HAS_NO_VARIADICS
770
771template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
772struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
773{
774#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
775 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
776#else
777 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
778#endif
779};
780
781template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
782struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
783{
784 typedef _Sp<_Up, _Args...> type;
785};
786
Howard Hinnant324bb032010-08-22 00:02:43 +0000787#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000788
789template <template <class> class _Sp, class _Tp, class _Up>
790struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
791{
792#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
793 typedef typename _Sp<_Tp>::template rebind<_Up> type;
794#else
795 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
796#endif
797};
798
799template <template <class> class _Sp, class _Tp, class _Up>
800struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
801{
802 typedef _Sp<_Up> type;
803};
804
805template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
806struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
807{
808#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
809 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
810#else
811 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
812#endif
813};
814
815template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
816struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
817{
818 typedef _Sp<_Up, _A0> type;
819};
820
821template <template <class, class, class> class _Sp, class _Tp, class _A0,
822 class _A1, class _Up>
823struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
824{
825#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
826 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
827#else
828 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
829#endif
830};
831
832template <template <class, class, class> class _Sp, class _Tp, class _A0,
833 class _A1, class _Up>
834struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
835{
836 typedef _Sp<_Up, _A0, _A1> type;
837};
838
839template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
840 class _A1, class _A2, class _Up>
841struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
842{
843#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
844 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
845#else
846 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
847#endif
848};
849
850template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
851 class _A1, class _A2, class _Up>
852struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
853{
854 typedef _Sp<_Up, _A0, _A1, _A2> type;
855};
856
Howard Hinnant324bb032010-08-22 00:02:43 +0000857#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000858
859template <class _Ptr>
Howard Hinnant82894812010-09-22 16:48:34 +0000860struct _LIBCPP_VISIBLE pointer_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000861{
862 typedef _Ptr pointer;
863 typedef typename __pointer_traits_element_type<pointer>::type element_type;
864 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
865
866#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant6b41c602011-05-11 20:21:19 +0000867 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000868#else
869 template <class _Up> struct rebind
870 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant324bb032010-08-22 00:02:43 +0000871#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000872
873private:
874 struct __nat {};
875public:
Howard Hinnant82894812010-09-22 16:48:34 +0000876 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000877 static pointer pointer_to(typename conditional<is_void<element_type>::value,
878 __nat, element_type>::type& __r)
879 {return pointer::pointer_to(__r);}
880};
881
882template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +0000883struct _LIBCPP_VISIBLE pointer_traits<_Tp*>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000884{
885 typedef _Tp* pointer;
886 typedef _Tp element_type;
887 typedef ptrdiff_t difference_type;
888
889#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
890 template <class _Up> using rebind = _Up*;
891#else
892 template <class _Up> struct rebind {typedef _Up* other;};
893#endif
894
895private:
896 struct __nat {};
897public:
Howard Hinnant82894812010-09-22 16:48:34 +0000898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000899 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant1694d232011-05-28 14:41:13 +0000900 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000901 {return _STD::addressof(__r);}
902};
903
904// allocator_traits
905
906namespace __has_pointer_type_imp
907{
908 template <class _Up> static __two test(...);
909 template <class _Up> static char test(typename _Up::pointer* = 0);
910}
911
912template <class _Tp>
913struct __has_pointer_type
914 : public integral_constant<bool, sizeof(__has_pointer_type_imp::test<_Tp>(0)) == 1>
915{
916};
917
918namespace __pointer_type_imp
919{
920
921template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
922struct __pointer_type
923{
924 typedef typename _Dp::pointer type;
925};
926
927template <class _Tp, class _Dp>
928struct __pointer_type<_Tp, _Dp, false>
929{
930 typedef _Tp* type;
931};
932
Howard Hinnant47761072010-11-18 01:40:00 +0000933} // __pointer_type_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000934
935template <class _Tp, class _Dp>
936struct __pointer_type
937{
938 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
939};
940
941template <class _Tp>
942struct __has_const_pointer
943{
944private:
945 struct __two {char _; char __;};
946 template <class _Up> static __two __test(...);
947 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
948public:
949 static const bool value = sizeof(__test<_Tp>(0)) == 1;
950};
951
952template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
953struct __const_pointer
954{
955 typedef typename _Alloc::const_pointer type;
956};
957
958template <class _Tp, class _Ptr, class _Alloc>
959struct __const_pointer<_Tp, _Ptr, _Alloc, false>
960{
961#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
962 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
963#else
964 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
965#endif
966};
967
968template <class _Tp>
969struct __has_void_pointer
970{
971private:
972 struct __two {char _; char __;};
973 template <class _Up> static __two __test(...);
974 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
975public:
976 static const bool value = sizeof(__test<_Tp>(0)) == 1;
977};
978
979template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
980struct __void_pointer
981{
982 typedef typename _Alloc::void_pointer type;
983};
984
985template <class _Ptr, class _Alloc>
986struct __void_pointer<_Ptr, _Alloc, false>
987{
988#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
989 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
990#else
991 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
992#endif
993};
994
995template <class _Tp>
996struct __has_const_void_pointer
997{
998private:
999 struct __two {char _; char __;};
1000 template <class _Up> static __two __test(...);
1001 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1002public:
1003 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1004};
1005
1006template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1007struct __const_void_pointer
1008{
1009 typedef typename _Alloc::const_void_pointer type;
1010};
1011
1012template <class _Ptr, class _Alloc>
1013struct __const_void_pointer<_Ptr, _Alloc, false>
1014{
1015#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1016 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1017#else
1018 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1019#endif
1020};
1021
1022template <class _T>
1023inline _LIBCPP_INLINE_VISIBILITY
1024_T*
Howard Hinnant1694d232011-05-28 14:41:13 +00001025__to_raw_pointer(_T* __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001026{
1027 return __p;
1028}
1029
1030template <class _Pointer>
1031inline _LIBCPP_INLINE_VISIBILITY
1032typename pointer_traits<_Pointer>::element_type*
Howard Hinnant1694d232011-05-28 14:41:13 +00001033__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001034{
1035 return _STD::__to_raw_pointer(__p.operator->());
1036}
1037
1038template <class _Tp>
1039struct __has_size_type
1040{
1041private:
1042 struct __two {char _; char __;};
1043 template <class _Up> static __two __test(...);
1044 template <class _Up> static char __test(typename _Up::size_type* = 0);
1045public:
1046 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1047};
1048
Howard Hinnant47761072010-11-18 01:40:00 +00001049template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001050struct __size_type
1051{
Howard Hinnant47761072010-11-18 01:40:00 +00001052 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001053};
1054
Howard Hinnant47761072010-11-18 01:40:00 +00001055template <class _Alloc, class _DiffType>
1056struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001057{
1058 typedef typename _Alloc::size_type type;
1059};
1060
1061template <class _Tp>
1062struct __has_propagate_on_container_copy_assignment
1063{
1064private:
1065 struct __two {char _; char __;};
1066 template <class _Up> static __two __test(...);
1067 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1068public:
1069 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1070};
1071
1072template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1073struct __propagate_on_container_copy_assignment
1074{
1075 typedef false_type type;
1076};
1077
1078template <class _Alloc>
1079struct __propagate_on_container_copy_assignment<_Alloc, true>
1080{
1081 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1082};
1083
1084template <class _Tp>
1085struct __has_propagate_on_container_move_assignment
1086{
1087private:
1088 struct __two {char _; char __;};
1089 template <class _Up> static __two __test(...);
1090 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1091public:
1092 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1093};
1094
1095template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1096struct __propagate_on_container_move_assignment
1097{
1098 typedef false_type type;
1099};
1100
1101template <class _Alloc>
1102struct __propagate_on_container_move_assignment<_Alloc, true>
1103{
1104 typedef typename _Alloc::propagate_on_container_move_assignment type;
1105};
1106
1107template <class _Tp>
1108struct __has_propagate_on_container_swap
1109{
1110private:
1111 struct __two {char _; char __;};
1112 template <class _Up> static __two __test(...);
1113 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1114public:
1115 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1116};
1117
1118template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1119struct __propagate_on_container_swap
1120{
1121 typedef false_type type;
1122};
1123
1124template <class _Alloc>
1125struct __propagate_on_container_swap<_Alloc, true>
1126{
1127 typedef typename _Alloc::propagate_on_container_swap type;
1128};
1129
1130template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1131struct __has_rebind_other
1132{
1133private:
1134 struct __two {char _; char __;};
1135 template <class _Xp> static __two __test(...);
1136 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1137public:
1138 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1139};
1140
1141template <class _Tp, class _Up>
1142struct __has_rebind_other<_Tp, _Up, false>
1143{
1144 static const bool value = false;
1145};
1146
1147template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1148struct __allocator_traits_rebind
1149{
1150 typedef typename _Tp::template rebind<_Up>::other type;
1151};
1152
1153#ifndef _LIBCPP_HAS_NO_VARIADICS
1154
1155template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1156struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1157{
1158 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1159};
1160
1161template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1162struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1163{
1164 typedef _Alloc<_Up, _Args...> type;
1165};
1166
Howard Hinnant324bb032010-08-22 00:02:43 +00001167#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001168
1169template <template <class> class _Alloc, class _Tp, class _Up>
1170struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1171{
1172 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1173};
1174
1175template <template <class> class _Alloc, class _Tp, class _Up>
1176struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1177{
1178 typedef _Alloc<_Up> type;
1179};
1180
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001181template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1182struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1183{
1184 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1185};
1186
1187template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1188struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1189{
1190 typedef _Alloc<_Up, _A0> type;
1191};
1192
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001193template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1194 class _A1, class _Up>
1195struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1196{
1197 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1198};
1199
1200template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1201 class _A1, class _Up>
1202struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1203{
1204 typedef _Alloc<_Up, _A0, _A1> type;
1205};
1206
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001207template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1208 class _A1, class _A2, class _Up>
1209struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1210{
1211 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1212};
1213
1214template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1215 class _A1, class _A2, class _Up>
1216struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1217{
1218 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1219};
1220
Howard Hinnant324bb032010-08-22 00:02:43 +00001221#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001222
1223#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1224
1225template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1226auto
1227__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1228 -> decltype(__a.allocate(__sz, __p), true_type());
1229
1230template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1231auto
1232__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1233 -> false_type;
1234
1235template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1236struct __has_allocate_hint
1237 : integral_constant<bool,
1238 is_same<
1239 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1240 declval<_SizeType>(),
1241 declval<_ConstVoidPtr>())),
1242 true_type>::value>
1243{
1244};
1245
Howard Hinnant324bb032010-08-22 00:02:43 +00001246#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001247
1248template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1249struct __has_allocate_hint
1250 : true_type
1251{
1252};
1253
Howard Hinnant324bb032010-08-22 00:02:43 +00001254#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001255
1256#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1257
1258template <class _Alloc, class _Tp, class ..._Args>
1259decltype(_STD::declval<_Alloc>().construct(_STD::declval<_Tp*>(),
1260 _STD::declval<_Args>()...),
1261 true_type())
1262__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1263
1264template <class _Alloc, class _Pointer, class ..._Args>
1265false_type
1266__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1267
1268template <class _Alloc, class _Pointer, class ..._Args>
1269struct __has_construct
1270 : integral_constant<bool,
1271 is_same<
1272 decltype(__has_construct_test(declval<_Alloc>(),
1273 declval<_Pointer>(),
1274 declval<_Args>()...)),
1275 true_type>::value>
1276{
1277};
1278
1279template <class _Alloc, class _Pointer>
1280auto
1281__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1282 -> decltype(__a.destroy(__p), true_type());
1283
1284template <class _Alloc, class _Pointer>
1285auto
1286__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1287 -> false_type;
1288
1289template <class _Alloc, class _Pointer>
1290struct __has_destroy
1291 : integral_constant<bool,
1292 is_same<
1293 decltype(__has_destroy_test(declval<_Alloc>(),
1294 declval<_Pointer>())),
1295 true_type>::value>
1296{
1297};
1298
1299template <class _Alloc>
1300auto
1301__has_max_size_test(_Alloc&& __a)
1302 -> decltype(__a.max_size(), true_type());
1303
1304template <class _Alloc>
1305auto
1306__has_max_size_test(const volatile _Alloc& __a)
1307 -> false_type;
1308
1309template <class _Alloc>
1310struct __has_max_size
1311 : integral_constant<bool,
1312 is_same<
1313 decltype(__has_max_size_test(declval<_Alloc&>())),
1314 true_type>::value>
1315{
1316};
1317
1318template <class _Alloc>
1319auto
1320__has_select_on_container_copy_construction_test(_Alloc&& __a)
1321 -> decltype(__a.select_on_container_copy_construction(), true_type());
1322
1323template <class _Alloc>
1324auto
1325__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1326 -> false_type;
1327
1328template <class _Alloc>
1329struct __has_select_on_container_copy_construction
1330 : integral_constant<bool,
1331 is_same<
1332 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1333 true_type>::value>
1334{
1335};
1336
Howard Hinnant324bb032010-08-22 00:02:43 +00001337#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001338
1339#ifndef _LIBCPP_HAS_NO_VARIADICS
1340
1341template <class _Alloc, class _Pointer, class ..._Args>
1342struct __has_construct
1343 : false_type
1344{
1345};
1346
Howard Hinnant324bb032010-08-22 00:02:43 +00001347#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001348
1349template <class _Alloc, class _Pointer>
1350struct __has_destroy
1351 : false_type
1352{
1353};
1354
1355template <class _Alloc>
1356struct __has_max_size
1357 : true_type
1358{
1359};
1360
1361template <class _Alloc>
1362struct __has_select_on_container_copy_construction
1363 : false_type
1364{
1365};
1366
Howard Hinnant324bb032010-08-22 00:02:43 +00001367#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001368
Howard Hinnant47761072010-11-18 01:40:00 +00001369template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1370struct __alloc_traits_difference_type
1371{
1372 typedef typename pointer_traits<_Ptr>::difference_type type;
1373};
1374
1375template <class _Alloc, class _Ptr>
1376struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1377{
1378 typedef typename _Alloc::difference_type type;
1379};
1380
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001381template <class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001382struct _LIBCPP_VISIBLE allocator_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001383{
1384 typedef _Alloc allocator_type;
1385 typedef typename allocator_type::value_type value_type;
1386
1387 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1388 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1389 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1390 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1391
Howard Hinnant47761072010-11-18 01:40:00 +00001392 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1393 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001394
1395 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1396 propagate_on_container_copy_assignment;
1397 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1398 propagate_on_container_move_assignment;
1399 typedef typename __propagate_on_container_swap<allocator_type>::type
1400 propagate_on_container_swap;
1401
1402#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1403 template <class _Tp> using rebind_alloc =
Howard Hinnant6b41c602011-05-11 20:21:19 +00001404 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001405 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant324bb032010-08-22 00:02:43 +00001406#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001407 template <class _Tp> struct rebind_alloc
1408 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1409 template <class _Tp> struct rebind_traits
1410 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant324bb032010-08-22 00:02:43 +00001411#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001412
Howard Hinnant82894812010-09-22 16:48:34 +00001413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001414 static pointer allocate(allocator_type& __a, size_type __n)
1415 {return __a.allocate(__n);}
Howard Hinnant82894812010-09-22 16:48:34 +00001416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001417 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1418 {return allocate(__a, __n, __hint,
1419 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1420
Howard Hinnant82894812010-09-22 16:48:34 +00001421 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001422 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001423 {__a.deallocate(__p, __n);}
1424
1425#ifndef _LIBCPP_HAS_NO_VARIADICS
1426 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001427 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001428 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
1429 {__construct(__has_construct<allocator_type, pointer, _Args...>(),
1430 __a, __p, _STD::forward<_Args>(__args)...);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001431#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001432 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001433 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001434 static void construct(allocator_type& __a, _Tp* __p)
1435 {
1436 ::new ((void*)__p) _Tp();
1437 }
1438 template <class _Tp, class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00001439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001440 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1441 {
1442 ::new ((void*)__p) _Tp(__a0);
1443 }
1444 template <class _Tp, class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00001445 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001446 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1447 const _A1& __a1)
1448 {
1449 ::new ((void*)__p) _Tp(__a0, __a1);
1450 }
1451 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00001452 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001453 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1454 const _A1& __a1, const _A2& __a2)
1455 {
1456 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1457 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001458#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001459
1460 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001461 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001462 static void destroy(allocator_type& __a, _Tp* __p)
1463 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1464
Howard Hinnant82894812010-09-22 16:48:34 +00001465 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001466 static size_type max_size(const allocator_type& __a)
1467 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1468
Howard Hinnant82894812010-09-22 16:48:34 +00001469 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001470 static allocator_type
1471 select_on_container_copy_construction(const allocator_type& __a)
1472 {return select_on_container_copy_construction(
1473 __has_select_on_container_copy_construction<const allocator_type>(),
1474 __a);}
1475
1476private:
1477
Howard Hinnant82894812010-09-22 16:48:34 +00001478 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001479 static pointer allocate(allocator_type& __a, size_type __n,
1480 const_void_pointer __hint, true_type)
1481 {return __a.allocate(__n, __hint);}
Howard Hinnant82894812010-09-22 16:48:34 +00001482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001483 static pointer allocate(allocator_type& __a, size_type __n,
1484 const_void_pointer __hint, false_type)
1485 {return __a.allocate(__n);}
1486
1487#ifndef _LIBCPP_HAS_NO_VARIADICS
1488 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001490 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
1491 {__a.construct(__p, _STD::forward<_Args>(__args)...);}
1492 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001493 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001494 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1495 {
1496 ::new ((void*)__p) _Tp(_STD::forward<_Args>(__args)...);
1497 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001498#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001499
1500 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001501 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001502 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1503 {__a.destroy(__p);}
1504 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001505 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001506 static void __destroy(false_type, allocator_type&, _Tp* __p)
1507 {
1508 __p->~_Tp();
1509 }
1510
Howard Hinnant82894812010-09-22 16:48:34 +00001511 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001512 static size_type __max_size(true_type, const allocator_type& __a)
1513 {return __a.max_size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001514 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001515 static size_type __max_size(false_type, const allocator_type&)
1516 {return numeric_limits<size_type>::max();}
1517
Howard Hinnant82894812010-09-22 16:48:34 +00001518 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001519 static allocator_type
1520 select_on_container_copy_construction(true_type, const allocator_type& __a)
1521 {return __a.select_on_container_copy_construction();}
Howard Hinnant82894812010-09-22 16:48:34 +00001522 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001523 static allocator_type
1524 select_on_container_copy_construction(false_type, const allocator_type& __a)
1525 {return __a;}
1526};
1527
1528// uses_allocator
1529
1530template <class _Tp>
1531struct __has_allocator_type
1532{
1533private:
1534 struct __two {char _; char __;};
1535 template <class _Up> static __two __test(...);
1536 template <class _Up> static char __test(typename _Up::allocator_type* = 0);
1537public:
1538 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1539};
1540
1541template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
1542struct __uses_allocator
1543 : public integral_constant<bool,
1544 is_convertible<_Alloc, typename _Tp::allocator_type>::value>
1545{
1546};
1547
1548template <class _Tp, class _Alloc>
1549struct __uses_allocator<_Tp, _Alloc, false>
1550 : public false_type
1551{
1552};
1553
1554template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001555struct _LIBCPP_VISIBLE uses_allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001556 : public __uses_allocator<_Tp, _Alloc>
1557{
1558};
1559
Howard Hinnantac38bae2011-01-11 20:02:45 +00001560#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001561
1562// uses-allocator construction
1563
1564template <class _Tp, class _Alloc, class ..._Args>
1565struct __uses_alloc_ctor_imp
1566{
1567 static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
1568 static const bool __ic =
1569 is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
1570 static const int value = __ua ? 2 - __ic : 0;
1571};
1572
1573template <class _Tp, class _Alloc, class ..._Args>
1574struct __uses_alloc_ctor
1575 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
1576 {};
1577
Howard Hinnantac38bae2011-01-11 20:02:45 +00001578#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001579
1580// allocator
1581
1582template <class _Tp>
Howard Hinnant36cdf022010-09-10 16:42:26 +00001583class _LIBCPP_VISIBLE allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001584{
1585public:
1586 typedef size_t size_type;
1587 typedef ptrdiff_t difference_type;
1588 typedef _Tp* pointer;
1589 typedef const _Tp* const_pointer;
1590 typedef _Tp& reference;
1591 typedef const _Tp& const_reference;
1592 typedef _Tp value_type;
1593
1594 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1595
Howard Hinnant1694d232011-05-28 14:41:13 +00001596 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1597 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1598 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
1599 {return _STD::addressof(__x);}
1600 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1601 {return _STD::addressof(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001602 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1603 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
Howard Hinnant1694d232011-05-28 14:41:13 +00001604 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
1605 {::operator delete((void*)__p);}
1606 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1607 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001608#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001609 template <class _Up, class... _Args>
1610 _LIBCPP_INLINE_VISIBILITY
1611 void
1612 construct(_Up* __p, _Args&&... __args)
1613 {
1614 ::new((void*)__p) _Up(_STD::forward<_Args>(__args)...);
1615 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001616#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001617 _LIBCPP_INLINE_VISIBILITY
1618 void
1619 construct(pointer __p)
1620 {
1621 ::new((void*)__p) _Tp();
1622 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001623# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001624 template <class _A0>
1625 _LIBCPP_INLINE_VISIBILITY
1626 typename enable_if
1627 <
1628 !is_convertible<_A0, __rv<_A0> >::value,
1629 void
1630 >::type
1631 construct(pointer __p, _A0& __a0)
1632 {
1633 ::new((void*)__p) _Tp(__a0);
1634 }
1635 template <class _A0>
1636 _LIBCPP_INLINE_VISIBILITY
1637 typename enable_if
1638 <
1639 !is_convertible<_A0, __rv<_A0> >::value,
1640 void
1641 >::type
1642 construct(pointer __p, const _A0& __a0)
1643 {
1644 ::new((void*)__p) _Tp(__a0);
1645 }
1646 template <class _A0>
1647 _LIBCPP_INLINE_VISIBILITY
1648 typename enable_if
1649 <
1650 is_convertible<_A0, __rv<_A0> >::value,
1651 void
1652 >::type
1653 construct(pointer __p, _A0 __a0)
1654 {
1655 ::new((void*)__p) _Tp(_STD::move(__a0));
1656 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001657# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001658 template <class _A0, class _A1>
1659 _LIBCPP_INLINE_VISIBILITY
1660 void
1661 construct(pointer __p, _A0& __a0, _A1& __a1)
1662 {
1663 ::new((void*)__p) _Tp(__a0, __a1);
1664 }
1665 template <class _A0, class _A1>
1666 _LIBCPP_INLINE_VISIBILITY
1667 void
1668 construct(pointer __p, const _A0& __a0, _A1& __a1)
1669 {
1670 ::new((void*)__p) _Tp(__a0, __a1);
1671 }
1672 template <class _A0, class _A1>
1673 _LIBCPP_INLINE_VISIBILITY
1674 void
1675 construct(pointer __p, _A0& __a0, const _A1& __a1)
1676 {
1677 ::new((void*)__p) _Tp(__a0, __a1);
1678 }
1679 template <class _A0, class _A1>
1680 _LIBCPP_INLINE_VISIBILITY
1681 void
1682 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1683 {
1684 ::new((void*)__p) _Tp(__a0, __a1);
1685 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001686#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001687 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1688};
1689
1690template <class _Tp, class _Up>
1691inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001692bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001693
1694template <class _Tp, class _Up>
1695inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001696bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001697
1698template <class _OutputIterator, class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001699class _LIBCPP_VISIBLE raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001700 : public iterator<output_iterator_tag,
1701 _Tp, // purposefully not C++03
1702 ptrdiff_t, // purposefully not C++03
1703 _Tp*, // purposefully not C++03
1704 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1705{
1706private:
1707 _OutputIterator __x_;
1708public:
1709 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1710 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1711 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1712 {::new(&*__x_) _Tp(__element); return *this;}
1713 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1714 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1715 {raw_storage_iterator __t(*this); ++__x_; return __t;}
1716};
1717
1718template <class _Tp>
1719pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00001720get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001721{
1722 pair<_Tp*, ptrdiff_t> __r(0, 0);
1723 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1724 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1725 / sizeof(_Tp);
1726 if (__n > __m)
1727 __n = __m;
1728 while (__n > 0)
1729 {
1730 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1731 if (__r.first)
1732 {
1733 __r.second = __n;
1734 break;
1735 }
1736 __n /= 2;
1737 }
1738 return __r;
1739}
1740
1741template <class _Tp>
1742inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001743void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001744
1745template <class _Tp>
1746struct auto_ptr_ref
1747{
1748 _Tp* __ptr_;
1749};
1750
1751template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001752class _LIBCPP_VISIBLE auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001753{
1754private:
1755 _Tp* __ptr_;
1756public:
1757 typedef _Tp element_type;
1758
1759 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1760 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1761 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1762 : __ptr_(__p.release()) {}
1763 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1764 {reset(__p.release()); return *this;}
1765 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1766 {reset(__p.release()); return *this;}
1767 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1768 {reset(__p.__ptr_); return *this;}
1769 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1770
1771 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1772 {return *__ptr_;}
1773 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1774 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1775 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1776 {
1777 _Tp* __t = __ptr_;
1778 __ptr_ = 0;
1779 return __t;
1780 }
1781 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1782 {
1783 if (__ptr_ != __p)
1784 delete __ptr_;
1785 __ptr_ = __p;
1786 }
1787
1788 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1789 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1790 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1791 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1792 {return auto_ptr<_Up>(release());}
1793};
1794
1795template <>
Howard Hinnant82894812010-09-22 16:48:34 +00001796class _LIBCPP_VISIBLE auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001797{
1798public:
1799 typedef void element_type;
1800};
1801
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001802template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1803 typename remove_cv<_T2>::type>::value,
1804 bool = is_empty<_T1>::value,
1805 bool = is_empty<_T2>::value>
1806struct __libcpp_compressed_pair_switch;
1807
1808template <class _T1, class _T2, bool IsSame>
1809struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1810
1811template <class _T1, class _T2, bool IsSame>
1812struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1813
1814template <class _T1, class _T2, bool IsSame>
1815struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1816
1817template <class _T1, class _T2>
1818struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
1819
1820template <class _T1, class _T2>
1821struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
1822
1823template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
1824class __libcpp_compressed_pair_imp;
1825
1826template <class _T1, class _T2>
1827class __libcpp_compressed_pair_imp<_T1, _T2, 0>
1828{
1829private:
1830 _T1 __first_;
1831 _T2 __second_;
1832public:
1833 typedef _T1 _T1_param;
1834 typedef _T2 _T2_param;
1835
1836 typedef typename remove_reference<_T1>::type& _T1_reference;
1837 typedef typename remove_reference<_T2>::type& _T2_reference;
1838
1839 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1840 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1841
1842 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1843 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
1844 : __first_(_STD::forward<_T1_param>(__t1)) {}
1845 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
1846 : __second_(_STD::forward<_T2_param>(__t2)) {}
1847 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1848 : __first_(_STD::forward<_T1_param>(__t1)), __second_(_STD::forward<_T2_param>(__t2)) {}
1849
Howard Hinnant73d21a42010-09-04 23:28:19 +00001850#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001851 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00001852 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
1853 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001854 : __first_(_STD::forward<_T1>(__p.first())), __second_(_STD::forward<_T2>(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001855#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001856
Howard Hinnant1694d232011-05-28 14:41:13 +00001857 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
1858 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001859
Howard Hinnant1694d232011-05-28 14:41:13 +00001860 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
1861 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001862
1863 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00001864 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1865 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001866 {
1867 using _STD::swap;
1868 swap(__first_, __x.__first_);
1869 swap(__second_, __x.__second_);
1870 }
1871};
1872
1873template <class _T1, class _T2>
1874class __libcpp_compressed_pair_imp<_T1, _T2, 1>
1875 : private _T1
1876{
1877private:
1878 _T2 __second_;
1879public:
1880 typedef _T1 _T1_param;
1881 typedef _T2 _T2_param;
1882
1883 typedef _T1& _T1_reference;
1884 typedef typename remove_reference<_T2>::type& _T2_reference;
1885
1886 typedef const _T1& _T1_const_reference;
1887 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1888
1889 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1890 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
1891 : _T1(_STD::forward<_T1_param>(__t1)) {}
1892 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
1893 : __second_(_STD::forward<_T2_param>(__t2)) {}
1894 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1895 : _T1(_STD::forward<_T1_param>(__t1)), __second_(_STD::forward<_T2_param>(__t2)) {}
1896
Howard Hinnant73d21a42010-09-04 23:28:19 +00001897#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001898 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00001899 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
1900 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001901 : _T1(_STD::move(__p.first())), __second_(_STD::forward<_T2>(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001902#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001903
Howard Hinnant1694d232011-05-28 14:41:13 +00001904 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
1905 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001906
Howard Hinnant1694d232011-05-28 14:41:13 +00001907 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
1908 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001909
1910 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00001911 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1912 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001913 {
1914 using _STD::swap;
1915 swap(__second_, __x.__second_);
1916 }
1917};
1918
1919template <class _T1, class _T2>
1920class __libcpp_compressed_pair_imp<_T1, _T2, 2>
1921 : private _T2
1922{
1923private:
1924 _T1 __first_;
1925public:
1926 typedef _T1 _T1_param;
1927 typedef _T2 _T2_param;
1928
1929 typedef typename remove_reference<_T1>::type& _T1_reference;
1930 typedef _T2& _T2_reference;
1931
1932 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1933 typedef const _T2& _T2_const_reference;
1934
1935 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1936 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
1937 : __first_(_STD::forward<_T1_param>(__t1)) {}
1938 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
1939 : _T2(_STD::forward<_T2_param>(__t2)) {}
1940 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00001941 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
1942 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001943 : _T2(_STD::forward<_T2_param>(__t2)), __first_(_STD::forward<_T1_param>(__t1)) {}
1944
Howard Hinnant73d21a42010-09-04 23:28:19 +00001945#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001946 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
1947 : _T2(_STD::forward<_T2>(__p.second())), __first_(_STD::move(__p.first())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001948#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001949
Howard Hinnant1694d232011-05-28 14:41:13 +00001950 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
1951 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001952
Howard Hinnant1694d232011-05-28 14:41:13 +00001953 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
1954 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001955
1956 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00001957 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1958 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001959 {
1960 using _STD::swap;
1961 swap(__first_, __x.__first_);
1962 }
1963};
1964
1965template <class _T1, class _T2>
1966class __libcpp_compressed_pair_imp<_T1, _T2, 3>
1967 : private _T1,
1968 private _T2
1969{
1970public:
1971 typedef _T1 _T1_param;
1972 typedef _T2 _T2_param;
1973
1974 typedef _T1& _T1_reference;
1975 typedef _T2& _T2_reference;
1976
1977 typedef const _T1& _T1_const_reference;
1978 typedef const _T2& _T2_const_reference;
1979
1980 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1981 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
1982 : _T1(_STD::forward<_T1_param>(__t1)) {}
1983 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
1984 : _T2(_STD::forward<_T2_param>(__t2)) {}
1985 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
1986 : _T1(_STD::forward<_T1_param>(__t1)), _T2(_STD::forward<_T2_param>(__t2)) {}
1987
Howard Hinnant73d21a42010-09-04 23:28:19 +00001988#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001989 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00001990 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
1991 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001992 : _T1(_STD::move(__p.first())), _T2(_STD::move(__p.second())) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001993#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001994
Howard Hinnant1694d232011-05-28 14:41:13 +00001995 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
1996 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001997
Howard Hinnant1694d232011-05-28 14:41:13 +00001998 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
1999 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002000
2001 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002002 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2003 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002004 {
2005 }
2006};
2007
2008template <class _T1, class _T2>
2009class __compressed_pair
2010 : private __libcpp_compressed_pair_imp<_T1, _T2>
2011{
2012 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2013public:
2014 typedef typename base::_T1_param _T1_param;
2015 typedef typename base::_T2_param _T2_param;
2016
2017 typedef typename base::_T1_reference _T1_reference;
2018 typedef typename base::_T2_reference _T2_reference;
2019
2020 typedef typename base::_T1_const_reference _T1_const_reference;
2021 typedef typename base::_T2_const_reference _T2_const_reference;
2022
2023 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
2024 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1, int = 0)
2025 : base(_STD::forward<_T1_param>(__t1)) {}
2026 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2, int* = 0)
2027 : base(_STD::forward<_T2_param>(__t2)) {}
2028 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
2029 : base(_STD::forward<_T1_param>(__t1), _STD::forward<_T2_param>(__t2)) {}
2030
Howard Hinnant73d21a42010-09-04 23:28:19 +00002031#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002032 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002033 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2034 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002035 : base(_STD::move(__p)) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +00002036#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002037
Howard Hinnant1694d232011-05-28 14:41:13 +00002038 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2039 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002040
Howard Hinnant1694d232011-05-28 14:41:13 +00002041 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2042 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002043
Howard Hinnant1694d232011-05-28 14:41:13 +00002044 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2045 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2046 __is_nothrow_swappable<_T1>::value)
2047 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002048};
2049
2050template <class _T1, class _T2>
2051inline _LIBCPP_INLINE_VISIBILITY
2052void
2053swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002054 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2055 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002056 {__x.swap(__y);}
2057
2058template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002059struct _LIBCPP_VISIBLE default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002060{
Howard Hinnant1694d232011-05-28 14:41:13 +00002061 _LIBCPP_INLINE_VISIBILITY default_delete() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002062 template <class _Up>
2063 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002064 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2065 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002066 {
2067 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2068 delete __ptr;
2069 }
2070};
2071
2072template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002073struct _LIBCPP_VISIBLE default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002074{
Howard Hinnant1694d232011-05-28 14:41:13 +00002075 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002076 {
2077 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2078 delete [] __ptr;
2079 }
2080private:
2081 template <class _Up> void operator() (_Up*) const;
2082};
2083
2084template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant82894812010-09-22 16:48:34 +00002085class _LIBCPP_VISIBLE unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002086{
2087public:
2088 typedef _Tp element_type;
2089 typedef _Dp deleter_type;
2090 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2091private:
2092 __compressed_pair<pointer, deleter_type> __ptr_;
2093
Howard Hinnant73d21a42010-09-04 23:28:19 +00002094#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002095 unique_ptr(const unique_ptr&);
2096 unique_ptr& operator=(const unique_ptr&);
2097 template <class _Up, class _Ep>
2098 unique_ptr(const unique_ptr<_Up, _Ep>&);
2099 template <class _Up, class _Ep>
2100 unique_ptr& operator=(const unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002101#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002102 unique_ptr(unique_ptr&);
2103 template <class _Up, class _Ep>
2104 unique_ptr(unique_ptr<_Up, _Ep>&);
2105 unique_ptr& operator=(unique_ptr&);
2106 template <class _Up, class _Ep>
2107 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002108#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002109
2110 struct __nat {int __for_bool_;};
2111
2112 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2113 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2114public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002115 _LIBCPP_INLINE_VISIBILITY unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002116 : __ptr_(pointer())
2117 {
2118 static_assert(!is_pointer<deleter_type>::value,
2119 "unique_ptr constructed with null function pointer deleter");
2120 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002121 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002122 : __ptr_(pointer())
2123 {
2124 static_assert(!is_pointer<deleter_type>::value,
2125 "unique_ptr constructed with null function pointer deleter");
2126 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002127 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002128 : __ptr_(_STD::move(__p))
2129 {
2130 static_assert(!is_pointer<deleter_type>::value,
2131 "unique_ptr constructed with null function pointer deleter");
2132 }
2133
Howard Hinnant73d21a42010-09-04 23:28:19 +00002134#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002135 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2136 is_reference<deleter_type>::value,
2137 deleter_type,
2138 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002139 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002140 : __ptr_(__p, __d) {}
2141
2142 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002143 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002144 : __ptr_(__p, _STD::move(__d))
2145 {
2146 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2147 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002148 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002149 : __ptr_(__u.release(), _STD::forward<deleter_type>(__u.get_deleter())) {}
2150 template <class _Up, class _Ep>
2151 _LIBCPP_INLINE_VISIBILITY
2152 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2153 typename enable_if
2154 <
2155 !is_array<_Up>::value &&
2156 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2157 is_convertible<_Ep, deleter_type>::value &&
2158 (
2159 !is_reference<deleter_type>::value ||
2160 is_same<deleter_type, _Ep>::value
2161 ),
2162 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002163 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002164 : __ptr_(__u.release(), _STD::forward<_Ep>(__u.get_deleter())) {}
2165
2166 template <class _Up>
2167 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2168 typename enable_if<
2169 is_convertible<_Up*, _Tp*>::value &&
2170 is_same<_Dp, default_delete<_Tp> >::value,
2171 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002172 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002173 : __ptr_(__p.release())
2174 {
2175 }
2176
Howard Hinnant1694d232011-05-28 14:41:13 +00002177 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002178 {
2179 reset(__u.release());
2180 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2181 return *this;
2182 }
2183
2184 template <class _Up, class _Ep>
2185 _LIBCPP_INLINE_VISIBILITY
2186 typename enable_if
2187 <
2188 !is_array<_Up>::value,
2189 unique_ptr&
2190 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002191 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002192 {
2193 reset(__u.release());
2194 __ptr_.second() = _STD::forward<_Ep>(__u.get_deleter());
2195 return *this;
2196 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002197#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002198
2199 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2200 {
2201 return __rv<unique_ptr>(*this);
2202 }
2203
2204 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
2205 : __ptr_(__u->release(), _STD::forward<deleter_type>(__u->get_deleter())) {}
2206
2207 template <class _Up, class _Ep>
2208 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2209 {
2210 reset(__u.release());
2211 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2212 return *this;
2213 }
2214
2215 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
2216 : __ptr_(_STD::move(__p), _STD::move(__d)) {}
2217
2218 template <class _Up>
2219 _LIBCPP_INLINE_VISIBILITY
2220 typename enable_if<
2221 is_convertible<_Up*, _Tp*>::value &&
2222 is_same<_Dp, default_delete<_Tp> >::value,
2223 unique_ptr&
2224 >::type
2225 operator=(auto_ptr<_Up> __p)
2226 {reset(__p.release()); return *this;}
2227
Howard Hinnant73d21a42010-09-04 23:28:19 +00002228#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002229 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2230
Howard Hinnant1694d232011-05-28 14:41:13 +00002231 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002232 {
2233 reset();
2234 return *this;
2235 }
2236
2237 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2238 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002239 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2240 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2241 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2242 {return __ptr_.second();}
2243 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2244 {return __ptr_.second();}
2245 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const
2246 _NOEXCEPT
2247 {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002248
Howard Hinnant1694d232011-05-28 14:41:13 +00002249 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002250 {
2251 pointer __t = __ptr_.first();
2252 __ptr_.first() = pointer();
2253 return __t;
2254 }
2255
Howard Hinnant1694d232011-05-28 14:41:13 +00002256 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002257 {
2258 pointer __tmp = __ptr_.first();
2259 __ptr_.first() = __p;
2260 if (__tmp)
2261 __ptr_.second()(__tmp);
2262 }
2263
Howard Hinnant1694d232011-05-28 14:41:13 +00002264 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2265 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002266};
2267
2268template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002269class _LIBCPP_VISIBLE unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002270{
2271public:
2272 typedef _Tp element_type;
2273 typedef _Dp deleter_type;
2274 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2275private:
2276 __compressed_pair<pointer, deleter_type> __ptr_;
2277
Howard Hinnant73d21a42010-09-04 23:28:19 +00002278#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002279 unique_ptr(const unique_ptr&);
2280 unique_ptr& operator=(const unique_ptr&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002281#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002282 unique_ptr(unique_ptr&);
2283 template <class _Up>
2284 unique_ptr(unique_ptr<_Up>&);
2285 unique_ptr& operator=(unique_ptr&);
2286 template <class _Up>
2287 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002288#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002289
2290 struct __nat {int __for_bool_;};
2291
2292 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2293 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2294public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002295 _LIBCPP_INLINE_VISIBILITY unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002296 : __ptr_(pointer())
2297 {
2298 static_assert(!is_pointer<deleter_type>::value,
2299 "unique_ptr constructed with null function pointer deleter");
2300 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002301 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002302 : __ptr_(pointer())
2303 {
2304 static_assert(!is_pointer<deleter_type>::value,
2305 "unique_ptr constructed with null function pointer deleter");
2306 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002307#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002308 template <class _P,
2309 class = typename enable_if<is_same<_P, pointer>::value>::type
2310 >
Howard Hinnant1694d232011-05-28 14:41:13 +00002311 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_P __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002312 : __ptr_(__p)
2313 {
2314 static_assert(!is_pointer<deleter_type>::value,
2315 "unique_ptr constructed with null function pointer deleter");
2316 }
2317
2318 template <class _P,
2319 class = typename enable_if<is_same<_P, pointer>::value>::type
2320 >
2321 _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename conditional<
2322 is_reference<deleter_type>::value,
2323 deleter_type,
2324 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002325 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002326 : __ptr_(__p, __d) {}
2327
2328 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2329 is_reference<deleter_type>::value,
2330 deleter_type,
2331 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002332 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002333 : __ptr_(pointer(), __d) {}
2334
2335 template <class _P,
2336 class = typename enable_if<is_same<_P, pointer>::value ||
2337 is_same<_P, nullptr_t>::value>::type
2338 >
2339 _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002340 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002341 : __ptr_(__p, _STD::move(__d))
2342 {
2343 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2344 }
2345
2346 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002347 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002348 : __ptr_(pointer(), _STD::move(__d))
2349 {
2350 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2351 }
2352
Howard Hinnant1694d232011-05-28 14:41:13 +00002353 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002354 : __ptr_(__u.release(), _STD::forward<deleter_type>(__u.get_deleter())) {}
2355
Howard Hinnant1694d232011-05-28 14:41:13 +00002356 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002357 {
2358 reset(__u.release());
2359 __ptr_.second() = _STD::forward<deleter_type>(__u.get_deleter());
2360 return *this;
2361 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002362#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002363
2364 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2365 : __ptr_(__p)
2366 {
2367 static_assert(!is_pointer<deleter_type>::value,
2368 "unique_ptr constructed with null function pointer deleter");
2369 }
2370
2371 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
2372 : __ptr_(__p, _STD::forward<deleter_type>(__d)) {}
2373
2374 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
2375 : __ptr_(pointer(), _STD::forward<deleter_type>(__d)) {}
2376
2377 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2378 {
2379 return __rv<unique_ptr>(*this);
2380 }
2381
2382 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
2383 : __ptr_(__u->release(), _STD::forward<deleter_type>(__u->get_deleter())) {}
2384
2385 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2386 {
2387 reset(__u->release());
2388 __ptr_.second() = _STD::forward<deleter_type>(__u->get_deleter());
2389 return *this;
2390 }
2391
Howard Hinnant73d21a42010-09-04 23:28:19 +00002392#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002393 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2394
Howard Hinnant1694d232011-05-28 14:41:13 +00002395 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002396 {
2397 reset();
2398 return *this;
2399 }
2400
2401 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2402 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002403 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2404 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2405 {return __ptr_.second();}
2406 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2407 {return __ptr_.second();}
2408 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const _NOEXCEPT
2409 {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002410
Howard Hinnant1694d232011-05-28 14:41:13 +00002411 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002412 {
2413 pointer __t = __ptr_.first();
2414 __ptr_.first() = pointer();
2415 return __t;
2416 }
2417
Howard Hinnant73d21a42010-09-04 23:28:19 +00002418#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002419 template <class _P,
2420 class = typename enable_if<is_same<_P, pointer>::value>::type
2421 >
Howard Hinnant1694d232011-05-28 14:41:13 +00002422 _LIBCPP_INLINE_VISIBILITY void reset(_P __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002423 {
2424 pointer __tmp = __ptr_.first();
2425 __ptr_.first() = __p;
2426 if (__tmp)
2427 __ptr_.second()(__tmp);
2428 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002429 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002430 {
2431 pointer __tmp = __ptr_.first();
2432 __ptr_.first() = nullptr;
2433 if (__tmp)
2434 __ptr_.second()(__tmp);
2435 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002436 _LIBCPP_INLINE_VISIBILITY void reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002437 {
2438 pointer __tmp = __ptr_.first();
2439 __ptr_.first() = nullptr;
2440 if (__tmp)
2441 __ptr_.second()(__tmp);
2442 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002443#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002444 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2445 {
2446 pointer __tmp = __ptr_.first();
2447 __ptr_.first() = __p;
2448 if (__tmp)
2449 __ptr_.second()(__tmp);
2450 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002451#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002452
2453 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2454private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002455
Howard Hinnant73d21a42010-09-04 23:28:19 +00002456#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002457 template <class _Up>
2458 explicit unique_ptr(_Up);
2459 template <class _Up>
2460 unique_ptr(_Up __u,
2461 typename conditional<
2462 is_reference<deleter_type>::value,
2463 deleter_type,
2464 typename add_lvalue_reference<const deleter_type>::type>::type,
2465 typename enable_if
2466 <
2467 is_convertible<_Up, pointer>::value,
2468 __nat
2469 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002470#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002471};
2472
2473template <class _Tp, class _Dp>
2474inline _LIBCPP_INLINE_VISIBILITY
2475void
Howard Hinnant1694d232011-05-28 14:41:13 +00002476swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002477
2478template <class _T1, class _D1, class _T2, class _D2>
2479inline _LIBCPP_INLINE_VISIBILITY
2480bool
2481operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2482
2483template <class _T1, class _D1, class _T2, class _D2>
2484inline _LIBCPP_INLINE_VISIBILITY
2485bool
2486operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2487
2488template <class _T1, class _D1, class _T2, class _D2>
2489inline _LIBCPP_INLINE_VISIBILITY
2490bool
2491operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() < __y.get();}
2492
2493template <class _T1, class _D1, class _T2, class _D2>
2494inline _LIBCPP_INLINE_VISIBILITY
2495bool
2496operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2497
2498template <class _T1, class _D1, class _T2, class _D2>
2499inline _LIBCPP_INLINE_VISIBILITY
2500bool
2501operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2502
2503template <class _T1, class _D1, class _T2, class _D2>
2504inline _LIBCPP_INLINE_VISIBILITY
2505bool
2506operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2507
Howard Hinnant21aefc32010-06-03 16:42:57 +00002508template <class> struct hash;
2509
2510template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002511struct _LIBCPP_VISIBLE hash<_Tp*>
Howard Hinnant21aefc32010-06-03 16:42:57 +00002512 : public unary_function<_Tp*, size_t>
2513{
Howard Hinnant82894812010-09-22 16:48:34 +00002514 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002515 size_t operator()(_Tp* __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00002516 {
2517 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
2518 return *__p;
2519 }
2520};
2521
2522template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002523struct _LIBCPP_VISIBLE hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00002524{
2525 typedef unique_ptr<_Tp, _Dp> argument_type;
2526 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00002527 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002528 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00002529 {
2530 typedef typename argument_type::pointer pointer;
2531 return hash<pointer>()(__ptr.get());
2532 }
2533};
2534
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002535struct __destruct_n
2536{
2537private:
2538 size_t size;
2539
2540 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002541 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002542 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
2543
2544 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002545 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002546 {}
2547
Howard Hinnant1694d232011-05-28 14:41:13 +00002548 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002549 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00002550 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002551 {}
2552
Howard Hinnant1694d232011-05-28 14:41:13 +00002553 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002554 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00002555 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002556 {}
2557public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002558 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
2559 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002560
2561 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002562 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00002563 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002564
2565 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002566 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00002567 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002568
2569 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002570 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00002571 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002572};
2573
2574template <class _Alloc>
2575class __allocator_destructor
2576{
2577 typedef allocator_traits<_Alloc> __alloc_traits;
2578public:
2579 typedef typename __alloc_traits::pointer pointer;
2580 typedef typename __alloc_traits::size_type size_type;
2581private:
2582 _Alloc& __alloc_;
2583 size_type __s_;
2584public:
2585 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00002586 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002587 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00002588 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002589 void operator()(pointer __p) _NOEXCEPT
2590 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002591};
2592
2593template <class _InputIterator, class _ForwardIterator>
2594_ForwardIterator
2595uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2596{
2597 __destruct_n __d(0);
2598 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2599 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2600 for (; __f != __l; ++__f, ++__r, __d.__incr((value_type*)0))
2601 ::new(&*__r) value_type(*__f);
2602 __h.release();
2603 return __r;
2604}
2605
2606template <class _InputIterator, class _Size, class _ForwardIterator>
2607_ForwardIterator
2608uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2609{
2610 __destruct_n __d(0);
2611 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2612 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2613 for (; __n > 0; ++__f, ++__r, __d.__incr((value_type*)0), --__n)
2614 ::new(&*__r) value_type(*__f);
2615 __h.release();
2616 return __r;
2617}
2618
2619template <class _ForwardIterator, class _Tp>
2620void
2621uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2622{
2623 __destruct_n __d(0);
2624 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2625 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2626 for (; __f != __l; ++__f, __d.__incr((value_type*)0))
2627 ::new(&*__f) value_type(__x);
2628 __h.release();
2629}
2630
2631template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00002632_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002633uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2634{
2635 __destruct_n __d(0);
2636 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2637 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2638 for (; __n > 0; ++__f, --__n, __d.__incr((value_type*)0))
2639 ::new(&*__f) value_type(__x);
2640 __h.release();
Howard Hinnant2f6a6272010-11-18 16:13:03 +00002641 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002642}
2643
Howard Hinnant82894812010-09-22 16:48:34 +00002644class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002645 : public std::exception
2646{
2647public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002648 virtual ~bad_weak_ptr() _NOEXCEPT;
2649 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002650};
2651
2652template<class _Tp> class weak_ptr;
2653
2654class __shared_count
2655{
2656 __shared_count(const __shared_count&);
2657 __shared_count& operator=(const __shared_count&);
2658
2659protected:
2660 long __shared_owners_;
2661 virtual ~__shared_count();
2662private:
Howard Hinnant1694d232011-05-28 14:41:13 +00002663 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002664
2665public:
Howard Hinnant82894812010-09-22 16:48:34 +00002666 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002667 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002668 : __shared_owners_(__refs) {}
2669
Howard Hinnant1694d232011-05-28 14:41:13 +00002670 void __add_shared() _NOEXCEPT;
2671 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00002672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002673 long use_count() const _NOEXCEPT {return __shared_owners_ + 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002674};
2675
2676class __shared_weak_count
2677 : private __shared_count
2678{
2679 long __shared_weak_owners_;
2680
2681public:
Howard Hinnant82894812010-09-22 16:48:34 +00002682 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002683 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002684 : __shared_count(__refs),
2685 __shared_weak_owners_(__refs) {}
2686protected:
2687 virtual ~__shared_weak_count();
2688
2689public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002690 void __add_shared() _NOEXCEPT;
2691 void __add_weak() _NOEXCEPT;
2692 void __release_shared() _NOEXCEPT;
2693 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00002694 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002695 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
2696 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002697
Howard Hinnantd4444702010-08-11 17:04:31 +00002698#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00002699 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00002700#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002701private:
Howard Hinnant1694d232011-05-28 14:41:13 +00002702 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002703};
2704
2705template <class _Tp, class _Dp, class _Alloc>
2706class __shared_ptr_pointer
2707 : public __shared_weak_count
2708{
2709 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2710public:
Howard Hinnant82894812010-09-22 16:48:34 +00002711 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002712 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
2713 : __data_(__compressed_pair<_Tp, _Dp>(__p, _STD::move(__d)), _STD::move(__a)) {}
2714
Howard Hinnantd4444702010-08-11 17:04:31 +00002715#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00002716 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00002717#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002718
2719private:
Howard Hinnant1694d232011-05-28 14:41:13 +00002720 virtual void __on_zero_shared() _NOEXCEPT;
2721 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002722};
2723
Howard Hinnantd4444702010-08-11 17:04:31 +00002724#ifndef _LIBCPP_NO_RTTI
2725
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002726template <class _Tp, class _Dp, class _Alloc>
2727const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00002728__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002729{
2730 return __t == typeid(_Dp) ? &__data_.first().second() : 0;
2731}
2732
Howard Hinnant324bb032010-08-22 00:02:43 +00002733#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00002734
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002735template <class _Tp, class _Dp, class _Alloc>
2736void
Howard Hinnant1694d232011-05-28 14:41:13 +00002737__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002738{
2739 __data_.first().second()(__data_.first().first());
2740 __data_.first().second().~_Dp();
2741}
2742
2743template <class _Tp, class _Dp, class _Alloc>
2744void
Howard Hinnant1694d232011-05-28 14:41:13 +00002745__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002746{
2747 typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
2748 __data_.second().~_Alloc();
2749 __a.deallocate(this, 1);
2750}
2751
2752template <class _Tp, class _Alloc>
2753class __shared_ptr_emplace
2754 : public __shared_weak_count
2755{
2756 __compressed_pair<_Alloc, _Tp> __data_;
2757public:
2758#ifndef _LIBCPP_HAS_NO_VARIADICS
2759
Howard Hinnant82894812010-09-22 16:48:34 +00002760 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002761 __shared_ptr_emplace(_Alloc __a)
2762 : __data_(_STD::move(__a)) {}
2763
2764 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00002765 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002766 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
2767 : __data_(_STD::move(__a), _Tp(_STD::forward<_Args>(__args)...)) {}
2768
2769#else // _LIBCPP_HAS_NO_VARIADICS
2770
Howard Hinnant82894812010-09-22 16:48:34 +00002771 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002772 __shared_ptr_emplace(_Alloc __a)
2773 : __data_(__a) {}
2774
2775 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00002776 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002777 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
2778 : __data_(__a, _Tp(__a0)) {}
2779
2780 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00002781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002782 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
2783 : __data_(__a, _Tp(__a0, __a1)) {}
2784
2785 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00002786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002787 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
2788 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
2789
2790#endif // _LIBCPP_HAS_NO_VARIADICS
2791
2792private:
Howard Hinnant1694d232011-05-28 14:41:13 +00002793 virtual void __on_zero_shared() _NOEXCEPT;
2794 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002795public:
Howard Hinnant82894812010-09-22 16:48:34 +00002796 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002797 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002798};
2799
2800template <class _Tp, class _Alloc>
2801void
Howard Hinnant1694d232011-05-28 14:41:13 +00002802__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002803{
2804 __data_.second().~_Tp();
2805}
2806
2807template <class _Tp, class _Alloc>
2808void
Howard Hinnant1694d232011-05-28 14:41:13 +00002809__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002810{
2811 typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
2812 __data_.first().~_Alloc();
2813 __a.deallocate(this, 1);
2814}
2815
2816template<class _Tp> class enable_shared_from_this;
2817
2818template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002819class _LIBCPP_VISIBLE shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002820{
Howard Hinnant324bb032010-08-22 00:02:43 +00002821public:
2822 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002823private:
2824 element_type* __ptr_;
2825 __shared_weak_count* __cntrl_;
2826
2827 struct __nat {int __for_bool_;};
2828public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002829 shared_ptr() _NOEXCEPT;
2830 shared_ptr(nullptr_t) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002831 template<class _Yp> explicit shared_ptr(_Yp* __p);
Howard Hinnant324bb032010-08-22 00:02:43 +00002832 template<class _Yp, class _Dp> shared_ptr(_Yp* __p, _Dp __d);
2833 template<class _Yp, class _Dp, class _Alloc> shared_ptr(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002834 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
2835 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant1694d232011-05-28 14:41:13 +00002836 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
2837 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002838 template<class _Yp>
2839 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00002840 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
2841 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00002842#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00002843 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002844 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00002845 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
2846 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00002847#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002848 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00002849 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002850#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002851 template<class _Yp> shared_ptr(auto_ptr<_Yp>&& __r);
2852#else
2853 template<class _Yp> shared_ptr(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002854#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00002855#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002856private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002857 template <class _Yp, class _Dp> shared_ptr(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002858public:
2859 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
2860 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2861 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
2862 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002863#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002864 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
2865 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
2866 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
2867 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002868#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002869
2870 ~shared_ptr();
2871
Howard Hinnant1694d232011-05-28 14:41:13 +00002872 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
2873 template<class _Yp> shared_ptr& operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00002874#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00002875 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00002876 template<class _Yp> shared_ptr& operator=(shared_ptr<_Yp>&& __r);
2877 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002878#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002879 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002880#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00002881#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002882private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002883 template <class _Yp, class _Dp> shared_ptr& operator=(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002884public:
Howard Hinnant324bb032010-08-22 00:02:43 +00002885 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002886#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00002887 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002888#endif
2889
Howard Hinnant1694d232011-05-28 14:41:13 +00002890 void swap(shared_ptr& __r) _NOEXCEPT;
2891 void reset() _NOEXCEPT;
2892 template<class _Yp> void reset(_Yp* __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002893 template<class _Yp, class _Dp> void reset(_Yp* __p, _Dp __d);
2894 template<class _Yp, class _Dp, class _Alloc> void reset(_Yp* __p, _Dp __d, _Alloc __a);
2895
Howard Hinnant82894812010-09-22 16:48:34 +00002896 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002897 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002899 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
2900 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002902 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002903 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002904 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00002905 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002906 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00002907 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002908 /*explicit*/ operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00002909 template <class _U>
2910 _LIBCPP_INLINE_VISIBILITY
2911 bool owner_before(shared_ptr<_U> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002912 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00002913 template <class _U>
2914 _LIBCPP_INLINE_VISIBILITY
2915 bool owner_before(weak_ptr<_U> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002916 {return __cntrl_ < __p.__cntrl_;}
2917
Howard Hinnantd4444702010-08-11 17:04:31 +00002918#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002919 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002920 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002921 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002922 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00002923#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002924
2925#ifndef _LIBCPP_HAS_NO_VARIADICS
2926
2927 template<class ..._Args>
2928 static
2929 shared_ptr<_Tp>
2930 make_shared(_Args&& ...__args);
2931
2932 template<class _Alloc, class ..._Args>
2933 static
2934 shared_ptr<_Tp>
2935 allocate_shared(const _Alloc& __a, _Args&& ...__args);
2936
2937#else // _LIBCPP_HAS_NO_VARIADICS
2938
2939 static shared_ptr<_Tp> make_shared();
2940
2941 template<class _A0>
2942 static shared_ptr<_Tp> make_shared(_A0&);
2943
2944 template<class _A0, class _A1>
2945 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
2946
2947 template<class _A0, class _A1, class _A2>
2948 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
2949
2950 template<class _Alloc>
2951 static shared_ptr<_Tp>
2952 allocate_shared(const _Alloc& __a);
2953
2954 template<class _Alloc, class _A0>
2955 static shared_ptr<_Tp>
2956 allocate_shared(const _Alloc& __a, _A0& __a0);
2957
2958 template<class _Alloc, class _A0, class _A1>
2959 static shared_ptr<_Tp>
2960 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
2961
2962 template<class _Alloc, class _A0, class _A1, class _A2>
2963 static shared_ptr<_Tp>
2964 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
2965
2966#endif // _LIBCPP_HAS_NO_VARIADICS
2967
2968private:
2969
2970 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00002971 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002972 void
Howard Hinnant1694d232011-05-28 14:41:13 +00002973 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002974 {
2975 if (__e)
2976 __e->__weak_this_ = *this;
2977 }
2978
Howard Hinnant82894812010-09-22 16:48:34 +00002979 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002980 void __enable_weak_this(const void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002981
Howard Hinnant82894812010-09-22 16:48:34 +00002982 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
2983 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002984};
2985
2986template<class _Tp>
2987inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002988shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002989 : __ptr_(0),
2990 __cntrl_(0)
2991{
2992}
2993
2994template<class _Tp>
2995inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002996shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002997 : __ptr_(0),
2998 __cntrl_(0)
2999{
3000}
3001
3002template<class _Tp>
3003template<class _Yp>
3004shared_ptr<_Tp>::shared_ptr(_Yp* __p)
3005 : __ptr_(__p)
3006{
3007 unique_ptr<_Yp> __hold(__p);
3008 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3009 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
3010 __hold.release();
3011 __enable_weak_this(__p);
3012}
3013
3014template<class _Tp>
3015template<class _Yp, class _Dp>
3016shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
3017 : __ptr_(__p)
3018{
3019#ifndef _LIBCPP_NO_EXCEPTIONS
3020 try
3021 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003022#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003023 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3024 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
3025 __enable_weak_this(__p);
3026#ifndef _LIBCPP_NO_EXCEPTIONS
3027 }
3028 catch (...)
3029 {
3030 __d(__p);
3031 throw;
3032 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003033#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003034}
3035
3036template<class _Tp>
3037template<class _Dp>
3038shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3039 : __ptr_(0)
3040{
3041#ifndef _LIBCPP_NO_EXCEPTIONS
3042 try
3043 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003044#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003045 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
3046 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
3047#ifndef _LIBCPP_NO_EXCEPTIONS
3048 }
3049 catch (...)
3050 {
3051 __d(__p);
3052 throw;
3053 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003054#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003055}
3056
3057template<class _Tp>
3058template<class _Yp, class _Dp, class _Alloc>
3059shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
3060 : __ptr_(__p)
3061{
3062#ifndef _LIBCPP_NO_EXCEPTIONS
3063 try
3064 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003065#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003066 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
3067 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3068 typedef __allocator_destructor<_A2> _D2;
3069 _A2 __a2(__a);
3070 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3071 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
3072 __cntrl_ = __hold2.release();
3073 __enable_weak_this(__p);
3074#ifndef _LIBCPP_NO_EXCEPTIONS
3075 }
3076 catch (...)
3077 {
3078 __d(__p);
3079 throw;
3080 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003081#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003082}
3083
3084template<class _Tp>
3085template<class _Dp, class _Alloc>
3086shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
3087 : __ptr_(0)
3088{
3089#ifndef _LIBCPP_NO_EXCEPTIONS
3090 try
3091 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003092#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003093 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
3094 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3095 typedef __allocator_destructor<_A2> _D2;
3096 _A2 __a2(__a);
3097 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3098 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
3099 __cntrl_ = __hold2.release();
3100#ifndef _LIBCPP_NO_EXCEPTIONS
3101 }
3102 catch (...)
3103 {
3104 __d(__p);
3105 throw;
3106 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003107#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003108}
3109
3110template<class _Tp>
3111template<class _Yp>
3112inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003113shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003114 : __ptr_(__p),
3115 __cntrl_(__r.__cntrl_)
3116{
3117 if (__cntrl_)
3118 __cntrl_->__add_shared();
3119}
3120
3121template<class _Tp>
3122inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003123shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003124 : __ptr_(__r.__ptr_),
3125 __cntrl_(__r.__cntrl_)
3126{
3127 if (__cntrl_)
3128 __cntrl_->__add_shared();
3129}
3130
3131template<class _Tp>
3132template<class _Yp>
3133inline _LIBCPP_INLINE_VISIBILITY
3134shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
3135 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003136 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003137 : __ptr_(__r.__ptr_),
3138 __cntrl_(__r.__cntrl_)
3139{
3140 if (__cntrl_)
3141 __cntrl_->__add_shared();
3142}
3143
Howard Hinnant73d21a42010-09-04 23:28:19 +00003144#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003145
3146template<class _Tp>
3147inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003148shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003149 : __ptr_(__r.__ptr_),
3150 __cntrl_(__r.__cntrl_)
3151{
3152 __r.__ptr_ = 0;
3153 __r.__cntrl_ = 0;
3154}
3155
3156template<class _Tp>
3157template<class _Yp>
3158inline _LIBCPP_INLINE_VISIBILITY
3159shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
3160 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003161 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003162 : __ptr_(__r.__ptr_),
3163 __cntrl_(__r.__cntrl_)
3164{
3165 __r.__ptr_ = 0;
3166 __r.__cntrl_ = 0;
3167}
3168
Howard Hinnant73d21a42010-09-04 23:28:19 +00003169#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003170
3171template<class _Tp>
3172template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003173#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003174shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
3175#else
Howard Hinnant92172b82010-08-21 21:14:53 +00003176shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003177#endif
3178 : __ptr_(__r.get())
3179{
3180 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3181 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
3182 __enable_weak_this(__r.get());
3183 __r.release();
3184}
3185
3186template<class _Tp>
3187template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003188#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003189shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3190#else
3191shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3192#endif
3193 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
3194 : __ptr_(__r.get())
3195{
3196 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3197 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
3198 __enable_weak_this(__r.get());
3199 __r.release();
3200}
3201
3202template<class _Tp>
3203template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003204#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003205shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3206#else
3207shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3208#endif
3209 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
3210 : __ptr_(__r.get())
3211{
3212 typedef __shared_ptr_pointer<_Yp*,
3213 reference_wrapper<typename remove_reference<_Dp>::type>,
3214 allocator<_Yp> > _CntrlBlk;
3215 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
3216 __enable_weak_this(__r.get());
3217 __r.release();
3218}
3219
3220#ifndef _LIBCPP_HAS_NO_VARIADICS
3221
3222template<class _Tp>
3223template<class ..._Args>
3224shared_ptr<_Tp>
3225shared_ptr<_Tp>::make_shared(_Args&& ...__args)
3226{
3227 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3228 typedef allocator<_CntrlBlk> _A2;
3229 typedef __allocator_destructor<_A2> _D2;
3230 _A2 __a2;
3231 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3232 ::new(__hold2.get()) _CntrlBlk(__a2, _STD::forward<_Args>(__args)...);
3233 shared_ptr<_Tp> __r;
3234 __r.__ptr_ = __hold2.get()->get();
3235 __r.__cntrl_ = __hold2.release();
3236 __r.__enable_weak_this(__r.__ptr_);
3237 return __r;
3238}
3239
3240template<class _Tp>
3241template<class _Alloc, class ..._Args>
3242shared_ptr<_Tp>
3243shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
3244{
3245 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3246 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3247 typedef __allocator_destructor<_A2> _D2;
3248 _A2 __a2(__a);
3249 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3250 ::new(__hold2.get()) _CntrlBlk(__a, _STD::forward<_Args>(__args)...);
3251 shared_ptr<_Tp> __r;
3252 __r.__ptr_ = __hold2.get()->get();
3253 __r.__cntrl_ = __hold2.release();
3254 __r.__enable_weak_this(__r.__ptr_);
3255 return __r;
3256}
3257
3258#else // _LIBCPP_HAS_NO_VARIADICS
3259
3260template<class _Tp>
3261shared_ptr<_Tp>
3262shared_ptr<_Tp>::make_shared()
3263{
3264 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3265 typedef allocator<_CntrlBlk> _Alloc2;
3266 typedef __allocator_destructor<_Alloc2> _D2;
3267 _Alloc2 __alloc2;
3268 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3269 ::new(__hold2.get()) _CntrlBlk(__alloc2);
3270 shared_ptr<_Tp> __r;
3271 __r.__ptr_ = __hold2.get()->get();
3272 __r.__cntrl_ = __hold2.release();
3273 __r.__enable_weak_this(__r.__ptr_);
3274 return __r;
3275}
3276
3277template<class _Tp>
3278template<class _A0>
3279shared_ptr<_Tp>
3280shared_ptr<_Tp>::make_shared(_A0& __a0)
3281{
3282 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3283 typedef allocator<_CntrlBlk> _Alloc2;
3284 typedef __allocator_destructor<_Alloc2> _D2;
3285 _Alloc2 __alloc2;
3286 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3287 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
3288 shared_ptr<_Tp> __r;
3289 __r.__ptr_ = __hold2.get()->get();
3290 __r.__cntrl_ = __hold2.release();
3291 __r.__enable_weak_this(__r.__ptr_);
3292 return __r;
3293}
3294
3295template<class _Tp>
3296template<class _A0, class _A1>
3297shared_ptr<_Tp>
3298shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
3299{
3300 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3301 typedef allocator<_CntrlBlk> _Alloc2;
3302 typedef __allocator_destructor<_Alloc2> _D2;
3303 _Alloc2 __alloc2;
3304 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3305 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
3306 shared_ptr<_Tp> __r;
3307 __r.__ptr_ = __hold2.get()->get();
3308 __r.__cntrl_ = __hold2.release();
3309 __r.__enable_weak_this(__r.__ptr_);
3310 return __r;
3311}
3312
3313template<class _Tp>
3314template<class _A0, class _A1, class _A2>
3315shared_ptr<_Tp>
3316shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3317{
3318 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3319 typedef allocator<_CntrlBlk> _Alloc2;
3320 typedef __allocator_destructor<_Alloc2> _D2;
3321 _Alloc2 __alloc2;
3322 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3323 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
3324 shared_ptr<_Tp> __r;
3325 __r.__ptr_ = __hold2.get()->get();
3326 __r.__cntrl_ = __hold2.release();
3327 __r.__enable_weak_this(__r.__ptr_);
3328 return __r;
3329}
3330
3331template<class _Tp>
3332template<class _Alloc>
3333shared_ptr<_Tp>
3334shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
3335{
3336 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3337 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3338 typedef __allocator_destructor<_Alloc2> _D2;
3339 _Alloc2 __alloc2(__a);
3340 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3341 ::new(__hold2.get()) _CntrlBlk(__a);
3342 shared_ptr<_Tp> __r;
3343 __r.__ptr_ = __hold2.get()->get();
3344 __r.__cntrl_ = __hold2.release();
3345 __r.__enable_weak_this(__r.__ptr_);
3346 return __r;
3347}
3348
3349template<class _Tp>
3350template<class _Alloc, class _A0>
3351shared_ptr<_Tp>
3352shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
3353{
3354 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3355 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3356 typedef __allocator_destructor<_Alloc2> _D2;
3357 _Alloc2 __alloc2(__a);
3358 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3359 ::new(__hold2.get()) _CntrlBlk(__a, __a0);
3360 shared_ptr<_Tp> __r;
3361 __r.__ptr_ = __hold2.get()->get();
3362 __r.__cntrl_ = __hold2.release();
3363 __r.__enable_weak_this(__r.__ptr_);
3364 return __r;
3365}
3366
3367template<class _Tp>
3368template<class _Alloc, class _A0, class _A1>
3369shared_ptr<_Tp>
3370shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3371{
3372 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3373 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3374 typedef __allocator_destructor<_Alloc2> _D2;
3375 _Alloc2 __alloc2(__a);
3376 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3377 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
3378 shared_ptr<_Tp> __r;
3379 __r.__ptr_ = __hold2.get()->get();
3380 __r.__cntrl_ = __hold2.release();
3381 __r.__enable_weak_this(__r.__ptr_);
3382 return __r;
3383}
3384
3385template<class _Tp>
3386template<class _Alloc, class _A0, class _A1, class _A2>
3387shared_ptr<_Tp>
3388shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3389{
3390 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3391 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3392 typedef __allocator_destructor<_Alloc2> _D2;
3393 _Alloc2 __alloc2(__a);
3394 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3395 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
3396 shared_ptr<_Tp> __r;
3397 __r.__ptr_ = __hold2.get()->get();
3398 __r.__cntrl_ = __hold2.release();
3399 __r.__enable_weak_this(__r.__ptr_);
3400 return __r;
3401}
3402
3403#endif // _LIBCPP_HAS_NO_VARIADICS
3404
3405template<class _Tp>
3406shared_ptr<_Tp>::~shared_ptr()
3407{
3408 if (__cntrl_)
3409 __cntrl_->__release_shared();
3410}
3411
3412template<class _Tp>
3413inline _LIBCPP_INLINE_VISIBILITY
3414shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003415shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003416{
3417 shared_ptr(__r).swap(*this);
3418 return *this;
3419}
3420
3421template<class _Tp>
3422template<class _Yp>
3423inline _LIBCPP_INLINE_VISIBILITY
3424shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003425shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003426{
3427 shared_ptr(__r).swap(*this);
3428 return *this;
3429}
3430
Howard Hinnant73d21a42010-09-04 23:28:19 +00003431#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003432
3433template<class _Tp>
3434inline _LIBCPP_INLINE_VISIBILITY
3435shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003436shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003437{
3438 shared_ptr(_STD::move(__r)).swap(*this);
3439 return *this;
3440}
3441
3442template<class _Tp>
3443template<class _Yp>
3444inline _LIBCPP_INLINE_VISIBILITY
3445shared_ptr<_Tp>&
3446shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3447{
3448 shared_ptr(_STD::move(__r)).swap(*this);
3449 return *this;
3450}
3451
3452template<class _Tp>
3453template<class _Yp>
3454inline _LIBCPP_INLINE_VISIBILITY
3455shared_ptr<_Tp>&
3456shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3457{
Howard Hinnant6b41c602011-05-11 20:21:19 +00003458 shared_ptr(_STD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003459 return *this;
3460}
3461
3462template<class _Tp>
3463template <class _Yp, class _Dp>
3464inline _LIBCPP_INLINE_VISIBILITY
3465shared_ptr<_Tp>&
3466shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3467{
3468 shared_ptr(_STD::move(__r)).swap(*this);
3469 return *this;
3470}
3471
Howard Hinnant73d21a42010-09-04 23:28:19 +00003472#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003473
3474template<class _Tp>
3475template<class _Yp>
3476inline _LIBCPP_INLINE_VISIBILITY
3477shared_ptr<_Tp>&
Howard Hinnant324bb032010-08-22 00:02:43 +00003478shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003479{
3480 shared_ptr(__r).swap(*this);
3481 return *this;
3482}
3483
3484template<class _Tp>
3485template <class _Yp, class _Dp>
3486inline _LIBCPP_INLINE_VISIBILITY
3487shared_ptr<_Tp>&
3488shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
3489{
3490 shared_ptr(_STD::move(__r)).swap(*this);
3491 return *this;
3492}
3493
Howard Hinnant73d21a42010-09-04 23:28:19 +00003494#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003495
3496template<class _Tp>
3497inline _LIBCPP_INLINE_VISIBILITY
3498void
Howard Hinnant1694d232011-05-28 14:41:13 +00003499shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003500{
3501 _STD::swap(__ptr_, __r.__ptr_);
3502 _STD::swap(__cntrl_, __r.__cntrl_);
3503}
3504
3505template<class _Tp>
3506inline _LIBCPP_INLINE_VISIBILITY
3507void
Howard Hinnant1694d232011-05-28 14:41:13 +00003508shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003509{
3510 shared_ptr().swap(*this);
3511}
3512
3513template<class _Tp>
3514template<class _Yp>
3515inline _LIBCPP_INLINE_VISIBILITY
3516void
3517shared_ptr<_Tp>::reset(_Yp* __p)
3518{
3519 shared_ptr(__p).swap(*this);
3520}
3521
3522template<class _Tp>
3523template<class _Yp, class _Dp>
3524inline _LIBCPP_INLINE_VISIBILITY
3525void
3526shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3527{
3528 shared_ptr(__p, __d).swap(*this);
3529}
3530
3531template<class _Tp>
3532template<class _Yp, class _Dp, class _Alloc>
3533inline _LIBCPP_INLINE_VISIBILITY
3534void
3535shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3536{
3537 shared_ptr(__p, __d, __a).swap(*this);
3538}
3539
3540#ifndef _LIBCPP_HAS_NO_VARIADICS
3541
Howard Hinnant324bb032010-08-22 00:02:43 +00003542template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003543inline _LIBCPP_INLINE_VISIBILITY
3544shared_ptr<_Tp>
3545make_shared(_Args&& ...__args)
3546{
3547 return shared_ptr<_Tp>::make_shared(_STD::forward<_Args>(__args)...);
3548}
3549
Howard Hinnant324bb032010-08-22 00:02:43 +00003550template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003551inline _LIBCPP_INLINE_VISIBILITY
3552shared_ptr<_Tp>
3553allocate_shared(const _Alloc& __a, _Args&& ...__args)
3554{
3555 return shared_ptr<_Tp>::allocate_shared(__a, _STD::forward<_Args>(__args)...);
3556}
3557
3558#else // _LIBCPP_HAS_NO_VARIADICS
3559
3560template<class _Tp>
3561inline _LIBCPP_INLINE_VISIBILITY
3562shared_ptr<_Tp>
3563make_shared()
3564{
3565 return shared_ptr<_Tp>::make_shared();
3566}
3567
3568template<class _Tp, class _A0>
3569inline _LIBCPP_INLINE_VISIBILITY
3570shared_ptr<_Tp>
3571make_shared(_A0& __a0)
3572{
3573 return shared_ptr<_Tp>::make_shared(__a0);
3574}
3575
3576template<class _Tp, class _A0, class _A1>
3577inline _LIBCPP_INLINE_VISIBILITY
3578shared_ptr<_Tp>
3579make_shared(_A0& __a0, _A1& __a1)
3580{
3581 return shared_ptr<_Tp>::make_shared(__a0, __a1);
3582}
3583
Howard Hinnant324bb032010-08-22 00:02:43 +00003584template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003585inline _LIBCPP_INLINE_VISIBILITY
3586shared_ptr<_Tp>
3587make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3588{
3589 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
3590}
3591
3592template<class _Tp, class _Alloc>
3593inline _LIBCPP_INLINE_VISIBILITY
3594shared_ptr<_Tp>
3595allocate_shared(const _Alloc& __a)
3596{
3597 return shared_ptr<_Tp>::allocate_shared(__a);
3598}
3599
3600template<class _Tp, class _Alloc, class _A0>
3601inline _LIBCPP_INLINE_VISIBILITY
3602shared_ptr<_Tp>
3603allocate_shared(const _Alloc& __a, _A0& __a0)
3604{
3605 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
3606}
3607
3608template<class _Tp, class _Alloc, class _A0, class _A1>
3609inline _LIBCPP_INLINE_VISIBILITY
3610shared_ptr<_Tp>
3611allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3612{
3613 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
3614}
3615
3616template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
3617inline _LIBCPP_INLINE_VISIBILITY
3618shared_ptr<_Tp>
3619allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3620{
3621 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
3622}
3623
3624#endif // _LIBCPP_HAS_NO_VARIADICS
3625
3626template<class _Tp, class _Up>
3627inline _LIBCPP_INLINE_VISIBILITY
3628bool
Howard Hinnant1694d232011-05-28 14:41:13 +00003629operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003630{
3631 return __x.get() == __y.get();
3632}
3633
3634template<class _Tp, class _Up>
3635inline _LIBCPP_INLINE_VISIBILITY
3636bool
Howard Hinnant1694d232011-05-28 14:41:13 +00003637operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003638{
3639 return !(__x == __y);
3640}
3641
3642template<class _Tp, class _Up>
3643inline _LIBCPP_INLINE_VISIBILITY
3644bool
Howard Hinnant1694d232011-05-28 14:41:13 +00003645operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003646{
3647 return __x.get() < __y.get();
3648}
3649
3650template<class _Tp>
3651inline _LIBCPP_INLINE_VISIBILITY
3652void
Howard Hinnant1694d232011-05-28 14:41:13 +00003653swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003654{
3655 __x.swap(__y);
3656}
3657
3658template<class _Tp, class _Up>
3659inline _LIBCPP_INLINE_VISIBILITY
3660shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003661static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003662{
3663 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
3664}
3665
3666template<class _Tp, class _Up>
3667inline _LIBCPP_INLINE_VISIBILITY
3668shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003669dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003670{
3671 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
3672 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3673}
3674
3675template<class _Tp, class _Up>
3676shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003677const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003678{
3679 return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get()));
3680}
3681
Howard Hinnantd4444702010-08-11 17:04:31 +00003682#ifndef _LIBCPP_NO_RTTI
3683
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003684template<class _Dp, class _Tp>
3685inline _LIBCPP_INLINE_VISIBILITY
3686_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00003687get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003688{
3689 return __p.template __get_deleter<_Dp>();
3690}
3691
Howard Hinnant324bb032010-08-22 00:02:43 +00003692#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003693
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003694template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003695class _LIBCPP_VISIBLE weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003696{
Howard Hinnant324bb032010-08-22 00:02:43 +00003697public:
3698 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003699private:
3700 element_type* __ptr_;
3701 __shared_weak_count* __cntrl_;
3702
Howard Hinnant324bb032010-08-22 00:02:43 +00003703public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003704 weak_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003705 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003706 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3707 _NOEXCEPT;
3708 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003709 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003710 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3711 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00003712
3713 ~weak_ptr();
3714
Howard Hinnant1694d232011-05-28 14:41:13 +00003715 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
3716 template<class _Yp> weak_ptr& operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
3717 template<class _Yp> weak_ptr& operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00003718
Howard Hinnant1694d232011-05-28 14:41:13 +00003719 void swap(weak_ptr& __r) _NOEXCEPT;
3720 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00003721
Howard Hinnant82894812010-09-22 16:48:34 +00003722 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003723 long use_count() const _NOEXCEPT
3724 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003726 bool expired() const _NOEXCEPT
3727 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
3728 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003729 template<class _Up>
3730 _LIBCPP_INLINE_VISIBILITY
3731 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003732 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003733 template<class _Up>
3734 _LIBCPP_INLINE_VISIBILITY
3735 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003736 {return __cntrl_ < __r.__cntrl_;}
3737
Howard Hinnant82894812010-09-22 16:48:34 +00003738 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
3739 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003740};
3741
3742template<class _Tp>
3743inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003744weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003745 : __ptr_(0),
3746 __cntrl_(0)
3747{
3748}
3749
3750template<class _Tp>
3751inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003752weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003753 : __ptr_(__r.__ptr_),
3754 __cntrl_(__r.__cntrl_)
3755{
3756 if (__cntrl_)
3757 __cntrl_->__add_weak();
3758}
3759
3760template<class _Tp>
3761template<class _Yp>
3762inline _LIBCPP_INLINE_VISIBILITY
3763weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00003764 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003765 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003766 : __ptr_(__r.__ptr_),
3767 __cntrl_(__r.__cntrl_)
3768{
3769 if (__cntrl_)
3770 __cntrl_->__add_weak();
3771}
3772
3773template<class _Tp>
3774template<class _Yp>
3775inline _LIBCPP_INLINE_VISIBILITY
3776weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00003777 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003778 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003779 : __ptr_(__r.__ptr_),
3780 __cntrl_(__r.__cntrl_)
3781{
3782 if (__cntrl_)
3783 __cntrl_->__add_weak();
3784}
3785
3786template<class _Tp>
3787weak_ptr<_Tp>::~weak_ptr()
3788{
3789 if (__cntrl_)
3790 __cntrl_->__release_weak();
3791}
3792
3793template<class _Tp>
3794inline _LIBCPP_INLINE_VISIBILITY
3795weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003796weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003797{
3798 weak_ptr(__r).swap(*this);
3799 return *this;
3800}
3801
3802template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00003803template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003804inline _LIBCPP_INLINE_VISIBILITY
3805weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003806weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003807{
3808 weak_ptr(__r).swap(*this);
3809 return *this;
3810}
3811
3812template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00003813template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003814inline _LIBCPP_INLINE_VISIBILITY
3815weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003816weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003817{
3818 weak_ptr(__r).swap(*this);
3819 return *this;
3820}
3821
3822template<class _Tp>
3823inline _LIBCPP_INLINE_VISIBILITY
3824void
Howard Hinnant1694d232011-05-28 14:41:13 +00003825weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003826{
3827 _STD::swap(__ptr_, __r.__ptr_);
3828 _STD::swap(__cntrl_, __r.__cntrl_);
3829}
3830
3831template<class _Tp>
3832inline _LIBCPP_INLINE_VISIBILITY
3833void
Howard Hinnant1694d232011-05-28 14:41:13 +00003834swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003835{
3836 __x.swap(__y);
3837}
3838
3839template<class _Tp>
3840inline _LIBCPP_INLINE_VISIBILITY
3841void
Howard Hinnant1694d232011-05-28 14:41:13 +00003842weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003843{
3844 weak_ptr().swap(*this);
3845}
3846
3847template<class _Tp>
3848template<class _Yp>
3849shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
3850 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
3851 : __ptr_(__r.__ptr_),
3852 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
3853{
3854 if (__cntrl_ == 0)
3855#ifndef _LIBCPP_NO_EXCEPTIONS
3856 throw bad_weak_ptr();
3857#else
3858 assert(!"bad_weak_ptr");
3859#endif
3860}
3861
3862template<class _Tp>
3863shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003864weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003865{
3866 shared_ptr<_Tp> __r;
3867 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
3868 if (__r.__cntrl_)
3869 __r.__ptr_ = __ptr_;
3870 return __r;
3871}
3872
Howard Hinnant324bb032010-08-22 00:02:43 +00003873template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003874
3875template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003876struct _LIBCPP_VISIBLE owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003877 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00003878{
3879 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003880 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003881 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3882 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003883 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003884 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3885 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003886 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003887 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3888 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00003889};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003890
3891template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003892struct _LIBCPP_VISIBLE owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003893 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
3894{
Howard Hinnant324bb032010-08-22 00:02:43 +00003895 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003896 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003897 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3898 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003899 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003900 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
3901 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00003902 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003903 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
3904 {return __x.owner_before(__y);}
3905};
3906
3907template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003908class _LIBCPP_VISIBLE enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003909{
3910 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00003911protected:
Howard Hinnant82894812010-09-22 16:48:34 +00003912 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003913 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00003914 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003915 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00003916 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003917 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
3918 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00003919 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003920 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00003921public:
Howard Hinnant82894812010-09-22 16:48:34 +00003922 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003923 shared_ptr<_Tp> shared_from_this()
3924 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00003925 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003926 shared_ptr<_Tp const> shared_from_this() const
3927 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003928
3929 template <class _Up> friend class shared_ptr;
3930};
3931
Howard Hinnant21aefc32010-06-03 16:42:57 +00003932template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003933struct _LIBCPP_VISIBLE hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003934{
3935 typedef shared_ptr<_Tp> argument_type;
3936 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003937 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003938 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003939 {
3940 return hash<_Tp*>()(__ptr.get());
3941 }
3942};
3943
Howard Hinnant324bb032010-08-22 00:02:43 +00003944//enum class
Howard Hinnant82894812010-09-22 16:48:34 +00003945struct _LIBCPP_VISIBLE pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003946{
3947 enum _
3948 {
3949 relaxed,
3950 preferred,
3951 strict
3952 };
3953
3954 _ __v_;
3955
Howard Hinnant82894812010-09-22 16:48:34 +00003956 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003957 pointer_safety(_ __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003958 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003959 operator int() const {return __v_;}
3960};
3961
3962void declare_reachable(void* __p);
3963void declare_no_pointers(char* __p, size_t __n);
3964void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnant1694d232011-05-28 14:41:13 +00003965pointer_safety get_pointer_safety() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003966void* __undeclare_reachable(void*);
3967
3968template <class _Tp>
3969inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00003970_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003971undeclare_reachable(_Tp* __p)
3972{
3973 return static_cast<_Tp*>(__undeclare_reachable(__p));
3974}
3975
3976void* align(size_t, size_t, void*&, size_t&);
3977
3978_LIBCPP_END_NAMESPACE_STD
3979
3980#endif // _LIBCPP_MEMORY