blob: 0f3ccec50af46f44942166f324bab8762b7e9066 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_MEMORY
12#define _LIBCPP_MEMORY
13
14/*
15 memory synopsis
16
17namespace std
18{
19
20struct allocator_arg_t { };
21constexpr allocator_arg_t allocator_arg = allocator_arg_t();
22
23template <class T, class Alloc> struct uses_allocator;
24
25template <class Ptr>
26struct pointer_traits
27{
28 typedef Ptr pointer;
29 typedef <details> element_type;
30 typedef <details> difference_type;
Howard Hinnant324bb032010-08-22 00:02:43 +000031
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000032 template <class U> using rebind = <details>;
Howard Hinnant324bb032010-08-22 00:02:43 +000033
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000034 static pointer pointer_to(<details>);
35};
36
Howard Hinnant1694d232011-05-28 14:41:13 +000037template <class T>
38struct pointer_traits<T*>
39{
40 typedef T* pointer;
41 typedef T element_type;
42 typedef ptrdiff_t difference_type;
43
44 template <class U> using rebind = U*;
45
46 static pointer pointer_to(<details>) noexcept;
47};
48
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000049template <class Alloc>
50struct allocator_traits
51{
52 typedef Alloc allocator_type;
53 typedef typename allocator_type::value_type
54 value_type;
55
56 typedef Alloc::pointer | value_type* pointer;
57 typedef Alloc::const_pointer
58 | pointer_traits<pointer>::rebind<const value_type>
59 const_pointer;
60 typedef Alloc::void_pointer
61 | pointer_traits<pointer>::rebind<void>
62 void_pointer;
63 typedef Alloc::const_void_pointer
64 | pointer_traits<pointer>::rebind<const void>
65 const_void_pointer;
66 typedef Alloc::difference_type
Howard Hinnant47761072010-11-18 01:40:00 +000067 | pointer_traits<pointer>::difference_type
68 difference_type;
69 typedef Alloc::size_type
70 | make_unsigned<difference_type>::type
71 size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000072 typedef Alloc::propagate_on_container_copy_assignment
73 | false_type propagate_on_container_copy_assignment;
74 typedef Alloc::propagate_on_container_move_assignment
75 | false_type propagate_on_container_move_assignment;
76 typedef Alloc::propagate_on_container_swap
77 | false_type propagate_on_container_swap;
78
79 template <class T> using rebind_alloc = Alloc::rebind<U>::other | Alloc<T, Args...>;
80 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
81
82 static pointer allocate(allocator_type& a, size_type n);
83 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint);
84
Howard Hinnant1694d232011-05-28 14:41:13 +000085 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000086
87 template <class T, class... Args>
88 static void construct(allocator_type& a, T* p, Args&&... args);
89
90 template <class T>
91 static void destroy(allocator_type& a, T* p);
92
93 static size_type max_size(const allocator_type& a);
94
95 static allocator_type
96 select_on_container_copy_construction(const allocator_type& a);
97};
98
99template <>
100class allocator<void>
101{
102public:
103 typedef void* pointer;
104 typedef const void* const_pointer;
105 typedef void value_type;
106
107 template <class _Up> struct rebind {typedef allocator<_Up> other;};
108};
109
110template <class T>
111class allocator
112{
113public:
114 typedef size_t size_type;
115 typedef ptrdiff_t difference_type;
116 typedef T* pointer;
117 typedef const T* const_pointer;
118 typedef typename add_lvalue_reference<T>::type reference;
119 typedef typename add_lvalue_reference<const T>::type const_reference;
120 typedef T value_type;
121
122 template <class U> struct rebind {typedef allocator<U> other;};
123
Howard Hinnant1694d232011-05-28 14:41:13 +0000124 allocator() noexcept;
125 allocator(const allocator&) noexcept;
126 template <class U> allocator(const allocator<U>&) noexcept;
127 ~allocator();
128 pointer address(reference x) const noexcept;
129 const_pointer address(const_reference x) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000130 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
Howard Hinnant1694d232011-05-28 14:41:13 +0000131 void deallocate(pointer p, size_type n) noexcept;
132 size_type max_size() const noexcept;
133 template<class U, class... Args>
134 void construct(U* p, Args&&... args);
135 template <class U>
136 void destroy(U* p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000137};
138
139template <class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000140bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141
142template <class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000143bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000144
145template <class OutputIterator, class T>
146class raw_storage_iterator
147 : public iterator<output_iterator_tag,
148 T, // purposefully not C++03
149 ptrdiff_t, // purposefully not C++03
150 T*, // purposefully not C++03
151 raw_storage_iterator&> // purposefully not C++03
152{
153public:
154 explicit raw_storage_iterator(OutputIterator x);
155 raw_storage_iterator& operator*();
156 raw_storage_iterator& operator=(const T& element);
157 raw_storage_iterator& operator++();
158 raw_storage_iterator operator++(int);
159};
160
Howard Hinnant1694d232011-05-28 14:41:13 +0000161template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
162template <class T> void return_temporary_buffer(T* p) noexcept;
163
164template <class T> T* addressof(T& r) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000165
166template <class InputIterator, class ForwardIterator>
167ForwardIterator
168uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
169
Howard Hinnant1694d232011-05-28 14:41:13 +0000170template <class InputIterator, class Size, class ForwardIterator>
171ForwardIterator
172uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
173
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000174template <class ForwardIterator, class T>
175void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
176
177template <class ForwardIterator, class Size, class T>
Howard Hinnant2f6a6272010-11-18 16:13:03 +0000178ForwardIterator
179uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000180
181template <class Y> struct auto_ptr_ref {};
182
183template<class X>
184class auto_ptr
185{
186public:
187 typedef X element_type;
188
189 explicit auto_ptr(X* p =0) throw();
190 auto_ptr(auto_ptr&) throw();
191 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
192 auto_ptr& operator=(auto_ptr&) throw();
193 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
194 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
195 ~auto_ptr() throw();
196
197 typename add_lvalue_reference<X>::type operator*() const throw();
198 X* operator->() const throw();
199 X* get() const throw();
200 X* release() throw();
201 void reset(X* p =0) throw();
202
203 auto_ptr(auto_ptr_ref<X>) throw();
204 template<class Y> operator auto_ptr_ref<Y>() throw();
205 template<class Y> operator auto_ptr<Y>() throw();
206};
207
Howard Hinnante92c3d72010-08-19 18:39:17 +0000208template <class T>
209struct default_delete
210{
Howard Hinnant1694d232011-05-28 14:41:13 +0000211 constexpr default_delete() noexcept = default;
212 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000213
Howard Hinnant1694d232011-05-28 14:41:13 +0000214 void operator()(T*) const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000215};
216
217template <class T>
218struct default_delete<T[]>
219{
Howard Hinnant1694d232011-05-28 14:41:13 +0000220 constexpr default_delete() noexcept = default;
221 void operator()(T*) const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000222 template <class U> void operator()(U*) const = delete;
223};
224
Howard Hinnante92c3d72010-08-19 18:39:17 +0000225template <class T, class D = default_delete<T>>
226class unique_ptr
227{
228public:
229 typedef see below pointer;
230 typedef T element_type;
231 typedef D deleter_type;
232
233 // constructors
Howard Hinnant1694d232011-05-28 14:41:13 +0000234 constexpr unique_ptr() noexcept;
235 explicit unique_ptr(pointer p) noexcept;
236 unique_ptr(pointer p, see below d1) noexcept;
237 unique_ptr(pointer p, see below d2) noexcept;
238 unique_ptr(unique_ptr&& u) noexcept;
239 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnante92c3d72010-08-19 18:39:17 +0000240 template <class U, class E>
Howard Hinnant1694d232011-05-28 14:41:13 +0000241 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000242 template <class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000243 unique_ptr(auto_ptr<U>&& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000244
245 // destructor
246 ~unique_ptr();
247
248 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000249 unique_ptr& operator=(unique_ptr&& u) noexcept;
250 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
251 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000252
253 // observers
254 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant1694d232011-05-28 14:41:13 +0000255 pointer operator->() const noexcept;
256 pointer get() const noexcept;
257 deleter_type& get_deleter() noexcept;
258 const deleter_type& get_deleter() const noexcept;
259 explicit operator bool() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000260
261 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000262 pointer release() noexcept;
263 void reset(pointer p = pointer()) noexcept;
264 void swap(unique_ptr& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000265};
266
267template <class T, class D>
268class unique_ptr<T[], D>
269{
270public:
271 typedef implementation-defined pointer;
272 typedef T element_type;
273 typedef D deleter_type;
274
275 // constructors
Howard Hinnant1694d232011-05-28 14:41:13 +0000276 constexpr unique_ptr() noexcept;
277 explicit unique_ptr(pointer p) noexcept;
278 unique_ptr(pointer p, see below d) noexcept;
279 unique_ptr(pointer p, see below d) noexcept;
280 unique_ptr(unique_ptr&& u) noexcept;
281 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnante92c3d72010-08-19 18:39:17 +0000282
283 // destructor
Howard Hinnant324bb032010-08-22 00:02:43 +0000284 ~unique_ptr();
Howard Hinnante92c3d72010-08-19 18:39:17 +0000285
286 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000287 unique_ptr& operator=(unique_ptr&& u) noexcept;
288 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000289
290 // observers
291 T& operator[](size_t i) const;
Howard Hinnant1694d232011-05-28 14:41:13 +0000292 pointer get() const noexcept;
293 deleter_type& get_deleter() noexcept;
294 const deleter_type& get_deleter() const noexcept;
295 explicit operator bool() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000296
297 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000298 pointer release() noexcept;
299 void reset(pointer p = pointer()) noexcept;
300 void reset(nullptr_t) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000301 template <class U> void reset(U) = delete;
Howard Hinnant1694d232011-05-28 14:41:13 +0000302 void swap(unique_ptr& u) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000303};
304
305template <class T, class D>
Howard Hinnant1694d232011-05-28 14:41:13 +0000306 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000307
308template <class T1, class D1, class T2, class D2>
309 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
310template <class T1, class D1, class T2, class D2>
311 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
312template <class T1, class D1, class T2, class D2>
313 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
314template <class T1, class D1, class T2, class D2>
315 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
316template <class T1, class D1, class T2, class D2>
317 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
318template <class T1, class D1, class T2, class D2>
319 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
320
Howard Hinnant1694d232011-05-28 14:41:13 +0000321template <class T, class D>
322 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
323template <class T, class D>
324 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
325template <class T, class D>
326 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
327template <class T, class D>
328 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
329
330template <class T, class D>
331 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
332template <class T, class D>
333 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
334template <class T, class D>
335 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
336template <class T, class D>
337 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
338template <class T, class D>
339 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
340template <class T, class D>
341 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
342template <class T, class D>
343 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
344template <class T, class D>
345 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
346
Howard Hinnante92c3d72010-08-19 18:39:17 +0000347class bad_weak_ptr
348 : public std::exception
349{
Howard Hinnant1694d232011-05-28 14:41:13 +0000350 bad_weak_ptr() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000351};
352
353template<class T>
354class shared_ptr
355{
356public:
357 typedef T element_type;
358
359 // constructors:
Howard Hinnant1694d232011-05-28 14:41:13 +0000360 constexpr shared_ptr() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000361 template<class Y> explicit shared_ptr(Y* p);
362 template<class Y, class D> shared_ptr(Y* p, D d);
363 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
364 template <class D> shared_ptr(nullptr_t p, D d);
365 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant1694d232011-05-28 14:41:13 +0000366 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
367 shared_ptr(const shared_ptr& r) noexcept;
368 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
369 shared_ptr(shared_ptr&& r) noexcept;
370 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000371 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
372 template<class Y> shared_ptr(auto_ptr<Y>&& r);
373 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
374 shared_ptr(nullptr_t) : shared_ptr() { }
375
376 // destructor:
377 ~shared_ptr();
378
379 // assignment:
Howard Hinnant1694d232011-05-28 14:41:13 +0000380 shared_ptr& operator=(const shared_ptr& r) noexcept;
381 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
382 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000383 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
384 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r);
385 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
386
387 // modifiers:
Howard Hinnant1694d232011-05-28 14:41:13 +0000388 void swap(shared_ptr& r) noexcept;
389 void reset() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000390 template<class Y> void reset(Y* p);
391 template<class Y, class D> void reset(Y* p, D d);
392 template<class Y, class D, class A> void reset(Y* p, D d, A a);
393
Howard Hinnant1694d232011-05-28 14:41:13 +0000394 // observers:
395 T* get() const noexcept;
396 T& operator*() const noexcept;
397 T* operator->() const noexcept;
398 long use_count() const noexcept;
399 bool unique() const noexcept;
400 explicit operator bool() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000401 template<class U> bool owner_before(shared_ptr<U> const& b) const;
402 template<class U> bool owner_before(weak_ptr<U> const& b) const;
403};
404
405// shared_ptr comparisons:
406template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000407 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000408template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000409 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000410template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000411 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000412template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000413 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000414template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000415 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000416template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000417 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
418
419template <class T>
420 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
421template <class T>
422 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
423template <class T>
424 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
425template <class T>
426 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
427template <class T>
428 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
429template <class T>
430bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
431template <class T>
432 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
433template <class T>
434 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
435template <class T>
436 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
437template <class T>
438 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
439template <class T>
440 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
441template <class T>
442 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000443
444// shared_ptr specialized algorithms:
Howard Hinnant1694d232011-05-28 14:41:13 +0000445template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000446
447// shared_ptr casts:
448template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000449 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000450template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000451 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000452template<class T, class U>
Howard Hinnant1694d232011-05-28 14:41:13 +0000453 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000454
455// shared_ptr I/O:
456template<class E, class T, class Y>
457 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
458
459// shared_ptr get_deleter:
Howard Hinnant1694d232011-05-28 14:41:13 +0000460template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000461
462template<class T, class... Args>
463 shared_ptr<T> make_shared(Args&&... args);
464template<class T, class A, class... Args>
465 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
466
467template<class T>
468class weak_ptr
469{
470public:
471 typedef T element_type;
472
473 // constructors
Howard Hinnant1694d232011-05-28 14:41:13 +0000474 constexpr weak_ptr() noexcept;
475 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
476 weak_ptr(weak_ptr const& r) noexcept;
477 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000478
479 // destructor
480 ~weak_ptr();
481
482 // assignment
Howard Hinnant1694d232011-05-28 14:41:13 +0000483 weak_ptr& operator=(weak_ptr const& r) noexcept;
484 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
485 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000486
487 // modifiers
Howard Hinnant1694d232011-05-28 14:41:13 +0000488 void swap(weak_ptr& r) noexcept;
489 void reset() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000490
491 // observers
Howard Hinnant1694d232011-05-28 14:41:13 +0000492 long use_count() const noexcept;
493 bool expired() const noexcept;
494 shared_ptr<T> lock() const noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000495 template<class U> bool owner_before(shared_ptr<U> const& b);
496 template<class U> bool owner_before(weak_ptr<U> const& b);
497};
498
499// weak_ptr specialized algorithms:
Howard Hinnant1694d232011-05-28 14:41:13 +0000500template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000501
502// class owner_less:
503template<class T> struct owner_less;
504
505template<class T>
506struct owner_less<shared_ptr<T>>
507 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
508{
509 typedef bool result_type;
510 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const;
511 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
512 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
513};
514
515template<class T>
516struct owner_less<weak_ptr<T>>
517 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
518{
519 typedef bool result_type;
520 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const;
521 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
522 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
523};
524
525template<class T>
526class enable_shared_from_this
527{
528protected:
Howard Hinnant1694d232011-05-28 14:41:13 +0000529 constexpr enable_shared_from_this() noexcept;
530 enable_shared_from_this(enable_shared_from_this const&) noexcept;
531 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000532 ~enable_shared_from_this();
533public:
534 shared_ptr<T> shared_from_this();
535 shared_ptr<T const> shared_from_this() const;
536};
537
538template<class T>
539 bool atomic_is_lock_free(const shared_ptr<T>* p);
540template<class T>
541 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
542template<class T>
543 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
544template<class T>
545 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
546template<class T>
547 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
548template<class T>
549 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
550template<class T>
551 shared_ptr<T>
552 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
553template<class T>
554 bool
555 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
556template<class T>
557 bool
558 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
559template<class T>
560 bool
561 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
562 shared_ptr<T> w, memory_order success,
563 memory_order failure);
564template<class T>
565 bool
566 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
567 shared_ptr<T> w, memory_order success,
568 memory_order failure);
569// Hash support
570template <class T> struct hash;
571template <class T, class D> struct hash<unique_ptr<T, D> >;
572template <class T> struct hash<shared_ptr<T> >;
573
574// Pointer safety
575enum class pointer_safety { relaxed, preferred, strict };
576void declare_reachable(void *p);
577template <class T> T *undeclare_reachable(T *p);
578void declare_no_pointers(char *p, size_t n);
579void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant1694d232011-05-28 14:41:13 +0000580pointer_safety get_pointer_safety() noexcept;
Howard Hinnante92c3d72010-08-19 18:39:17 +0000581
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000582void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
583
584} // std
585
586*/
587
588#include <__config>
589#include <type_traits>
590#include <typeinfo>
591#include <cstddef>
592#include <cstdint>
593#include <new>
594#include <utility>
595#include <limits>
596#include <iterator>
597#include <__functional_base>
Howard Hinnant464aa5c2011-07-18 15:51:59 +0000598#include <iosfwd>
Howard Hinnant6cc99fa2011-12-19 17:58:44 +0000599#include <tuple>
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +0000600#include <cstring>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000601#if defined(_LIBCPP_NO_EXCEPTIONS)
602 #include <cassert>
603#endif
604
Howard Hinnant66c6f972011-11-29 16:45:27 +0000605#include <__undef_min_max>
606
Howard Hinnant08e17472011-10-17 20:05:10 +0000607#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000608#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000609#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000610
611_LIBCPP_BEGIN_NAMESPACE_STD
612
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000613// addressof
614
615template <class _Tp>
616inline _LIBCPP_INLINE_VISIBILITY
617_Tp*
Howard Hinnant1694d232011-05-28 14:41:13 +0000618addressof(_Tp& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000619{
620 return (_Tp*)&(char&)__x;
621}
622
Douglas Gregor35d2fcf2011-06-22 22:17:44 +0000623#if defined(_LIBCPP_HAS_OBJC_ARC) && !defined(_LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF)
624// Objective-C++ Automatic Reference Counting uses qualified pointers
625// that require special addressof() signatures. When
626// _LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF is defined, the compiler
627// itself is providing these definitions. Otherwise, we provide them.
628template <class _Tp>
629inline _LIBCPP_INLINE_VISIBILITY
630__strong _Tp*
631addressof(__strong _Tp& __x) _NOEXCEPT
632{
633 return &__x;
634}
635
636#ifdef _LIBCPP_HAS_OBJC_ARC_WEAK
637template <class _Tp>
638inline _LIBCPP_INLINE_VISIBILITY
639__weak _Tp*
640addressof(__weak _Tp& __x) _NOEXCEPT
641{
642 return &__x;
643}
644#endif
645
646template <class _Tp>
647inline _LIBCPP_INLINE_VISIBILITY
648__autoreleasing _Tp*
649addressof(__autoreleasing _Tp& __x) _NOEXCEPT
650{
651 return &__x;
652}
653
654template <class _Tp>
655inline _LIBCPP_INLINE_VISIBILITY
656__unsafe_unretained _Tp*
657addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT
658{
659 return &__x;
660}
661#endif
662
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000663template <class _Tp> class allocator;
664
665template <>
Howard Hinnant82894812010-09-22 16:48:34 +0000666class _LIBCPP_VISIBLE allocator<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000667{
668public:
669 typedef void* pointer;
670 typedef const void* const_pointer;
671 typedef void value_type;
672
673 template <class _Up> struct rebind {typedef allocator<_Up> other;};
674};
675
Howard Hinnanta1877872012-01-19 23:15:22 +0000676template <>
677class _LIBCPP_VISIBLE allocator<const void>
678{
679public:
680 typedef const void* pointer;
681 typedef const void* const_pointer;
682 typedef const void value_type;
683
684 template <class _Up> struct rebind {typedef allocator<_Up> other;};
685};
686
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000687// pointer_traits
688
689template <class _Tp>
690struct __has_element_type
691{
692private:
693 struct __two {char _; char __;};
694 template <class _Up> static __two __test(...);
695 template <class _Up> static char __test(typename _Up::element_type* = 0);
696public:
697 static const bool value = sizeof(__test<_Tp>(0)) == 1;
698};
699
700template <class _Ptr, bool = __has_element_type<_Ptr>::value>
701struct __pointer_traits_element_type;
702
703template <class _Ptr>
704struct __pointer_traits_element_type<_Ptr, true>
705{
706 typedef typename _Ptr::element_type type;
707};
708
709#ifndef _LIBCPP_HAS_NO_VARIADICS
710
711template <template <class, class...> class _Sp, class _Tp, class ..._Args>
712struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
713{
714 typedef typename _Sp<_Tp, _Args...>::element_type type;
715};
716
717template <template <class, class...> class _Sp, class _Tp, class ..._Args>
718struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
719{
720 typedef _Tp type;
721};
722
Howard Hinnant324bb032010-08-22 00:02:43 +0000723#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000724
725template <template <class> class _Sp, class _Tp>
726struct __pointer_traits_element_type<_Sp<_Tp>, true>
727{
728 typedef typename _Sp<_Tp>::element_type type;
729};
730
731template <template <class> class _Sp, class _Tp>
732struct __pointer_traits_element_type<_Sp<_Tp>, false>
733{
734 typedef _Tp type;
735};
736
737template <template <class, class> class _Sp, class _Tp, class _A0>
738struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
739{
740 typedef typename _Sp<_Tp, _A0>::element_type type;
741};
742
743template <template <class, class> class _Sp, class _Tp, class _A0>
744struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
745{
746 typedef _Tp type;
747};
748
749template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
750struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
751{
752 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
753};
754
755template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
756struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
757{
758 typedef _Tp type;
759};
760
761template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
762 class _A1, class _A2>
763struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
764{
765 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
766};
767
768template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
769 class _A1, class _A2>
770struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
771{
772 typedef _Tp type;
773};
774
Howard Hinnant324bb032010-08-22 00:02:43 +0000775#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000776
777template <class _Tp>
778struct __has_difference_type
779{
780private:
781 struct __two {char _; char __;};
782 template <class _Up> static __two __test(...);
783 template <class _Up> static char __test(typename _Up::difference_type* = 0);
784public:
785 static const bool value = sizeof(__test<_Tp>(0)) == 1;
786};
787
788template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
789struct __pointer_traits_difference_type
790{
791 typedef ptrdiff_t type;
792};
793
794template <class _Ptr>
795struct __pointer_traits_difference_type<_Ptr, true>
796{
797 typedef typename _Ptr::difference_type type;
798};
799
800template <class _Tp, class _Up>
801struct __has_rebind
802{
803private:
804 struct __two {char _; char __;};
805 template <class _Xp> static __two __test(...);
806 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
807public:
808 static const bool value = sizeof(__test<_Tp>(0)) == 1;
809};
810
811template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
812struct __pointer_traits_rebind
813{
814#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
815 typedef typename _Tp::template rebind<_Up> type;
816#else
817 typedef typename _Tp::template rebind<_Up>::other type;
818#endif
819};
820
821#ifndef _LIBCPP_HAS_NO_VARIADICS
822
823template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
824struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
825{
826#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
827 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
828#else
829 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
830#endif
831};
832
833template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
834struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
835{
836 typedef _Sp<_Up, _Args...> type;
837};
838
Howard Hinnant324bb032010-08-22 00:02:43 +0000839#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000840
841template <template <class> class _Sp, class _Tp, class _Up>
842struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
843{
844#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
845 typedef typename _Sp<_Tp>::template rebind<_Up> type;
846#else
847 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
848#endif
849};
850
851template <template <class> class _Sp, class _Tp, class _Up>
852struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
853{
854 typedef _Sp<_Up> type;
855};
856
857template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
858struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
859{
860#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
861 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
862#else
863 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
864#endif
865};
866
867template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
868struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
869{
870 typedef _Sp<_Up, _A0> type;
871};
872
873template <template <class, class, class> class _Sp, class _Tp, class _A0,
874 class _A1, class _Up>
875struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
876{
877#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
878 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
879#else
880 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
881#endif
882};
883
884template <template <class, class, class> class _Sp, class _Tp, class _A0,
885 class _A1, class _Up>
886struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
887{
888 typedef _Sp<_Up, _A0, _A1> type;
889};
890
891template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
892 class _A1, class _A2, class _Up>
893struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
894{
895#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
896 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
897#else
898 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
899#endif
900};
901
902template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
903 class _A1, class _A2, class _Up>
904struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
905{
906 typedef _Sp<_Up, _A0, _A1, _A2> type;
907};
908
Howard Hinnant324bb032010-08-22 00:02:43 +0000909#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000910
911template <class _Ptr>
Howard Hinnant82894812010-09-22 16:48:34 +0000912struct _LIBCPP_VISIBLE pointer_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000913{
914 typedef _Ptr pointer;
915 typedef typename __pointer_traits_element_type<pointer>::type element_type;
916 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
917
918#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant6b41c602011-05-11 20:21:19 +0000919 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000920#else
921 template <class _Up> struct rebind
922 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant324bb032010-08-22 00:02:43 +0000923#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000924
925private:
926 struct __nat {};
927public:
Howard Hinnant82894812010-09-22 16:48:34 +0000928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000929 static pointer pointer_to(typename conditional<is_void<element_type>::value,
930 __nat, element_type>::type& __r)
931 {return pointer::pointer_to(__r);}
932};
933
934template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +0000935struct _LIBCPP_VISIBLE pointer_traits<_Tp*>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000936{
937 typedef _Tp* pointer;
938 typedef _Tp element_type;
939 typedef ptrdiff_t difference_type;
940
941#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
942 template <class _Up> using rebind = _Up*;
943#else
944 template <class _Up> struct rebind {typedef _Up* other;};
945#endif
946
947private:
948 struct __nat {};
949public:
Howard Hinnant82894812010-09-22 16:48:34 +0000950 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000951 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant1694d232011-05-28 14:41:13 +0000952 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +0000953 {return _VSTD::addressof(__r);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000954};
955
956// allocator_traits
957
958namespace __has_pointer_type_imp
959{
960 template <class _Up> static __two test(...);
961 template <class _Up> static char test(typename _Up::pointer* = 0);
962}
963
964template <class _Tp>
965struct __has_pointer_type
966 : public integral_constant<bool, sizeof(__has_pointer_type_imp::test<_Tp>(0)) == 1>
967{
968};
969
970namespace __pointer_type_imp
971{
972
973template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
974struct __pointer_type
975{
976 typedef typename _Dp::pointer type;
977};
978
979template <class _Tp, class _Dp>
980struct __pointer_type<_Tp, _Dp, false>
981{
982 typedef _Tp* type;
983};
984
Howard Hinnant47761072010-11-18 01:40:00 +0000985} // __pointer_type_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000986
987template <class _Tp, class _Dp>
988struct __pointer_type
989{
990 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
991};
992
993template <class _Tp>
994struct __has_const_pointer
995{
996private:
997 struct __two {char _; char __;};
998 template <class _Up> static __two __test(...);
999 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
1000public:
1001 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1002};
1003
1004template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1005struct __const_pointer
1006{
1007 typedef typename _Alloc::const_pointer type;
1008};
1009
1010template <class _Tp, class _Ptr, class _Alloc>
1011struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1012{
1013#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1014 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1015#else
1016 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1017#endif
1018};
1019
1020template <class _Tp>
1021struct __has_void_pointer
1022{
1023private:
1024 struct __two {char _; char __;};
1025 template <class _Up> static __two __test(...);
1026 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1027public:
1028 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1029};
1030
1031template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1032struct __void_pointer
1033{
1034 typedef typename _Alloc::void_pointer type;
1035};
1036
1037template <class _Ptr, class _Alloc>
1038struct __void_pointer<_Ptr, _Alloc, false>
1039{
1040#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1041 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1042#else
1043 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1044#endif
1045};
1046
1047template <class _Tp>
1048struct __has_const_void_pointer
1049{
1050private:
1051 struct __two {char _; char __;};
1052 template <class _Up> static __two __test(...);
1053 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1054public:
1055 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1056};
1057
1058template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1059struct __const_void_pointer
1060{
1061 typedef typename _Alloc::const_void_pointer type;
1062};
1063
1064template <class _Ptr, class _Alloc>
1065struct __const_void_pointer<_Ptr, _Alloc, false>
1066{
1067#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1068 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1069#else
1070 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1071#endif
1072};
1073
Howard Hinnant99968442011-11-29 18:15:50 +00001074template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001075inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001076_Tp*
1077__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001078{
1079 return __p;
1080}
1081
1082template <class _Pointer>
1083inline _LIBCPP_INLINE_VISIBILITY
1084typename pointer_traits<_Pointer>::element_type*
Howard Hinnant1694d232011-05-28 14:41:13 +00001085__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001086{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001087 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001088}
1089
1090template <class _Tp>
1091struct __has_size_type
1092{
1093private:
1094 struct __two {char _; char __;};
1095 template <class _Up> static __two __test(...);
1096 template <class _Up> static char __test(typename _Up::size_type* = 0);
1097public:
1098 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1099};
1100
Howard Hinnant47761072010-11-18 01:40:00 +00001101template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001102struct __size_type
1103{
Howard Hinnant47761072010-11-18 01:40:00 +00001104 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001105};
1106
Howard Hinnant47761072010-11-18 01:40:00 +00001107template <class _Alloc, class _DiffType>
1108struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001109{
1110 typedef typename _Alloc::size_type type;
1111};
1112
1113template <class _Tp>
1114struct __has_propagate_on_container_copy_assignment
1115{
1116private:
1117 struct __two {char _; char __;};
1118 template <class _Up> static __two __test(...);
1119 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1120public:
1121 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1122};
1123
1124template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1125struct __propagate_on_container_copy_assignment
1126{
1127 typedef false_type type;
1128};
1129
1130template <class _Alloc>
1131struct __propagate_on_container_copy_assignment<_Alloc, true>
1132{
1133 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1134};
1135
1136template <class _Tp>
1137struct __has_propagate_on_container_move_assignment
1138{
1139private:
1140 struct __two {char _; char __;};
1141 template <class _Up> static __two __test(...);
1142 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1143public:
1144 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1145};
1146
1147template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1148struct __propagate_on_container_move_assignment
1149{
1150 typedef false_type type;
1151};
1152
1153template <class _Alloc>
1154struct __propagate_on_container_move_assignment<_Alloc, true>
1155{
1156 typedef typename _Alloc::propagate_on_container_move_assignment type;
1157};
1158
1159template <class _Tp>
1160struct __has_propagate_on_container_swap
1161{
1162private:
1163 struct __two {char _; char __;};
1164 template <class _Up> static __two __test(...);
1165 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1166public:
1167 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1168};
1169
1170template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1171struct __propagate_on_container_swap
1172{
1173 typedef false_type type;
1174};
1175
1176template <class _Alloc>
1177struct __propagate_on_container_swap<_Alloc, true>
1178{
1179 typedef typename _Alloc::propagate_on_container_swap type;
1180};
1181
1182template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1183struct __has_rebind_other
1184{
1185private:
1186 struct __two {char _; char __;};
1187 template <class _Xp> static __two __test(...);
1188 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1189public:
1190 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1191};
1192
1193template <class _Tp, class _Up>
1194struct __has_rebind_other<_Tp, _Up, false>
1195{
1196 static const bool value = false;
1197};
1198
1199template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1200struct __allocator_traits_rebind
1201{
1202 typedef typename _Tp::template rebind<_Up>::other type;
1203};
1204
1205#ifndef _LIBCPP_HAS_NO_VARIADICS
1206
1207template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1208struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1209{
1210 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1211};
1212
1213template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1214struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1215{
1216 typedef _Alloc<_Up, _Args...> type;
1217};
1218
Howard Hinnant324bb032010-08-22 00:02:43 +00001219#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001220
1221template <template <class> class _Alloc, class _Tp, class _Up>
1222struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1223{
1224 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1225};
1226
1227template <template <class> class _Alloc, class _Tp, class _Up>
1228struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1229{
1230 typedef _Alloc<_Up> type;
1231};
1232
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001233template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1234struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1235{
1236 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1237};
1238
1239template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1240struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1241{
1242 typedef _Alloc<_Up, _A0> type;
1243};
1244
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001245template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1246 class _A1, class _Up>
1247struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1248{
1249 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1250};
1251
1252template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1253 class _A1, class _Up>
1254struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1255{
1256 typedef _Alloc<_Up, _A0, _A1> type;
1257};
1258
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001259template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1260 class _A1, class _A2, class _Up>
1261struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1262{
1263 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1264};
1265
1266template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1267 class _A1, class _A2, class _Up>
1268struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1269{
1270 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1271};
1272
Howard Hinnant324bb032010-08-22 00:02:43 +00001273#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001274
1275#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1276
1277template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1278auto
1279__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1280 -> decltype(__a.allocate(__sz, __p), true_type());
1281
1282template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1283auto
1284__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1285 -> false_type;
1286
1287template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1288struct __has_allocate_hint
1289 : integral_constant<bool,
1290 is_same<
1291 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1292 declval<_SizeType>(),
1293 declval<_ConstVoidPtr>())),
1294 true_type>::value>
1295{
1296};
1297
Howard Hinnant324bb032010-08-22 00:02:43 +00001298#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001299
1300template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1301struct __has_allocate_hint
1302 : true_type
1303{
1304};
1305
Howard Hinnant324bb032010-08-22 00:02:43 +00001306#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001307
Howard Hinnant23369ee2011-07-29 21:35:53 +00001308#if !defined(_LIBCPP_HAS_NO_ADVANCED_SFINAE) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001309
1310template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnant0949eed2011-06-30 21:18:19 +00001311decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1312 _VSTD::declval<_Args>()...),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001313 true_type())
1314__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1315
1316template <class _Alloc, class _Pointer, class ..._Args>
1317false_type
1318__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1319
1320template <class _Alloc, class _Pointer, class ..._Args>
1321struct __has_construct
1322 : integral_constant<bool,
1323 is_same<
1324 decltype(__has_construct_test(declval<_Alloc>(),
1325 declval<_Pointer>(),
1326 declval<_Args>()...)),
1327 true_type>::value>
1328{
1329};
1330
1331template <class _Alloc, class _Pointer>
1332auto
1333__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1334 -> decltype(__a.destroy(__p), true_type());
1335
1336template <class _Alloc, class _Pointer>
1337auto
1338__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1339 -> false_type;
1340
1341template <class _Alloc, class _Pointer>
1342struct __has_destroy
1343 : integral_constant<bool,
1344 is_same<
1345 decltype(__has_destroy_test(declval<_Alloc>(),
1346 declval<_Pointer>())),
1347 true_type>::value>
1348{
1349};
1350
1351template <class _Alloc>
1352auto
1353__has_max_size_test(_Alloc&& __a)
1354 -> decltype(__a.max_size(), true_type());
1355
1356template <class _Alloc>
1357auto
1358__has_max_size_test(const volatile _Alloc& __a)
1359 -> false_type;
1360
1361template <class _Alloc>
1362struct __has_max_size
1363 : integral_constant<bool,
1364 is_same<
1365 decltype(__has_max_size_test(declval<_Alloc&>())),
1366 true_type>::value>
1367{
1368};
1369
1370template <class _Alloc>
1371auto
1372__has_select_on_container_copy_construction_test(_Alloc&& __a)
1373 -> decltype(__a.select_on_container_copy_construction(), true_type());
1374
1375template <class _Alloc>
1376auto
1377__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1378 -> false_type;
1379
1380template <class _Alloc>
1381struct __has_select_on_container_copy_construction
1382 : integral_constant<bool,
1383 is_same<
1384 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1385 true_type>::value>
1386{
1387};
1388
Howard Hinnant324bb032010-08-22 00:02:43 +00001389#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001390
1391#ifndef _LIBCPP_HAS_NO_VARIADICS
1392
1393template <class _Alloc, class _Pointer, class ..._Args>
1394struct __has_construct
1395 : false_type
1396{
1397};
1398
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001399#else // _LIBCPP_HAS_NO_VARIADICS
1400
1401template <class _Alloc, class _Pointer, class _Args>
1402struct __has_construct
1403 : false_type
1404{
1405};
1406
Howard Hinnant324bb032010-08-22 00:02:43 +00001407#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001408
1409template <class _Alloc, class _Pointer>
1410struct __has_destroy
1411 : false_type
1412{
1413};
1414
1415template <class _Alloc>
1416struct __has_max_size
1417 : true_type
1418{
1419};
1420
1421template <class _Alloc>
1422struct __has_select_on_container_copy_construction
1423 : false_type
1424{
1425};
1426
Howard Hinnant324bb032010-08-22 00:02:43 +00001427#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001428
Howard Hinnant47761072010-11-18 01:40:00 +00001429template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1430struct __alloc_traits_difference_type
1431{
1432 typedef typename pointer_traits<_Ptr>::difference_type type;
1433};
1434
1435template <class _Alloc, class _Ptr>
1436struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1437{
1438 typedef typename _Alloc::difference_type type;
1439};
1440
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001441template <class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001442struct _LIBCPP_VISIBLE allocator_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001443{
1444 typedef _Alloc allocator_type;
1445 typedef typename allocator_type::value_type value_type;
1446
1447 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1448 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1449 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1450 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1451
Howard Hinnant47761072010-11-18 01:40:00 +00001452 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1453 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001454
1455 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1456 propagate_on_container_copy_assignment;
1457 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1458 propagate_on_container_move_assignment;
1459 typedef typename __propagate_on_container_swap<allocator_type>::type
1460 propagate_on_container_swap;
1461
1462#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1463 template <class _Tp> using rebind_alloc =
Howard Hinnant6b41c602011-05-11 20:21:19 +00001464 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001465 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant324bb032010-08-22 00:02:43 +00001466#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001467 template <class _Tp> struct rebind_alloc
1468 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1469 template <class _Tp> struct rebind_traits
1470 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant324bb032010-08-22 00:02:43 +00001471#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001472
Howard Hinnant82894812010-09-22 16:48:34 +00001473 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001474 static pointer allocate(allocator_type& __a, size_type __n)
1475 {return __a.allocate(__n);}
Howard Hinnant82894812010-09-22 16:48:34 +00001476 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001477 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1478 {return allocate(__a, __n, __hint,
1479 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1480
Howard Hinnant82894812010-09-22 16:48:34 +00001481 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001482 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001483 {__a.deallocate(__p, __n);}
1484
1485#ifndef _LIBCPP_HAS_NO_VARIADICS
1486 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001488 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
1489 {__construct(__has_construct<allocator_type, pointer, _Args...>(),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001490 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant324bb032010-08-22 00:02:43 +00001491#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001492 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001493 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001494 static void construct(allocator_type& __a, _Tp* __p)
1495 {
1496 ::new ((void*)__p) _Tp();
1497 }
1498 template <class _Tp, class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00001499 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001500 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1501 {
1502 ::new ((void*)__p) _Tp(__a0);
1503 }
1504 template <class _Tp, class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00001505 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001506 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1507 const _A1& __a1)
1508 {
1509 ::new ((void*)__p) _Tp(__a0, __a1);
1510 }
1511 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00001512 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001513 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1514 const _A1& __a1, const _A2& __a2)
1515 {
1516 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1517 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001518#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001519
1520 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001521 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001522 static void destroy(allocator_type& __a, _Tp* __p)
1523 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1524
Howard Hinnant82894812010-09-22 16:48:34 +00001525 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001526 static size_type max_size(const allocator_type& __a)
1527 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1528
Howard Hinnant82894812010-09-22 16:48:34 +00001529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530 static allocator_type
1531 select_on_container_copy_construction(const allocator_type& __a)
1532 {return select_on_container_copy_construction(
1533 __has_select_on_container_copy_construction<const allocator_type>(),
1534 __a);}
1535
Howard Hinnantb0bfd9b2012-02-15 00:41:34 +00001536 template <class _Ptr>
1537 _LIBCPP_INLINE_VISIBILITY
1538 static
1539 void
1540 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1541 {
1542 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1543 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1544 }
1545
1546 template <class _Tp>
1547 _LIBCPP_INLINE_VISIBILITY
1548 static
1549 typename enable_if
1550 <
1551 (is_same<allocator_type, allocator<_Tp> >::value
1552 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1553 is_trivially_move_constructible<_Tp>::value,
1554 void
1555 >::type
1556 __construct_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1557 {
1558 ptrdiff_t _Np = __end1 - __begin1;
1559 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
1560 __begin2 += _Np;
1561 }
1562
1563 template <class _Ptr>
1564 _LIBCPP_INLINE_VISIBILITY
1565 static
1566 void
1567 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1568 {
1569 while (__end1 != __begin1)
1570 construct(__a, _VSTD::__to_raw_pointer(--__end2), _VSTD::move_if_noexcept(*--__end1));
1571 }
1572
1573 template <class _Tp>
1574 _LIBCPP_INLINE_VISIBILITY
1575 static
1576 typename enable_if
1577 <
1578 (is_same<allocator_type, allocator<_Tp> >::value
1579 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1580 is_trivially_move_constructible<_Tp>::value,
1581 void
1582 >::type
1583 __construct_backward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
1584 {
1585 ptrdiff_t _Np = __end1 - __begin1;
1586 __end2 -= _Np;
1587 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
1588 }
1589
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001590private:
1591
Howard Hinnant82894812010-09-22 16:48:34 +00001592 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001593 static pointer allocate(allocator_type& __a, size_type __n,
1594 const_void_pointer __hint, true_type)
1595 {return __a.allocate(__n, __hint);}
Howard Hinnant82894812010-09-22 16:48:34 +00001596 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001597 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnantec3773c2011-12-01 20:21:04 +00001598 const_void_pointer, false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001599 {return __a.allocate(__n);}
1600
1601#ifndef _LIBCPP_HAS_NO_VARIADICS
1602 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001603 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001604 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001605 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001606 template <class _Tp, class... _Args>
Howard Hinnant82894812010-09-22 16:48:34 +00001607 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001608 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1609 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001610 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001612#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001613
1614 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001616 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1617 {__a.destroy(__p);}
1618 template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001619 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001620 static void __destroy(false_type, allocator_type&, _Tp* __p)
1621 {
1622 __p->~_Tp();
1623 }
1624
Howard Hinnant82894812010-09-22 16:48:34 +00001625 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001626 static size_type __max_size(true_type, const allocator_type& __a)
1627 {return __a.max_size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001628 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001629 static size_type __max_size(false_type, const allocator_type&)
1630 {return numeric_limits<size_type>::max();}
1631
Howard Hinnant82894812010-09-22 16:48:34 +00001632 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001633 static allocator_type
1634 select_on_container_copy_construction(true_type, const allocator_type& __a)
1635 {return __a.select_on_container_copy_construction();}
Howard Hinnant82894812010-09-22 16:48:34 +00001636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001637 static allocator_type
1638 select_on_container_copy_construction(false_type, const allocator_type& __a)
1639 {return __a;}
1640};
1641
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001642// allocator
1643
1644template <class _Tp>
Howard Hinnant36cdf022010-09-10 16:42:26 +00001645class _LIBCPP_VISIBLE allocator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001646{
1647public:
1648 typedef size_t size_type;
1649 typedef ptrdiff_t difference_type;
1650 typedef _Tp* pointer;
1651 typedef const _Tp* const_pointer;
1652 typedef _Tp& reference;
1653 typedef const _Tp& const_reference;
1654 typedef _Tp value_type;
1655
Howard Hinnant18884f42011-06-02 21:38:57 +00001656 typedef true_type propagate_on_container_move_assignment;
1657
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001658 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1659
Howard Hinnant1694d232011-05-28 14:41:13 +00001660 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1661 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1662 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001663 {return _VSTD::addressof(__x);}
Howard Hinnant1694d232011-05-28 14:41:13 +00001664 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00001665 {return _VSTD::addressof(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001666 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1667 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
Howard Hinnant1694d232011-05-28 14:41:13 +00001668 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
1669 {::operator delete((void*)__p);}
1670 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1671 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +00001672#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001673 template <class _Up, class... _Args>
1674 _LIBCPP_INLINE_VISIBILITY
1675 void
1676 construct(_Up* __p, _Args&&... __args)
1677 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001678 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001679 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001680#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001681 _LIBCPP_INLINE_VISIBILITY
1682 void
1683 construct(pointer __p)
1684 {
1685 ::new((void*)__p) _Tp();
1686 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001687# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001688
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001689 template <class _A0>
1690 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001691 void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001692 construct(pointer __p, _A0& __a0)
1693 {
1694 ::new((void*)__p) _Tp(__a0);
1695 }
1696 template <class _A0>
1697 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001698 void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001699 construct(pointer __p, const _A0& __a0)
1700 {
1701 ::new((void*)__p) _Tp(__a0);
1702 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001703# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001704 template <class _A0, class _A1>
1705 _LIBCPP_INLINE_VISIBILITY
1706 void
1707 construct(pointer __p, _A0& __a0, _A1& __a1)
1708 {
1709 ::new((void*)__p) _Tp(__a0, __a1);
1710 }
1711 template <class _A0, class _A1>
1712 _LIBCPP_INLINE_VISIBILITY
1713 void
1714 construct(pointer __p, const _A0& __a0, _A1& __a1)
1715 {
1716 ::new((void*)__p) _Tp(__a0, __a1);
1717 }
1718 template <class _A0, class _A1>
1719 _LIBCPP_INLINE_VISIBILITY
1720 void
1721 construct(pointer __p, _A0& __a0, const _A1& __a1)
1722 {
1723 ::new((void*)__p) _Tp(__a0, __a1);
1724 }
1725 template <class _A0, class _A1>
1726 _LIBCPP_INLINE_VISIBILITY
1727 void
1728 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1729 {
1730 ::new((void*)__p) _Tp(__a0, __a1);
1731 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001732#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001733 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1734};
1735
Howard Hinnant57199402012-01-02 17:56:02 +00001736template <class _Tp>
1737class _LIBCPP_VISIBLE allocator<const _Tp>
1738{
1739public:
1740 typedef size_t size_type;
1741 typedef ptrdiff_t difference_type;
1742 typedef const _Tp* pointer;
1743 typedef const _Tp* const_pointer;
1744 typedef const _Tp& reference;
1745 typedef const _Tp& const_reference;
1746 typedef _Tp value_type;
1747
1748 typedef true_type propagate_on_container_move_assignment;
1749
1750 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1751
1752 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1753 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1754 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1755 {return _VSTD::addressof(__x);}
1756 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1757 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
1758 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
1759 {::operator delete((void*)__p);}
1760 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1761 {return size_type(~0) / sizeof(_Tp);}
1762#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1763 template <class _Up, class... _Args>
1764 _LIBCPP_INLINE_VISIBILITY
1765 void
1766 construct(_Up* __p, _Args&&... __args)
1767 {
1768 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1769 }
1770#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1771 _LIBCPP_INLINE_VISIBILITY
1772 void
1773 construct(pointer __p)
1774 {
1775 ::new((void*)__p) _Tp();
1776 }
1777# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant87073e42012-05-01 15:37:54 +00001778
Howard Hinnant57199402012-01-02 17:56:02 +00001779 template <class _A0>
1780 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001781 void
Howard Hinnant57199402012-01-02 17:56:02 +00001782 construct(pointer __p, _A0& __a0)
1783 {
1784 ::new((void*)__p) _Tp(__a0);
1785 }
1786 template <class _A0>
1787 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant87073e42012-05-01 15:37:54 +00001788 void
Howard Hinnant57199402012-01-02 17:56:02 +00001789 construct(pointer __p, const _A0& __a0)
1790 {
1791 ::new((void*)__p) _Tp(__a0);
1792 }
Howard Hinnant57199402012-01-02 17:56:02 +00001793# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1794 template <class _A0, class _A1>
1795 _LIBCPP_INLINE_VISIBILITY
1796 void
1797 construct(pointer __p, _A0& __a0, _A1& __a1)
1798 {
1799 ::new((void*)__p) _Tp(__a0, __a1);
1800 }
1801 template <class _A0, class _A1>
1802 _LIBCPP_INLINE_VISIBILITY
1803 void
1804 construct(pointer __p, const _A0& __a0, _A1& __a1)
1805 {
1806 ::new((void*)__p) _Tp(__a0, __a1);
1807 }
1808 template <class _A0, class _A1>
1809 _LIBCPP_INLINE_VISIBILITY
1810 void
1811 construct(pointer __p, _A0& __a0, const _A1& __a1)
1812 {
1813 ::new((void*)__p) _Tp(__a0, __a1);
1814 }
1815 template <class _A0, class _A1>
1816 _LIBCPP_INLINE_VISIBILITY
1817 void
1818 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1819 {
1820 ::new((void*)__p) _Tp(__a0, __a1);
1821 }
1822#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1823 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1824};
1825
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001826template <class _Tp, class _Up>
1827inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001828bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001829
1830template <class _Tp, class _Up>
1831inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001832bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001833
1834template <class _OutputIterator, class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001835class _LIBCPP_VISIBLE raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001836 : public iterator<output_iterator_tag,
1837 _Tp, // purposefully not C++03
1838 ptrdiff_t, // purposefully not C++03
1839 _Tp*, // purposefully not C++03
1840 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1841{
1842private:
1843 _OutputIterator __x_;
1844public:
1845 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1846 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1847 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1848 {::new(&*__x_) _Tp(__element); return *this;}
1849 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1850 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1851 {raw_storage_iterator __t(*this); ++__x_; return __t;}
1852};
1853
1854template <class _Tp>
1855pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00001856get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001857{
1858 pair<_Tp*, ptrdiff_t> __r(0, 0);
1859 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1860 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1861 / sizeof(_Tp);
1862 if (__n > __m)
1863 __n = __m;
1864 while (__n > 0)
1865 {
1866 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1867 if (__r.first)
1868 {
1869 __r.second = __n;
1870 break;
1871 }
1872 __n /= 2;
1873 }
1874 return __r;
1875}
1876
1877template <class _Tp>
1878inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001879void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001880
1881template <class _Tp>
1882struct auto_ptr_ref
1883{
1884 _Tp* __ptr_;
1885};
1886
1887template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001888class _LIBCPP_VISIBLE auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001889{
1890private:
1891 _Tp* __ptr_;
1892public:
1893 typedef _Tp element_type;
1894
1895 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1896 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1897 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1898 : __ptr_(__p.release()) {}
1899 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1900 {reset(__p.release()); return *this;}
1901 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1902 {reset(__p.release()); return *this;}
1903 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1904 {reset(__p.__ptr_); return *this;}
1905 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1906
1907 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1908 {return *__ptr_;}
1909 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1910 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1911 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1912 {
1913 _Tp* __t = __ptr_;
1914 __ptr_ = 0;
1915 return __t;
1916 }
1917 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1918 {
1919 if (__ptr_ != __p)
1920 delete __ptr_;
1921 __ptr_ = __p;
1922 }
1923
1924 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1925 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1926 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1927 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1928 {return auto_ptr<_Up>(release());}
1929};
1930
1931template <>
Howard Hinnant82894812010-09-22 16:48:34 +00001932class _LIBCPP_VISIBLE auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001933{
1934public:
1935 typedef void element_type;
1936};
1937
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001938template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1939 typename remove_cv<_T2>::type>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00001940 bool = is_empty<_T1>::value
1941#if __has_feature(is_final)
1942 && !__is_final(_T1)
1943#endif
1944 ,
1945 bool = is_empty<_T2>::value
1946#if __has_feature(is_final)
1947 && !__is_final(_T2)
1948#endif
1949 >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001950struct __libcpp_compressed_pair_switch;
1951
1952template <class _T1, class _T2, bool IsSame>
1953struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1954
1955template <class _T1, class _T2, bool IsSame>
1956struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1957
1958template <class _T1, class _T2, bool IsSame>
1959struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1960
1961template <class _T1, class _T2>
1962struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
1963
1964template <class _T1, class _T2>
1965struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
1966
1967template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
1968class __libcpp_compressed_pair_imp;
1969
1970template <class _T1, class _T2>
1971class __libcpp_compressed_pair_imp<_T1, _T2, 0>
1972{
1973private:
1974 _T1 __first_;
1975 _T2 __second_;
1976public:
1977 typedef _T1 _T1_param;
1978 typedef _T2 _T2_param;
1979
1980 typedef typename remove_reference<_T1>::type& _T1_reference;
1981 typedef typename remove_reference<_T2>::type& _T2_reference;
1982
1983 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
1984 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
1985
1986 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant57199402012-01-02 17:56:02 +00001987 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001988 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00001989 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001990 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001991 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001992 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001993
Howard Hinnant61aa6012011-07-01 19:24:36 +00001994#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
1995
1996 _LIBCPP_INLINE_VISIBILITY
1997 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
1998 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
1999 is_nothrow_copy_constructible<_T2>::value)
2000 : __first_(__p.first()),
2001 __second_(__p.second()) {}
2002
2003 _LIBCPP_INLINE_VISIBILITY
2004 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2005 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2006 is_nothrow_copy_assignable<_T2>::value)
2007 {
2008 __first_ = __p.first();
2009 __second_ = __p.second();
2010 return *this;
2011 }
2012
Howard Hinnant73d21a42010-09-04 23:28:19 +00002013#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002014
2015 _LIBCPP_INLINE_VISIBILITY
2016 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002017 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2018 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002019 : __first_(_VSTD::forward<_T1>(__p.first())),
2020 __second_(_VSTD::forward<_T2>(__p.second())) {}
2021
2022 _LIBCPP_INLINE_VISIBILITY
2023 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2024 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2025 is_nothrow_move_assignable<_T2>::value)
2026 {
2027 __first_ = _VSTD::forward<_T1>(__p.first());
2028 __second_ = _VSTD::forward<_T2>(__p.second());
2029 return *this;
2030 }
2031
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002032#ifndef _LIBCPP_HAS_NO_VARIADICS
2033
2034 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2035 _LIBCPP_INLINE_VISIBILITY
2036 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2037 tuple<_Args1...> __first_args,
2038 tuple<_Args2...> __second_args,
2039 __tuple_indices<_I1...>,
2040 __tuple_indices<_I2...>)
2041 : __first_(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
2042 __second_(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
2043 {}
2044
2045#endif // _LIBCPP_HAS_NO_VARIADICS
2046
Howard Hinnant73d21a42010-09-04 23:28:19 +00002047#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002048
Howard Hinnant61aa6012011-07-01 19:24:36 +00002049#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2050
Howard Hinnant1694d232011-05-28 14:41:13 +00002051 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2052 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002053
Howard Hinnant1694d232011-05-28 14:41:13 +00002054 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2055 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002056
2057 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002058 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2059 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002060 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002061 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002062 swap(__first_, __x.__first_);
2063 swap(__second_, __x.__second_);
2064 }
2065};
2066
2067template <class _T1, class _T2>
2068class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2069 : private _T1
2070{
2071private:
2072 _T2 __second_;
2073public:
2074 typedef _T1 _T1_param;
2075 typedef _T2 _T2_param;
2076
2077 typedef _T1& _T1_reference;
2078 typedef typename remove_reference<_T2>::type& _T2_reference;
2079
2080 typedef const _T1& _T1_const_reference;
2081 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2082
2083 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002084 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002085 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002086 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002087 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002088 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002089 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002090
Howard Hinnant61aa6012011-07-01 19:24:36 +00002091#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2092
2093 _LIBCPP_INLINE_VISIBILITY
2094 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2095 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2096 is_nothrow_copy_constructible<_T2>::value)
2097 : _T1(__p.first()), __second_(__p.second()) {}
2098
2099 _LIBCPP_INLINE_VISIBILITY
2100 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2101 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2102 is_nothrow_copy_assignable<_T2>::value)
2103 {
2104 _T1::operator=(__p.first());
2105 __second_ = __p.second();
2106 return *this;
2107 }
2108
Howard Hinnant73d21a42010-09-04 23:28:19 +00002109#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002110
2111 _LIBCPP_INLINE_VISIBILITY
2112 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002113 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2114 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002115 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002116
2117 _LIBCPP_INLINE_VISIBILITY
2118 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2119 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2120 is_nothrow_move_assignable<_T2>::value)
2121 {
2122 _T1::operator=(_VSTD::move(__p.first()));
2123 __second_ = _VSTD::forward<_T2>(__p.second());
2124 return *this;
2125 }
2126
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002127#ifndef _LIBCPP_HAS_NO_VARIADICS
2128
2129 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2130 _LIBCPP_INLINE_VISIBILITY
2131 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2132 tuple<_Args1...> __first_args,
2133 tuple<_Args2...> __second_args,
2134 __tuple_indices<_I1...>,
2135 __tuple_indices<_I2...>)
2136 : _T1(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
2137 __second_(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
2138 {}
2139
2140#endif // _LIBCPP_HAS_NO_VARIADICS
2141
Howard Hinnant73d21a42010-09-04 23:28:19 +00002142#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002143
Howard Hinnant61aa6012011-07-01 19:24:36 +00002144#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2145
Howard Hinnant1694d232011-05-28 14:41:13 +00002146 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2147 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002148
Howard Hinnant1694d232011-05-28 14:41:13 +00002149 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2150 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002151
2152 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002153 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2154 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002155 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002156 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002157 swap(__second_, __x.__second_);
2158 }
2159};
2160
2161template <class _T1, class _T2>
2162class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2163 : private _T2
2164{
2165private:
2166 _T1 __first_;
2167public:
2168 typedef _T1 _T1_param;
2169 typedef _T2 _T2_param;
2170
2171 typedef typename remove_reference<_T1>::type& _T1_reference;
2172 typedef _T2& _T2_reference;
2173
2174 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2175 typedef const _T2& _T2_const_reference;
2176
2177 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2178 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002179 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002180 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002181 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002182 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00002183 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2184 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002185 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002186
Howard Hinnant61aa6012011-07-01 19:24:36 +00002187#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2188
2189 _LIBCPP_INLINE_VISIBILITY
2190 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2191 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2192 is_nothrow_copy_constructible<_T2>::value)
2193 : _T2(__p.second()), __first_(__p.first()) {}
2194
2195 _LIBCPP_INLINE_VISIBILITY
2196 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2197 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2198 is_nothrow_copy_assignable<_T2>::value)
2199 {
2200 _T2::operator=(__p.second());
2201 __first_ = __p.first();
2202 return *this;
2203 }
2204
Howard Hinnant73d21a42010-09-04 23:28:19 +00002205#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002206
2207 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002208 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002209 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2210 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002211 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002212
2213 _LIBCPP_INLINE_VISIBILITY
2214 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2215 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2216 is_nothrow_move_assignable<_T2>::value)
2217 {
2218 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2219 __first_ = _VSTD::move(__p.first());
2220 return *this;
2221 }
2222
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002223#ifndef _LIBCPP_HAS_NO_VARIADICS
2224
2225 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2226 _LIBCPP_INLINE_VISIBILITY
2227 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2228 tuple<_Args1...> __first_args,
2229 tuple<_Args2...> __second_args,
2230 __tuple_indices<_I1...>,
2231 __tuple_indices<_I2...>)
2232 : _T2(_VSTD::forward<_Args2>(get<_I2>(__second_args))...),
2233 __first_(_VSTD::forward<_Args1>(get<_I1>(__first_args))...)
2234
2235 {}
2236
2237#endif // _LIBCPP_HAS_NO_VARIADICS
2238
Howard Hinnant73d21a42010-09-04 23:28:19 +00002239#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002240
Howard Hinnant61aa6012011-07-01 19:24:36 +00002241#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2242
Howard Hinnant1694d232011-05-28 14:41:13 +00002243 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2244 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002245
Howard Hinnant1694d232011-05-28 14:41:13 +00002246 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2247 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002248
2249 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002250 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2251 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002252 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002253 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002254 swap(__first_, __x.__first_);
2255 }
2256};
2257
2258template <class _T1, class _T2>
2259class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2260 : private _T1,
2261 private _T2
2262{
2263public:
2264 typedef _T1 _T1_param;
2265 typedef _T2 _T2_param;
2266
2267 typedef _T1& _T1_reference;
2268 typedef _T2& _T2_reference;
2269
2270 typedef const _T1& _T1_const_reference;
2271 typedef const _T2& _T2_const_reference;
2272
2273 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2274 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002275 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002276 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002277 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002278 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002279 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002280
Howard Hinnant61aa6012011-07-01 19:24:36 +00002281#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2282
2283 _LIBCPP_INLINE_VISIBILITY
2284 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2285 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2286 is_nothrow_copy_constructible<_T2>::value)
2287 : _T1(__p.first()), _T2(__p.second()) {}
2288
2289 _LIBCPP_INLINE_VISIBILITY
2290 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2291 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2292 is_nothrow_copy_assignable<_T2>::value)
2293 {
2294 _T1::operator=(__p.first());
2295 _T2::operator=(__p.second());
2296 return *this;
2297 }
2298
Howard Hinnant73d21a42010-09-04 23:28:19 +00002299#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002300
2301 _LIBCPP_INLINE_VISIBILITY
2302 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002303 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2304 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002305 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002306
2307 _LIBCPP_INLINE_VISIBILITY
2308 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2309 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2310 is_nothrow_move_assignable<_T2>::value)
2311 {
2312 _T1::operator=(_VSTD::move(__p.first()));
2313 _T2::operator=(_VSTD::move(__p.second()));
2314 return *this;
2315 }
2316
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002317#ifndef _LIBCPP_HAS_NO_VARIADICS
2318
2319 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2320 _LIBCPP_INLINE_VISIBILITY
2321 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2322 tuple<_Args1...> __first_args,
2323 tuple<_Args2...> __second_args,
2324 __tuple_indices<_I1...>,
2325 __tuple_indices<_I2...>)
2326 : _T1(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
2327 _T2(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
2328 {}
2329
2330#endif // _LIBCPP_HAS_NO_VARIADICS
2331
Howard Hinnant73d21a42010-09-04 23:28:19 +00002332#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002333
Howard Hinnant61aa6012011-07-01 19:24:36 +00002334#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2335
Howard Hinnant1694d232011-05-28 14:41:13 +00002336 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2337 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002338
Howard Hinnant1694d232011-05-28 14:41:13 +00002339 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2340 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002341
Howard Hinnantec3773c2011-12-01 20:21:04 +00002342 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant1694d232011-05-28 14:41:13 +00002343 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2344 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002345 {
2346 }
2347};
2348
2349template <class _T1, class _T2>
2350class __compressed_pair
2351 : private __libcpp_compressed_pair_imp<_T1, _T2>
2352{
2353 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2354public:
2355 typedef typename base::_T1_param _T1_param;
2356 typedef typename base::_T2_param _T2_param;
2357
2358 typedef typename base::_T1_reference _T1_reference;
2359 typedef typename base::_T2_reference _T2_reference;
2360
2361 typedef typename base::_T1_const_reference _T1_const_reference;
2362 typedef typename base::_T2_const_reference _T2_const_reference;
2363
2364 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002365 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002366 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002367 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002368 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002369 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002370 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002371
Howard Hinnant61aa6012011-07-01 19:24:36 +00002372#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2373
2374 _LIBCPP_INLINE_VISIBILITY
2375 __compressed_pair(const __compressed_pair& __p)
2376 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2377 is_nothrow_copy_constructible<_T2>::value)
2378 : base(__p) {}
2379
2380 _LIBCPP_INLINE_VISIBILITY
2381 __compressed_pair& operator=(const __compressed_pair& __p)
2382 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2383 is_nothrow_copy_assignable<_T2>::value)
2384 {
2385 base::operator=(__p);
2386 return *this;
2387 }
2388
Howard Hinnant73d21a42010-09-04 23:28:19 +00002389#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002391 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002392 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2393 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002394 : base(_VSTD::move(__p)) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002395
2396 _LIBCPP_INLINE_VISIBILITY
2397 __compressed_pair& operator=(__compressed_pair&& __p)
2398 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2399 is_nothrow_move_assignable<_T2>::value)
2400 {
2401 base::operator=(_VSTD::move(__p));
2402 return *this;
2403 }
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002404
2405#ifndef _LIBCPP_HAS_NO_VARIADICS
2406
2407 template <class... _Args1, class... _Args2>
2408 _LIBCPP_INLINE_VISIBILITY
2409 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2410 tuple<_Args2...> __second_args)
Howard Hinnant57199402012-01-02 17:56:02 +00002411 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002412 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2413 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2414 {}
2415
2416#endif // _LIBCPP_HAS_NO_VARIADICS
2417
Howard Hinnant73d21a42010-09-04 23:28:19 +00002418#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002419
Howard Hinnant61aa6012011-07-01 19:24:36 +00002420#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2421
Howard Hinnant1694d232011-05-28 14:41:13 +00002422 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2423 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002424
Howard Hinnant1694d232011-05-28 14:41:13 +00002425 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2426 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002427
Howard Hinnant1694d232011-05-28 14:41:13 +00002428 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2429 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2430 __is_nothrow_swappable<_T1>::value)
2431 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002432};
2433
2434template <class _T1, class _T2>
2435inline _LIBCPP_INLINE_VISIBILITY
2436void
2437swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002438 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2439 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002440 {__x.swap(__y);}
2441
Howard Hinnant57199402012-01-02 17:56:02 +00002442// __same_or_less_cv_qualified
2443
2444template <class _Ptr1, class _Ptr2,
2445 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2446 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2447 >::value
2448 >
2449struct __same_or_less_cv_qualified_imp
2450 : is_convertible<_Ptr1, _Ptr2> {};
2451
2452template <class _Ptr1, class _Ptr2>
2453struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2454 : false_type {};
2455
2456template <class _Ptr1, class _Ptr2, bool = is_scalar<_Ptr1>::value &&
2457 !is_pointer<_Ptr1>::value>
2458struct __same_or_less_cv_qualified
2459 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2460
2461template <class _Ptr1, class _Ptr2>
2462struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, true>
2463 : false_type {};
2464
2465// default_delete
2466
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002467template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002468struct _LIBCPP_VISIBLE default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002469{
Howard Hinnant1694d232011-05-28 14:41:13 +00002470 _LIBCPP_INLINE_VISIBILITY default_delete() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002471 template <class _Up>
2472 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002473 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2474 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002475 {
2476 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2477 delete __ptr;
2478 }
2479};
2480
2481template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002482struct _LIBCPP_VISIBLE default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002483{
Howard Hinnant8e843502011-12-18 21:19:44 +00002484public:
2485 _LIBCPP_INLINE_VISIBILITY default_delete() _NOEXCEPT {}
2486 template <class _Up>
2487 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant57199402012-01-02 17:56:02 +00002488 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant8e843502011-12-18 21:19:44 +00002489 template <class _Up>
2490 _LIBCPP_INLINE_VISIBILITY
2491 void operator() (_Up* __ptr,
Howard Hinnant57199402012-01-02 17:56:02 +00002492 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002493 {
2494 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2495 delete [] __ptr;
2496 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002497};
2498
2499template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant82894812010-09-22 16:48:34 +00002500class _LIBCPP_VISIBLE unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002501{
2502public:
2503 typedef _Tp element_type;
2504 typedef _Dp deleter_type;
2505 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2506private:
2507 __compressed_pair<pointer, deleter_type> __ptr_;
2508
Howard Hinnant57199402012-01-02 17:56:02 +00002509#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002510 unique_ptr(unique_ptr&);
2511 template <class _Up, class _Ep>
2512 unique_ptr(unique_ptr<_Up, _Ep>&);
2513 unique_ptr& operator=(unique_ptr&);
2514 template <class _Up, class _Ep>
2515 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002516#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002517
2518 struct __nat {int __for_bool_;};
2519
2520 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2521 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2522public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002523 _LIBCPP_INLINE_VISIBILITY unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002524 : __ptr_(pointer())
2525 {
2526 static_assert(!is_pointer<deleter_type>::value,
2527 "unique_ptr constructed with null function pointer deleter");
2528 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002529 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002530 : __ptr_(pointer())
2531 {
2532 static_assert(!is_pointer<deleter_type>::value,
2533 "unique_ptr constructed with null function pointer deleter");
2534 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002535 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002536 : __ptr_(_VSTD::move(__p))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002537 {
2538 static_assert(!is_pointer<deleter_type>::value,
2539 "unique_ptr constructed with null function pointer deleter");
2540 }
2541
Howard Hinnant73d21a42010-09-04 23:28:19 +00002542#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002543 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2544 is_reference<deleter_type>::value,
2545 deleter_type,
2546 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002547 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002548 : __ptr_(__p, __d) {}
2549
2550 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002551 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002552 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002553 {
2554 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2555 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002556 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002557 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002558 template <class _Up, class _Ep>
2559 _LIBCPP_INLINE_VISIBILITY
2560 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2561 typename enable_if
2562 <
2563 !is_array<_Up>::value &&
2564 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2565 is_convertible<_Ep, deleter_type>::value &&
2566 (
2567 !is_reference<deleter_type>::value ||
2568 is_same<deleter_type, _Ep>::value
2569 ),
2570 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002571 >::type = __nat()) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002572 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002573
2574 template <class _Up>
2575 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2576 typename enable_if<
2577 is_convertible<_Up*, _Tp*>::value &&
2578 is_same<_Dp, default_delete<_Tp> >::value,
2579 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002580 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002581 : __ptr_(__p.release())
2582 {
2583 }
2584
Howard Hinnant1694d232011-05-28 14:41:13 +00002585 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002586 {
2587 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002588 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002589 return *this;
2590 }
2591
2592 template <class _Up, class _Ep>
2593 _LIBCPP_INLINE_VISIBILITY
2594 typename enable_if
2595 <
Howard Hinnant57199402012-01-02 17:56:02 +00002596 !is_array<_Up>::value &&
2597 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2598 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002599 unique_ptr&
2600 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002601 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002602 {
2603 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002604 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002605 return *this;
2606 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002607#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002608
2609 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2610 {
2611 return __rv<unique_ptr>(*this);
2612 }
2613
2614 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002615 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002616
2617 template <class _Up, class _Ep>
2618 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2619 {
2620 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002621 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002622 return *this;
2623 }
2624
2625 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002626 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002627
2628 template <class _Up>
2629 _LIBCPP_INLINE_VISIBILITY
2630 typename enable_if<
2631 is_convertible<_Up*, _Tp*>::value &&
2632 is_same<_Dp, default_delete<_Tp> >::value,
2633 unique_ptr&
2634 >::type
2635 operator=(auto_ptr<_Up> __p)
2636 {reset(__p.release()); return *this;}
2637
Howard Hinnant73d21a42010-09-04 23:28:19 +00002638#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002639 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2640
Howard Hinnant1694d232011-05-28 14:41:13 +00002641 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002642 {
2643 reset();
2644 return *this;
2645 }
2646
2647 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2648 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002649 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2650 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2651 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2652 {return __ptr_.second();}
2653 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2654 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002655 _LIBCPP_INLINE_VISIBILITY
2656 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2657 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002658
Howard Hinnant1694d232011-05-28 14:41:13 +00002659 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002660 {
2661 pointer __t = __ptr_.first();
2662 __ptr_.first() = pointer();
2663 return __t;
2664 }
2665
Howard Hinnant1694d232011-05-28 14:41:13 +00002666 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002667 {
2668 pointer __tmp = __ptr_.first();
2669 __ptr_.first() = __p;
2670 if (__tmp)
2671 __ptr_.second()(__tmp);
2672 }
2673
Howard Hinnant1694d232011-05-28 14:41:13 +00002674 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2675 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002676};
2677
2678template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002679class _LIBCPP_VISIBLE unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002680{
2681public:
2682 typedef _Tp element_type;
2683 typedef _Dp deleter_type;
2684 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2685private:
2686 __compressed_pair<pointer, deleter_type> __ptr_;
2687
Howard Hinnant57199402012-01-02 17:56:02 +00002688#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002689 unique_ptr(unique_ptr&);
2690 template <class _Up>
2691 unique_ptr(unique_ptr<_Up>&);
2692 unique_ptr& operator=(unique_ptr&);
2693 template <class _Up>
2694 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002695#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002696
2697 struct __nat {int __for_bool_;};
2698
2699 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2700 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2701public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002702 _LIBCPP_INLINE_VISIBILITY unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002703 : __ptr_(pointer())
2704 {
2705 static_assert(!is_pointer<deleter_type>::value,
2706 "unique_ptr constructed with null function pointer deleter");
2707 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002708 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002709 : __ptr_(pointer())
2710 {
2711 static_assert(!is_pointer<deleter_type>::value,
2712 "unique_ptr constructed with null function pointer deleter");
2713 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002714#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002715 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002716 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002717 >
Howard Hinnant99968442011-11-29 18:15:50 +00002718 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002719 : __ptr_(__p)
2720 {
2721 static_assert(!is_pointer<deleter_type>::value,
2722 "unique_ptr constructed with null function pointer deleter");
2723 }
2724
Howard Hinnant99968442011-11-29 18:15:50 +00002725 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002726 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002727 >
Howard Hinnant99968442011-11-29 18:15:50 +00002728 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002729 is_reference<deleter_type>::value,
2730 deleter_type,
2731 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002732 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002733 : __ptr_(__p, __d) {}
2734
2735 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2736 is_reference<deleter_type>::value,
2737 deleter_type,
2738 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002739 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002740 : __ptr_(pointer(), __d) {}
2741
Howard Hinnant99968442011-11-29 18:15:50 +00002742 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002743 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002744 >
Howard Hinnant99968442011-11-29 18:15:50 +00002745 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002746 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002747 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002748 {
2749 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2750 }
2751
2752 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002753 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002754 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002755 {
2756 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2757 }
2758
Howard Hinnant1694d232011-05-28 14:41:13 +00002759 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002760 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002761
Howard Hinnant1694d232011-05-28 14:41:13 +00002762 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002763 {
2764 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002765 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002766 return *this;
2767 }
Howard Hinnant8e843502011-12-18 21:19:44 +00002768
2769 template <class _Up, class _Ep>
2770 _LIBCPP_INLINE_VISIBILITY
2771 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2772 typename enable_if
2773 <
2774 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002775 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant8e843502011-12-18 21:19:44 +00002776 && is_convertible<_Ep, deleter_type>::value &&
2777 (
2778 !is_reference<deleter_type>::value ||
2779 is_same<deleter_type, _Ep>::value
2780 ),
2781 __nat
2782 >::type = __nat()
2783 ) _NOEXCEPT
2784 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2785
2786
2787 template <class _Up, class _Ep>
2788 _LIBCPP_INLINE_VISIBILITY
2789 typename enable_if
2790 <
2791 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002792 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2793 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant8e843502011-12-18 21:19:44 +00002794 unique_ptr&
2795 >::type
2796 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2797 {
2798 reset(__u.release());
2799 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2800 return *this;
2801 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002802#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002803
2804 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2805 : __ptr_(__p)
2806 {
2807 static_assert(!is_pointer<deleter_type>::value,
2808 "unique_ptr constructed with null function pointer deleter");
2809 }
2810
2811 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002812 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002813
2814 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002815 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002816
2817 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2818 {
2819 return __rv<unique_ptr>(*this);
2820 }
2821
2822 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002823 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002824
2825 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2826 {
2827 reset(__u->release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002828 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002829 return *this;
2830 }
2831
Howard Hinnant73d21a42010-09-04 23:28:19 +00002832#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002833 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2834
Howard Hinnant1694d232011-05-28 14:41:13 +00002835 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002836 {
2837 reset();
2838 return *this;
2839 }
2840
2841 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2842 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002843 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2844 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2845 {return __ptr_.second();}
2846 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2847 {return __ptr_.second();}
Howard Hinnant77861882012-02-21 21:46:43 +00002848 _LIBCPP_INLINE_VISIBILITY
2849 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2850 {return __ptr_.first() != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002851
Howard Hinnant1694d232011-05-28 14:41:13 +00002852 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002853 {
2854 pointer __t = __ptr_.first();
2855 __ptr_.first() = pointer();
2856 return __t;
2857 }
2858
Howard Hinnant73d21a42010-09-04 23:28:19 +00002859#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002860 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002861 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002862 >
Howard Hinnant99968442011-11-29 18:15:50 +00002863 _LIBCPP_INLINE_VISIBILITY void reset(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002864 {
2865 pointer __tmp = __ptr_.first();
2866 __ptr_.first() = __p;
2867 if (__tmp)
2868 __ptr_.second()(__tmp);
2869 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002870 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002871 {
2872 pointer __tmp = __ptr_.first();
2873 __ptr_.first() = nullptr;
2874 if (__tmp)
2875 __ptr_.second()(__tmp);
2876 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002877 _LIBCPP_INLINE_VISIBILITY void reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002878 {
2879 pointer __tmp = __ptr_.first();
2880 __ptr_.first() = nullptr;
2881 if (__tmp)
2882 __ptr_.second()(__tmp);
2883 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002884#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002885 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2886 {
2887 pointer __tmp = __ptr_.first();
2888 __ptr_.first() = __p;
2889 if (__tmp)
2890 __ptr_.second()(__tmp);
2891 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002892#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002893
2894 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2895private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002896
Howard Hinnant73d21a42010-09-04 23:28:19 +00002897#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002898 template <class _Up>
2899 explicit unique_ptr(_Up);
2900 template <class _Up>
2901 unique_ptr(_Up __u,
2902 typename conditional<
2903 is_reference<deleter_type>::value,
2904 deleter_type,
2905 typename add_lvalue_reference<const deleter_type>::type>::type,
2906 typename enable_if
2907 <
2908 is_convertible<_Up, pointer>::value,
2909 __nat
2910 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002911#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002912};
2913
2914template <class _Tp, class _Dp>
2915inline _LIBCPP_INLINE_VISIBILITY
2916void
Howard Hinnant1694d232011-05-28 14:41:13 +00002917swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002918
2919template <class _T1, class _D1, class _T2, class _D2>
2920inline _LIBCPP_INLINE_VISIBILITY
2921bool
2922operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2923
2924template <class _T1, class _D1, class _T2, class _D2>
2925inline _LIBCPP_INLINE_VISIBILITY
2926bool
2927operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2928
2929template <class _T1, class _D1, class _T2, class _D2>
2930inline _LIBCPP_INLINE_VISIBILITY
2931bool
Howard Hinnant3fadda32012-02-21 21:02:58 +00002932operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2933{
2934 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2935 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
2936 typedef typename common_type<_P1, _P2>::type _V;
2937 return less<_V>()(__x.get(), __y.get());
2938}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002939
2940template <class _T1, class _D1, class _T2, class _D2>
2941inline _LIBCPP_INLINE_VISIBILITY
2942bool
2943operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2944
2945template <class _T1, class _D1, class _T2, class _D2>
2946inline _LIBCPP_INLINE_VISIBILITY
2947bool
2948operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2949
2950template <class _T1, class _D1, class _T2, class _D2>
2951inline _LIBCPP_INLINE_VISIBILITY
2952bool
2953operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2954
Howard Hinnant3fadda32012-02-21 21:02:58 +00002955template <class _T1, class _D1>
2956inline _LIBCPP_INLINE_VISIBILITY
2957bool
2958operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2959{
2960 return !__x;
2961}
2962
2963template <class _T1, class _D1>
2964inline _LIBCPP_INLINE_VISIBILITY
2965bool
2966operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2967{
2968 return !__x;
2969}
2970
2971template <class _T1, class _D1>
2972inline _LIBCPP_INLINE_VISIBILITY
2973bool
2974operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2975{
2976 return static_cast<bool>(__x);
2977}
2978
2979template <class _T1, class _D1>
2980inline _LIBCPP_INLINE_VISIBILITY
2981bool
2982operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2983{
2984 return static_cast<bool>(__x);
2985}
2986
2987template <class _T1, class _D1>
2988inline _LIBCPP_INLINE_VISIBILITY
2989bool
2990operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2991{
2992 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2993 return less<_P1>()(__x.get(), nullptr);
2994}
2995
2996template <class _T1, class _D1>
2997inline _LIBCPP_INLINE_VISIBILITY
2998bool
2999operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3000{
3001 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3002 return less<_P1>()(nullptr, __x.get());
3003}
3004
3005template <class _T1, class _D1>
3006inline _LIBCPP_INLINE_VISIBILITY
3007bool
3008operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3009{
3010 return nullptr < __x;
3011}
3012
3013template <class _T1, class _D1>
3014inline _LIBCPP_INLINE_VISIBILITY
3015bool
3016operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3017{
3018 return __x < nullptr;
3019}
3020
3021template <class _T1, class _D1>
3022inline _LIBCPP_INLINE_VISIBILITY
3023bool
3024operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3025{
3026 return !(nullptr < __x);
3027}
3028
3029template <class _T1, class _D1>
3030inline _LIBCPP_INLINE_VISIBILITY
3031bool
3032operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3033{
3034 return !(__x < nullptr);
3035}
3036
3037template <class _T1, class _D1>
3038inline _LIBCPP_INLINE_VISIBILITY
3039bool
3040operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3041{
3042 return !(__x < nullptr);
3043}
3044
3045template <class _T1, class _D1>
3046inline _LIBCPP_INLINE_VISIBILITY
3047bool
3048operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3049{
3050 return !(nullptr < __x);
3051}
3052
Howard Hinnant87073e42012-05-01 15:37:54 +00003053#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3054
3055template <class _Tp, class _Dp>
3056inline _LIBCPP_INLINE_VISIBILITY
3057unique_ptr<_Tp, _Dp>
3058move(unique_ptr<_Tp, _Dp>& __t)
3059{
3060 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3061}
3062
3063#endif
3064
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00003065template <class _Tp> struct hash;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003066
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003067// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
3068// is 64 bits. This is because cityhash64 uses 64bit x 64bit
3069// multiplication, which can be very slow on 32-bit systems.
Howard Hinnant40c13d32011-12-05 00:08:45 +00003070template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003071struct __murmur2_or_cityhash;
Howard Hinnant40c13d32011-12-05 00:08:45 +00003072
3073template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003074struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003075{
3076 _Size operator()(const void* __key, _Size __len);
3077};
3078
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003079// murmur2
Howard Hinnant40c13d32011-12-05 00:08:45 +00003080template <class _Size>
3081_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003082__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003083{
3084 const _Size __m = 0x5bd1e995;
3085 const _Size __r = 24;
3086 _Size __h = __len;
3087 const unsigned char* __data = static_cast<const unsigned char*>(__key);
3088 for (; __len >= 4; __data += 4, __len -= 4)
3089 {
3090 _Size __k = *(const _Size*)__data;
3091 __k *= __m;
3092 __k ^= __k >> __r;
3093 __k *= __m;
3094 __h *= __m;
3095 __h ^= __k;
3096 }
3097 switch (__len)
3098 {
3099 case 3:
3100 __h ^= __data[2] << 16;
3101 case 2:
3102 __h ^= __data[1] << 8;
3103 case 1:
3104 __h ^= __data[0];
3105 __h *= __m;
3106 }
3107 __h ^= __h >> 13;
3108 __h *= __m;
3109 __h ^= __h >> 15;
3110 return __h;
3111}
3112
3113template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003114struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003115{
3116 _Size operator()(const void* __key, _Size __len);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003117
3118 private:
3119 // Some primes between 2^63 and 2^64.
3120 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
3121 static const _Size __k1 = 0xb492b66fbe98f273ULL;
3122 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
3123 static const _Size __k3 = 0xc949d7c7509e6557ULL;
3124
3125 static _Size __rotate(_Size __val, int __shift) {
3126 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
3127 }
3128
3129 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
3130 return (__val >> __shift) | (__val << (64 - __shift));
3131 }
3132
3133 static _Size __shift_mix(_Size __val) {
3134 return __val ^ (__val >> 47);
3135 }
3136
3137 static _Size __hash_len_16(_Size __u, _Size __v) {
3138 const _Size __mul = 0x9ddfea08eb382d69ULL;
3139 _Size __a = (__u ^ __v) * __mul;
3140 __a ^= (__a >> 47);
3141 _Size __b = (__v ^ __a) * __mul;
3142 __b ^= (__b >> 47);
3143 __b *= __mul;
3144 return __b;
3145 }
3146
3147 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
3148 if (__len > 8) {
3149 const _Size __a = *(const _Size*)__s;
3150 const _Size __b = *(const _Size*)(__s + __len - 8);
3151 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
3152 }
3153 if (__len >= 4) {
3154 const uint32_t __a = *(const uint32_t*)(__s);
3155 const uint32_t __b = *(const uint32_t*)(__s + __len - 4);
3156 return __hash_len_16(__len + (__a << 3), __b);
3157 }
3158 if (__len > 0) {
3159 const unsigned char __a = __s[0];
3160 const unsigned char __b = __s[__len >> 1];
3161 const unsigned char __c = __s[__len - 1];
3162 const uint32_t __y = static_cast<uint32_t>(__a) +
3163 (static_cast<uint32_t>(__b) << 8);
3164 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3165 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3166 }
3167 return __k2;
3168 }
3169
3170 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
3171 const _Size __a = *(const _Size*)(__s) * __k1;
3172 const _Size __b = *(const _Size*)(__s + 8);
3173 const _Size __c = *(const _Size*)(__s + __len - 8) * __k2;
3174 const _Size __d = *(const _Size*)(__s + __len - 16) * __k0;
3175 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3176 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3177 }
3178
3179 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3180 // Callers do best to use "random-looking" values for a and b.
3181 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3182 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3183 __a += __w;
3184 __b = __rotate(__b + __a + __z, 21);
3185 const _Size __c = __a;
3186 __a += __x;
3187 __a += __y;
3188 __b += __rotate(__a, 44);
3189 return pair<_Size, _Size>(__a + __z, __b + __c);
3190 }
3191
3192 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3193 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3194 const char* __s, _Size __a, _Size __b) {
3195 return __weak_hash_len_32_with_seeds(*(const _Size*)(__s),
3196 *(const _Size*)(__s + 8),
3197 *(const _Size*)(__s + 16),
3198 *(const _Size*)(__s + 24),
3199 __a,
3200 __b);
3201 }
3202
3203 // Return an 8-byte hash for 33 to 64 bytes.
3204 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
3205 _Size __z = *(const _Size*)(__s + 24);
3206 _Size __a = *(const _Size*)(__s) +
3207 (__len + *(const _Size*)(__s + __len - 16)) * __k0;
3208 _Size __b = __rotate(__a + __z, 52);
3209 _Size __c = __rotate(__a, 37);
3210 __a += *(const _Size*)(__s + 8);
3211 __c += __rotate(__a, 7);
3212 __a += *(const _Size*)(__s + 16);
3213 _Size __vf = __a + __z;
3214 _Size __vs = __b + __rotate(__a, 31) + __c;
3215 __a = *(const _Size*)(__s + 16) + *(const _Size*)(__s + __len - 32);
3216 __z += *(const _Size*)(__s + __len - 8);
3217 __b = __rotate(__a + __z, 52);
3218 __c = __rotate(__a, 37);
3219 __a += *(const _Size*)(__s + __len - 24);
3220 __c += __rotate(__a, 7);
3221 __a += *(const _Size*)(__s + __len - 16);
3222 _Size __wf = __a + __z;
3223 _Size __ws = __b + __rotate(__a, 31) + __c;
3224 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3225 return __shift_mix(__r * __k0 + __vs) * __k2;
3226 }
Howard Hinnant40c13d32011-12-05 00:08:45 +00003227};
3228
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003229// cityhash64
Howard Hinnant40c13d32011-12-05 00:08:45 +00003230template <class _Size>
3231_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003232__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003233{
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003234 const char* __s = static_cast<const char*>(__key);
3235 if (__len <= 32) {
3236 if (__len <= 16) {
3237 return __hash_len_0_to_16(__s, __len);
3238 } else {
3239 return __hash_len_17_to_32(__s, __len);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003240 }
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003241 } else if (__len <= 64) {
3242 return __hash_len_33_to_64(__s, __len);
3243 }
3244
3245 // For strings over 64 bytes we hash the end first, and then as we
3246 // loop we keep 56 bytes of state: v, w, x, y, and z.
3247 _Size __x = *(const _Size*)(__s + __len - 40);
3248 _Size __y = *(const _Size*)(__s + __len - 16) +
3249 *(const _Size*)(__s + __len - 56);
3250 _Size __z = __hash_len_16(*(const _Size*)(__s + __len - 48) + __len,
3251 *(const _Size*)(__s + __len - 24));
3252 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3253 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
3254 __x = __x * __k1 + *(const _Size*)(__s);
3255
3256 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3257 __len = (__len - 1) & ~static_cast<_Size>(63);
3258 do {
3259 __x = __rotate(__x + __y + __v.first + *(const _Size*)(__s + 8), 37) * __k1;
3260 __y = __rotate(__y + __v.second + *(const _Size*)(__s + 48), 42) * __k1;
3261 __x ^= __w.second;
3262 __y += __v.first + *(const _Size*)(__s + 40);
3263 __z = __rotate(__z + __w.first, 33) * __k1;
3264 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3265 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
3266 __y + *(const _Size*)(__s + 16));
3267 std::swap(__z, __x);
3268 __s += 64;
3269 __len -= 64;
3270 } while (__len != 0);
3271 return __hash_len_16(
3272 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3273 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003274}
3275
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003276template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3277struct __scalar_hash;
3278
3279template <class _Tp>
3280struct __scalar_hash<_Tp, 0>
3281 : public unary_function<_Tp, size_t>
Howard Hinnant21aefc32010-06-03 16:42:57 +00003282{
Howard Hinnant82894812010-09-22 16:48:34 +00003283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003284 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003285 {
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003286 union
3287 {
3288 _Tp __t;
3289 size_t __a;
3290 } __u;
3291 __u.__a = 0;
3292 __u.__t = __v;
3293 return __u.__a;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003294 }
3295};
3296
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003297template <class _Tp>
3298struct __scalar_hash<_Tp, 1>
3299 : public unary_function<_Tp, size_t>
3300{
3301 _LIBCPP_INLINE_VISIBILITY
3302 size_t operator()(_Tp __v) const _NOEXCEPT
3303 {
3304 union
3305 {
3306 _Tp __t;
3307 size_t __a;
3308 } __u;
3309 __u.__t = __v;
3310 return __u.__a;
3311 }
3312};
3313
3314template <class _Tp>
3315struct __scalar_hash<_Tp, 2>
3316 : public unary_function<_Tp, size_t>
3317{
3318 _LIBCPP_INLINE_VISIBILITY
3319 size_t operator()(_Tp __v) const _NOEXCEPT
3320 {
3321 union
3322 {
3323 _Tp __t;
3324 struct
3325 {
3326 size_t __a;
3327 size_t __b;
3328 };
3329 } __u;
3330 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003331 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003332 }
3333};
3334
3335template <class _Tp>
3336struct __scalar_hash<_Tp, 3>
3337 : public unary_function<_Tp, size_t>
3338{
3339 _LIBCPP_INLINE_VISIBILITY
3340 size_t operator()(_Tp __v) const _NOEXCEPT
3341 {
3342 union
3343 {
3344 _Tp __t;
3345 struct
3346 {
3347 size_t __a;
3348 size_t __b;
3349 size_t __c;
3350 };
3351 } __u;
3352 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003353 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003354 }
3355};
3356
3357template <class _Tp>
3358struct __scalar_hash<_Tp, 4>
3359 : public unary_function<_Tp, size_t>
3360{
3361 _LIBCPP_INLINE_VISIBILITY
3362 size_t operator()(_Tp __v) const _NOEXCEPT
3363 {
3364 union
3365 {
3366 _Tp __t;
3367 struct
3368 {
3369 size_t __a;
3370 size_t __b;
3371 size_t __c;
3372 size_t __d;
3373 };
3374 } __u;
3375 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003376 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003377 }
3378};
3379
3380template<class _Tp>
3381struct _LIBCPP_VISIBLE hash<_Tp*>
3382 : public __scalar_hash<_Tp*>
3383{
3384};
3385
Howard Hinnant21aefc32010-06-03 16:42:57 +00003386template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00003387struct _LIBCPP_VISIBLE hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003388{
3389 typedef unique_ptr<_Tp, _Dp> argument_type;
3390 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003392 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003393 {
3394 typedef typename argument_type::pointer pointer;
3395 return hash<pointer>()(__ptr.get());
3396 }
3397};
3398
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003399struct __destruct_n
3400{
3401private:
3402 size_t size;
3403
3404 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003405 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003406 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3407
3408 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003409 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003410 {}
3411
Howard Hinnant1694d232011-05-28 14:41:13 +00003412 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003413 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003414 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003415 {}
3416
Howard Hinnant1694d232011-05-28 14:41:13 +00003417 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003418 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003419 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003420 {}
3421public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003422 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3423 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003424
3425 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003426 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003427 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003428
3429 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003430 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003431 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003432
3433 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003434 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003435 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003436};
3437
3438template <class _Alloc>
3439class __allocator_destructor
3440{
3441 typedef allocator_traits<_Alloc> __alloc_traits;
3442public:
3443 typedef typename __alloc_traits::pointer pointer;
3444 typedef typename __alloc_traits::size_type size_type;
3445private:
3446 _Alloc& __alloc_;
3447 size_type __s_;
3448public:
3449 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00003450 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003451 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003452 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003453 void operator()(pointer __p) _NOEXCEPT
3454 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003455};
3456
3457template <class _InputIterator, class _ForwardIterator>
3458_ForwardIterator
3459uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3460{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003461 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003462#ifndef _LIBCPP_NO_EXCEPTIONS
3463 _ForwardIterator __s = __r;
3464 try
3465 {
3466#endif
3467 for (; __f != __l; ++__f, ++__r)
3468 ::new(&*__r) value_type(*__f);
3469#ifndef _LIBCPP_NO_EXCEPTIONS
3470 }
3471 catch (...)
3472 {
3473 for (; __s != __r; ++__s)
3474 __s->~value_type();
3475 throw;
3476 }
3477#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003478 return __r;
3479}
3480
3481template <class _InputIterator, class _Size, class _ForwardIterator>
3482_ForwardIterator
3483uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3484{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003485 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003486#ifndef _LIBCPP_NO_EXCEPTIONS
3487 _ForwardIterator __s = __r;
3488 try
3489 {
3490#endif
3491 for (; __n > 0; ++__f, ++__r, --__n)
3492 ::new(&*__r) value_type(*__f);
3493#ifndef _LIBCPP_NO_EXCEPTIONS
3494 }
3495 catch (...)
3496 {
3497 for (; __s != __r; ++__s)
3498 __s->~value_type();
3499 throw;
3500 }
3501#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003502 return __r;
3503}
3504
3505template <class _ForwardIterator, class _Tp>
3506void
3507uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3508{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003509 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003510#ifndef _LIBCPP_NO_EXCEPTIONS
3511 _ForwardIterator __s = __f;
3512 try
3513 {
3514#endif
3515 for (; __f != __l; ++__f)
3516 ::new(&*__f) value_type(__x);
3517#ifndef _LIBCPP_NO_EXCEPTIONS
3518 }
3519 catch (...)
3520 {
3521 for (; __s != __f; ++__s)
3522 __s->~value_type();
3523 throw;
3524 }
3525#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003526}
3527
3528template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003529_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003530uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3531{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003532 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003533#ifndef _LIBCPP_NO_EXCEPTIONS
3534 _ForwardIterator __s = __f;
3535 try
3536 {
3537#endif
3538 for (; __n > 0; ++__f, --__n)
3539 ::new(&*__f) value_type(__x);
3540#ifndef _LIBCPP_NO_EXCEPTIONS
3541 }
3542 catch (...)
3543 {
3544 for (; __s != __f; ++__s)
3545 __s->~value_type();
3546 throw;
3547 }
3548#endif
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003549 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003550}
3551
Howard Hinnant82894812010-09-22 16:48:34 +00003552class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003553 : public std::exception
3554{
3555public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003556 virtual ~bad_weak_ptr() _NOEXCEPT;
3557 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003558};
3559
3560template<class _Tp> class weak_ptr;
3561
3562class __shared_count
3563{
3564 __shared_count(const __shared_count&);
3565 __shared_count& operator=(const __shared_count&);
3566
3567protected:
3568 long __shared_owners_;
3569 virtual ~__shared_count();
3570private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003571 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003572
3573public:
Howard Hinnant82894812010-09-22 16:48:34 +00003574 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003575 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003576 : __shared_owners_(__refs) {}
3577
Howard Hinnant1694d232011-05-28 14:41:13 +00003578 void __add_shared() _NOEXCEPT;
3579 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003580 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003581 long use_count() const _NOEXCEPT {return __shared_owners_ + 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003582};
3583
3584class __shared_weak_count
3585 : private __shared_count
3586{
3587 long __shared_weak_owners_;
3588
3589public:
Howard Hinnant82894812010-09-22 16:48:34 +00003590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003591 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003592 : __shared_count(__refs),
3593 __shared_weak_owners_(__refs) {}
3594protected:
3595 virtual ~__shared_weak_count();
3596
3597public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003598 void __add_shared() _NOEXCEPT;
3599 void __add_weak() _NOEXCEPT;
3600 void __release_shared() _NOEXCEPT;
3601 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003602 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003603 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3604 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003605
Howard Hinnant9b763e02012-05-19 20:20:49 +00003606 // purposefully not protected with #ifndef _LIBCPP_NO_RTTI because doing so
3607 // breaks ABI for those clients who need to compile their projects with
3608 // -fno-rtti and yet link against a libc++.dylib compiled without -fno-rtti.
Howard Hinnant1694d232011-05-28 14:41:13 +00003609 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003610private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003611 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003612};
3613
3614template <class _Tp, class _Dp, class _Alloc>
3615class __shared_ptr_pointer
3616 : public __shared_weak_count
3617{
3618 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3619public:
Howard Hinnant82894812010-09-22 16:48:34 +00003620 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003621 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003622 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003623
Howard Hinnantd4444702010-08-11 17:04:31 +00003624#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00003625 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00003626#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003627
3628private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003629 virtual void __on_zero_shared() _NOEXCEPT;
3630 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003631};
3632
Howard Hinnantd4444702010-08-11 17:04:31 +00003633#ifndef _LIBCPP_NO_RTTI
3634
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003635template <class _Tp, class _Dp, class _Alloc>
3636const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00003637__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003638{
3639 return __t == typeid(_Dp) ? &__data_.first().second() : 0;
3640}
3641
Howard Hinnant324bb032010-08-22 00:02:43 +00003642#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003643
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003644template <class _Tp, class _Dp, class _Alloc>
3645void
Howard Hinnant1694d232011-05-28 14:41:13 +00003646__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003647{
3648 __data_.first().second()(__data_.first().first());
3649 __data_.first().second().~_Dp();
3650}
3651
3652template <class _Tp, class _Dp, class _Alloc>
3653void
Howard Hinnant1694d232011-05-28 14:41:13 +00003654__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003655{
3656 typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
3657 __data_.second().~_Alloc();
3658 __a.deallocate(this, 1);
3659}
3660
3661template <class _Tp, class _Alloc>
3662class __shared_ptr_emplace
3663 : public __shared_weak_count
3664{
3665 __compressed_pair<_Alloc, _Tp> __data_;
3666public:
3667#ifndef _LIBCPP_HAS_NO_VARIADICS
3668
Howard Hinnant82894812010-09-22 16:48:34 +00003669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003670 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003671 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003672
3673 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00003674 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003675 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00003676 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3677 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003678
3679#else // _LIBCPP_HAS_NO_VARIADICS
3680
Howard Hinnant82894812010-09-22 16:48:34 +00003681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003682 __shared_ptr_emplace(_Alloc __a)
3683 : __data_(__a) {}
3684
3685 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00003686 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003687 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3688 : __data_(__a, _Tp(__a0)) {}
3689
3690 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00003691 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003692 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3693 : __data_(__a, _Tp(__a0, __a1)) {}
3694
3695 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00003696 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003697 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3698 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3699
3700#endif // _LIBCPP_HAS_NO_VARIADICS
3701
3702private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003703 virtual void __on_zero_shared() _NOEXCEPT;
3704 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003705public:
Howard Hinnant82894812010-09-22 16:48:34 +00003706 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003707 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003708};
3709
3710template <class _Tp, class _Alloc>
3711void
Howard Hinnant1694d232011-05-28 14:41:13 +00003712__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003713{
3714 __data_.second().~_Tp();
3715}
3716
3717template <class _Tp, class _Alloc>
3718void
Howard Hinnant1694d232011-05-28 14:41:13 +00003719__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003720{
3721 typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
3722 __data_.first().~_Alloc();
3723 __a.deallocate(this, 1);
3724}
3725
3726template<class _Tp> class enable_shared_from_this;
3727
3728template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003729class _LIBCPP_VISIBLE shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003730{
Howard Hinnant324bb032010-08-22 00:02:43 +00003731public:
3732 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003733private:
3734 element_type* __ptr_;
3735 __shared_weak_count* __cntrl_;
3736
3737 struct __nat {int __for_bool_;};
3738public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003739 shared_ptr() _NOEXCEPT;
3740 shared_ptr(nullptr_t) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003741 template<class _Yp,
3742 class = typename enable_if
3743 <
3744 is_convertible<_Yp*, element_type*>::value
3745 >::type
3746 >
3747 explicit shared_ptr(_Yp* __p);
3748 template<class _Yp, class _Dp,
3749 class = typename enable_if
3750 <
3751 is_convertible<_Yp*, element_type*>::value
3752 >::type
3753 >
3754 shared_ptr(_Yp* __p, _Dp __d);
3755 template<class _Yp, class _Dp, class _Alloc,
3756 class = typename enable_if
3757 <
3758 is_convertible<_Yp*, element_type*>::value
3759 >::type
3760 >
3761 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003762 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3763 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant1694d232011-05-28 14:41:13 +00003764 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3765 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003766 template<class _Yp>
3767 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003768 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3769 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003770#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003771 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003772 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003773 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3774 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003775#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003776 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003777 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003778#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003779 template<class _Yp,
3780 class = typename enable_if
3781 <
3782 is_convertible<_Yp*, element_type*>::value
3783 >::type
3784 >
3785 shared_ptr(auto_ptr<_Yp>&& __r);
Howard Hinnant324bb032010-08-22 00:02:43 +00003786#else
Howard Hinnant57199402012-01-02 17:56:02 +00003787 template<class _Yp,
3788 class = typename enable_if
3789 <
3790 is_convertible<_Yp*, element_type*>::value
3791 >::type
3792 >
3793 shared_ptr(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003794#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003795#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003796 template <class _Yp, class _Dp,
3797 class = typename enable_if
3798 <
3799 !is_array<_Yp>::value &&
3800 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3801 >::type
3802 >
3803 shared_ptr(unique_ptr<_Yp, _Dp>&&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003804 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant57199402012-01-02 17:56:02 +00003805 template <class _Yp, class _Dp,
3806 class = typename enable_if
3807 <
3808 !is_array<_Yp>::value &&
3809 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3810 >::type
3811 >
3812 shared_ptr(unique_ptr<_Yp, _Dp>&&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003813 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003814#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003815 template <class _Yp, class _Dp,
3816 class = typename enable_if
3817 <
3818 !is_array<_Yp>::value &&
3819 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3820 >::type
3821 > shared_ptr(unique_ptr<_Yp, _Dp>,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003822 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant57199402012-01-02 17:56:02 +00003823 template <class _Yp, class _Dp,
3824 class = typename enable_if
3825 <
3826 !is_array<_Yp>::value &&
3827 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3828 >::type
3829 >
3830 shared_ptr(unique_ptr<_Yp, _Dp>,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003831 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003832#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003833
3834 ~shared_ptr();
3835
Howard Hinnant1694d232011-05-28 14:41:13 +00003836 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003837 template<class _Yp>
3838 typename enable_if
3839 <
3840 is_convertible<_Yp*, element_type*>::value,
3841 shared_ptr&
3842 >::type
3843 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003844#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003845 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003846 template<class _Yp>
3847 typename enable_if
3848 <
3849 is_convertible<_Yp*, element_type*>::value,
3850 shared_ptr<_Tp>&
3851 >::type
3852 operator=(shared_ptr<_Yp>&& __r);
3853 template<class _Yp>
3854 typename enable_if
3855 <
3856 !is_array<_Yp>::value &&
3857 is_convertible<_Yp*, element_type*>::value,
3858 shared_ptr&
3859 >::type
3860 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003861#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003862 template<class _Yp>
3863 typename enable_if
3864 <
3865 !is_array<_Yp>::value &&
3866 is_convertible<_Yp*, element_type*>::value,
3867 shared_ptr&
3868 >::type
3869 operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003870#endif
Howard Hinnant57199402012-01-02 17:56:02 +00003871 template <class _Yp, class _Dp>
3872 typename enable_if
3873 <
3874 !is_array<_Yp>::value &&
3875 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3876 shared_ptr&
3877 >::type
Howard Hinnant73d21a42010-09-04 23:28:19 +00003878#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003879 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003880#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003881 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003882#endif
3883
Howard Hinnant1694d232011-05-28 14:41:13 +00003884 void swap(shared_ptr& __r) _NOEXCEPT;
3885 void reset() _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003886 template<class _Yp>
3887 typename enable_if
3888 <
3889 is_convertible<_Yp*, element_type*>::value,
3890 void
3891 >::type
3892 reset(_Yp* __p);
3893 template<class _Yp, class _Dp>
3894 typename enable_if
3895 <
3896 is_convertible<_Yp*, element_type*>::value,
3897 void
3898 >::type
3899 reset(_Yp* __p, _Dp __d);
3900 template<class _Yp, class _Dp, class _Alloc>
3901 typename enable_if
3902 <
3903 is_convertible<_Yp*, element_type*>::value,
3904 void
3905 >::type
3906 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003907
Howard Hinnant82894812010-09-22 16:48:34 +00003908 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003909 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003910 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003911 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3912 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003914 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003915 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003916 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003917 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003918 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00003919 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant77861882012-02-21 21:46:43 +00003920 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant99968442011-11-29 18:15:50 +00003921 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00003922 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003923 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003924 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant99968442011-11-29 18:15:50 +00003925 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00003926 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003927 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003928 {return __cntrl_ < __p.__cntrl_;}
3929
Howard Hinnantd4444702010-08-11 17:04:31 +00003930#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003931 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00003932 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003933 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003934 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00003935#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003936
3937#ifndef _LIBCPP_HAS_NO_VARIADICS
3938
3939 template<class ..._Args>
3940 static
3941 shared_ptr<_Tp>
3942 make_shared(_Args&& ...__args);
3943
3944 template<class _Alloc, class ..._Args>
3945 static
3946 shared_ptr<_Tp>
3947 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3948
3949#else // _LIBCPP_HAS_NO_VARIADICS
3950
3951 static shared_ptr<_Tp> make_shared();
3952
3953 template<class _A0>
3954 static shared_ptr<_Tp> make_shared(_A0&);
3955
3956 template<class _A0, class _A1>
3957 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3958
3959 template<class _A0, class _A1, class _A2>
3960 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3961
3962 template<class _Alloc>
3963 static shared_ptr<_Tp>
3964 allocate_shared(const _Alloc& __a);
3965
3966 template<class _Alloc, class _A0>
3967 static shared_ptr<_Tp>
3968 allocate_shared(const _Alloc& __a, _A0& __a0);
3969
3970 template<class _Alloc, class _A0, class _A1>
3971 static shared_ptr<_Tp>
3972 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3973
3974 template<class _Alloc, class _A0, class _A1, class _A2>
3975 static shared_ptr<_Tp>
3976 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3977
3978#endif // _LIBCPP_HAS_NO_VARIADICS
3979
3980private:
3981
3982 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00003983 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003984 void
Howard Hinnant1694d232011-05-28 14:41:13 +00003985 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003986 {
3987 if (__e)
3988 __e->__weak_this_ = *this;
3989 }
3990
Howard Hinnant82894812010-09-22 16:48:34 +00003991 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003992 void __enable_weak_this(const void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003993
Howard Hinnant82894812010-09-22 16:48:34 +00003994 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
3995 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003996};
3997
3998template<class _Tp>
3999inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004000shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004001 : __ptr_(0),
4002 __cntrl_(0)
4003{
4004}
4005
4006template<class _Tp>
4007inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004008shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004009 : __ptr_(0),
4010 __cntrl_(0)
4011{
4012}
4013
4014template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004015template<class _Yp, class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004016shared_ptr<_Tp>::shared_ptr(_Yp* __p)
4017 : __ptr_(__p)
4018{
4019 unique_ptr<_Yp> __hold(__p);
4020 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4021 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
4022 __hold.release();
4023 __enable_weak_this(__p);
4024}
4025
4026template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004027template<class _Yp, class _Dp, class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004028shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
4029 : __ptr_(__p)
4030{
4031#ifndef _LIBCPP_NO_EXCEPTIONS
4032 try
4033 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004034#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004035 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4036 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
4037 __enable_weak_this(__p);
4038#ifndef _LIBCPP_NO_EXCEPTIONS
4039 }
4040 catch (...)
4041 {
4042 __d(__p);
4043 throw;
4044 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004045#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004046}
4047
4048template<class _Tp>
4049template<class _Dp>
4050shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4051 : __ptr_(0)
4052{
4053#ifndef _LIBCPP_NO_EXCEPTIONS
4054 try
4055 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004056#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004057 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4058 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4059#ifndef _LIBCPP_NO_EXCEPTIONS
4060 }
4061 catch (...)
4062 {
4063 __d(__p);
4064 throw;
4065 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004066#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004067}
4068
4069template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004070template<class _Yp, class _Dp, class _Alloc, class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004071shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
4072 : __ptr_(__p)
4073{
4074#ifndef _LIBCPP_NO_EXCEPTIONS
4075 try
4076 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004077#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004078 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
4079 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
4080 typedef __allocator_destructor<_A2> _D2;
4081 _A2 __a2(__a);
4082 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4083 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
4084 __cntrl_ = __hold2.release();
4085 __enable_weak_this(__p);
4086#ifndef _LIBCPP_NO_EXCEPTIONS
4087 }
4088 catch (...)
4089 {
4090 __d(__p);
4091 throw;
4092 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004093#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004094}
4095
4096template<class _Tp>
4097template<class _Dp, class _Alloc>
4098shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4099 : __ptr_(0)
4100{
4101#ifndef _LIBCPP_NO_EXCEPTIONS
4102 try
4103 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004104#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004105 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
4106 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
4107 typedef __allocator_destructor<_A2> _D2;
4108 _A2 __a2(__a);
4109 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4110 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
4111 __cntrl_ = __hold2.release();
4112#ifndef _LIBCPP_NO_EXCEPTIONS
4113 }
4114 catch (...)
4115 {
4116 __d(__p);
4117 throw;
4118 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004119#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004120}
4121
4122template<class _Tp>
4123template<class _Yp>
4124inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004125shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004126 : __ptr_(__p),
4127 __cntrl_(__r.__cntrl_)
4128{
4129 if (__cntrl_)
4130 __cntrl_->__add_shared();
4131}
4132
4133template<class _Tp>
4134inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004135shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004136 : __ptr_(__r.__ptr_),
4137 __cntrl_(__r.__cntrl_)
4138{
4139 if (__cntrl_)
4140 __cntrl_->__add_shared();
4141}
4142
4143template<class _Tp>
4144template<class _Yp>
4145inline _LIBCPP_INLINE_VISIBILITY
4146shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4147 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004148 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004149 : __ptr_(__r.__ptr_),
4150 __cntrl_(__r.__cntrl_)
4151{
4152 if (__cntrl_)
4153 __cntrl_->__add_shared();
4154}
4155
Howard Hinnant73d21a42010-09-04 23:28:19 +00004156#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004157
4158template<class _Tp>
4159inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004160shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004161 : __ptr_(__r.__ptr_),
4162 __cntrl_(__r.__cntrl_)
4163{
4164 __r.__ptr_ = 0;
4165 __r.__cntrl_ = 0;
4166}
4167
4168template<class _Tp>
4169template<class _Yp>
4170inline _LIBCPP_INLINE_VISIBILITY
4171shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4172 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004173 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004174 : __ptr_(__r.__ptr_),
4175 __cntrl_(__r.__cntrl_)
4176{
4177 __r.__ptr_ = 0;
4178 __r.__cntrl_ = 0;
4179}
4180
Howard Hinnant73d21a42010-09-04 23:28:19 +00004181#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004182
4183template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004184template<class _Yp, class>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004185#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004186shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
4187#else
Howard Hinnant92172b82010-08-21 21:14:53 +00004188shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004189#endif
4190 : __ptr_(__r.get())
4191{
4192 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4193 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
4194 __enable_weak_this(__r.get());
4195 __r.release();
4196}
4197
4198template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004199template <class _Yp, class _Dp, class>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004200#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004201shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4202#else
4203shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4204#endif
4205 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
4206 : __ptr_(__r.get())
4207{
4208 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4209 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
4210 __enable_weak_this(__r.get());
4211 __r.release();
4212}
4213
4214template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004215template <class _Yp, class _Dp, class>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004216#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004217shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4218#else
4219shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4220#endif
4221 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
4222 : __ptr_(__r.get())
4223{
4224 typedef __shared_ptr_pointer<_Yp*,
4225 reference_wrapper<typename remove_reference<_Dp>::type>,
4226 allocator<_Yp> > _CntrlBlk;
4227 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
4228 __enable_weak_this(__r.get());
4229 __r.release();
4230}
4231
4232#ifndef _LIBCPP_HAS_NO_VARIADICS
4233
4234template<class _Tp>
4235template<class ..._Args>
4236shared_ptr<_Tp>
4237shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4238{
4239 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4240 typedef allocator<_CntrlBlk> _A2;
4241 typedef __allocator_destructor<_A2> _D2;
4242 _A2 __a2;
4243 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004244 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004245 shared_ptr<_Tp> __r;
4246 __r.__ptr_ = __hold2.get()->get();
4247 __r.__cntrl_ = __hold2.release();
4248 __r.__enable_weak_this(__r.__ptr_);
4249 return __r;
4250}
4251
4252template<class _Tp>
4253template<class _Alloc, class ..._Args>
4254shared_ptr<_Tp>
4255shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4256{
4257 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4258 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
4259 typedef __allocator_destructor<_A2> _D2;
4260 _A2 __a2(__a);
4261 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004262 ::new(__hold2.get()) _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004263 shared_ptr<_Tp> __r;
4264 __r.__ptr_ = __hold2.get()->get();
4265 __r.__cntrl_ = __hold2.release();
4266 __r.__enable_weak_this(__r.__ptr_);
4267 return __r;
4268}
4269
4270#else // _LIBCPP_HAS_NO_VARIADICS
4271
4272template<class _Tp>
4273shared_ptr<_Tp>
4274shared_ptr<_Tp>::make_shared()
4275{
4276 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4277 typedef allocator<_CntrlBlk> _Alloc2;
4278 typedef __allocator_destructor<_Alloc2> _D2;
4279 _Alloc2 __alloc2;
4280 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4281 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4282 shared_ptr<_Tp> __r;
4283 __r.__ptr_ = __hold2.get()->get();
4284 __r.__cntrl_ = __hold2.release();
4285 __r.__enable_weak_this(__r.__ptr_);
4286 return __r;
4287}
4288
4289template<class _Tp>
4290template<class _A0>
4291shared_ptr<_Tp>
4292shared_ptr<_Tp>::make_shared(_A0& __a0)
4293{
4294 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4295 typedef allocator<_CntrlBlk> _Alloc2;
4296 typedef __allocator_destructor<_Alloc2> _D2;
4297 _Alloc2 __alloc2;
4298 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4299 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4300 shared_ptr<_Tp> __r;
4301 __r.__ptr_ = __hold2.get()->get();
4302 __r.__cntrl_ = __hold2.release();
4303 __r.__enable_weak_this(__r.__ptr_);
4304 return __r;
4305}
4306
4307template<class _Tp>
4308template<class _A0, class _A1>
4309shared_ptr<_Tp>
4310shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4311{
4312 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4313 typedef allocator<_CntrlBlk> _Alloc2;
4314 typedef __allocator_destructor<_Alloc2> _D2;
4315 _Alloc2 __alloc2;
4316 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4317 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4318 shared_ptr<_Tp> __r;
4319 __r.__ptr_ = __hold2.get()->get();
4320 __r.__cntrl_ = __hold2.release();
4321 __r.__enable_weak_this(__r.__ptr_);
4322 return __r;
4323}
4324
4325template<class _Tp>
4326template<class _A0, class _A1, class _A2>
4327shared_ptr<_Tp>
4328shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4329{
4330 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4331 typedef allocator<_CntrlBlk> _Alloc2;
4332 typedef __allocator_destructor<_Alloc2> _D2;
4333 _Alloc2 __alloc2;
4334 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4335 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4336 shared_ptr<_Tp> __r;
4337 __r.__ptr_ = __hold2.get()->get();
4338 __r.__cntrl_ = __hold2.release();
4339 __r.__enable_weak_this(__r.__ptr_);
4340 return __r;
4341}
4342
4343template<class _Tp>
4344template<class _Alloc>
4345shared_ptr<_Tp>
4346shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4347{
4348 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4349 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4350 typedef __allocator_destructor<_Alloc2> _D2;
4351 _Alloc2 __alloc2(__a);
4352 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4353 ::new(__hold2.get()) _CntrlBlk(__a);
4354 shared_ptr<_Tp> __r;
4355 __r.__ptr_ = __hold2.get()->get();
4356 __r.__cntrl_ = __hold2.release();
4357 __r.__enable_weak_this(__r.__ptr_);
4358 return __r;
4359}
4360
4361template<class _Tp>
4362template<class _Alloc, class _A0>
4363shared_ptr<_Tp>
4364shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4365{
4366 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4367 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4368 typedef __allocator_destructor<_Alloc2> _D2;
4369 _Alloc2 __alloc2(__a);
4370 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4371 ::new(__hold2.get()) _CntrlBlk(__a, __a0);
4372 shared_ptr<_Tp> __r;
4373 __r.__ptr_ = __hold2.get()->get();
4374 __r.__cntrl_ = __hold2.release();
4375 __r.__enable_weak_this(__r.__ptr_);
4376 return __r;
4377}
4378
4379template<class _Tp>
4380template<class _Alloc, class _A0, class _A1>
4381shared_ptr<_Tp>
4382shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4383{
4384 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4385 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4386 typedef __allocator_destructor<_Alloc2> _D2;
4387 _Alloc2 __alloc2(__a);
4388 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4389 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
4390 shared_ptr<_Tp> __r;
4391 __r.__ptr_ = __hold2.get()->get();
4392 __r.__cntrl_ = __hold2.release();
4393 __r.__enable_weak_this(__r.__ptr_);
4394 return __r;
4395}
4396
4397template<class _Tp>
4398template<class _Alloc, class _A0, class _A1, class _A2>
4399shared_ptr<_Tp>
4400shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4401{
4402 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4403 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4404 typedef __allocator_destructor<_Alloc2> _D2;
4405 _Alloc2 __alloc2(__a);
4406 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4407 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
4408 shared_ptr<_Tp> __r;
4409 __r.__ptr_ = __hold2.get()->get();
4410 __r.__cntrl_ = __hold2.release();
4411 __r.__enable_weak_this(__r.__ptr_);
4412 return __r;
4413}
4414
4415#endif // _LIBCPP_HAS_NO_VARIADICS
4416
4417template<class _Tp>
4418shared_ptr<_Tp>::~shared_ptr()
4419{
4420 if (__cntrl_)
4421 __cntrl_->__release_shared();
4422}
4423
4424template<class _Tp>
4425inline _LIBCPP_INLINE_VISIBILITY
4426shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004427shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004428{
4429 shared_ptr(__r).swap(*this);
4430 return *this;
4431}
4432
4433template<class _Tp>
4434template<class _Yp>
4435inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004436typename enable_if
4437<
4438 is_convertible<_Yp*, _Tp*>::value,
4439 shared_ptr<_Tp>&
4440>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004441shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004442{
4443 shared_ptr(__r).swap(*this);
4444 return *this;
4445}
4446
Howard Hinnant73d21a42010-09-04 23:28:19 +00004447#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004448
4449template<class _Tp>
4450inline _LIBCPP_INLINE_VISIBILITY
4451shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004452shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004453{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004454 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004455 return *this;
4456}
4457
4458template<class _Tp>
4459template<class _Yp>
4460inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004461typename enable_if
4462<
4463 is_convertible<_Yp*, _Tp*>::value,
4464 shared_ptr<_Tp>&
4465>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004466shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4467{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004468 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004469 return *this;
4470}
4471
4472template<class _Tp>
4473template<class _Yp>
4474inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004475typename enable_if
4476<
4477 !is_array<_Yp>::value &&
4478 is_convertible<_Yp*, _Tp*>::value,
4479 shared_ptr<_Tp>&
4480>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004481shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4482{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004483 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004484 return *this;
4485}
4486
4487template<class _Tp>
4488template <class _Yp, class _Dp>
4489inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004490typename enable_if
4491<
4492 !is_array<_Yp>::value &&
4493 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4494 shared_ptr<_Tp>&
4495>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004496shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4497{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004498 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004499 return *this;
4500}
4501
Howard Hinnant73d21a42010-09-04 23:28:19 +00004502#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004503
4504template<class _Tp>
4505template<class _Yp>
4506inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004507typename enable_if
4508<
4509 !is_array<_Yp>::value &&
4510 is_convertible<_Yp*, _Tp*>::value,
4511 shared_ptr<_Tp>&
4512>::type
Howard Hinnant324bb032010-08-22 00:02:43 +00004513shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004514{
4515 shared_ptr(__r).swap(*this);
4516 return *this;
4517}
4518
4519template<class _Tp>
4520template <class _Yp, class _Dp>
4521inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004522typename enable_if
4523<
4524 !is_array<_Yp>::value &&
4525 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4526 shared_ptr<_Tp>&
4527>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004528shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4529{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004530 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004531 return *this;
4532}
4533
Howard Hinnant73d21a42010-09-04 23:28:19 +00004534#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004535
4536template<class _Tp>
4537inline _LIBCPP_INLINE_VISIBILITY
4538void
Howard Hinnant1694d232011-05-28 14:41:13 +00004539shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004540{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004541 _VSTD::swap(__ptr_, __r.__ptr_);
4542 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004543}
4544
4545template<class _Tp>
4546inline _LIBCPP_INLINE_VISIBILITY
4547void
Howard Hinnant1694d232011-05-28 14:41:13 +00004548shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004549{
4550 shared_ptr().swap(*this);
4551}
4552
4553template<class _Tp>
4554template<class _Yp>
4555inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004556typename enable_if
4557<
4558 is_convertible<_Yp*, _Tp*>::value,
4559 void
4560>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004561shared_ptr<_Tp>::reset(_Yp* __p)
4562{
4563 shared_ptr(__p).swap(*this);
4564}
4565
4566template<class _Tp>
4567template<class _Yp, class _Dp>
4568inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004569typename enable_if
4570<
4571 is_convertible<_Yp*, _Tp*>::value,
4572 void
4573>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004574shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4575{
4576 shared_ptr(__p, __d).swap(*this);
4577}
4578
4579template<class _Tp>
4580template<class _Yp, class _Dp, class _Alloc>
4581inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004582typename enable_if
4583<
4584 is_convertible<_Yp*, _Tp*>::value,
4585 void
4586>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004587shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4588{
4589 shared_ptr(__p, __d, __a).swap(*this);
4590}
4591
4592#ifndef _LIBCPP_HAS_NO_VARIADICS
4593
Howard Hinnant324bb032010-08-22 00:02:43 +00004594template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004595inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004596typename enable_if
4597<
4598 !is_array<_Tp>::value,
4599 shared_ptr<_Tp>
4600>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004601make_shared(_Args&& ...__args)
4602{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004603 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004604}
4605
Howard Hinnant324bb032010-08-22 00:02:43 +00004606template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004607inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004608typename enable_if
4609<
4610 !is_array<_Tp>::value,
4611 shared_ptr<_Tp>
4612>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004613allocate_shared(const _Alloc& __a, _Args&& ...__args)
4614{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004615 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004616}
4617
4618#else // _LIBCPP_HAS_NO_VARIADICS
4619
4620template<class _Tp>
4621inline _LIBCPP_INLINE_VISIBILITY
4622shared_ptr<_Tp>
4623make_shared()
4624{
4625 return shared_ptr<_Tp>::make_shared();
4626}
4627
4628template<class _Tp, class _A0>
4629inline _LIBCPP_INLINE_VISIBILITY
4630shared_ptr<_Tp>
4631make_shared(_A0& __a0)
4632{
4633 return shared_ptr<_Tp>::make_shared(__a0);
4634}
4635
4636template<class _Tp, class _A0, class _A1>
4637inline _LIBCPP_INLINE_VISIBILITY
4638shared_ptr<_Tp>
4639make_shared(_A0& __a0, _A1& __a1)
4640{
4641 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4642}
4643
Howard Hinnant324bb032010-08-22 00:02:43 +00004644template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004645inline _LIBCPP_INLINE_VISIBILITY
4646shared_ptr<_Tp>
4647make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4648{
4649 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4650}
4651
4652template<class _Tp, class _Alloc>
4653inline _LIBCPP_INLINE_VISIBILITY
4654shared_ptr<_Tp>
4655allocate_shared(const _Alloc& __a)
4656{
4657 return shared_ptr<_Tp>::allocate_shared(__a);
4658}
4659
4660template<class _Tp, class _Alloc, class _A0>
4661inline _LIBCPP_INLINE_VISIBILITY
4662shared_ptr<_Tp>
4663allocate_shared(const _Alloc& __a, _A0& __a0)
4664{
4665 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4666}
4667
4668template<class _Tp, class _Alloc, class _A0, class _A1>
4669inline _LIBCPP_INLINE_VISIBILITY
4670shared_ptr<_Tp>
4671allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4672{
4673 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4674}
4675
4676template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4677inline _LIBCPP_INLINE_VISIBILITY
4678shared_ptr<_Tp>
4679allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4680{
4681 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4682}
4683
4684#endif // _LIBCPP_HAS_NO_VARIADICS
4685
4686template<class _Tp, class _Up>
4687inline _LIBCPP_INLINE_VISIBILITY
4688bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004689operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004690{
4691 return __x.get() == __y.get();
4692}
4693
4694template<class _Tp, class _Up>
4695inline _LIBCPP_INLINE_VISIBILITY
4696bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004697operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004698{
4699 return !(__x == __y);
4700}
4701
4702template<class _Tp, class _Up>
4703inline _LIBCPP_INLINE_VISIBILITY
4704bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004705operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004706{
Howard Hinnant3fadda32012-02-21 21:02:58 +00004707 typedef typename common_type<_Tp*, _Up*>::type _V;
4708 return less<_V>()(__x.get(), __y.get());
4709}
4710
4711template<class _Tp, class _Up>
4712inline _LIBCPP_INLINE_VISIBILITY
4713bool
4714operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4715{
4716 return __y < __x;
4717}
4718
4719template<class _Tp, class _Up>
4720inline _LIBCPP_INLINE_VISIBILITY
4721bool
4722operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4723{
4724 return !(__y < __x);
4725}
4726
4727template<class _Tp, class _Up>
4728inline _LIBCPP_INLINE_VISIBILITY
4729bool
4730operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4731{
4732 return !(__x < __y);
4733}
4734
4735template<class _Tp>
4736inline _LIBCPP_INLINE_VISIBILITY
4737bool
4738operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4739{
4740 return !__x;
4741}
4742
4743template<class _Tp>
4744inline _LIBCPP_INLINE_VISIBILITY
4745bool
4746operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4747{
4748 return !__x;
4749}
4750
4751template<class _Tp>
4752inline _LIBCPP_INLINE_VISIBILITY
4753bool
4754operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4755{
4756 return static_cast<bool>(__x);
4757}
4758
4759template<class _Tp>
4760inline _LIBCPP_INLINE_VISIBILITY
4761bool
4762operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4763{
4764 return static_cast<bool>(__x);
4765}
4766
4767template<class _Tp>
4768inline _LIBCPP_INLINE_VISIBILITY
4769bool
4770operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4771{
4772 return less<_Tp*>()(__x.get(), nullptr);
4773}
4774
4775template<class _Tp>
4776inline _LIBCPP_INLINE_VISIBILITY
4777bool
4778operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4779{
4780 return less<_Tp*>()(nullptr, __x.get());
4781}
4782
4783template<class _Tp>
4784inline _LIBCPP_INLINE_VISIBILITY
4785bool
4786operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4787{
4788 return nullptr < __x;
4789}
4790
4791template<class _Tp>
4792inline _LIBCPP_INLINE_VISIBILITY
4793bool
4794operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4795{
4796 return __x < nullptr;
4797}
4798
4799template<class _Tp>
4800inline _LIBCPP_INLINE_VISIBILITY
4801bool
4802operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4803{
4804 return !(nullptr < __x);
4805}
4806
4807template<class _Tp>
4808inline _LIBCPP_INLINE_VISIBILITY
4809bool
4810operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4811{
4812 return !(__x < nullptr);
4813}
4814
4815template<class _Tp>
4816inline _LIBCPP_INLINE_VISIBILITY
4817bool
4818operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4819{
4820 return !(__x < nullptr);
4821}
4822
4823template<class _Tp>
4824inline _LIBCPP_INLINE_VISIBILITY
4825bool
4826operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4827{
4828 return !(nullptr < __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004829}
4830
4831template<class _Tp>
4832inline _LIBCPP_INLINE_VISIBILITY
4833void
Howard Hinnant1694d232011-05-28 14:41:13 +00004834swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004835{
4836 __x.swap(__y);
4837}
4838
4839template<class _Tp, class _Up>
4840inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004841typename enable_if
4842<
4843 !is_array<_Tp>::value && !is_array<_Up>::value,
4844 shared_ptr<_Tp>
4845>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004846static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004847{
4848 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4849}
4850
4851template<class _Tp, class _Up>
4852inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004853typename enable_if
4854<
4855 !is_array<_Tp>::value && !is_array<_Up>::value,
4856 shared_ptr<_Tp>
4857>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004858dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004859{
4860 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4861 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4862}
4863
4864template<class _Tp, class _Up>
Howard Hinnant57199402012-01-02 17:56:02 +00004865typename enable_if
4866<
4867 is_array<_Tp>::value == is_array<_Up>::value,
4868 shared_ptr<_Tp>
4869>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004870const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004871{
Howard Hinnant57199402012-01-02 17:56:02 +00004872 typedef typename remove_extent<_Tp>::type _RTp;
4873 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004874}
4875
Howard Hinnantd4444702010-08-11 17:04:31 +00004876#ifndef _LIBCPP_NO_RTTI
4877
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004878template<class _Dp, class _Tp>
4879inline _LIBCPP_INLINE_VISIBILITY
4880_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00004881get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004882{
4883 return __p.template __get_deleter<_Dp>();
4884}
4885
Howard Hinnant324bb032010-08-22 00:02:43 +00004886#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00004887
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004888template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004889class _LIBCPP_VISIBLE weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004890{
Howard Hinnant324bb032010-08-22 00:02:43 +00004891public:
4892 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004893private:
4894 element_type* __ptr_;
4895 __shared_weak_count* __cntrl_;
4896
Howard Hinnant324bb032010-08-22 00:02:43 +00004897public:
Howard Hinnant1694d232011-05-28 14:41:13 +00004898 weak_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004899 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00004900 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4901 _NOEXCEPT;
4902 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004903 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00004904 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4905 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004906
Howard Hinnant57199402012-01-02 17:56:02 +00004907#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4908 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
4909 template<class _Yp> weak_ptr(weak_ptr<_Yp>&& __r,
4910 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4911 _NOEXCEPT;
4912#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00004913 ~weak_ptr();
4914
Howard Hinnant1694d232011-05-28 14:41:13 +00004915 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00004916 template<class _Yp>
4917 typename enable_if
4918 <
4919 is_convertible<_Yp*, element_type*>::value,
4920 weak_ptr&
4921 >::type
4922 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4923
4924#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4925
4926 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4927 template<class _Yp>
4928 typename enable_if
4929 <
4930 is_convertible<_Yp*, element_type*>::value,
4931 weak_ptr&
4932 >::type
4933 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4934
4935#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4936
4937 template<class _Yp>
4938 typename enable_if
4939 <
4940 is_convertible<_Yp*, element_type*>::value,
4941 weak_ptr&
4942 >::type
4943 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004944
Howard Hinnant1694d232011-05-28 14:41:13 +00004945 void swap(weak_ptr& __r) _NOEXCEPT;
4946 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004947
Howard Hinnant82894812010-09-22 16:48:34 +00004948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004949 long use_count() const _NOEXCEPT
4950 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00004951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004952 bool expired() const _NOEXCEPT
4953 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4954 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00004955 template<class _Up>
4956 _LIBCPP_INLINE_VISIBILITY
4957 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004958 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004959 template<class _Up>
4960 _LIBCPP_INLINE_VISIBILITY
4961 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004962 {return __cntrl_ < __r.__cntrl_;}
4963
Howard Hinnant82894812010-09-22 16:48:34 +00004964 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
4965 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004966};
4967
4968template<class _Tp>
4969inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004970weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004971 : __ptr_(0),
4972 __cntrl_(0)
4973{
4974}
4975
4976template<class _Tp>
4977inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004978weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004979 : __ptr_(__r.__ptr_),
4980 __cntrl_(__r.__cntrl_)
4981{
4982 if (__cntrl_)
4983 __cntrl_->__add_weak();
4984}
4985
4986template<class _Tp>
4987template<class _Yp>
4988inline _LIBCPP_INLINE_VISIBILITY
4989weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00004990 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004991 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004992 : __ptr_(__r.__ptr_),
4993 __cntrl_(__r.__cntrl_)
4994{
4995 if (__cntrl_)
4996 __cntrl_->__add_weak();
4997}
4998
4999template<class _Tp>
5000template<class _Yp>
5001inline _LIBCPP_INLINE_VISIBILITY
5002weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005003 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005004 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005005 : __ptr_(__r.__ptr_),
5006 __cntrl_(__r.__cntrl_)
5007{
5008 if (__cntrl_)
5009 __cntrl_->__add_weak();
5010}
5011
Howard Hinnant57199402012-01-02 17:56:02 +00005012#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5013
5014template<class _Tp>
5015inline _LIBCPP_INLINE_VISIBILITY
5016weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5017 : __ptr_(__r.__ptr_),
5018 __cntrl_(__r.__cntrl_)
5019{
5020 __r.__ptr_ = 0;
5021 __r.__cntrl_ = 0;
5022}
5023
5024template<class _Tp>
5025template<class _Yp>
5026inline _LIBCPP_INLINE_VISIBILITY
5027weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5028 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5029 _NOEXCEPT
5030 : __ptr_(__r.__ptr_),
5031 __cntrl_(__r.__cntrl_)
5032{
5033 __r.__ptr_ = 0;
5034 __r.__cntrl_ = 0;
5035}
5036
5037#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5038
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005039template<class _Tp>
5040weak_ptr<_Tp>::~weak_ptr()
5041{
5042 if (__cntrl_)
5043 __cntrl_->__release_weak();
5044}
5045
5046template<class _Tp>
5047inline _LIBCPP_INLINE_VISIBILITY
5048weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00005049weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005050{
5051 weak_ptr(__r).swap(*this);
5052 return *this;
5053}
5054
5055template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005056template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005057inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005058typename enable_if
5059<
5060 is_convertible<_Yp*, _Tp*>::value,
5061 weak_ptr<_Tp>&
5062>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005063weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005064{
5065 weak_ptr(__r).swap(*this);
5066 return *this;
5067}
5068
Howard Hinnant57199402012-01-02 17:56:02 +00005069#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5070
5071template<class _Tp>
5072inline _LIBCPP_INLINE_VISIBILITY
5073weak_ptr<_Tp>&
5074weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5075{
5076 weak_ptr(_VSTD::move(__r)).swap(*this);
5077 return *this;
5078}
5079
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005080template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005081template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005082inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005083typename enable_if
5084<
5085 is_convertible<_Yp*, _Tp*>::value,
5086 weak_ptr<_Tp>&
5087>::type
5088weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5089{
5090 weak_ptr(_VSTD::move(__r)).swap(*this);
5091 return *this;
5092}
5093
5094#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5095
5096template<class _Tp>
5097template<class _Yp>
5098inline _LIBCPP_INLINE_VISIBILITY
5099typename enable_if
5100<
5101 is_convertible<_Yp*, _Tp*>::value,
5102 weak_ptr<_Tp>&
5103>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005104weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005105{
5106 weak_ptr(__r).swap(*this);
5107 return *this;
5108}
5109
5110template<class _Tp>
5111inline _LIBCPP_INLINE_VISIBILITY
5112void
Howard Hinnant1694d232011-05-28 14:41:13 +00005113weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005114{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005115 _VSTD::swap(__ptr_, __r.__ptr_);
5116 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005117}
5118
5119template<class _Tp>
5120inline _LIBCPP_INLINE_VISIBILITY
5121void
Howard Hinnant1694d232011-05-28 14:41:13 +00005122swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005123{
5124 __x.swap(__y);
5125}
5126
5127template<class _Tp>
5128inline _LIBCPP_INLINE_VISIBILITY
5129void
Howard Hinnant1694d232011-05-28 14:41:13 +00005130weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005131{
5132 weak_ptr().swap(*this);
5133}
5134
5135template<class _Tp>
5136template<class _Yp>
5137shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5138 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
5139 : __ptr_(__r.__ptr_),
5140 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5141{
5142 if (__cntrl_ == 0)
5143#ifndef _LIBCPP_NO_EXCEPTIONS
5144 throw bad_weak_ptr();
5145#else
5146 assert(!"bad_weak_ptr");
5147#endif
5148}
5149
5150template<class _Tp>
5151shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00005152weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005153{
5154 shared_ptr<_Tp> __r;
5155 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5156 if (__r.__cntrl_)
5157 __r.__ptr_ = __ptr_;
5158 return __r;
5159}
5160
Howard Hinnant324bb032010-08-22 00:02:43 +00005161template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005162
5163template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00005164struct _LIBCPP_VISIBLE owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005165 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00005166{
5167 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005169 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5170 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005172 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5173 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005174 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005175 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5176 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00005177};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005178
5179template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00005180struct _LIBCPP_VISIBLE owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005181 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5182{
Howard Hinnant324bb032010-08-22 00:02:43 +00005183 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005184 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005185 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5186 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005187 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005188 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5189 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005191 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5192 {return __x.owner_before(__y);}
5193};
5194
5195template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00005196class _LIBCPP_VISIBLE enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005197{
5198 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00005199protected:
Howard Hinnant82894812010-09-22 16:48:34 +00005200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005201 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005203 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005204 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005205 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5206 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00005207 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005208 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00005209public:
Howard Hinnant82894812010-09-22 16:48:34 +00005210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005211 shared_ptr<_Tp> shared_from_this()
5212 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00005213 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005214 shared_ptr<_Tp const> shared_from_this() const
5215 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005216
5217 template <class _Up> friend class shared_ptr;
5218};
5219
Howard Hinnant21aefc32010-06-03 16:42:57 +00005220template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00005221struct _LIBCPP_VISIBLE hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00005222{
5223 typedef shared_ptr<_Tp> argument_type;
5224 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005225 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005226 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00005227 {
5228 return hash<_Tp*>()(__ptr.get());
5229 }
5230};
5231
Howard Hinnant99968442011-11-29 18:15:50 +00005232template<class _CharT, class _Traits, class _Yp>
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005233inline _LIBCPP_INLINE_VISIBILITY
5234basic_ostream<_CharT, _Traits>&
Howard Hinnant99968442011-11-29 18:15:50 +00005235operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005236
Howard Hinnant324bb032010-08-22 00:02:43 +00005237//enum class
Howard Hinnant82894812010-09-22 16:48:34 +00005238struct _LIBCPP_VISIBLE pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005239{
5240 enum _
5241 {
5242 relaxed,
5243 preferred,
5244 strict
5245 };
5246
5247 _ __v_;
5248
Howard Hinnant82894812010-09-22 16:48:34 +00005249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005250 pointer_safety(_ __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00005251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005252 operator int() const {return __v_;}
5253};
5254
5255void declare_reachable(void* __p);
5256void declare_no_pointers(char* __p, size_t __n);
5257void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnant1694d232011-05-28 14:41:13 +00005258pointer_safety get_pointer_safety() _NOEXCEPT;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00005259void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005260
5261template <class _Tp>
5262inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005263_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005264undeclare_reachable(_Tp* __p)
5265{
5266 return static_cast<_Tp*>(__undeclare_reachable(__p));
5267}
5268
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00005269void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005270
5271_LIBCPP_END_NAMESPACE_STD
5272
5273#endif // _LIBCPP_MEMORY