blob: 3a0064a91b347ff1aaf966d2f79cb4e75695a133 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_MEMORY
12#define _LIBCPP_MEMORY
13
14/*
15 memory synopsis
16
17namespace std
18{
19
20struct allocator_arg_t { };
21constexpr allocator_arg_t allocator_arg = allocator_arg_t();
22
23template <class T, class Alloc> struct uses_allocator;
24
25template <class Ptr>
26struct pointer_traits
27{
28 typedef Ptr pointer;
29 typedef <details> element_type;
30 typedef <details> difference_type;
Howard Hinnant324bb032010-08-22 00:02:43 +000031
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000032 template <class U> using rebind = <details>;
Howard Hinnant324bb032010-08-22 00:02:43 +000033
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000034 static pointer pointer_to(<details>);
35};
36
Howard Hinnant1694d232011-05-28 14:41:13 +000037template <class T>
38struct pointer_traits<T*>
39{
40 typedef T* pointer;
41 typedef T element_type;
42 typedef ptrdiff_t difference_type;
43
44 template <class U> using rebind = U*;
45
46 static pointer pointer_to(<details>) noexcept;
47};
48
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000049template <class Alloc>
50struct allocator_traits
51{
52 typedef Alloc allocator_type;
53 typedef typename allocator_type::value_type
54 value_type;
55
56 typedef Alloc::pointer | value_type* pointer;
57 typedef Alloc::const_pointer
58 | pointer_traits<pointer>::rebind<const value_type>
59 const_pointer;
60 typedef Alloc::void_pointer
61 | pointer_traits<pointer>::rebind<void>
62 void_pointer;
63 typedef Alloc::const_void_pointer
64 | pointer_traits<pointer>::rebind<const void>
65 const_void_pointer;
66 typedef Alloc::difference_type
Howard Hinnant47761072010-11-18 01:40:00 +000067 | pointer_traits<pointer>::difference_type
68 difference_type;
69 typedef Alloc::size_type
70 | make_unsigned<difference_type>::type
71 size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000072 typedef Alloc::propagate_on_container_copy_assignment
73 | false_type propagate_on_container_copy_assignment;
74 typedef Alloc::propagate_on_container_move_assignment
75 | false_type propagate_on_container_move_assignment;
76 typedef Alloc::propagate_on_container_swap
77 | false_type propagate_on_container_swap;
78
79 template <class T> using rebind_alloc = Alloc::rebind<U>::other | Alloc<T, Args...>;
80 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
81
82 static pointer allocate(allocator_type& a, size_type n);
83 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint);
84
Howard Hinnant1694d232011-05-28 14:41:13 +000085 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000086
87 template <class T, class... Args>
88 static void construct(allocator_type& a, T* p, Args&&... args);
89
90 template <class T>
91 static void destroy(allocator_type& a, T* p);
92
93 static size_type max_size(const allocator_type& a);
94
95 static allocator_type
96 select_on_container_copy_construction(const allocator_type& a);
97};
98
99template <>
100class allocator<void>
101{
102public:
103 typedef void* pointer;
104 typedef const void* const_pointer;
105 typedef void value_type;
106
107 template <class _Up> struct rebind {typedef allocator<_Up> other;};
108};
109
110template <class T>
111class allocator
112{
113public:
114 typedef size_t size_type;
115 typedef ptrdiff_t difference_type;
116 typedef T* pointer;
117 typedef const T* const_pointer;
118 typedef typename add_lvalue_reference<T>::type reference;
119 typedef typename add_lvalue_reference<const T>::type const_reference;
120 typedef T value_type;
121
122 template <class U> struct rebind {typedef allocator<U> other;};
123
Howard Hinnant1694d232011-05-28 14:41:13 +0000124 allocator() noexcept;
125 allocator(const allocator&) noexcept;
126 template <class U> allocator(const allocator<U>&) noexcept;
127 ~allocator();
128 pointer address(reference x) const noexcept;
129 const_pointer address(const_reference x) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000130 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
Howard Hinnant1694d232011-05-28 14:41:13 +0000131 void deallocate(pointer p, size_type n) noexcept;
132 size_type max_size() const noexcept;
133 template<class U, class... Args>
134 void construct(U* p, Args&&... args);
135 template <class U>
136 void destroy(U* p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000137};
138
139template <class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000140bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141
142template <class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000143bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000144
145template <class OutputIterator, class T>
146class raw_storage_iterator
147 : public iterator<output_iterator_tag,
148 T, // purposefully not C++03
149 ptrdiff_t, // purposefully not C++03
150 T*, // purposefully not C++03
151 raw_storage_iterator&> // purposefully not C++03
152{
153public:
154 explicit raw_storage_iterator(OutputIterator x);
155 raw_storage_iterator& operator*();
156 raw_storage_iterator& operator=(const T& element);
157 raw_storage_iterator& operator++();
158 raw_storage_iterator operator++(int);
159};
160
Howard Hinnant1694d232011-05-28 14:41:13 +0000161template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
162template <class T> void return_temporary_buffer(T* p) noexcept;
163
164template <class T> T* addressof(T& r) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000165
166template <class InputIterator, class ForwardIterator>
167ForwardIterator
168uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
169
Howard Hinnant1694d232011-05-28 14:41:13 +0000170template <class InputIterator, class Size, class ForwardIterator>
171ForwardIterator
172uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
173
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000174template <class ForwardIterator, class T>
175void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
176
177template <class ForwardIterator, class Size, class T>
Howard Hinnant2f6a6272010-11-18 16:13:03 +0000178ForwardIterator
179uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000180
181template <class Y> struct auto_ptr_ref {};
182
183template<class X>
184class auto_ptr
185{
186public:
187 typedef X element_type;
188
189 explicit auto_ptr(X* p =0) throw();
190 auto_ptr(auto_ptr&) throw();
191 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
192 auto_ptr& operator=(auto_ptr&) throw();
193 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
194 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
195 ~auto_ptr() throw();
196
197 typename add_lvalue_reference<X>::type operator*() const throw();
198 X* operator->() const throw();
199 X* get() const throw();
200 X* release() throw();
201 void reset(X* p =0) throw();
202
203 auto_ptr(auto_ptr_ref<X>) throw();
204 template<class Y> operator auto_ptr_ref<Y>() throw();
205 template<class Y> operator auto_ptr<Y>() throw();
206};
207
Howard Hinnante92c3d72010-08-19 18:39:17 +0000208template <class T>
209struct default_delete
210{
Howard Hinnant1694d232011-05-28 14:41:13 +0000211 constexpr default_delete() noexcept = default;
212 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000213
Howard Hinnant1694d232011-05-28 14:41:13 +0000214 void operator()(T*) const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000215};
216
217template <class T>
218struct default_delete<T[]>
219{
Howard Hinnant1694d232011-05-28 14:41:13 +0000220 constexpr default_delete() noexcept = default;
221 void operator()(T*) const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000222 template <class U> void operator()(U*) const = delete;
223};
224
Howard Hinnante92c3d72010-08-19 18:39:17 +0000225template <class T, class D = default_delete<T>>
226class unique_ptr
227{
228public:
229 typedef see below pointer;
230 typedef T element_type;
231 typedef D deleter_type;
232
233 // constructors
Howard Hinnant1694d232011-05-28 14:41:13 +0000234 constexpr unique_ptr() noexcept;
235 explicit unique_ptr(pointer p) noexcept;
236 unique_ptr(pointer p, see below d1) noexcept;
237 unique_ptr(pointer p, see below d2) noexcept;
238 unique_ptr(unique_ptr&& u) noexcept;
239 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnante92c3d72010-08-19 18:39:17 +0000240 template <class U, class E>
Howard Hinnant1694d232011-05-28 14:41:13 +0000241 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000242 template <class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000243 unique_ptr(auto_ptr<U>&& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000244
245 // destructor
246 ~unique_ptr();
247
248 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000249 unique_ptr& operator=(unique_ptr&& u) noexcept;
250 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
251 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000252
253 // observers
254 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant1694d232011-05-28 14:41:13 +0000255 pointer operator->() const noexcept;
256 pointer get() const noexcept;
257 deleter_type& get_deleter() noexcept;
258 const deleter_type& get_deleter() const noexcept;
259 explicit operator bool() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000260
261 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000262 pointer release() noexcept;
263 void reset(pointer p = pointer()) noexcept;
264 void swap(unique_ptr& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000265};
266
267template <class T, class D>
268class unique_ptr<T[], D>
269{
270public:
271 typedef implementation-defined pointer;
272 typedef T element_type;
273 typedef D deleter_type;
274
275 // constructors
Howard Hinnant1694d232011-05-28 14:41:13 +0000276 constexpr unique_ptr() noexcept;
277 explicit unique_ptr(pointer p) noexcept;
278 unique_ptr(pointer p, see below d) noexcept;
279 unique_ptr(pointer p, see below d) noexcept;
280 unique_ptr(unique_ptr&& u) noexcept;
281 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnante92c3d72010-08-19 18:39:17 +0000282
283 // destructor
Howard Hinnant324bb032010-08-22 00:02:43 +0000284 ~unique_ptr();
Howard Hinnante92c3d72010-08-19 18:39:17 +0000285
286 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000287 unique_ptr& operator=(unique_ptr&& u) noexcept;
288 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000289
290 // observers
291 T& operator[](size_t i) const;
Howard Hinnant1694d232011-05-28 14:41:13 +0000292 pointer get() const noexcept;
293 deleter_type& get_deleter() noexcept;
294 const deleter_type& get_deleter() const noexcept;
295 explicit operator bool() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000296
297 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000298 pointer release() noexcept;
299 void reset(pointer p = pointer()) noexcept;
300 void reset(nullptr_t) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000301 template <class U> void reset(U) = delete;
Howard Hinnant1694d232011-05-28 14:41:13 +0000302 void swap(unique_ptr& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000303};
304
305template <class T, class D>
Howard Hinnant1694d232011-05-28 14:41:13 +0000306 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000307
308template <class T1, class D1, class T2, class D2>
309 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
310template <class T1, class D1, class T2, class D2>
311 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
312template <class T1, class D1, class T2, class D2>
313 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
314template <class T1, class D1, class T2, class D2>
315 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
316template <class T1, class D1, class T2, class D2>
317 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
318template <class T1, class D1, class T2, class D2>
319 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
320
Howard Hinnant1694d232011-05-28 14:41:13 +0000321template <class T, class D>
322 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
323template <class T, class D>
324 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
325template <class T, class D>
326 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
327template <class T, class D>
328 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
329
330template <class T, class D>
331 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
332template <class T, class D>
333 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
334template <class T, class D>
335 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
336template <class T, class D>
337 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
338template <class T, class D>
339 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
340template <class T, class D>
341 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
342template <class T, class D>
343 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
344template <class T, class D>
345 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
346
Howard Hinnante92c3d72010-08-19 18:39:17 +0000347class bad_weak_ptr
348 : public std::exception
349{
Howard Hinnant1694d232011-05-28 14:41:13 +0000350 bad_weak_ptr() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000351};
352
353template<class T>
354class shared_ptr
355{
356public:
357 typedef T element_type;
358
359 // constructors:
Howard Hinnant1694d232011-05-28 14:41:13 +0000360 constexpr shared_ptr() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000361 template<class Y> explicit shared_ptr(Y* p);
362 template<class Y, class D> shared_ptr(Y* p, D d);
363 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
364 template <class D> shared_ptr(nullptr_t p, D d);
365 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant1694d232011-05-28 14:41:13 +0000366 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
367 shared_ptr(const shared_ptr& r) noexcept;
368 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
369 shared_ptr(shared_ptr&& r) noexcept;
370 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000371 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
372 template<class Y> shared_ptr(auto_ptr<Y>&& r);
373 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
374 shared_ptr(nullptr_t) : shared_ptr() { }
375
376 // destructor:
377 ~shared_ptr();
378
379 // assignment:
Howard Hinnant1694d232011-05-28 14:41:13 +0000380 shared_ptr& operator=(const shared_ptr& r) noexcept;
381 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
382 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000383 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
384 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r);
385 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
386
387 // modifiers:
Howard Hinnant1694d232011-05-28 14:41:13 +0000388 void swap(shared_ptr& r) noexcept;
389 void reset() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000390 template<class Y> void reset(Y* p);
391 template<class Y, class D> void reset(Y* p, D d);
392 template<class Y, class D, class A> void reset(Y* p, D d, A a);
393
Howard Hinnant1694d232011-05-28 14:41:13 +0000394 // observers:
395 T* get() const noexcept;
396 T& operator*() const noexcept;
397 T* operator->() const noexcept;
398 long use_count() const noexcept;
399 bool unique() const noexcept;
400 explicit operator bool() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000401 template<class U> bool owner_before(shared_ptr<U> const& b) const;
402 template<class U> bool owner_before(weak_ptr<U> const& b) const;
403};
404
405// shared_ptr comparisons:
406template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000407 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000408template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000409 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000410template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000411 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000412template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000413 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000414template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000415 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000416template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000417 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
418
419template <class T>
420 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
421template <class T>
422 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
423template <class T>
424 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
425template <class T>
426 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
427template <class T>
428 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
429template <class T>
430bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
431template <class T>
432 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
433template <class T>
434 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
435template <class T>
436 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
437template <class T>
438 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
439template <class T>
440 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
441template <class T>
442 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000443
444// shared_ptr specialized algorithms:
Howard Hinnant1694d232011-05-28 14:41:13 +0000445template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000446
447// shared_ptr casts:
448template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000449 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000450template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000451 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000452template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000453 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000454
455// shared_ptr I/O:
456template<class E, class T, class Y>
457 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
458
459// shared_ptr get_deleter:
Howard Hinnant1694d232011-05-28 14:41:13 +0000460template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000461
462template<class T, class... Args>
463 shared_ptr<T> make_shared(Args&&... args);
464template<class T, class A, class... Args>
465 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
466
467template<class T>
468class weak_ptr
469{
470public:
471 typedef T element_type;
472
473 // constructors
Howard Hinnant1694d232011-05-28 14:41:13 +0000474 constexpr weak_ptr() noexcept;
475 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
476 weak_ptr(weak_ptr const& r) noexcept;
477 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000478
479 // destructor
480 ~weak_ptr();
481
482 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000483 weak_ptr& operator=(weak_ptr const& r) noexcept;
484 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
485 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000486
487 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000488 void swap(weak_ptr& r) noexcept;
489 void reset() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000490
491 // observers
Howard Hinnant1694d232011-05-28 14:41:13 +0000492 long use_count() const noexcept;
493 bool expired() const noexcept;
494 shared_ptr<T> lock() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000495 template<class U> bool owner_before(shared_ptr<U> const& b);
496 template<class U> bool owner_before(weak_ptr<U> const& b);
497};
498
499// weak_ptr specialized algorithms:
Howard Hinnant1694d232011-05-28 14:41:13 +0000500template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000501
502// class owner_less:
503template<class T> struct owner_less;
504
505template<class T>
506struct owner_less<shared_ptr<T>>
507 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
508{
509 typedef bool result_type;
510 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const;
511 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
512 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
513};
514
515template<class T>
516struct owner_less<weak_ptr<T>>
517 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
518{
519 typedef bool result_type;
520 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const;
521 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
522 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
523};
524
525template<class T>
526class enable_shared_from_this
527{
528protected:
Howard Hinnant1694d232011-05-28 14:41:13 +0000529 constexpr enable_shared_from_this() noexcept;
530 enable_shared_from_this(enable_shared_from_this const&) noexcept;
531 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000532 ~enable_shared_from_this();
533public:
534 shared_ptr<T> shared_from_this();
535 shared_ptr<T const> shared_from_this() const;
536};
537
538template<class T>
539 bool atomic_is_lock_free(const shared_ptr<T>* p);
540template<class T>
541 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
542template<class T>
543 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
544template<class T>
545 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
546template<class T>
547 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
548template<class T>
549 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
550template<class T>
551 shared_ptr<T>
552 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
553template<class T>
554 bool
555 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
556template<class T>
557 bool
558 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
559template<class T>
560 bool
561 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
562 shared_ptr<T> w, memory_order success,
563 memory_order failure);
564template<class T>
565 bool
566 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
567 shared_ptr<T> w, memory_order success,
568 memory_order failure);
569// Hash support
570template <class T> struct hash;
571template <class T, class D> struct hash<unique_ptr<T, D> >;
572template <class T> struct hash<shared_ptr<T> >;
573
574// Pointer safety
575enum class pointer_safety { relaxed, preferred, strict };
576void declare_reachable(void *p);
577template <class T> T *undeclare_reachable(T *p);
578void declare_no_pointers(char *p, size_t n);
579void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant1694d232011-05-28 14:41:13 +0000580pointer_safety get_pointer_safety() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000581
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000582void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
583
584} // std
585
586*/
587
588#include <__config>
589#include <type_traits>
590#include <typeinfo>
591#include <cstddef>
592#include <cstdint>
593#include <new>
594#include <utility>
595#include <limits>
596#include <iterator>
597#include <__functional_base>
598#if defined(_LIBCPP_NO_EXCEPTIONS)
599 #include <cassert>
600#endif
601
602#pragma GCC system_header
603
604_LIBCPP_BEGIN_NAMESPACE_STD
605
606// allocator_arg_t
607
Howard Hinnant82894812010-09-22 16:48:34 +0000608struct _LIBCPP_VISIBLE allocator_arg_t { };
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000609
610extern const allocator_arg_t allocator_arg;
611
612// addressof
613
614template <class _Tp>
615inline _LIBCPP_INLINE_VISIBILITY
616_Tp*
Howard Hinnant1694d232011-05-28 14:41:13 +0000617addressof(_Tp& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000618{
619 return (_Tp*)&(char&)__x;
620}
621
Douglas Gregor35d2fcf2011-06-22 22:17:44 +0000622#if defined(_LIBCPP_HAS_OBJC_ARC) && !defined(_LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF)
623// Objective-C++ Automatic Reference Counting uses qualified pointers
624// that require special addressof() signatures. When
625// _LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF is defined, the compiler
626// itself is providing these definitions. Otherwise, we provide them.
627template <class _Tp>
628inline _LIBCPP_INLINE_VISIBILITY
629__strong _Tp*
630addressof(__strong _Tp& __x) _NOEXCEPT
631{
632 return &__x;
633}
634
635#ifdef _LIBCPP_HAS_OBJC_ARC_WEAK
636template <class _Tp>
637inline _LIBCPP_INLINE_VISIBILITY
638__weak _Tp*
639addressof(__weak _Tp& __x) _NOEXCEPT
640{
641 return &__x;
642}
643#endif
644
645template <class _Tp>
646inline _LIBCPP_INLINE_VISIBILITY
647__autoreleasing _Tp*
648addressof(__autoreleasing _Tp& __x) _NOEXCEPT
649{
650 return &__x;
651}
652
653template <class _Tp>
654inline _LIBCPP_INLINE_VISIBILITY
655__unsafe_unretained _Tp*
656addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT
657{
658 return &__x;
659}
660#endif
661
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000662template <class _Tp> class allocator;
663
664template <>
Howard Hinnant82894812010-09-22 16:48:34 +0000665class _LIBCPP_VISIBLE allocator<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000666{
667public:
668 typedef void* pointer;
669 typedef const void* const_pointer;
670 typedef void value_type;
671
672 template <class _Up> struct rebind {typedef allocator<_Up> other;};
673};
674
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000675// pointer_traits
676
677template <class _Tp>
678struct __has_element_type
679{
680private:
681 struct __two {char _; char __;};
682 template <class _Up> static __two __test(...);
683 template <class _Up> static char __test(typename _Up::element_type* = 0);
684public:
685 static const bool value = sizeof(__test<_Tp>(0)) == 1;
686};
687
688template <class _Ptr, bool = __has_element_type<_Ptr>::value>
689struct __pointer_traits_element_type;
690
691template <class _Ptr>
692struct __pointer_traits_element_type<_Ptr, true>
693{
694 typedef typename _Ptr::element_type type;
695};
696
697#ifndef _LIBCPP_HAS_NO_VARIADICS
698
699template <template <class, class...> class _Sp, class _Tp, class ..._Args>
700struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
701{
702 typedef typename _Sp<_Tp, _Args...>::element_type type;
703};
704
705template <template <class, class...> class _Sp, class _Tp, class ..._Args>
706struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
707{
708 typedef _Tp type;
709};
710
Howard Hinnant324bb032010-08-22 00:02:43 +0000711#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000712
713template <template <class> class _Sp, class _Tp>
714struct __pointer_traits_element_type<_Sp<_Tp>, true>
715{
716 typedef typename _Sp<_Tp>::element_type type;
717};
718
719template <template <class> class _Sp, class _Tp>
720struct __pointer_traits_element_type<_Sp<_Tp>, false>
721{
722 typedef _Tp type;
723};
724
725template <template <class, class> class _Sp, class _Tp, class _A0>
726struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
727{
728 typedef typename _Sp<_Tp, _A0>::element_type type;
729};
730
731template <template <class, class> class _Sp, class _Tp, class _A0>
732struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
733{
734 typedef _Tp type;
735};
736
737template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
738struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
739{
740 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
741};
742
743template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
744struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
745{
746 typedef _Tp type;
747};
748
749template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
750 class _A1, class _A2>
751struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
752{
753 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
754};
755
756template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
757 class _A1, class _A2>
758struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
759{
760 typedef _Tp type;
761};
762
Howard Hinnant324bb032010-08-22 00:02:43 +0000763#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000764
765template <class _Tp>
766struct __has_difference_type
767{
768private:
769 struct __two {char _; char __;};
770 template <class _Up> static __two __test(...);
771 template <class _Up> static char __test(typename _Up::difference_type* = 0);
772public:
773 static const bool value = sizeof(__test<_Tp>(0)) == 1;
774};
775
776template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
777struct __pointer_traits_difference_type
778{
779 typedef ptrdiff_t type;
780};
781
782template <class _Ptr>
783struct __pointer_traits_difference_type<_Ptr, true>
784{
785 typedef typename _Ptr::difference_type type;
786};
787
788template <class _Tp, class _Up>
789struct __has_rebind
790{
791private:
792 struct __two {char _; char __;};
793 template <class _Xp> static __two __test(...);
794 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
795public:
796 static const bool value = sizeof(__test<_Tp>(0)) == 1;
797};
798
799template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
800struct __pointer_traits_rebind
801{
802#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
803 typedef typename _Tp::template rebind<_Up> type;
804#else
805 typedef typename _Tp::template rebind<_Up>::other type;
806#endif
807};
808
809#ifndef _LIBCPP_HAS_NO_VARIADICS
810
811template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
812struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
813{
814#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
815 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
816#else
817 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
818#endif
819};
820
821template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
822struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
823{
824 typedef _Sp<_Up, _Args...> type;
825};
826
Howard Hinnant324bb032010-08-22 00:02:43 +0000827#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000828
829template <template <class> class _Sp, class _Tp, class _Up>
830struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
831{
832#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
833 typedef typename _Sp<_Tp>::template rebind<_Up> type;
834#else
835 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
836#endif
837};
838
839template <template <class> class _Sp, class _Tp, class _Up>
840struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
841{
842 typedef _Sp<_Up> type;
843};
844
845template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
846struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
847{
848#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
849 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
850#else
851 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
852#endif
853};
854
855template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
856struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
857{
858 typedef _Sp<_Up, _A0> type;
859};
860
861template <template <class, class, class> class _Sp, class _Tp, class _A0,
862 class _A1, class _Up>
863struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
864{
865#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
866 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
867#else
868 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
869#endif
870};
871
872template <template <class, class, class> class _Sp, class _Tp, class _A0,
873 class _A1, class _Up>
874struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
875{
876 typedef _Sp<_Up, _A0, _A1> type;
877};
878
879template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
880 class _A1, class _A2, class _Up>
881struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
882{
883#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
884 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
885#else
886 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
887#endif
888};
889
890template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
891 class _A1, class _A2, class _Up>
892struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
893{
894 typedef _Sp<_Up, _A0, _A1, _A2> type;
895};
896
Howard Hinnant324bb032010-08-22 00:02:43 +0000897#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000898
899template <class _Ptr>
Howard Hinnant82894812010-09-22 16:48:34 +0000900struct _LIBCPP_VISIBLE pointer_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000901{
902 typedef _Ptr pointer;
903 typedef typename __pointer_traits_element_type<pointer>::type element_type;
904 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
905
906#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant6b41c602011-05-11 20:21:19 +0000907 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000908#else
909 template <class _Up> struct rebind
910 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant324bb032010-08-22 00:02:43 +0000911#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000912
913private:
914 struct __nat {};
915public:
Howard Hinnant82894812010-09-22 16:48:34 +0000916 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000917 static pointer pointer_to(typename conditional<is_void<element_type>::value,
918 __nat, element_type>::type& __r)
919 {return pointer::pointer_to(__r);}
920};
921
922template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +0000923struct _LIBCPP_VISIBLE pointer_traits<_Tp*>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000924{
925 typedef _Tp* pointer;
926 typedef _Tp element_type;
927 typedef ptrdiff_t difference_type;
928
929#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
930 template <class _Up> using rebind = _Up*;
931#else
932 template <class _Up> struct rebind {typedef _Up* other;};
933#endif
934
935private:
936 struct __nat {};
937public:
Howard Hinnant82894812010-09-22 16:48:34 +0000938 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000939 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant1694d232011-05-28 14:41:13 +0000940 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000941 {return _VSTD::addressof(__r);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000942};
943
944// allocator_traits
945
946namespace __has_pointer_type_imp
947{
948 template <class _Up> static __two test(...);
949 template <class _Up> static char test(typename _Up::pointer* = 0);
950}
951
952template <class _Tp>
953struct __has_pointer_type
954 : public integral_constant<bool, sizeof(__has_pointer_type_imp::test<_Tp>(0)) == 1>
955{
956};
957
958namespace __pointer_type_imp
959{
960
961template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
962struct __pointer_type
963{
964 typedef typename _Dp::pointer type;
965};
966
967template <class _Tp, class _Dp>
968struct __pointer_type<_Tp, _Dp, false>
969{
970 typedef _Tp* type;
971};
972
Howard Hinnant47761072010-11-18 01:40:00 +0000973} // __pointer_type_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000974
975template <class _Tp, class _Dp>
976struct __pointer_type
977{
978 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
979};
980
981template <class _Tp>
982struct __has_const_pointer
983{
984private:
985 struct __two {char _; char __;};
986 template <class _Up> static __two __test(...);
987 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
988public:
989 static const bool value = sizeof(__test<_Tp>(0)) == 1;
990};
991
992template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
993struct __const_pointer
994{
995 typedef typename _Alloc::const_pointer type;
996};
997
998template <class _Tp, class _Ptr, class _Alloc>
999struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1000{
1001#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1002 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1003#else
1004 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1005#endif
1006};
1007
1008template <class _Tp>
1009struct __has_void_pointer
1010{
1011private:
1012 struct __two {char _; char __;};
1013 template <class _Up> static __two __test(...);
1014 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1015public:
1016 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1017};
1018
1019template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1020struct __void_pointer
1021{
1022 typedef typename _Alloc::void_pointer type;
1023};
1024
1025template <class _Ptr, class _Alloc>
1026struct __void_pointer<_Ptr, _Alloc, false>
1027{
1028#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1029 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1030#else
1031 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1032#endif
1033};
1034
1035template <class _Tp>
1036struct __has_const_void_pointer
1037{
1038private:
1039 struct __two {char _; char __;};
1040 template <class _Up> static __two __test(...);
1041 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1042public:
1043 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1044};
1045
1046template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1047struct __const_void_pointer
1048{
1049 typedef typename _Alloc::const_void_pointer type;
1050};
1051
1052template <class _Ptr, class _Alloc>
1053struct __const_void_pointer<_Ptr, _Alloc, false>
1054{
1055#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1056 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1057#else
1058 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1059#endif
1060};
1061
1062template <class _T>
1063inline _LIBCPP_INLINE_VISIBILITY
1064_T*
Howard Hinnant1694d232011-05-28 14:41:13 +00001065__to_raw_pointer(_T* __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001066{
1067 return __p;
1068}
1069
1070template <class _Pointer>
1071inline _LIBCPP_INLINE_VISIBILITY
1072typename pointer_traits<_Pointer>::element_type*
Howard Hinnant1694d232011-05-28 14:41:13 +00001073__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001075 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001076}
1077
1078template <class _Tp>
1079struct __has_size_type
1080{
1081private:
1082 struct __two {char _; char __;};
1083 template <class _Up> static __two __test(...);
1084 template <class _Up> static char __test(typename _Up::size_type* = 0);
1085public:
1086 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1087};
1088
Howard Hinnant47761072010-11-18 01:40:00 +00001089template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001090struct __size_type
1091{
Howard Hinnant47761072010-11-18 01:40:00 +00001092 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001093};
1094
Howard Hinnant47761072010-11-18 01:40:00 +00001095template <class _Alloc, class _DiffType>
1096struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001097{
1098 typedef typename _Alloc::size_type type;
1099};
1100
1101template <class _Tp>
1102struct __has_propagate_on_container_copy_assignment
1103{
1104private:
1105 struct __two {char _; char __;};
1106 template <class _Up> static __two __test(...);
1107 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1108public:
1109 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1110};
1111
1112template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1113struct __propagate_on_container_copy_assignment
1114{
1115 typedef false_type type;
1116};
1117
1118template <class _Alloc>
1119struct __propagate_on_container_copy_assignment<_Alloc, true>
1120{
1121 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1122};
1123
1124template <class _Tp>
1125struct __has_propagate_on_container_move_assignment
1126{
1127private:
1128 struct __two {char _; char __;};
1129 template <class _Up> static __two __test(...);
1130 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1131public:
1132 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1133};
1134
1135template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1136struct __propagate_on_container_move_assignment
1137{
1138 typedef false_type type;
1139};
1140
1141template <class _Alloc>
1142struct __propagate_on_container_move_assignment<_Alloc, true>
1143{
1144 typedef typename _Alloc::propagate_on_container_move_assignment type;
1145};
1146
1147template <class _Tp>
1148struct __has_propagate_on_container_swap
1149{
1150private:
1151 struct __two {char _; char __;};
1152 template <class _Up> static __two __test(...);
1153 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1154public:
1155 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1156};
1157
1158template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1159struct __propagate_on_container_swap
1160{
1161 typedef false_type type;
1162};
1163
1164template <class _Alloc>
1165struct __propagate_on_container_swap<_Alloc, true>
1166{
1167 typedef typename _Alloc::propagate_on_container_swap type;
1168};
1169
1170template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1171struct __has_rebind_other
1172{
1173private:
1174 struct __two {char _; char __;};
1175 template <class _Xp> static __two __test(...);
1176 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1177public:
1178 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1179};
1180
1181template <class _Tp, class _Up>
1182struct __has_rebind_other<_Tp, _Up, false>
1183{
1184 static const bool value = false;
1185};
1186
1187template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1188struct __allocator_traits_rebind
1189{
1190 typedef typename _Tp::template rebind<_Up>::other type;
1191};
1192
1193#ifndef _LIBCPP_HAS_NO_VARIADICS
1194
1195template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1196struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1197{
1198 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1199};
1200
1201template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1202struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1203{
1204 typedef _Alloc<_Up, _Args...> type;
1205};
1206
Howard Hinnant324bb032010-08-22 00:02:43 +00001207#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001208
1209template <template <class> class _Alloc, class _Tp, class _Up>
1210struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1211{
1212 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1213};
1214
1215template <template <class> class _Alloc, class _Tp, class _Up>
1216struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1217{
1218 typedef _Alloc<_Up> type;
1219};
1220
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001221template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1222struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1223{
1224 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1225};
1226
1227template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1228struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1229{
1230 typedef _Alloc<_Up, _A0> type;
1231};
1232
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001233template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1234 class _A1, class _Up>
1235struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1236{
1237 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1238};
1239
1240template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1241 class _A1, class _Up>
1242struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1243{
1244 typedef _Alloc<_Up, _A0, _A1> type;
1245};
1246
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001247template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1248 class _A1, class _A2, class _Up>
1249struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1250{
1251 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1252};
1253
1254template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1255 class _A1, class _A2, class _Up>
1256struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1257{
1258 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1259};
1260
Howard Hinnant324bb032010-08-22 00:02:43 +00001261#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001262
1263#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1264
1265template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1266auto
1267__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1268 -> decltype(__a.allocate(__sz, __p), true_type());
1269
1270template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1271auto
1272__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1273 -> false_type;
1274
1275template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1276struct __has_allocate_hint
1277 : integral_constant<bool,
1278 is_same<
1279 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1280 declval<_SizeType>(),
1281 declval<_ConstVoidPtr>())),
1282 true_type>::value>
1283{
1284};
1285
Howard Hinnant324bb032010-08-22 00:02:43 +00001286#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001287
1288template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1289struct __has_allocate_hint
1290 : true_type
1291{
1292};
1293
Howard Hinnant324bb032010-08-22 00:02:43 +00001294#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001295
1296#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1297
1298template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnant0949eed2011-06-30 21:18:19 +00001299decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1300 _VSTD::declval<_Args>()...),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001301 true_type())
1302__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1303
1304template <class _Alloc, class _Pointer, class ..._Args>
1305false_type
1306__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1307
1308template <class _Alloc, class _Pointer, class ..._Args>
1309struct __has_construct
1310 : integral_constant<bool,
1311 is_same<
1312 decltype(__has_construct_test(declval<_Alloc>(),
1313 declval<_Pointer>(),
1314 declval<_Args>()...)),
1315 true_type>::value>
1316{
1317};
1318
1319template <class _Alloc, class _Pointer>
1320auto
1321__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1322 -> decltype(__a.destroy(__p), true_type());
1323
1324template <class _Alloc, class _Pointer>
1325auto
1326__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1327 -> false_type;
1328
1329template <class _Alloc, class _Pointer>
1330struct __has_destroy
1331 : integral_constant<bool,
1332 is_same<
1333 decltype(__has_destroy_test(declval<_Alloc>(),
1334 declval<_Pointer>())),
1335 true_type>::value>
1336{
1337};
1338
1339template <class _Alloc>
1340auto
1341__has_max_size_test(_Alloc&& __a)
1342 -> decltype(__a.max_size(), true_type());
1343
1344template <class _Alloc>
1345auto
1346__has_max_size_test(const volatile _Alloc& __a)
1347 -> false_type;
1348
1349template <class _Alloc>
1350struct __has_max_size
1351 : integral_constant<bool,
1352 is_same<
1353 decltype(__has_max_size_test(declval<_Alloc&>())),
1354 true_type>::value>
1355{
1356};
1357
1358template <class _Alloc>
1359auto
1360__has_select_on_container_copy_construction_test(_Alloc&& __a)
1361 -> decltype(__a.select_on_container_copy_construction(), true_type());
1362
1363template <class _Alloc>
1364auto
1365__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1366 -> false_type;
1367
1368template <class _Alloc>
1369struct __has_select_on_container_copy_construction
1370 : integral_constant<bool,
1371 is_same<
1372 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1373 true_type>::value>
1374{
1375};
1376
Howard Hinnant324bb032010-08-22 00:02:43 +00001377#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001378
1379#ifndef _LIBCPP_HAS_NO_VARIADICS
1380
1381template <class _Alloc, class _Pointer, class ..._Args>
1382struct __has_construct
1383 : false_type
1384{
1385};
1386
Howard Hinnant324bb032010-08-22 00:02:43 +00001387#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001388
1389template <class _Alloc, class _Pointer>
1390struct __has_destroy
1391 : false_type
1392{
1393};
1394
1395template <class _Alloc>
1396struct __has_max_size
1397 : true_type
1398{
1399};
1400
1401template <class _Alloc>
1402struct __has_select_on_container_copy_construction
1403 : false_type
1404{
1405};
1406
Howard Hinnant324bb032010-08-22 00:02:43 +00001407#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001408
Howard Hinnant47761072010-11-18 01:40:00 +00001409template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1410struct __alloc_traits_difference_type
1411{
1412 typedef typename pointer_traits<_Ptr>::difference_type type;
1413};
1414
1415template <class _Alloc, class _Ptr>
1416struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1417{
1418 typedef typename _Alloc::difference_type type;
1419};
1420
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001421template <class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001422struct _LIBCPP_VISIBLE allocator_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001423{
1424 typedef _Alloc allocator_type;
1425 typedef typename allocator_type::value_type value_type;
1426
1427 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1428 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1429 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1430 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1431
Howard Hinnant47761072010-11-18 01:40:00 +00001432 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1433 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001434
1435 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1436 propagate_on_container_copy_assignment;
1437 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1438 propagate_on_container_move_assignment;
1439 typedef typename __propagate_on_container_swap<allocator_type>::type
1440 propagate_on_container_swap;
1441
1442#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1443 template <class _Tp> using rebind_alloc =
Howard Hinnant6b41c602011-05-11 20:21:19 +00001444 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001445 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant324bb032010-08-22 00:02:43 +00001446#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001447 template <class _Tp> struct rebind_alloc
1448 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1449 template <class _Tp> struct rebind_traits
1450 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant324bb032010-08-22 00:02:43 +00001451#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001452
Howard Hinnant82894812010-09-22 16:48:34 +00001453 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001454 static pointer allocate(allocator_type& __a, size_type __n)
1455 {return __a.allocate(__n);}
Howard Hinnant82894812010-09-22 16:48:34 +00001456 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001457 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1458 {return allocate(__a, __n, __hint,
1459 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1460
Howard Hinnant82894812010-09-22 16:48:34 +00001461 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001462 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001463 {__a.deallocate(__p, __n);}
1464
1465#ifndef _LIBCPP_HAS_NO_VARIADICS
1466 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001468 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
1469 {__construct(__has_construct<allocator_type, pointer, _Args...>(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001470 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001471#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001472 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001473 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001474 static void construct(allocator_type& __a, _Tp* __p)
1475 {
1476 ::new ((void*)__p) _Tp();
1477 }
1478 template <class _Tp, class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00001479 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001480 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1481 {
1482 ::new ((void*)__p) _Tp(__a0);
1483 }
1484 template <class _Tp, class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00001485 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001486 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1487 const _A1& __a1)
1488 {
1489 ::new ((void*)__p) _Tp(__a0, __a1);
1490 }
1491 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00001492 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001493 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1494 const _A1& __a1, const _A2& __a2)
1495 {
1496 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1497 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001498#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001499
1500 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001501 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001502 static void destroy(allocator_type& __a, _Tp* __p)
1503 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1504
Howard Hinnant82894812010-09-22 16:48:34 +00001505 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001506 static size_type max_size(const allocator_type& __a)
1507 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1508
Howard Hinnant82894812010-09-22 16:48:34 +00001509 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001510 static allocator_type
1511 select_on_container_copy_construction(const allocator_type& __a)
1512 {return select_on_container_copy_construction(
1513 __has_select_on_container_copy_construction<const allocator_type>(),
1514 __a);}
1515
1516private:
1517
Howard Hinnant82894812010-09-22 16:48:34 +00001518 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001519 static pointer allocate(allocator_type& __a, size_type __n,
1520 const_void_pointer __hint, true_type)
1521 {return __a.allocate(__n, __hint);}
Howard Hinnant82894812010-09-22 16:48:34 +00001522 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001523 static pointer allocate(allocator_type& __a, size_type __n,
1524 const_void_pointer __hint, false_type)
1525 {return __a.allocate(__n);}
1526
1527#ifndef _LIBCPP_HAS_NO_VARIADICS
1528 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001531 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001532 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001534 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1535 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001536 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001537 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001538#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001539
1540 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001542 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1543 {__a.destroy(__p);}
1544 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001546 static void __destroy(false_type, allocator_type&, _Tp* __p)
1547 {
1548 __p->~_Tp();
1549 }
1550
Howard Hinnant82894812010-09-22 16:48:34 +00001551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001552 static size_type __max_size(true_type, const allocator_type& __a)
1553 {return __a.max_size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001555 static size_type __max_size(false_type, const allocator_type&)
1556 {return numeric_limits<size_type>::max();}
1557
Howard Hinnant82894812010-09-22 16:48:34 +00001558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001559 static allocator_type
1560 select_on_container_copy_construction(true_type, const allocator_type& __a)
1561 {return __a.select_on_container_copy_construction();}
Howard Hinnant82894812010-09-22 16:48:34 +00001562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001563 static allocator_type
1564 select_on_container_copy_construction(false_type, const allocator_type& __a)
1565 {return __a;}
1566};
1567
1568// uses_allocator
1569
1570template <class _Tp>
1571struct __has_allocator_type
1572{
1573private:
1574 struct __two {char _; char __;};
1575 template <class _Up> static __two __test(...);
1576 template <class _Up> static char __test(typename _Up::allocator_type* = 0);
1577public:
1578 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1579};
1580
1581template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
1582struct __uses_allocator
1583 : public integral_constant<bool,
1584 is_convertible<_Alloc, typename _Tp::allocator_type>::value>
1585{
1586};
1587
1588template <class _Tp, class _Alloc>
1589struct __uses_allocator<_Tp, _Alloc, false>
1590 : public false_type
1591{
1592};
1593
1594template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001595struct _LIBCPP_VISIBLE uses_allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001596 : public __uses_allocator<_Tp, _Alloc>
1597{
1598};
1599
Howard Hinnantac38bae2011-01-11 20:02:45 +00001600#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001601
1602// uses-allocator construction
1603
1604template <class _Tp, class _Alloc, class ..._Args>
1605struct __uses_alloc_ctor_imp
1606{
1607 static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
1608 static const bool __ic =
1609 is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
1610 static const int value = __ua ? 2 - __ic : 0;
1611};
1612
1613template <class _Tp, class _Alloc, class ..._Args>
1614struct __uses_alloc_ctor
1615 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
1616 {};
1617
Howard Hinnantac38bae2011-01-11 20:02:45 +00001618#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001619
1620// allocator
1621
1622template <class _Tp>
Howard Hinnant36cdf022010-09-10 16:42:26 +00001623class _LIBCPP_VISIBLE allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001624{
1625public:
1626 typedef size_t size_type;
1627 typedef ptrdiff_t difference_type;
1628 typedef _Tp* pointer;
1629 typedef const _Tp* const_pointer;
1630 typedef _Tp& reference;
1631 typedef const _Tp& const_reference;
1632 typedef _Tp value_type;
1633
Howard Hinnant18884f42011-06-02 21:38:57 +00001634 typedef true_type propagate_on_container_move_assignment;
1635
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001636 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1637
Howard Hinnant1694d232011-05-28 14:41:13 +00001638 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1639 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1640 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001641 {return _VSTD::addressof(__x);}
Howard Hinnant1694d232011-05-28 14:41:13 +00001642 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001643 {return _VSTD::addressof(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001644 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1645 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
Howard Hinnant1694d232011-05-28 14:41:13 +00001646 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
1647 {::operator delete((void*)__p);}
1648 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1649 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001650#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001651 template <class _Up, class... _Args>
1652 _LIBCPP_INLINE_VISIBILITY
1653 void
1654 construct(_Up* __p, _Args&&... __args)
1655 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001656 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001657 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001658#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001659 _LIBCPP_INLINE_VISIBILITY
1660 void
1661 construct(pointer __p)
1662 {
1663 ::new((void*)__p) _Tp();
1664 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001665# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001666 template <class _A0>
1667 _LIBCPP_INLINE_VISIBILITY
1668 typename enable_if
1669 <
1670 !is_convertible<_A0, __rv<_A0> >::value,
1671 void
1672 >::type
1673 construct(pointer __p, _A0& __a0)
1674 {
1675 ::new((void*)__p) _Tp(__a0);
1676 }
1677 template <class _A0>
1678 _LIBCPP_INLINE_VISIBILITY
1679 typename enable_if
1680 <
1681 !is_convertible<_A0, __rv<_A0> >::value,
1682 void
1683 >::type
1684 construct(pointer __p, const _A0& __a0)
1685 {
1686 ::new((void*)__p) _Tp(__a0);
1687 }
1688 template <class _A0>
1689 _LIBCPP_INLINE_VISIBILITY
1690 typename enable_if
1691 <
1692 is_convertible<_A0, __rv<_A0> >::value,
1693 void
1694 >::type
1695 construct(pointer __p, _A0 __a0)
1696 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001697 ::new((void*)__p) _Tp(_VSTD::move(__a0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001698 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001699# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001700 template <class _A0, class _A1>
1701 _LIBCPP_INLINE_VISIBILITY
1702 void
1703 construct(pointer __p, _A0& __a0, _A1& __a1)
1704 {
1705 ::new((void*)__p) _Tp(__a0, __a1);
1706 }
1707 template <class _A0, class _A1>
1708 _LIBCPP_INLINE_VISIBILITY
1709 void
1710 construct(pointer __p, const _A0& __a0, _A1& __a1)
1711 {
1712 ::new((void*)__p) _Tp(__a0, __a1);
1713 }
1714 template <class _A0, class _A1>
1715 _LIBCPP_INLINE_VISIBILITY
1716 void
1717 construct(pointer __p, _A0& __a0, const _A1& __a1)
1718 {
1719 ::new((void*)__p) _Tp(__a0, __a1);
1720 }
1721 template <class _A0, class _A1>
1722 _LIBCPP_INLINE_VISIBILITY
1723 void
1724 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1725 {
1726 ::new((void*)__p) _Tp(__a0, __a1);
1727 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001728#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001729 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1730};
1731
1732template <class _Tp, class _Up>
1733inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001734bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001735
1736template <class _Tp, class _Up>
1737inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001738bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001739
1740template <class _OutputIterator, class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001741class _LIBCPP_VISIBLE raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001742 : public iterator<output_iterator_tag,
1743 _Tp, // purposefully not C++03
1744 ptrdiff_t, // purposefully not C++03
1745 _Tp*, // purposefully not C++03
1746 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1747{
1748private:
1749 _OutputIterator __x_;
1750public:
1751 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1752 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1753 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1754 {::new(&*__x_) _Tp(__element); return *this;}
1755 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1756 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1757 {raw_storage_iterator __t(*this); ++__x_; return __t;}
1758};
1759
1760template <class _Tp>
1761pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00001762get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001763{
1764 pair<_Tp*, ptrdiff_t> __r(0, 0);
1765 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1766 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1767 / sizeof(_Tp);
1768 if (__n > __m)
1769 __n = __m;
1770 while (__n > 0)
1771 {
1772 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1773 if (__r.first)
1774 {
1775 __r.second = __n;
1776 break;
1777 }
1778 __n /= 2;
1779 }
1780 return __r;
1781}
1782
1783template <class _Tp>
1784inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001785void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001786
1787template <class _Tp>
1788struct auto_ptr_ref
1789{
1790 _Tp* __ptr_;
1791};
1792
1793template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001794class _LIBCPP_VISIBLE auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001795{
1796private:
1797 _Tp* __ptr_;
1798public:
1799 typedef _Tp element_type;
1800
1801 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1802 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1803 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1804 : __ptr_(__p.release()) {}
1805 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1806 {reset(__p.release()); return *this;}
1807 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1808 {reset(__p.release()); return *this;}
1809 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1810 {reset(__p.__ptr_); return *this;}
1811 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1812
1813 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1814 {return *__ptr_;}
1815 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1816 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1817 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1818 {
1819 _Tp* __t = __ptr_;
1820 __ptr_ = 0;
1821 return __t;
1822 }
1823 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1824 {
1825 if (__ptr_ != __p)
1826 delete __ptr_;
1827 __ptr_ = __p;
1828 }
1829
1830 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1831 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1832 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1833 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1834 {return auto_ptr<_Up>(release());}
1835};
1836
1837template <>
Howard Hinnant82894812010-09-22 16:48:34 +00001838class _LIBCPP_VISIBLE auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001839{
1840public:
1841 typedef void element_type;
1842};
1843
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001844template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1845 typename remove_cv<_T2>::type>::value,
1846 bool = is_empty<_T1>::value,
1847 bool = is_empty<_T2>::value>
1848struct __libcpp_compressed_pair_switch;
1849
1850template <class _T1, class _T2, bool IsSame>
1851struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1852
1853template <class _T1, class _T2, bool IsSame>
1854struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1855
1856template <class _T1, class _T2, bool IsSame>
1857struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1858
1859template <class _T1, class _T2>
1860struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
1861
1862template <class _T1, class _T2>
1863struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
1864
1865template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
1866class __libcpp_compressed_pair_imp;
1867
1868template <class _T1, class _T2>
1869class __libcpp_compressed_pair_imp<_T1, _T2, 0>
1870{
1871private:
1872 _T1 __first_;
1873 _T2 __second_;
1874public:
1875 typedef _T1 _T1_param;
1876 typedef _T2 _T2_param;
1877
1878 typedef typename remove_reference<_T1>::type& _T1_reference;
1879 typedef typename remove_reference<_T2>::type& _T2_reference;
1880
1881 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1882 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1883
1884 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1885 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001886 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001887 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001888 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001889 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001890 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001891
Howard Hinnant61aa6012011-07-01 19:24:36 +00001892#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
1893
1894 _LIBCPP_INLINE_VISIBILITY
1895 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
1896 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
1897 is_nothrow_copy_constructible<_T2>::value)
1898 : __first_(__p.first()),
1899 __second_(__p.second()) {}
1900
1901 _LIBCPP_INLINE_VISIBILITY
1902 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
1903 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
1904 is_nothrow_copy_assignable<_T2>::value)
1905 {
1906 __first_ = __p.first();
1907 __second_ = __p.second();
1908 return *this;
1909 }
1910
Howard Hinnant73d21a42010-09-04 23:28:19 +00001911#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00001912
1913 _LIBCPP_INLINE_VISIBILITY
1914 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00001915 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
1916 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant61aa6012011-07-01 19:24:36 +00001917 : __first_(_VSTD::forward<_T1>(__p.first())),
1918 __second_(_VSTD::forward<_T2>(__p.second())) {}
1919
1920 _LIBCPP_INLINE_VISIBILITY
1921 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
1922 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
1923 is_nothrow_move_assignable<_T2>::value)
1924 {
1925 __first_ = _VSTD::forward<_T1>(__p.first());
1926 __second_ = _VSTD::forward<_T2>(__p.second());
1927 return *this;
1928 }
1929
Howard Hinnant73d21a42010-09-04 23:28:19 +00001930#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001931
Howard Hinnant61aa6012011-07-01 19:24:36 +00001932#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
1933
Howard Hinnant1694d232011-05-28 14:41:13 +00001934 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
1935 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001936
Howard Hinnant1694d232011-05-28 14:41:13 +00001937 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
1938 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001939
1940 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00001941 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
1942 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001943 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001944 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001945 swap(__first_, __x.__first_);
1946 swap(__second_, __x.__second_);
1947 }
1948};
1949
1950template <class _T1, class _T2>
1951class __libcpp_compressed_pair_imp<_T1, _T2, 1>
1952 : private _T1
1953{
1954private:
1955 _T2 __second_;
1956public:
1957 typedef _T1 _T1_param;
1958 typedef _T2 _T2_param;
1959
1960 typedef _T1& _T1_reference;
1961 typedef typename remove_reference<_T2>::type& _T2_reference;
1962
1963 typedef const _T1& _T1_const_reference;
1964 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1965
1966 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
1967 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1, int = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001968 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001969 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2, int* = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001970 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001971 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001972 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001973
Howard Hinnant61aa6012011-07-01 19:24:36 +00001974#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
1975
1976 _LIBCPP_INLINE_VISIBILITY
1977 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
1978 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
1979 is_nothrow_copy_constructible<_T2>::value)
1980 : _T1(__p.first()), __second_(__p.second()) {}
1981
1982 _LIBCPP_INLINE_VISIBILITY
1983 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
1984 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
1985 is_nothrow_copy_assignable<_T2>::value)
1986 {
1987 _T1::operator=(__p.first());
1988 __second_ = __p.second();
1989 return *this;
1990 }
1991
Howard Hinnant73d21a42010-09-04 23:28:19 +00001992#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00001993
1994 _LIBCPP_INLINE_VISIBILITY
1995 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00001996 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
1997 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001998 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00001999
2000 _LIBCPP_INLINE_VISIBILITY
2001 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2002 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2003 is_nothrow_move_assignable<_T2>::value)
2004 {
2005 _T1::operator=(_VSTD::move(__p.first()));
2006 __second_ = _VSTD::forward<_T2>(__p.second());
2007 return *this;
2008 }
2009
Howard Hinnant73d21a42010-09-04 23:28:19 +00002010#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002011
Howard Hinnant61aa6012011-07-01 19:24:36 +00002012#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2013
Howard Hinnant1694d232011-05-28 14:41:13 +00002014 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2015 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002016
Howard Hinnant1694d232011-05-28 14:41:13 +00002017 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2018 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002019
2020 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002021 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2022 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002023 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002024 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002025 swap(__second_, __x.__second_);
2026 }
2027};
2028
2029template <class _T1, class _T2>
2030class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2031 : private _T2
2032{
2033private:
2034 _T1 __first_;
2035public:
2036 typedef _T1 _T1_param;
2037 typedef _T2 _T2_param;
2038
2039 typedef typename remove_reference<_T1>::type& _T1_reference;
2040 typedef _T2& _T2_reference;
2041
2042 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2043 typedef const _T2& _T2_const_reference;
2044
2045 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2046 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002047 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002048 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002049 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002050 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00002051 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2052 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002053 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002054
Howard Hinnant61aa6012011-07-01 19:24:36 +00002055#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2056
2057 _LIBCPP_INLINE_VISIBILITY
2058 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2059 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2060 is_nothrow_copy_constructible<_T2>::value)
2061 : _T2(__p.second()), __first_(__p.first()) {}
2062
2063 _LIBCPP_INLINE_VISIBILITY
2064 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2065 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2066 is_nothrow_copy_assignable<_T2>::value)
2067 {
2068 _T2::operator=(__p.second());
2069 __first_ = __p.first();
2070 return *this;
2071 }
2072
Howard Hinnant73d21a42010-09-04 23:28:19 +00002073#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002074
2075 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002076 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002077 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2078 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002079 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002080
2081 _LIBCPP_INLINE_VISIBILITY
2082 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2083 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2084 is_nothrow_move_assignable<_T2>::value)
2085 {
2086 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2087 __first_ = _VSTD::move(__p.first());
2088 return *this;
2089 }
2090
Howard Hinnant73d21a42010-09-04 23:28:19 +00002091#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002092
Howard Hinnant61aa6012011-07-01 19:24:36 +00002093#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2094
Howard Hinnant1694d232011-05-28 14:41:13 +00002095 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2096 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002097
Howard Hinnant1694d232011-05-28 14:41:13 +00002098 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2099 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002100
2101 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002102 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2103 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002104 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002105 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002106 swap(__first_, __x.__first_);
2107 }
2108};
2109
2110template <class _T1, class _T2>
2111class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2112 : private _T1,
2113 private _T2
2114{
2115public:
2116 typedef _T1 _T1_param;
2117 typedef _T2 _T2_param;
2118
2119 typedef _T1& _T1_reference;
2120 typedef _T2& _T2_reference;
2121
2122 typedef const _T1& _T1_const_reference;
2123 typedef const _T2& _T2_const_reference;
2124
2125 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2126 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002127 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002128 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002129 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002130 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002131 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002132
Howard Hinnant61aa6012011-07-01 19:24:36 +00002133#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2134
2135 _LIBCPP_INLINE_VISIBILITY
2136 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2137 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2138 is_nothrow_copy_constructible<_T2>::value)
2139 : _T1(__p.first()), _T2(__p.second()) {}
2140
2141 _LIBCPP_INLINE_VISIBILITY
2142 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2143 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2144 is_nothrow_copy_assignable<_T2>::value)
2145 {
2146 _T1::operator=(__p.first());
2147 _T2::operator=(__p.second());
2148 return *this;
2149 }
2150
Howard Hinnant73d21a42010-09-04 23:28:19 +00002151#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002152
2153 _LIBCPP_INLINE_VISIBILITY
2154 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002155 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2156 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002157 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002158
2159 _LIBCPP_INLINE_VISIBILITY
2160 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2161 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2162 is_nothrow_move_assignable<_T2>::value)
2163 {
2164 _T1::operator=(_VSTD::move(__p.first()));
2165 _T2::operator=(_VSTD::move(__p.second()));
2166 return *this;
2167 }
2168
Howard Hinnant73d21a42010-09-04 23:28:19 +00002169#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002170
Howard Hinnant61aa6012011-07-01 19:24:36 +00002171#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2172
Howard Hinnant1694d232011-05-28 14:41:13 +00002173 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2174 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002175
Howard Hinnant1694d232011-05-28 14:41:13 +00002176 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2177 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002178
2179 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002180 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2181 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002182 {
2183 }
2184};
2185
2186template <class _T1, class _T2>
2187class __compressed_pair
2188 : private __libcpp_compressed_pair_imp<_T1, _T2>
2189{
2190 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2191public:
2192 typedef typename base::_T1_param _T1_param;
2193 typedef typename base::_T2_param _T2_param;
2194
2195 typedef typename base::_T1_reference _T1_reference;
2196 typedef typename base::_T2_reference _T2_reference;
2197
2198 typedef typename base::_T1_const_reference _T1_const_reference;
2199 typedef typename base::_T2_const_reference _T2_const_reference;
2200
2201 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
2202 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1, int = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002203 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002204 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2, int* = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002205 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002206 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002207 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002208
Howard Hinnant61aa6012011-07-01 19:24:36 +00002209#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2210
2211 _LIBCPP_INLINE_VISIBILITY
2212 __compressed_pair(const __compressed_pair& __p)
2213 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2214 is_nothrow_copy_constructible<_T2>::value)
2215 : base(__p) {}
2216
2217 _LIBCPP_INLINE_VISIBILITY
2218 __compressed_pair& operator=(const __compressed_pair& __p)
2219 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2220 is_nothrow_copy_assignable<_T2>::value)
2221 {
2222 base::operator=(__p);
2223 return *this;
2224 }
2225
Howard Hinnant73d21a42010-09-04 23:28:19 +00002226#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002227 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002228 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002229 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2230 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002231 : base(_VSTD::move(__p)) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002232
2233 _LIBCPP_INLINE_VISIBILITY
2234 __compressed_pair& operator=(__compressed_pair&& __p)
2235 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2236 is_nothrow_move_assignable<_T2>::value)
2237 {
2238 base::operator=(_VSTD::move(__p));
2239 return *this;
2240 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002241#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002242
Howard Hinnant61aa6012011-07-01 19:24:36 +00002243#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2244
Howard Hinnant1694d232011-05-28 14:41:13 +00002245 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2246 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002247
Howard Hinnant1694d232011-05-28 14:41:13 +00002248 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2249 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002250
Howard Hinnant1694d232011-05-28 14:41:13 +00002251 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2252 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2253 __is_nothrow_swappable<_T1>::value)
2254 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002255};
2256
2257template <class _T1, class _T2>
2258inline _LIBCPP_INLINE_VISIBILITY
2259void
2260swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002261 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2262 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002263 {__x.swap(__y);}
2264
2265template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002266struct _LIBCPP_VISIBLE default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002267{
Howard Hinnant1694d232011-05-28 14:41:13 +00002268 _LIBCPP_INLINE_VISIBILITY default_delete() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002269 template <class _Up>
2270 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002271 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2272 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002273 {
2274 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2275 delete __ptr;
2276 }
2277};
2278
2279template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002280struct _LIBCPP_VISIBLE default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002281{
Howard Hinnant1694d232011-05-28 14:41:13 +00002282 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002283 {
2284 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2285 delete [] __ptr;
2286 }
2287private:
2288 template <class _Up> void operator() (_Up*) const;
2289};
2290
2291template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant82894812010-09-22 16:48:34 +00002292class _LIBCPP_VISIBLE unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002293{
2294public:
2295 typedef _Tp element_type;
2296 typedef _Dp deleter_type;
2297 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2298private:
2299 __compressed_pair<pointer, deleter_type> __ptr_;
2300
Howard Hinnant73d21a42010-09-04 23:28:19 +00002301#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002302 unique_ptr(const unique_ptr&);
2303 unique_ptr& operator=(const unique_ptr&);
2304 template <class _Up, class _Ep>
2305 unique_ptr(const unique_ptr<_Up, _Ep>&);
2306 template <class _Up, class _Ep>
2307 unique_ptr& operator=(const unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002308#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002309 unique_ptr(unique_ptr&);
2310 template <class _Up, class _Ep>
2311 unique_ptr(unique_ptr<_Up, _Ep>&);
2312 unique_ptr& operator=(unique_ptr&);
2313 template <class _Up, class _Ep>
2314 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002315#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002316
2317 struct __nat {int __for_bool_;};
2318
2319 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2320 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2321public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002322 _LIBCPP_INLINE_VISIBILITY unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002323 : __ptr_(pointer())
2324 {
2325 static_assert(!is_pointer<deleter_type>::value,
2326 "unique_ptr constructed with null function pointer deleter");
2327 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002328 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002329 : __ptr_(pointer())
2330 {
2331 static_assert(!is_pointer<deleter_type>::value,
2332 "unique_ptr constructed with null function pointer deleter");
2333 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002334 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002335 : __ptr_(_VSTD::move(__p))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002336 {
2337 static_assert(!is_pointer<deleter_type>::value,
2338 "unique_ptr constructed with null function pointer deleter");
2339 }
2340
Howard Hinnant73d21a42010-09-04 23:28:19 +00002341#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002342 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2343 is_reference<deleter_type>::value,
2344 deleter_type,
2345 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002346 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002347 : __ptr_(__p, __d) {}
2348
2349 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002350 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002351 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002352 {
2353 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2354 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002355 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002356 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002357 template <class _Up, class _Ep>
2358 _LIBCPP_INLINE_VISIBILITY
2359 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2360 typename enable_if
2361 <
2362 !is_array<_Up>::value &&
2363 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2364 is_convertible<_Ep, deleter_type>::value &&
2365 (
2366 !is_reference<deleter_type>::value ||
2367 is_same<deleter_type, _Ep>::value
2368 ),
2369 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002370 >::type = __nat()) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002371 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002372
2373 template <class _Up>
2374 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2375 typename enable_if<
2376 is_convertible<_Up*, _Tp*>::value &&
2377 is_same<_Dp, default_delete<_Tp> >::value,
2378 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002379 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002380 : __ptr_(__p.release())
2381 {
2382 }
2383
Howard Hinnant1694d232011-05-28 14:41:13 +00002384 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002385 {
2386 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002387 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002388 return *this;
2389 }
2390
2391 template <class _Up, class _Ep>
2392 _LIBCPP_INLINE_VISIBILITY
2393 typename enable_if
2394 <
2395 !is_array<_Up>::value,
2396 unique_ptr&
2397 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002398 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002399 {
2400 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002401 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002402 return *this;
2403 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002404#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002405
2406 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2407 {
2408 return __rv<unique_ptr>(*this);
2409 }
2410
2411 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002412 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002413
2414 template <class _Up, class _Ep>
2415 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2416 {
2417 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002418 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002419 return *this;
2420 }
2421
2422 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002423 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002424
2425 template <class _Up>
2426 _LIBCPP_INLINE_VISIBILITY
2427 typename enable_if<
2428 is_convertible<_Up*, _Tp*>::value &&
2429 is_same<_Dp, default_delete<_Tp> >::value,
2430 unique_ptr&
2431 >::type
2432 operator=(auto_ptr<_Up> __p)
2433 {reset(__p.release()); return *this;}
2434
Howard Hinnant73d21a42010-09-04 23:28:19 +00002435#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002436 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2437
Howard Hinnant1694d232011-05-28 14:41:13 +00002438 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002439 {
2440 reset();
2441 return *this;
2442 }
2443
2444 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2445 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002446 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2447 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2448 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2449 {return __ptr_.second();}
2450 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2451 {return __ptr_.second();}
2452 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const
2453 _NOEXCEPT
2454 {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002455
Howard Hinnant1694d232011-05-28 14:41:13 +00002456 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002457 {
2458 pointer __t = __ptr_.first();
2459 __ptr_.first() = pointer();
2460 return __t;
2461 }
2462
Howard Hinnant1694d232011-05-28 14:41:13 +00002463 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002464 {
2465 pointer __tmp = __ptr_.first();
2466 __ptr_.first() = __p;
2467 if (__tmp)
2468 __ptr_.second()(__tmp);
2469 }
2470
Howard Hinnant1694d232011-05-28 14:41:13 +00002471 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2472 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002473};
2474
2475template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002476class _LIBCPP_VISIBLE unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002477{
2478public:
2479 typedef _Tp element_type;
2480 typedef _Dp deleter_type;
2481 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2482private:
2483 __compressed_pair<pointer, deleter_type> __ptr_;
2484
Howard Hinnant73d21a42010-09-04 23:28:19 +00002485#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002486 unique_ptr(const unique_ptr&);
2487 unique_ptr& operator=(const unique_ptr&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002488#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002489 unique_ptr(unique_ptr&);
2490 template <class _Up>
2491 unique_ptr(unique_ptr<_Up>&);
2492 unique_ptr& operator=(unique_ptr&);
2493 template <class _Up>
2494 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002495#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002496
2497 struct __nat {int __for_bool_;};
2498
2499 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2500 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2501public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002502 _LIBCPP_INLINE_VISIBILITY unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002503 : __ptr_(pointer())
2504 {
2505 static_assert(!is_pointer<deleter_type>::value,
2506 "unique_ptr constructed with null function pointer deleter");
2507 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002508 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002509 : __ptr_(pointer())
2510 {
2511 static_assert(!is_pointer<deleter_type>::value,
2512 "unique_ptr constructed with null function pointer deleter");
2513 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002514#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002515 template <class _P,
2516 class = typename enable_if<is_same<_P, pointer>::value>::type
2517 >
Howard Hinnant1694d232011-05-28 14:41:13 +00002518 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_P __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002519 : __ptr_(__p)
2520 {
2521 static_assert(!is_pointer<deleter_type>::value,
2522 "unique_ptr constructed with null function pointer deleter");
2523 }
2524
2525 template <class _P,
2526 class = typename enable_if<is_same<_P, pointer>::value>::type
2527 >
2528 _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename conditional<
2529 is_reference<deleter_type>::value,
2530 deleter_type,
2531 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002532 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002533 : __ptr_(__p, __d) {}
2534
2535 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2536 is_reference<deleter_type>::value,
2537 deleter_type,
2538 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002539 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002540 : __ptr_(pointer(), __d) {}
2541
2542 template <class _P,
2543 class = typename enable_if<is_same<_P, pointer>::value ||
2544 is_same<_P, nullptr_t>::value>::type
2545 >
2546 _LIBCPP_INLINE_VISIBILITY unique_ptr(_P __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002547 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002548 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002549 {
2550 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2551 }
2552
2553 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002554 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002555 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002556 {
2557 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2558 }
2559
Howard Hinnant1694d232011-05-28 14:41:13 +00002560 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002561 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002562
Howard Hinnant1694d232011-05-28 14:41:13 +00002563 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002564 {
2565 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002566 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002567 return *this;
2568 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002569#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002570
2571 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2572 : __ptr_(__p)
2573 {
2574 static_assert(!is_pointer<deleter_type>::value,
2575 "unique_ptr constructed with null function pointer deleter");
2576 }
2577
2578 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002579 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002580
2581 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002582 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002583
2584 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2585 {
2586 return __rv<unique_ptr>(*this);
2587 }
2588
2589 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002590 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002591
2592 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2593 {
2594 reset(__u->release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002595 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002596 return *this;
2597 }
2598
Howard Hinnant73d21a42010-09-04 23:28:19 +00002599#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002600 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2601
Howard Hinnant1694d232011-05-28 14:41:13 +00002602 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002603 {
2604 reset();
2605 return *this;
2606 }
2607
2608 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2609 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002610 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2611 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2612 {return __ptr_.second();}
2613 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2614 {return __ptr_.second();}
2615 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const _NOEXCEPT
2616 {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002617
Howard Hinnant1694d232011-05-28 14:41:13 +00002618 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002619 {
2620 pointer __t = __ptr_.first();
2621 __ptr_.first() = pointer();
2622 return __t;
2623 }
2624
Howard Hinnant73d21a42010-09-04 23:28:19 +00002625#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002626 template <class _P,
2627 class = typename enable_if<is_same<_P, pointer>::value>::type
2628 >
Howard Hinnant1694d232011-05-28 14:41:13 +00002629 _LIBCPP_INLINE_VISIBILITY void reset(_P __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002630 {
2631 pointer __tmp = __ptr_.first();
2632 __ptr_.first() = __p;
2633 if (__tmp)
2634 __ptr_.second()(__tmp);
2635 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002636 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002637 {
2638 pointer __tmp = __ptr_.first();
2639 __ptr_.first() = nullptr;
2640 if (__tmp)
2641 __ptr_.second()(__tmp);
2642 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002643 _LIBCPP_INLINE_VISIBILITY void reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002644 {
2645 pointer __tmp = __ptr_.first();
2646 __ptr_.first() = nullptr;
2647 if (__tmp)
2648 __ptr_.second()(__tmp);
2649 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002650#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002651 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2652 {
2653 pointer __tmp = __ptr_.first();
2654 __ptr_.first() = __p;
2655 if (__tmp)
2656 __ptr_.second()(__tmp);
2657 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002658#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002659
2660 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2661private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002662
Howard Hinnant73d21a42010-09-04 23:28:19 +00002663#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002664 template <class _Up>
2665 explicit unique_ptr(_Up);
2666 template <class _Up>
2667 unique_ptr(_Up __u,
2668 typename conditional<
2669 is_reference<deleter_type>::value,
2670 deleter_type,
2671 typename add_lvalue_reference<const deleter_type>::type>::type,
2672 typename enable_if
2673 <
2674 is_convertible<_Up, pointer>::value,
2675 __nat
2676 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002677#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002678};
2679
2680template <class _Tp, class _Dp>
2681inline _LIBCPP_INLINE_VISIBILITY
2682void
Howard Hinnant1694d232011-05-28 14:41:13 +00002683swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002684
2685template <class _T1, class _D1, class _T2, class _D2>
2686inline _LIBCPP_INLINE_VISIBILITY
2687bool
2688operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2689
2690template <class _T1, class _D1, class _T2, class _D2>
2691inline _LIBCPP_INLINE_VISIBILITY
2692bool
2693operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2694
2695template <class _T1, class _D1, class _T2, class _D2>
2696inline _LIBCPP_INLINE_VISIBILITY
2697bool
2698operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() < __y.get();}
2699
2700template <class _T1, class _D1, class _T2, class _D2>
2701inline _LIBCPP_INLINE_VISIBILITY
2702bool
2703operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2704
2705template <class _T1, class _D1, class _T2, class _D2>
2706inline _LIBCPP_INLINE_VISIBILITY
2707bool
2708operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2709
2710template <class _T1, class _D1, class _T2, class _D2>
2711inline _LIBCPP_INLINE_VISIBILITY
2712bool
2713operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2714
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00002715template <class _Tp> struct hash;
Howard Hinnant21aefc32010-06-03 16:42:57 +00002716
2717template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002718struct _LIBCPP_VISIBLE hash<_Tp*>
Howard Hinnant21aefc32010-06-03 16:42:57 +00002719 : public unary_function<_Tp*, size_t>
2720{
Howard Hinnant82894812010-09-22 16:48:34 +00002721 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002722 size_t operator()(_Tp* __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00002723 {
2724 const size_t* const __p = reinterpret_cast<const size_t*>(&__v);
2725 return *__p;
2726 }
2727};
2728
2729template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002730struct _LIBCPP_VISIBLE hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00002731{
2732 typedef unique_ptr<_Tp, _Dp> argument_type;
2733 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00002734 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002735 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00002736 {
2737 typedef typename argument_type::pointer pointer;
2738 return hash<pointer>()(__ptr.get());
2739 }
2740};
2741
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002742struct __destruct_n
2743{
2744private:
2745 size_t size;
2746
2747 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002748 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002749 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
2750
2751 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002752 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002753 {}
2754
Howard Hinnant1694d232011-05-28 14:41:13 +00002755 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002756 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00002757 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002758 {}
2759
Howard Hinnant1694d232011-05-28 14:41:13 +00002760 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002761 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00002762 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002763 {}
2764public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002765 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
2766 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002767
2768 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002769 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00002770 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002771
2772 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002773 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00002774 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002775
2776 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00002777 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00002778 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002779};
2780
2781template <class _Alloc>
2782class __allocator_destructor
2783{
2784 typedef allocator_traits<_Alloc> __alloc_traits;
2785public:
2786 typedef typename __alloc_traits::pointer pointer;
2787 typedef typename __alloc_traits::size_type size_type;
2788private:
2789 _Alloc& __alloc_;
2790 size_type __s_;
2791public:
2792 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00002793 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002794 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00002795 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002796 void operator()(pointer __p) _NOEXCEPT
2797 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002798};
2799
2800template <class _InputIterator, class _ForwardIterator>
2801_ForwardIterator
2802uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
2803{
2804 __destruct_n __d(0);
2805 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2806 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2807 for (; __f != __l; ++__f, ++__r, __d.__incr((value_type*)0))
2808 ::new(&*__r) value_type(*__f);
2809 __h.release();
2810 return __r;
2811}
2812
2813template <class _InputIterator, class _Size, class _ForwardIterator>
2814_ForwardIterator
2815uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
2816{
2817 __destruct_n __d(0);
2818 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2819 unique_ptr<value_type, __destruct_n&> __h(&*__r, __d);
2820 for (; __n > 0; ++__f, ++__r, __d.__incr((value_type*)0), --__n)
2821 ::new(&*__r) value_type(*__f);
2822 __h.release();
2823 return __r;
2824}
2825
2826template <class _ForwardIterator, class _Tp>
2827void
2828uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
2829{
2830 __destruct_n __d(0);
2831 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2832 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2833 for (; __f != __l; ++__f, __d.__incr((value_type*)0))
2834 ::new(&*__f) value_type(__x);
2835 __h.release();
2836}
2837
2838template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00002839_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002840uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
2841{
2842 __destruct_n __d(0);
2843 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2844 unique_ptr<value_type, __destruct_n&> __h(&*__f, __d);
2845 for (; __n > 0; ++__f, --__n, __d.__incr((value_type*)0))
2846 ::new(&*__f) value_type(__x);
2847 __h.release();
Howard Hinnant2f6a6272010-11-18 16:13:03 +00002848 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002849}
2850
Howard Hinnant82894812010-09-22 16:48:34 +00002851class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002852 : public std::exception
2853{
2854public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002855 virtual ~bad_weak_ptr() _NOEXCEPT;
2856 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002857};
2858
2859template<class _Tp> class weak_ptr;
2860
2861class __shared_count
2862{
2863 __shared_count(const __shared_count&);
2864 __shared_count& operator=(const __shared_count&);
2865
2866protected:
2867 long __shared_owners_;
2868 virtual ~__shared_count();
2869private:
Howard Hinnant1694d232011-05-28 14:41:13 +00002870 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002871
2872public:
Howard Hinnant82894812010-09-22 16:48:34 +00002873 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002874 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002875 : __shared_owners_(__refs) {}
2876
Howard Hinnant1694d232011-05-28 14:41:13 +00002877 void __add_shared() _NOEXCEPT;
2878 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00002879 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002880 long use_count() const _NOEXCEPT {return __shared_owners_ + 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002881};
2882
2883class __shared_weak_count
2884 : private __shared_count
2885{
2886 long __shared_weak_owners_;
2887
2888public:
Howard Hinnant82894812010-09-22 16:48:34 +00002889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002890 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002891 : __shared_count(__refs),
2892 __shared_weak_owners_(__refs) {}
2893protected:
2894 virtual ~__shared_weak_count();
2895
2896public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002897 void __add_shared() _NOEXCEPT;
2898 void __add_weak() _NOEXCEPT;
2899 void __release_shared() _NOEXCEPT;
2900 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00002901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00002902 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
2903 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002904
Howard Hinnantd4444702010-08-11 17:04:31 +00002905#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00002906 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00002907#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002908private:
Howard Hinnant1694d232011-05-28 14:41:13 +00002909 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002910};
2911
2912template <class _Tp, class _Dp, class _Alloc>
2913class __shared_ptr_pointer
2914 : public __shared_weak_count
2915{
2916 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
2917public:
Howard Hinnant82894812010-09-22 16:48:34 +00002918 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002919 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002920 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002921
Howard Hinnantd4444702010-08-11 17:04:31 +00002922#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00002923 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00002924#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002925
2926private:
Howard Hinnant1694d232011-05-28 14:41:13 +00002927 virtual void __on_zero_shared() _NOEXCEPT;
2928 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002929};
2930
Howard Hinnantd4444702010-08-11 17:04:31 +00002931#ifndef _LIBCPP_NO_RTTI
2932
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002933template <class _Tp, class _Dp, class _Alloc>
2934const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00002935__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002936{
2937 return __t == typeid(_Dp) ? &__data_.first().second() : 0;
2938}
2939
Howard Hinnant324bb032010-08-22 00:02:43 +00002940#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00002941
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002942template <class _Tp, class _Dp, class _Alloc>
2943void
Howard Hinnant1694d232011-05-28 14:41:13 +00002944__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002945{
2946 __data_.first().second()(__data_.first().first());
2947 __data_.first().second().~_Dp();
2948}
2949
2950template <class _Tp, class _Dp, class _Alloc>
2951void
Howard Hinnant1694d232011-05-28 14:41:13 +00002952__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002953{
2954 typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
2955 __data_.second().~_Alloc();
2956 __a.deallocate(this, 1);
2957}
2958
2959template <class _Tp, class _Alloc>
2960class __shared_ptr_emplace
2961 : public __shared_weak_count
2962{
2963 __compressed_pair<_Alloc, _Tp> __data_;
2964public:
2965#ifndef _LIBCPP_HAS_NO_VARIADICS
2966
Howard Hinnant82894812010-09-22 16:48:34 +00002967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002968 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002969 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002970
2971 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00002972 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002973 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002974 : __data_(_VSTD::move(__a), _Tp(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002975
2976#else // _LIBCPP_HAS_NO_VARIADICS
2977
Howard Hinnant82894812010-09-22 16:48:34 +00002978 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002979 __shared_ptr_emplace(_Alloc __a)
2980 : __data_(__a) {}
2981
2982 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00002983 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002984 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
2985 : __data_(__a, _Tp(__a0)) {}
2986
2987 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00002988 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002989 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
2990 : __data_(__a, _Tp(__a0, __a1)) {}
2991
2992 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00002993 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002994 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
2995 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
2996
2997#endif // _LIBCPP_HAS_NO_VARIADICS
2998
2999private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003000 virtual void __on_zero_shared() _NOEXCEPT;
3001 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003002public:
Howard Hinnant82894812010-09-22 16:48:34 +00003003 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003004 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003005};
3006
3007template <class _Tp, class _Alloc>
3008void
Howard Hinnant1694d232011-05-28 14:41:13 +00003009__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003010{
3011 __data_.second().~_Tp();
3012}
3013
3014template <class _Tp, class _Alloc>
3015void
Howard Hinnant1694d232011-05-28 14:41:13 +00003016__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003017{
3018 typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
3019 __data_.first().~_Alloc();
3020 __a.deallocate(this, 1);
3021}
3022
3023template<class _Tp> class enable_shared_from_this;
3024
3025template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003026class _LIBCPP_VISIBLE shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003027{
Howard Hinnant324bb032010-08-22 00:02:43 +00003028public:
3029 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003030private:
3031 element_type* __ptr_;
3032 __shared_weak_count* __cntrl_;
3033
3034 struct __nat {int __for_bool_;};
3035public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003036 shared_ptr() _NOEXCEPT;
3037 shared_ptr(nullptr_t) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003038 template<class _Yp> explicit shared_ptr(_Yp* __p);
Howard Hinnant324bb032010-08-22 00:02:43 +00003039 template<class _Yp, class _Dp> shared_ptr(_Yp* __p, _Dp __d);
3040 template<class _Yp, class _Dp, class _Alloc> shared_ptr(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003041 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3042 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant1694d232011-05-28 14:41:13 +00003043 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3044 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003045 template<class _Yp>
3046 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003047 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3048 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003049#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003050 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003051 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003052 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3053 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003054#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003055 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003056 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003057#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00003058 template<class _Yp> shared_ptr(auto_ptr<_Yp>&& __r);
3059#else
3060 template<class _Yp> shared_ptr(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003061#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003062#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003063private:
Howard Hinnant324bb032010-08-22 00:02:43 +00003064 template <class _Yp, class _Dp> shared_ptr(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003065public:
3066 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
3067 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
3068 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>&&,
3069 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003070#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003071 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
3072 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
3073 template <class _Yp, class _Dp> shared_ptr(unique_ptr<_Yp, _Dp>,
3074 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003075#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003076
3077 ~shared_ptr();
3078
Howard Hinnant1694d232011-05-28 14:41:13 +00003079 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
3080 template<class _Yp> shared_ptr& operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003081#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003082 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00003083 template<class _Yp> shared_ptr& operator=(shared_ptr<_Yp>&& __r);
3084 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003085#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00003086 template<class _Yp> shared_ptr& operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003087#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003088#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003089private:
Howard Hinnant324bb032010-08-22 00:02:43 +00003090 template <class _Yp, class _Dp> shared_ptr& operator=(const unique_ptr<_Yp, _Dp>& __r);// = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003091public:
Howard Hinnant324bb032010-08-22 00:02:43 +00003092 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003093#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00003094 template <class _Yp, class _Dp> shared_ptr& operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003095#endif
3096
Howard Hinnant1694d232011-05-28 14:41:13 +00003097 void swap(shared_ptr& __r) _NOEXCEPT;
3098 void reset() _NOEXCEPT;
3099 template<class _Yp> void reset(_Yp* __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003100 template<class _Yp, class _Dp> void reset(_Yp* __p, _Dp __d);
3101 template<class _Yp, class _Dp, class _Alloc> void reset(_Yp* __p, _Dp __d, _Alloc __a);
3102
Howard Hinnant82894812010-09-22 16:48:34 +00003103 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003104 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003105 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003106 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3107 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003108 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003109 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003110 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003111 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003112 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003113 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00003114 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003115 /*explicit*/ operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003116 template <class _U>
3117 _LIBCPP_INLINE_VISIBILITY
3118 bool owner_before(shared_ptr<_U> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003119 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003120 template <class _U>
3121 _LIBCPP_INLINE_VISIBILITY
3122 bool owner_before(weak_ptr<_U> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003123 {return __cntrl_ < __p.__cntrl_;}
3124
Howard Hinnantd4444702010-08-11 17:04:31 +00003125#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003126 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00003127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003128 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003129 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00003130#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003131
3132#ifndef _LIBCPP_HAS_NO_VARIADICS
3133
3134 template<class ..._Args>
3135 static
3136 shared_ptr<_Tp>
3137 make_shared(_Args&& ...__args);
3138
3139 template<class _Alloc, class ..._Args>
3140 static
3141 shared_ptr<_Tp>
3142 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3143
3144#else // _LIBCPP_HAS_NO_VARIADICS
3145
3146 static shared_ptr<_Tp> make_shared();
3147
3148 template<class _A0>
3149 static shared_ptr<_Tp> make_shared(_A0&);
3150
3151 template<class _A0, class _A1>
3152 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3153
3154 template<class _A0, class _A1, class _A2>
3155 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3156
3157 template<class _Alloc>
3158 static shared_ptr<_Tp>
3159 allocate_shared(const _Alloc& __a);
3160
3161 template<class _Alloc, class _A0>
3162 static shared_ptr<_Tp>
3163 allocate_shared(const _Alloc& __a, _A0& __a0);
3164
3165 template<class _Alloc, class _A0, class _A1>
3166 static shared_ptr<_Tp>
3167 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3168
3169 template<class _Alloc, class _A0, class _A1, class _A2>
3170 static shared_ptr<_Tp>
3171 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3172
3173#endif // _LIBCPP_HAS_NO_VARIADICS
3174
3175private:
3176
3177 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00003178 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003179 void
Howard Hinnant1694d232011-05-28 14:41:13 +00003180 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003181 {
3182 if (__e)
3183 __e->__weak_this_ = *this;
3184 }
3185
Howard Hinnant82894812010-09-22 16:48:34 +00003186 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003187 void __enable_weak_this(const void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003188
Howard Hinnant82894812010-09-22 16:48:34 +00003189 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
3190 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003191};
3192
3193template<class _Tp>
3194inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003195shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003196 : __ptr_(0),
3197 __cntrl_(0)
3198{
3199}
3200
3201template<class _Tp>
3202inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003203shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003204 : __ptr_(0),
3205 __cntrl_(0)
3206{
3207}
3208
3209template<class _Tp>
3210template<class _Yp>
3211shared_ptr<_Tp>::shared_ptr(_Yp* __p)
3212 : __ptr_(__p)
3213{
3214 unique_ptr<_Yp> __hold(__p);
3215 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3216 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
3217 __hold.release();
3218 __enable_weak_this(__p);
3219}
3220
3221template<class _Tp>
3222template<class _Yp, class _Dp>
3223shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
3224 : __ptr_(__p)
3225{
3226#ifndef _LIBCPP_NO_EXCEPTIONS
3227 try
3228 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003229#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003230 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3231 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
3232 __enable_weak_this(__p);
3233#ifndef _LIBCPP_NO_EXCEPTIONS
3234 }
3235 catch (...)
3236 {
3237 __d(__p);
3238 throw;
3239 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003240#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003241}
3242
3243template<class _Tp>
3244template<class _Dp>
3245shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
3246 : __ptr_(0)
3247{
3248#ifndef _LIBCPP_NO_EXCEPTIONS
3249 try
3250 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003251#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003252 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
3253 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
3254#ifndef _LIBCPP_NO_EXCEPTIONS
3255 }
3256 catch (...)
3257 {
3258 __d(__p);
3259 throw;
3260 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003261#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003262}
3263
3264template<class _Tp>
3265template<class _Yp, class _Dp, class _Alloc>
3266shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
3267 : __ptr_(__p)
3268{
3269#ifndef _LIBCPP_NO_EXCEPTIONS
3270 try
3271 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003272#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003273 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
3274 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3275 typedef __allocator_destructor<_A2> _D2;
3276 _A2 __a2(__a);
3277 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3278 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
3279 __cntrl_ = __hold2.release();
3280 __enable_weak_this(__p);
3281#ifndef _LIBCPP_NO_EXCEPTIONS
3282 }
3283 catch (...)
3284 {
3285 __d(__p);
3286 throw;
3287 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003288#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003289}
3290
3291template<class _Tp>
3292template<class _Dp, class _Alloc>
3293shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
3294 : __ptr_(0)
3295{
3296#ifndef _LIBCPP_NO_EXCEPTIONS
3297 try
3298 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003299#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003300 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
3301 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3302 typedef __allocator_destructor<_A2> _D2;
3303 _A2 __a2(__a);
3304 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
3305 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
3306 __cntrl_ = __hold2.release();
3307#ifndef _LIBCPP_NO_EXCEPTIONS
3308 }
3309 catch (...)
3310 {
3311 __d(__p);
3312 throw;
3313 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003314#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003315}
3316
3317template<class _Tp>
3318template<class _Yp>
3319inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003320shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003321 : __ptr_(__p),
3322 __cntrl_(__r.__cntrl_)
3323{
3324 if (__cntrl_)
3325 __cntrl_->__add_shared();
3326}
3327
3328template<class _Tp>
3329inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003330shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003331 : __ptr_(__r.__ptr_),
3332 __cntrl_(__r.__cntrl_)
3333{
3334 if (__cntrl_)
3335 __cntrl_->__add_shared();
3336}
3337
3338template<class _Tp>
3339template<class _Yp>
3340inline _LIBCPP_INLINE_VISIBILITY
3341shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
3342 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003343 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003344 : __ptr_(__r.__ptr_),
3345 __cntrl_(__r.__cntrl_)
3346{
3347 if (__cntrl_)
3348 __cntrl_->__add_shared();
3349}
3350
Howard Hinnant73d21a42010-09-04 23:28:19 +00003351#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003352
3353template<class _Tp>
3354inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003355shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003356 : __ptr_(__r.__ptr_),
3357 __cntrl_(__r.__cntrl_)
3358{
3359 __r.__ptr_ = 0;
3360 __r.__cntrl_ = 0;
3361}
3362
3363template<class _Tp>
3364template<class _Yp>
3365inline _LIBCPP_INLINE_VISIBILITY
3366shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
3367 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003368 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003369 : __ptr_(__r.__ptr_),
3370 __cntrl_(__r.__cntrl_)
3371{
3372 __r.__ptr_ = 0;
3373 __r.__cntrl_ = 0;
3374}
3375
Howard Hinnant73d21a42010-09-04 23:28:19 +00003376#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003377
3378template<class _Tp>
3379template<class _Yp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003380#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003381shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
3382#else
Howard Hinnant92172b82010-08-21 21:14:53 +00003383shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003384#endif
3385 : __ptr_(__r.get())
3386{
3387 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3388 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
3389 __enable_weak_this(__r.get());
3390 __r.release();
3391}
3392
3393template<class _Tp>
3394template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003395#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003396shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3397#else
3398shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3399#endif
3400 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
3401 : __ptr_(__r.get())
3402{
3403 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3404 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
3405 __enable_weak_this(__r.get());
3406 __r.release();
3407}
3408
3409template<class _Tp>
3410template <class _Yp, class _Dp>
Howard Hinnant73d21a42010-09-04 23:28:19 +00003411#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003412shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
3413#else
3414shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
3415#endif
3416 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
3417 : __ptr_(__r.get())
3418{
3419 typedef __shared_ptr_pointer<_Yp*,
3420 reference_wrapper<typename remove_reference<_Dp>::type>,
3421 allocator<_Yp> > _CntrlBlk;
3422 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
3423 __enable_weak_this(__r.get());
3424 __r.release();
3425}
3426
3427#ifndef _LIBCPP_HAS_NO_VARIADICS
3428
3429template<class _Tp>
3430template<class ..._Args>
3431shared_ptr<_Tp>
3432shared_ptr<_Tp>::make_shared(_Args&& ...__args)
3433{
3434 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3435 typedef allocator<_CntrlBlk> _A2;
3436 typedef __allocator_destructor<_A2> _D2;
3437 _A2 __a2;
3438 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003439 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003440 shared_ptr<_Tp> __r;
3441 __r.__ptr_ = __hold2.get()->get();
3442 __r.__cntrl_ = __hold2.release();
3443 __r.__enable_weak_this(__r.__ptr_);
3444 return __r;
3445}
3446
3447template<class _Tp>
3448template<class _Alloc, class ..._Args>
3449shared_ptr<_Tp>
3450shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
3451{
3452 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3453 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
3454 typedef __allocator_destructor<_A2> _D2;
3455 _A2 __a2(__a);
3456 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003457 ::new(__hold2.get()) _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003458 shared_ptr<_Tp> __r;
3459 __r.__ptr_ = __hold2.get()->get();
3460 __r.__cntrl_ = __hold2.release();
3461 __r.__enable_weak_this(__r.__ptr_);
3462 return __r;
3463}
3464
3465#else // _LIBCPP_HAS_NO_VARIADICS
3466
3467template<class _Tp>
3468shared_ptr<_Tp>
3469shared_ptr<_Tp>::make_shared()
3470{
3471 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3472 typedef allocator<_CntrlBlk> _Alloc2;
3473 typedef __allocator_destructor<_Alloc2> _D2;
3474 _Alloc2 __alloc2;
3475 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3476 ::new(__hold2.get()) _CntrlBlk(__alloc2);
3477 shared_ptr<_Tp> __r;
3478 __r.__ptr_ = __hold2.get()->get();
3479 __r.__cntrl_ = __hold2.release();
3480 __r.__enable_weak_this(__r.__ptr_);
3481 return __r;
3482}
3483
3484template<class _Tp>
3485template<class _A0>
3486shared_ptr<_Tp>
3487shared_ptr<_Tp>::make_shared(_A0& __a0)
3488{
3489 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3490 typedef allocator<_CntrlBlk> _Alloc2;
3491 typedef __allocator_destructor<_Alloc2> _D2;
3492 _Alloc2 __alloc2;
3493 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3494 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
3495 shared_ptr<_Tp> __r;
3496 __r.__ptr_ = __hold2.get()->get();
3497 __r.__cntrl_ = __hold2.release();
3498 __r.__enable_weak_this(__r.__ptr_);
3499 return __r;
3500}
3501
3502template<class _Tp>
3503template<class _A0, class _A1>
3504shared_ptr<_Tp>
3505shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
3506{
3507 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3508 typedef allocator<_CntrlBlk> _Alloc2;
3509 typedef __allocator_destructor<_Alloc2> _D2;
3510 _Alloc2 __alloc2;
3511 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3512 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
3513 shared_ptr<_Tp> __r;
3514 __r.__ptr_ = __hold2.get()->get();
3515 __r.__cntrl_ = __hold2.release();
3516 __r.__enable_weak_this(__r.__ptr_);
3517 return __r;
3518}
3519
3520template<class _Tp>
3521template<class _A0, class _A1, class _A2>
3522shared_ptr<_Tp>
3523shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3524{
3525 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
3526 typedef allocator<_CntrlBlk> _Alloc2;
3527 typedef __allocator_destructor<_Alloc2> _D2;
3528 _Alloc2 __alloc2;
3529 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3530 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
3531 shared_ptr<_Tp> __r;
3532 __r.__ptr_ = __hold2.get()->get();
3533 __r.__cntrl_ = __hold2.release();
3534 __r.__enable_weak_this(__r.__ptr_);
3535 return __r;
3536}
3537
3538template<class _Tp>
3539template<class _Alloc>
3540shared_ptr<_Tp>
3541shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
3542{
3543 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3544 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3545 typedef __allocator_destructor<_Alloc2> _D2;
3546 _Alloc2 __alloc2(__a);
3547 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3548 ::new(__hold2.get()) _CntrlBlk(__a);
3549 shared_ptr<_Tp> __r;
3550 __r.__ptr_ = __hold2.get()->get();
3551 __r.__cntrl_ = __hold2.release();
3552 __r.__enable_weak_this(__r.__ptr_);
3553 return __r;
3554}
3555
3556template<class _Tp>
3557template<class _Alloc, class _A0>
3558shared_ptr<_Tp>
3559shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
3560{
3561 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3562 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3563 typedef __allocator_destructor<_Alloc2> _D2;
3564 _Alloc2 __alloc2(__a);
3565 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3566 ::new(__hold2.get()) _CntrlBlk(__a, __a0);
3567 shared_ptr<_Tp> __r;
3568 __r.__ptr_ = __hold2.get()->get();
3569 __r.__cntrl_ = __hold2.release();
3570 __r.__enable_weak_this(__r.__ptr_);
3571 return __r;
3572}
3573
3574template<class _Tp>
3575template<class _Alloc, class _A0, class _A1>
3576shared_ptr<_Tp>
3577shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3578{
3579 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3580 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3581 typedef __allocator_destructor<_Alloc2> _D2;
3582 _Alloc2 __alloc2(__a);
3583 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3584 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
3585 shared_ptr<_Tp> __r;
3586 __r.__ptr_ = __hold2.get()->get();
3587 __r.__cntrl_ = __hold2.release();
3588 __r.__enable_weak_this(__r.__ptr_);
3589 return __r;
3590}
3591
3592template<class _Tp>
3593template<class _Alloc, class _A0, class _A1, class _A2>
3594shared_ptr<_Tp>
3595shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3596{
3597 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
3598 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
3599 typedef __allocator_destructor<_Alloc2> _D2;
3600 _Alloc2 __alloc2(__a);
3601 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
3602 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
3603 shared_ptr<_Tp> __r;
3604 __r.__ptr_ = __hold2.get()->get();
3605 __r.__cntrl_ = __hold2.release();
3606 __r.__enable_weak_this(__r.__ptr_);
3607 return __r;
3608}
3609
3610#endif // _LIBCPP_HAS_NO_VARIADICS
3611
3612template<class _Tp>
3613shared_ptr<_Tp>::~shared_ptr()
3614{
3615 if (__cntrl_)
3616 __cntrl_->__release_shared();
3617}
3618
3619template<class _Tp>
3620inline _LIBCPP_INLINE_VISIBILITY
3621shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003622shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003623{
3624 shared_ptr(__r).swap(*this);
3625 return *this;
3626}
3627
3628template<class _Tp>
3629template<class _Yp>
3630inline _LIBCPP_INLINE_VISIBILITY
3631shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003632shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003633{
3634 shared_ptr(__r).swap(*this);
3635 return *this;
3636}
3637
Howard Hinnant73d21a42010-09-04 23:28:19 +00003638#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003639
3640template<class _Tp>
3641inline _LIBCPP_INLINE_VISIBILITY
3642shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00003643shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003644{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003645 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003646 return *this;
3647}
3648
3649template<class _Tp>
3650template<class _Yp>
3651inline _LIBCPP_INLINE_VISIBILITY
3652shared_ptr<_Tp>&
3653shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
3654{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003655 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003656 return *this;
3657}
3658
3659template<class _Tp>
3660template<class _Yp>
3661inline _LIBCPP_INLINE_VISIBILITY
3662shared_ptr<_Tp>&
3663shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
3664{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003665 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003666 return *this;
3667}
3668
3669template<class _Tp>
3670template <class _Yp, class _Dp>
3671inline _LIBCPP_INLINE_VISIBILITY
3672shared_ptr<_Tp>&
3673shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
3674{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003675 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003676 return *this;
3677}
3678
Howard Hinnant73d21a42010-09-04 23:28:19 +00003679#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003680
3681template<class _Tp>
3682template<class _Yp>
3683inline _LIBCPP_INLINE_VISIBILITY
3684shared_ptr<_Tp>&
Howard Hinnant324bb032010-08-22 00:02:43 +00003685shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003686{
3687 shared_ptr(__r).swap(*this);
3688 return *this;
3689}
3690
3691template<class _Tp>
3692template <class _Yp, class _Dp>
3693inline _LIBCPP_INLINE_VISIBILITY
3694shared_ptr<_Tp>&
3695shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
3696{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003697 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003698 return *this;
3699}
3700
Howard Hinnant73d21a42010-09-04 23:28:19 +00003701#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003702
3703template<class _Tp>
3704inline _LIBCPP_INLINE_VISIBILITY
3705void
Howard Hinnant1694d232011-05-28 14:41:13 +00003706shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003707{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003708 _VSTD::swap(__ptr_, __r.__ptr_);
3709 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003710}
3711
3712template<class _Tp>
3713inline _LIBCPP_INLINE_VISIBILITY
3714void
Howard Hinnant1694d232011-05-28 14:41:13 +00003715shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003716{
3717 shared_ptr().swap(*this);
3718}
3719
3720template<class _Tp>
3721template<class _Yp>
3722inline _LIBCPP_INLINE_VISIBILITY
3723void
3724shared_ptr<_Tp>::reset(_Yp* __p)
3725{
3726 shared_ptr(__p).swap(*this);
3727}
3728
3729template<class _Tp>
3730template<class _Yp, class _Dp>
3731inline _LIBCPP_INLINE_VISIBILITY
3732void
3733shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
3734{
3735 shared_ptr(__p, __d).swap(*this);
3736}
3737
3738template<class _Tp>
3739template<class _Yp, class _Dp, class _Alloc>
3740inline _LIBCPP_INLINE_VISIBILITY
3741void
3742shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
3743{
3744 shared_ptr(__p, __d, __a).swap(*this);
3745}
3746
3747#ifndef _LIBCPP_HAS_NO_VARIADICS
3748
Howard Hinnant324bb032010-08-22 00:02:43 +00003749template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003750inline _LIBCPP_INLINE_VISIBILITY
3751shared_ptr<_Tp>
3752make_shared(_Args&& ...__args)
3753{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003754 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003755}
3756
Howard Hinnant324bb032010-08-22 00:02:43 +00003757template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003758inline _LIBCPP_INLINE_VISIBILITY
3759shared_ptr<_Tp>
3760allocate_shared(const _Alloc& __a, _Args&& ...__args)
3761{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003762 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003763}
3764
3765#else // _LIBCPP_HAS_NO_VARIADICS
3766
3767template<class _Tp>
3768inline _LIBCPP_INLINE_VISIBILITY
3769shared_ptr<_Tp>
3770make_shared()
3771{
3772 return shared_ptr<_Tp>::make_shared();
3773}
3774
3775template<class _Tp, class _A0>
3776inline _LIBCPP_INLINE_VISIBILITY
3777shared_ptr<_Tp>
3778make_shared(_A0& __a0)
3779{
3780 return shared_ptr<_Tp>::make_shared(__a0);
3781}
3782
3783template<class _Tp, class _A0, class _A1>
3784inline _LIBCPP_INLINE_VISIBILITY
3785shared_ptr<_Tp>
3786make_shared(_A0& __a0, _A1& __a1)
3787{
3788 return shared_ptr<_Tp>::make_shared(__a0, __a1);
3789}
3790
Howard Hinnant324bb032010-08-22 00:02:43 +00003791template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003792inline _LIBCPP_INLINE_VISIBILITY
3793shared_ptr<_Tp>
3794make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
3795{
3796 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
3797}
3798
3799template<class _Tp, class _Alloc>
3800inline _LIBCPP_INLINE_VISIBILITY
3801shared_ptr<_Tp>
3802allocate_shared(const _Alloc& __a)
3803{
3804 return shared_ptr<_Tp>::allocate_shared(__a);
3805}
3806
3807template<class _Tp, class _Alloc, class _A0>
3808inline _LIBCPP_INLINE_VISIBILITY
3809shared_ptr<_Tp>
3810allocate_shared(const _Alloc& __a, _A0& __a0)
3811{
3812 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
3813}
3814
3815template<class _Tp, class _Alloc, class _A0, class _A1>
3816inline _LIBCPP_INLINE_VISIBILITY
3817shared_ptr<_Tp>
3818allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
3819{
3820 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
3821}
3822
3823template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
3824inline _LIBCPP_INLINE_VISIBILITY
3825shared_ptr<_Tp>
3826allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
3827{
3828 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
3829}
3830
3831#endif // _LIBCPP_HAS_NO_VARIADICS
3832
3833template<class _Tp, class _Up>
3834inline _LIBCPP_INLINE_VISIBILITY
3835bool
Howard Hinnant1694d232011-05-28 14:41:13 +00003836operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003837{
3838 return __x.get() == __y.get();
3839}
3840
3841template<class _Tp, class _Up>
3842inline _LIBCPP_INLINE_VISIBILITY
3843bool
Howard Hinnant1694d232011-05-28 14:41:13 +00003844operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003845{
3846 return !(__x == __y);
3847}
3848
3849template<class _Tp, class _Up>
3850inline _LIBCPP_INLINE_VISIBILITY
3851bool
Howard Hinnant1694d232011-05-28 14:41:13 +00003852operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003853{
3854 return __x.get() < __y.get();
3855}
3856
3857template<class _Tp>
3858inline _LIBCPP_INLINE_VISIBILITY
3859void
Howard Hinnant1694d232011-05-28 14:41:13 +00003860swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003861{
3862 __x.swap(__y);
3863}
3864
3865template<class _Tp, class _Up>
3866inline _LIBCPP_INLINE_VISIBILITY
3867shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003868static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003869{
3870 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
3871}
3872
3873template<class _Tp, class _Up>
3874inline _LIBCPP_INLINE_VISIBILITY
3875shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003876dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003877{
3878 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
3879 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
3880}
3881
3882template<class _Tp, class _Up>
3883shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003884const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003885{
3886 return shared_ptr<_Tp>(__r, const_cast<_Tp*>(__r.get()));
3887}
3888
Howard Hinnantd4444702010-08-11 17:04:31 +00003889#ifndef _LIBCPP_NO_RTTI
3890
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003891template<class _Dp, class _Tp>
3892inline _LIBCPP_INLINE_VISIBILITY
3893_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00003894get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003895{
3896 return __p.template __get_deleter<_Dp>();
3897}
3898
Howard Hinnant324bb032010-08-22 00:02:43 +00003899#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003900
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003901template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003902class _LIBCPP_VISIBLE weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003903{
Howard Hinnant324bb032010-08-22 00:02:43 +00003904public:
3905 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003906private:
3907 element_type* __ptr_;
3908 __shared_weak_count* __cntrl_;
3909
Howard Hinnant324bb032010-08-22 00:02:43 +00003910public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003911 weak_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003912 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003913 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3914 _NOEXCEPT;
3915 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003916 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003917 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
3918 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00003919
3920 ~weak_ptr();
3921
Howard Hinnant1694d232011-05-28 14:41:13 +00003922 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
3923 template<class _Yp> weak_ptr& operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
3924 template<class _Yp> weak_ptr& operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00003925
Howard Hinnant1694d232011-05-28 14:41:13 +00003926 void swap(weak_ptr& __r) _NOEXCEPT;
3927 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00003928
Howard Hinnant82894812010-09-22 16:48:34 +00003929 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003930 long use_count() const _NOEXCEPT
3931 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003932 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003933 bool expired() const _NOEXCEPT
3934 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
3935 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003936 template<class _Up>
3937 _LIBCPP_INLINE_VISIBILITY
3938 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003939 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003940 template<class _Up>
3941 _LIBCPP_INLINE_VISIBILITY
3942 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003943 {return __cntrl_ < __r.__cntrl_;}
3944
Howard Hinnant82894812010-09-22 16:48:34 +00003945 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
3946 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003947};
3948
3949template<class _Tp>
3950inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003951weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003952 : __ptr_(0),
3953 __cntrl_(0)
3954{
3955}
3956
3957template<class _Tp>
3958inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003959weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003960 : __ptr_(__r.__ptr_),
3961 __cntrl_(__r.__cntrl_)
3962{
3963 if (__cntrl_)
3964 __cntrl_->__add_weak();
3965}
3966
3967template<class _Tp>
3968template<class _Yp>
3969inline _LIBCPP_INLINE_VISIBILITY
3970weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00003971 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003972 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003973 : __ptr_(__r.__ptr_),
3974 __cntrl_(__r.__cntrl_)
3975{
3976 if (__cntrl_)
3977 __cntrl_->__add_weak();
3978}
3979
3980template<class _Tp>
3981template<class _Yp>
3982inline _LIBCPP_INLINE_VISIBILITY
3983weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00003984 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00003985 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003986 : __ptr_(__r.__ptr_),
3987 __cntrl_(__r.__cntrl_)
3988{
3989 if (__cntrl_)
3990 __cntrl_->__add_weak();
3991}
3992
3993template<class _Tp>
3994weak_ptr<_Tp>::~weak_ptr()
3995{
3996 if (__cntrl_)
3997 __cntrl_->__release_weak();
3998}
3999
4000template<class _Tp>
4001inline _LIBCPP_INLINE_VISIBILITY
4002weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004003weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004004{
4005 weak_ptr(__r).swap(*this);
4006 return *this;
4007}
4008
4009template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00004010template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004011inline _LIBCPP_INLINE_VISIBILITY
4012weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004013weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004014{
4015 weak_ptr(__r).swap(*this);
4016 return *this;
4017}
4018
4019template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00004020template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004021inline _LIBCPP_INLINE_VISIBILITY
4022weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004023weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004024{
4025 weak_ptr(__r).swap(*this);
4026 return *this;
4027}
4028
4029template<class _Tp>
4030inline _LIBCPP_INLINE_VISIBILITY
4031void
Howard Hinnant1694d232011-05-28 14:41:13 +00004032weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004033{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004034 _VSTD::swap(__ptr_, __r.__ptr_);
4035 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004036}
4037
4038template<class _Tp>
4039inline _LIBCPP_INLINE_VISIBILITY
4040void
Howard Hinnant1694d232011-05-28 14:41:13 +00004041swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004042{
4043 __x.swap(__y);
4044}
4045
4046template<class _Tp>
4047inline _LIBCPP_INLINE_VISIBILITY
4048void
Howard Hinnant1694d232011-05-28 14:41:13 +00004049weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004050{
4051 weak_ptr().swap(*this);
4052}
4053
4054template<class _Tp>
4055template<class _Yp>
4056shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
4057 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
4058 : __ptr_(__r.__ptr_),
4059 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
4060{
4061 if (__cntrl_ == 0)
4062#ifndef _LIBCPP_NO_EXCEPTIONS
4063 throw bad_weak_ptr();
4064#else
4065 assert(!"bad_weak_ptr");
4066#endif
4067}
4068
4069template<class _Tp>
4070shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00004071weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004072{
4073 shared_ptr<_Tp> __r;
4074 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
4075 if (__r.__cntrl_)
4076 __r.__ptr_ = __ptr_;
4077 return __r;
4078}
4079
Howard Hinnant324bb032010-08-22 00:02:43 +00004080template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004081
4082template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004083struct _LIBCPP_VISIBLE owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004084 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00004085{
4086 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00004087 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004088 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
4089 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004090 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004091 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
4092 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004093 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004094 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
4095 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00004096};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004097
4098template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004099struct _LIBCPP_VISIBLE owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004100 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
4101{
Howard Hinnant324bb032010-08-22 00:02:43 +00004102 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00004103 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004104 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
4105 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004106 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004107 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
4108 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00004109 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004110 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
4111 {return __x.owner_before(__y);}
4112};
4113
4114template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004115class _LIBCPP_VISIBLE enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004116{
4117 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00004118protected:
Howard Hinnant82894812010-09-22 16:48:34 +00004119 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004120 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00004121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004122 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00004123 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004124 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
4125 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00004126 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004127 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00004128public:
Howard Hinnant82894812010-09-22 16:48:34 +00004129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004130 shared_ptr<_Tp> shared_from_this()
4131 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00004132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004133 shared_ptr<_Tp const> shared_from_this() const
4134 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004135
4136 template <class _Up> friend class shared_ptr;
4137};
4138
Howard Hinnant21aefc32010-06-03 16:42:57 +00004139template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004140struct _LIBCPP_VISIBLE hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00004141{
4142 typedef shared_ptr<_Tp> argument_type;
4143 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00004144 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004145 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00004146 {
4147 return hash<_Tp*>()(__ptr.get());
4148 }
4149};
4150
Howard Hinnant324bb032010-08-22 00:02:43 +00004151//enum class
Howard Hinnant82894812010-09-22 16:48:34 +00004152struct _LIBCPP_VISIBLE pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004153{
4154 enum _
4155 {
4156 relaxed,
4157 preferred,
4158 strict
4159 };
4160
4161 _ __v_;
4162
Howard Hinnant82894812010-09-22 16:48:34 +00004163 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004164 pointer_safety(_ __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00004165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004166 operator int() const {return __v_;}
4167};
4168
4169void declare_reachable(void* __p);
4170void declare_no_pointers(char* __p, size_t __n);
4171void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnant1694d232011-05-28 14:41:13 +00004172pointer_safety get_pointer_safety() _NOEXCEPT;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00004173void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004174
4175template <class _Tp>
4176inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00004177_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004178undeclare_reachable(_Tp* __p)
4179{
4180 return static_cast<_Tp*>(__undeclare_reachable(__p));
4181}
4182
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00004183void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004184
4185_LIBCPP_END_NAMESPACE_STD
4186
4187#endif // _LIBCPP_MEMORY