blob: e3dd467b30fa4aa0a81c80c097900353de83a616 [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 Hinnantbc8d3f92010-05-11 19:42:16 +00001688 template <class _A0>
1689 _LIBCPP_INLINE_VISIBILITY
1690 typename enable_if
1691 <
1692 !is_convertible<_A0, __rv<_A0> >::value,
1693 void
1694 >::type
1695 construct(pointer __p, _A0& __a0)
1696 {
1697 ::new((void*)__p) _Tp(__a0);
1698 }
1699 template <class _A0>
1700 _LIBCPP_INLINE_VISIBILITY
1701 typename enable_if
1702 <
1703 !is_convertible<_A0, __rv<_A0> >::value,
1704 void
1705 >::type
1706 construct(pointer __p, const _A0& __a0)
1707 {
1708 ::new((void*)__p) _Tp(__a0);
1709 }
1710 template <class _A0>
1711 _LIBCPP_INLINE_VISIBILITY
1712 typename enable_if
1713 <
1714 is_convertible<_A0, __rv<_A0> >::value,
1715 void
1716 >::type
1717 construct(pointer __p, _A0 __a0)
1718 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001719 ::new((void*)__p) _Tp(_VSTD::move(__a0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001720 }
Michael J. Spencer626916f2010-12-10 19:47:54 +00001721# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001722 template <class _A0, class _A1>
1723 _LIBCPP_INLINE_VISIBILITY
1724 void
1725 construct(pointer __p, _A0& __a0, _A1& __a1)
1726 {
1727 ::new((void*)__p) _Tp(__a0, __a1);
1728 }
1729 template <class _A0, class _A1>
1730 _LIBCPP_INLINE_VISIBILITY
1731 void
1732 construct(pointer __p, const _A0& __a0, _A1& __a1)
1733 {
1734 ::new((void*)__p) _Tp(__a0, __a1);
1735 }
1736 template <class _A0, class _A1>
1737 _LIBCPP_INLINE_VISIBILITY
1738 void
1739 construct(pointer __p, _A0& __a0, const _A1& __a1)
1740 {
1741 ::new((void*)__p) _Tp(__a0, __a1);
1742 }
1743 template <class _A0, class _A1>
1744 _LIBCPP_INLINE_VISIBILITY
1745 void
1746 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1747 {
1748 ::new((void*)__p) _Tp(__a0, __a1);
1749 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001750#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001751 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1752};
1753
Howard Hinnant57199402012-01-02 17:56:02 +00001754template <class _Tp>
1755class _LIBCPP_VISIBLE allocator<const _Tp>
1756{
1757public:
1758 typedef size_t size_type;
1759 typedef ptrdiff_t difference_type;
1760 typedef const _Tp* pointer;
1761 typedef const _Tp* const_pointer;
1762 typedef const _Tp& reference;
1763 typedef const _Tp& const_reference;
1764 typedef _Tp value_type;
1765
1766 typedef true_type propagate_on_container_move_assignment;
1767
1768 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1769
1770 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1771 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1772 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1773 {return _VSTD::addressof(__x);}
1774 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
1775 {return static_cast<pointer>(::operator new(__n * sizeof(_Tp)));}
1776 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
1777 {::operator delete((void*)__p);}
1778 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1779 {return size_type(~0) / sizeof(_Tp);}
1780#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1781 template <class _Up, class... _Args>
1782 _LIBCPP_INLINE_VISIBILITY
1783 void
1784 construct(_Up* __p, _Args&&... __args)
1785 {
1786 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1787 }
1788#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1789 _LIBCPP_INLINE_VISIBILITY
1790 void
1791 construct(pointer __p)
1792 {
1793 ::new((void*)__p) _Tp();
1794 }
1795# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1796 template <class _A0>
1797 _LIBCPP_INLINE_VISIBILITY
1798 typename enable_if
1799 <
1800 !is_convertible<_A0, __rv<_A0> >::value,
1801 void
1802 >::type
1803 construct(pointer __p, _A0& __a0)
1804 {
1805 ::new((void*)__p) _Tp(__a0);
1806 }
1807 template <class _A0>
1808 _LIBCPP_INLINE_VISIBILITY
1809 typename enable_if
1810 <
1811 !is_convertible<_A0, __rv<_A0> >::value,
1812 void
1813 >::type
1814 construct(pointer __p, const _A0& __a0)
1815 {
1816 ::new((void*)__p) _Tp(__a0);
1817 }
1818 template <class _A0>
1819 _LIBCPP_INLINE_VISIBILITY
1820 typename enable_if
1821 <
1822 is_convertible<_A0, __rv<_A0> >::value,
1823 void
1824 >::type
1825 construct(pointer __p, _A0 __a0)
1826 {
1827 ::new((void*)__p) _Tp(_VSTD::move(__a0));
1828 }
1829# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1830 template <class _A0, class _A1>
1831 _LIBCPP_INLINE_VISIBILITY
1832 void
1833 construct(pointer __p, _A0& __a0, _A1& __a1)
1834 {
1835 ::new((void*)__p) _Tp(__a0, __a1);
1836 }
1837 template <class _A0, class _A1>
1838 _LIBCPP_INLINE_VISIBILITY
1839 void
1840 construct(pointer __p, const _A0& __a0, _A1& __a1)
1841 {
1842 ::new((void*)__p) _Tp(__a0, __a1);
1843 }
1844 template <class _A0, class _A1>
1845 _LIBCPP_INLINE_VISIBILITY
1846 void
1847 construct(pointer __p, _A0& __a0, const _A1& __a1)
1848 {
1849 ::new((void*)__p) _Tp(__a0, __a1);
1850 }
1851 template <class _A0, class _A1>
1852 _LIBCPP_INLINE_VISIBILITY
1853 void
1854 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1855 {
1856 ::new((void*)__p) _Tp(__a0, __a1);
1857 }
1858#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1859 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1860};
1861
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001862template <class _Tp, class _Up>
1863inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001864bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001865
1866template <class _Tp, class _Up>
1867inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001868bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001869
1870template <class _OutputIterator, class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001871class _LIBCPP_VISIBLE raw_storage_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001872 : public iterator<output_iterator_tag,
1873 _Tp, // purposefully not C++03
1874 ptrdiff_t, // purposefully not C++03
1875 _Tp*, // purposefully not C++03
1876 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1877{
1878private:
1879 _OutputIterator __x_;
1880public:
1881 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1882 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1883 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1884 {::new(&*__x_) _Tp(__element); return *this;}
1885 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1886 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1887 {raw_storage_iterator __t(*this); ++__x_; return __t;}
1888};
1889
1890template <class _Tp>
1891pair<_Tp*, ptrdiff_t>
Howard Hinnant1694d232011-05-28 14:41:13 +00001892get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001893{
1894 pair<_Tp*, ptrdiff_t> __r(0, 0);
1895 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1896 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1897 / sizeof(_Tp);
1898 if (__n > __m)
1899 __n = __m;
1900 while (__n > 0)
1901 {
1902 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1903 if (__r.first)
1904 {
1905 __r.second = __n;
1906 break;
1907 }
1908 __n /= 2;
1909 }
1910 return __r;
1911}
1912
1913template <class _Tp>
1914inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00001915void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001916
1917template <class _Tp>
1918struct auto_ptr_ref
1919{
1920 _Tp* __ptr_;
1921};
1922
1923template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00001924class _LIBCPP_VISIBLE auto_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001925{
1926private:
1927 _Tp* __ptr_;
1928public:
1929 typedef _Tp element_type;
1930
1931 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
1932 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
1933 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
1934 : __ptr_(__p.release()) {}
1935 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
1936 {reset(__p.release()); return *this;}
1937 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
1938 {reset(__p.release()); return *this;}
1939 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
1940 {reset(__p.__ptr_); return *this;}
1941 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
1942
1943 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
1944 {return *__ptr_;}
1945 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
1946 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
1947 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
1948 {
1949 _Tp* __t = __ptr_;
1950 __ptr_ = 0;
1951 return __t;
1952 }
1953 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
1954 {
1955 if (__ptr_ != __p)
1956 delete __ptr_;
1957 __ptr_ = __p;
1958 }
1959
1960 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
1961 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
1962 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
1963 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
1964 {return auto_ptr<_Up>(release());}
1965};
1966
1967template <>
Howard Hinnant82894812010-09-22 16:48:34 +00001968class _LIBCPP_VISIBLE auto_ptr<void>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001969{
1970public:
1971 typedef void element_type;
1972};
1973
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001974template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
1975 typename remove_cv<_T2>::type>::value,
Howard Hinnantd4cf2152011-12-11 20:31:33 +00001976 bool = is_empty<_T1>::value
1977#if __has_feature(is_final)
1978 && !__is_final(_T1)
1979#endif
1980 ,
1981 bool = is_empty<_T2>::value
1982#if __has_feature(is_final)
1983 && !__is_final(_T2)
1984#endif
1985 >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001986struct __libcpp_compressed_pair_switch;
1987
1988template <class _T1, class _T2, bool IsSame>
1989struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
1990
1991template <class _T1, class _T2, bool IsSame>
1992struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
1993
1994template <class _T1, class _T2, bool IsSame>
1995struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
1996
1997template <class _T1, class _T2>
1998struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
1999
2000template <class _T1, class _T2>
2001struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
2002
2003template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
2004class __libcpp_compressed_pair_imp;
2005
2006template <class _T1, class _T2>
2007class __libcpp_compressed_pair_imp<_T1, _T2, 0>
2008{
2009private:
2010 _T1 __first_;
2011 _T2 __second_;
2012public:
2013 typedef _T1 _T1_param;
2014 typedef _T2 _T2_param;
2015
2016 typedef typename remove_reference<_T1>::type& _T1_reference;
2017 typedef typename remove_reference<_T2>::type& _T2_reference;
2018
2019 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2020 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2021
2022 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002023 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002024 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002025 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002026 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002027 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002028 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002029
Howard Hinnant61aa6012011-07-01 19:24:36 +00002030#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2031
2032 _LIBCPP_INLINE_VISIBILITY
2033 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2034 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2035 is_nothrow_copy_constructible<_T2>::value)
2036 : __first_(__p.first()),
2037 __second_(__p.second()) {}
2038
2039 _LIBCPP_INLINE_VISIBILITY
2040 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2041 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2042 is_nothrow_copy_assignable<_T2>::value)
2043 {
2044 __first_ = __p.first();
2045 __second_ = __p.second();
2046 return *this;
2047 }
2048
Howard Hinnant73d21a42010-09-04 23:28:19 +00002049#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002050
2051 _LIBCPP_INLINE_VISIBILITY
2052 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002053 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2054 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002055 : __first_(_VSTD::forward<_T1>(__p.first())),
2056 __second_(_VSTD::forward<_T2>(__p.second())) {}
2057
2058 _LIBCPP_INLINE_VISIBILITY
2059 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2060 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2061 is_nothrow_move_assignable<_T2>::value)
2062 {
2063 __first_ = _VSTD::forward<_T1>(__p.first());
2064 __second_ = _VSTD::forward<_T2>(__p.second());
2065 return *this;
2066 }
2067
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002068#ifndef _LIBCPP_HAS_NO_VARIADICS
2069
2070 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2071 _LIBCPP_INLINE_VISIBILITY
2072 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2073 tuple<_Args1...> __first_args,
2074 tuple<_Args2...> __second_args,
2075 __tuple_indices<_I1...>,
2076 __tuple_indices<_I2...>)
2077 : __first_(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
2078 __second_(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
2079 {}
2080
2081#endif // _LIBCPP_HAS_NO_VARIADICS
2082
Howard Hinnant73d21a42010-09-04 23:28:19 +00002083#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002084
Howard Hinnant61aa6012011-07-01 19:24:36 +00002085#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2086
Howard Hinnant1694d232011-05-28 14:41:13 +00002087 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2088 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002089
Howard Hinnant1694d232011-05-28 14:41:13 +00002090 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2091 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002092
2093 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002094 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2095 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002096 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002097 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002098 swap(__first_, __x.__first_);
2099 swap(__second_, __x.__second_);
2100 }
2101};
2102
2103template <class _T1, class _T2>
2104class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2105 : private _T1
2106{
2107private:
2108 _T2 __second_;
2109public:
2110 typedef _T1 _T1_param;
2111 typedef _T2 _T2_param;
2112
2113 typedef _T1& _T1_reference;
2114 typedef typename remove_reference<_T2>::type& _T2_reference;
2115
2116 typedef const _T1& _T1_const_reference;
2117 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2118
2119 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002120 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002121 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002122 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002123 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002124 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002125 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002126
Howard Hinnant61aa6012011-07-01 19:24:36 +00002127#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2128
2129 _LIBCPP_INLINE_VISIBILITY
2130 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2131 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2132 is_nothrow_copy_constructible<_T2>::value)
2133 : _T1(__p.first()), __second_(__p.second()) {}
2134
2135 _LIBCPP_INLINE_VISIBILITY
2136 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2137 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2138 is_nothrow_copy_assignable<_T2>::value)
2139 {
2140 _T1::operator=(__p.first());
2141 __second_ = __p.second();
2142 return *this;
2143 }
2144
Howard Hinnant73d21a42010-09-04 23:28:19 +00002145#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002146
2147 _LIBCPP_INLINE_VISIBILITY
2148 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002149 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2150 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002151 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002152
2153 _LIBCPP_INLINE_VISIBILITY
2154 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2155 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2156 is_nothrow_move_assignable<_T2>::value)
2157 {
2158 _T1::operator=(_VSTD::move(__p.first()));
2159 __second_ = _VSTD::forward<_T2>(__p.second());
2160 return *this;
2161 }
2162
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002163#ifndef _LIBCPP_HAS_NO_VARIADICS
2164
2165 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2166 _LIBCPP_INLINE_VISIBILITY
2167 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2168 tuple<_Args1...> __first_args,
2169 tuple<_Args2...> __second_args,
2170 __tuple_indices<_I1...>,
2171 __tuple_indices<_I2...>)
2172 : _T1(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
2173 __second_(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
2174 {}
2175
2176#endif // _LIBCPP_HAS_NO_VARIADICS
2177
Howard Hinnant73d21a42010-09-04 23:28:19 +00002178#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002179
Howard Hinnant61aa6012011-07-01 19:24:36 +00002180#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2181
Howard Hinnant1694d232011-05-28 14:41:13 +00002182 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2183 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002184
Howard Hinnant1694d232011-05-28 14:41:13 +00002185 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2186 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002187
2188 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002189 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2190 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002191 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002192 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002193 swap(__second_, __x.__second_);
2194 }
2195};
2196
2197template <class _T1, class _T2>
2198class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2199 : private _T2
2200{
2201private:
2202 _T1 __first_;
2203public:
2204 typedef _T1 _T1_param;
2205 typedef _T2 _T2_param;
2206
2207 typedef typename remove_reference<_T1>::type& _T1_reference;
2208 typedef _T2& _T2_reference;
2209
2210 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2211 typedef const _T2& _T2_const_reference;
2212
2213 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2214 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002215 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002216 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002217 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002218 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant1694d232011-05-28 14:41:13 +00002219 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2220 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002221 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002222
Howard Hinnant61aa6012011-07-01 19:24:36 +00002223#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2224
2225 _LIBCPP_INLINE_VISIBILITY
2226 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2227 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2228 is_nothrow_copy_constructible<_T2>::value)
2229 : _T2(__p.second()), __first_(__p.first()) {}
2230
2231 _LIBCPP_INLINE_VISIBILITY
2232 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2233 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2234 is_nothrow_copy_assignable<_T2>::value)
2235 {
2236 _T2::operator=(__p.second());
2237 __first_ = __p.first();
2238 return *this;
2239 }
2240
Howard Hinnant73d21a42010-09-04 23:28:19 +00002241#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002242
2243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002244 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant61aa6012011-07-01 19:24:36 +00002245 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2246 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002247 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002248
2249 _LIBCPP_INLINE_VISIBILITY
2250 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2251 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2252 is_nothrow_move_assignable<_T2>::value)
2253 {
2254 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2255 __first_ = _VSTD::move(__p.first());
2256 return *this;
2257 }
2258
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002259#ifndef _LIBCPP_HAS_NO_VARIADICS
2260
2261 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2262 _LIBCPP_INLINE_VISIBILITY
2263 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2264 tuple<_Args1...> __first_args,
2265 tuple<_Args2...> __second_args,
2266 __tuple_indices<_I1...>,
2267 __tuple_indices<_I2...>)
2268 : _T2(_VSTD::forward<_Args2>(get<_I2>(__second_args))...),
2269 __first_(_VSTD::forward<_Args1>(get<_I1>(__first_args))...)
2270
2271 {}
2272
2273#endif // _LIBCPP_HAS_NO_VARIADICS
2274
Howard Hinnant73d21a42010-09-04 23:28:19 +00002275#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002276
Howard Hinnant61aa6012011-07-01 19:24:36 +00002277#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2278
Howard Hinnant1694d232011-05-28 14:41:13 +00002279 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2280 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002281
Howard Hinnant1694d232011-05-28 14:41:13 +00002282 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2283 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002284
2285 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant1694d232011-05-28 14:41:13 +00002286 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2287 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002288 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002289 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002290 swap(__first_, __x.__first_);
2291 }
2292};
2293
2294template <class _T1, class _T2>
2295class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2296 : private _T1,
2297 private _T2
2298{
2299public:
2300 typedef _T1 _T1_param;
2301 typedef _T2 _T2_param;
2302
2303 typedef _T1& _T1_reference;
2304 typedef _T2& _T2_reference;
2305
2306 typedef const _T1& _T1_const_reference;
2307 typedef const _T2& _T2_const_reference;
2308
2309 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2310 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002311 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002312 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002313 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002314 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002315 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002316
Howard Hinnant61aa6012011-07-01 19:24:36 +00002317#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2318
2319 _LIBCPP_INLINE_VISIBILITY
2320 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2321 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2322 is_nothrow_copy_constructible<_T2>::value)
2323 : _T1(__p.first()), _T2(__p.second()) {}
2324
2325 _LIBCPP_INLINE_VISIBILITY
2326 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2327 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2328 is_nothrow_copy_assignable<_T2>::value)
2329 {
2330 _T1::operator=(__p.first());
2331 _T2::operator=(__p.second());
2332 return *this;
2333 }
2334
Howard Hinnant73d21a42010-09-04 23:28:19 +00002335#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002336
2337 _LIBCPP_INLINE_VISIBILITY
2338 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002339 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2340 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002341 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002342
2343 _LIBCPP_INLINE_VISIBILITY
2344 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2345 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2346 is_nothrow_move_assignable<_T2>::value)
2347 {
2348 _T1::operator=(_VSTD::move(__p.first()));
2349 _T2::operator=(_VSTD::move(__p.second()));
2350 return *this;
2351 }
2352
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002353#ifndef _LIBCPP_HAS_NO_VARIADICS
2354
2355 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2356 _LIBCPP_INLINE_VISIBILITY
2357 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2358 tuple<_Args1...> __first_args,
2359 tuple<_Args2...> __second_args,
2360 __tuple_indices<_I1...>,
2361 __tuple_indices<_I2...>)
2362 : _T1(_VSTD::forward<_Args1>(get<_I1>(__first_args))...),
2363 _T2(_VSTD::forward<_Args2>(get<_I2>(__second_args))...)
2364 {}
2365
2366#endif // _LIBCPP_HAS_NO_VARIADICS
2367
Howard Hinnant73d21a42010-09-04 23:28:19 +00002368#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002369
Howard Hinnant61aa6012011-07-01 19:24:36 +00002370#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2371
Howard Hinnant1694d232011-05-28 14:41:13 +00002372 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2373 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002374
Howard Hinnant1694d232011-05-28 14:41:13 +00002375 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2376 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002377
Howard Hinnantec3773c2011-12-01 20:21:04 +00002378 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant1694d232011-05-28 14:41:13 +00002379 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2380 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002381 {
2382 }
2383};
2384
2385template <class _T1, class _T2>
2386class __compressed_pair
2387 : private __libcpp_compressed_pair_imp<_T1, _T2>
2388{
2389 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2390public:
2391 typedef typename base::_T1_param _T1_param;
2392 typedef typename base::_T2_param _T2_param;
2393
2394 typedef typename base::_T1_reference _T1_reference;
2395 typedef typename base::_T2_reference _T2_reference;
2396
2397 typedef typename base::_T1_const_reference _T1_const_reference;
2398 typedef typename base::_T2_const_reference _T2_const_reference;
2399
2400 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant57199402012-01-02 17:56:02 +00002401 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002402 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant57199402012-01-02 17:56:02 +00002403 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002404 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002405 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002406 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002407
Howard Hinnant61aa6012011-07-01 19:24:36 +00002408#ifdef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2409
2410 _LIBCPP_INLINE_VISIBILITY
2411 __compressed_pair(const __compressed_pair& __p)
2412 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2413 is_nothrow_copy_constructible<_T2>::value)
2414 : base(__p) {}
2415
2416 _LIBCPP_INLINE_VISIBILITY
2417 __compressed_pair& operator=(const __compressed_pair& __p)
2418 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2419 is_nothrow_copy_assignable<_T2>::value)
2420 {
2421 base::operator=(__p);
2422 return *this;
2423 }
2424
Howard Hinnant73d21a42010-09-04 23:28:19 +00002425#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant61aa6012011-07-01 19:24:36 +00002426 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002427 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant1694d232011-05-28 14:41:13 +00002428 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2429 is_nothrow_move_constructible<_T2>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002430 : base(_VSTD::move(__p)) {}
Howard Hinnant61aa6012011-07-01 19:24:36 +00002431
2432 _LIBCPP_INLINE_VISIBILITY
2433 __compressed_pair& operator=(__compressed_pair&& __p)
2434 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2435 is_nothrow_move_assignable<_T2>::value)
2436 {
2437 base::operator=(_VSTD::move(__p));
2438 return *this;
2439 }
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002440
2441#ifndef _LIBCPP_HAS_NO_VARIADICS
2442
2443 template <class... _Args1, class... _Args2>
2444 _LIBCPP_INLINE_VISIBILITY
2445 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2446 tuple<_Args2...> __second_args)
Howard Hinnant57199402012-01-02 17:56:02 +00002447 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00002448 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2449 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2450 {}
2451
2452#endif // _LIBCPP_HAS_NO_VARIADICS
2453
Howard Hinnant73d21a42010-09-04 23:28:19 +00002454#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002455
Howard Hinnant61aa6012011-07-01 19:24:36 +00002456#endif // _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2457
Howard Hinnant1694d232011-05-28 14:41:13 +00002458 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2459 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002460
Howard Hinnant1694d232011-05-28 14:41:13 +00002461 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2462 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002463
Howard Hinnant1694d232011-05-28 14:41:13 +00002464 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2465 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2466 __is_nothrow_swappable<_T1>::value)
2467 {base::swap(__x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002468};
2469
2470template <class _T1, class _T2>
2471inline _LIBCPP_INLINE_VISIBILITY
2472void
2473swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant1694d232011-05-28 14:41:13 +00002474 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2475 __is_nothrow_swappable<_T1>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002476 {__x.swap(__y);}
2477
Howard Hinnant57199402012-01-02 17:56:02 +00002478// __same_or_less_cv_qualified
2479
2480template <class _Ptr1, class _Ptr2,
2481 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2482 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2483 >::value
2484 >
2485struct __same_or_less_cv_qualified_imp
2486 : is_convertible<_Ptr1, _Ptr2> {};
2487
2488template <class _Ptr1, class _Ptr2>
2489struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2490 : false_type {};
2491
2492template <class _Ptr1, class _Ptr2, bool = is_scalar<_Ptr1>::value &&
2493 !is_pointer<_Ptr1>::value>
2494struct __same_or_less_cv_qualified
2495 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2496
2497template <class _Ptr1, class _Ptr2>
2498struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, true>
2499 : false_type {};
2500
2501// default_delete
2502
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002503template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002504struct _LIBCPP_VISIBLE default_delete
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002505{
Howard Hinnant1694d232011-05-28 14:41:13 +00002506 _LIBCPP_INLINE_VISIBILITY default_delete() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002507 template <class _Up>
2508 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant1694d232011-05-28 14:41:13 +00002509 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2510 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002511 {
2512 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2513 delete __ptr;
2514 }
2515};
2516
2517template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00002518struct _LIBCPP_VISIBLE default_delete<_Tp[]>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002519{
Howard Hinnant8e843502011-12-18 21:19:44 +00002520public:
2521 _LIBCPP_INLINE_VISIBILITY default_delete() _NOEXCEPT {}
2522 template <class _Up>
2523 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant57199402012-01-02 17:56:02 +00002524 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant8e843502011-12-18 21:19:44 +00002525 template <class _Up>
2526 _LIBCPP_INLINE_VISIBILITY
2527 void operator() (_Up* __ptr,
Howard Hinnant57199402012-01-02 17:56:02 +00002528 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002529 {
2530 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
2531 delete [] __ptr;
2532 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002533};
2534
2535template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnant82894812010-09-22 16:48:34 +00002536class _LIBCPP_VISIBLE unique_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002537{
2538public:
2539 typedef _Tp element_type;
2540 typedef _Dp deleter_type;
2541 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2542private:
2543 __compressed_pair<pointer, deleter_type> __ptr_;
2544
Howard Hinnant57199402012-01-02 17:56:02 +00002545#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002546 unique_ptr(unique_ptr&);
2547 template <class _Up, class _Ep>
2548 unique_ptr(unique_ptr<_Up, _Ep>&);
2549 unique_ptr& operator=(unique_ptr&);
2550 template <class _Up, class _Ep>
2551 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002552#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002553
2554 struct __nat {int __for_bool_;};
2555
2556 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2557 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2558public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002559 _LIBCPP_INLINE_VISIBILITY unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002560 : __ptr_(pointer())
2561 {
2562 static_assert(!is_pointer<deleter_type>::value,
2563 "unique_ptr constructed with null function pointer deleter");
2564 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002565 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002566 : __ptr_(pointer())
2567 {
2568 static_assert(!is_pointer<deleter_type>::value,
2569 "unique_ptr constructed with null function pointer deleter");
2570 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002571 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002572 : __ptr_(_VSTD::move(__p))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002573 {
2574 static_assert(!is_pointer<deleter_type>::value,
2575 "unique_ptr constructed with null function pointer deleter");
2576 }
2577
Howard Hinnant73d21a42010-09-04 23:28:19 +00002578#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002579 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2580 is_reference<deleter_type>::value,
2581 deleter_type,
2582 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002583 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002584 : __ptr_(__p, __d) {}
2585
2586 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002587 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002588 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002589 {
2590 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2591 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002592 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002593 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002594 template <class _Up, class _Ep>
2595 _LIBCPP_INLINE_VISIBILITY
2596 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2597 typename enable_if
2598 <
2599 !is_array<_Up>::value &&
2600 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2601 is_convertible<_Ep, deleter_type>::value &&
2602 (
2603 !is_reference<deleter_type>::value ||
2604 is_same<deleter_type, _Ep>::value
2605 ),
2606 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002607 >::type = __nat()) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002608 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002609
2610 template <class _Up>
2611 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2612 typename enable_if<
2613 is_convertible<_Up*, _Tp*>::value &&
2614 is_same<_Dp, default_delete<_Tp> >::value,
2615 __nat
Howard Hinnant1694d232011-05-28 14:41:13 +00002616 >::type = __nat()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002617 : __ptr_(__p.release())
2618 {
2619 }
2620
Howard Hinnant1694d232011-05-28 14:41:13 +00002621 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002622 {
2623 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002624 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002625 return *this;
2626 }
2627
2628 template <class _Up, class _Ep>
2629 _LIBCPP_INLINE_VISIBILITY
2630 typename enable_if
2631 <
Howard Hinnant57199402012-01-02 17:56:02 +00002632 !is_array<_Up>::value &&
2633 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2634 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002635 unique_ptr&
2636 >::type
Howard Hinnant1694d232011-05-28 14:41:13 +00002637 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002638 {
2639 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002640 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002641 return *this;
2642 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002643#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002644
2645 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2646 {
2647 return __rv<unique_ptr>(*this);
2648 }
2649
2650 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002651 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002652
2653 template <class _Up, class _Ep>
2654 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr<_Up, _Ep> __u)
2655 {
2656 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002657 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002658 return *this;
2659 }
2660
2661 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002662 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002663
2664 template <class _Up>
2665 _LIBCPP_INLINE_VISIBILITY
2666 typename enable_if<
2667 is_convertible<_Up*, _Tp*>::value &&
2668 is_same<_Dp, default_delete<_Tp> >::value,
2669 unique_ptr&
2670 >::type
2671 operator=(auto_ptr<_Up> __p)
2672 {reset(__p.release()); return *this;}
2673
Howard Hinnant73d21a42010-09-04 23:28:19 +00002674#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002675 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2676
Howard Hinnant1694d232011-05-28 14:41:13 +00002677 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002678 {
2679 reset();
2680 return *this;
2681 }
2682
2683 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2684 {return *__ptr_.first();}
Howard Hinnant1694d232011-05-28 14:41:13 +00002685 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2686 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2687 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2688 {return __ptr_.second();}
2689 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2690 {return __ptr_.second();}
2691 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const
2692 _NOEXCEPT
2693 {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002694
Howard Hinnant1694d232011-05-28 14:41:13 +00002695 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002696 {
2697 pointer __t = __ptr_.first();
2698 __ptr_.first() = pointer();
2699 return __t;
2700 }
2701
Howard Hinnant1694d232011-05-28 14:41:13 +00002702 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002703 {
2704 pointer __tmp = __ptr_.first();
2705 __ptr_.first() = __p;
2706 if (__tmp)
2707 __ptr_.second()(__tmp);
2708 }
2709
Howard Hinnant1694d232011-05-28 14:41:13 +00002710 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2711 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002712};
2713
2714template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00002715class _LIBCPP_VISIBLE unique_ptr<_Tp[], _Dp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002716{
2717public:
2718 typedef _Tp element_type;
2719 typedef _Dp deleter_type;
2720 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2721private:
2722 __compressed_pair<pointer, deleter_type> __ptr_;
2723
Howard Hinnant57199402012-01-02 17:56:02 +00002724#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002725 unique_ptr(unique_ptr&);
2726 template <class _Up>
2727 unique_ptr(unique_ptr<_Up>&);
2728 unique_ptr& operator=(unique_ptr&);
2729 template <class _Up>
2730 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant73d21a42010-09-04 23:28:19 +00002731#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002732
2733 struct __nat {int __for_bool_;};
2734
2735 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2736 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2737public:
Howard Hinnant1694d232011-05-28 14:41:13 +00002738 _LIBCPP_INLINE_VISIBILITY unique_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002739 : __ptr_(pointer())
2740 {
2741 static_assert(!is_pointer<deleter_type>::value,
2742 "unique_ptr constructed with null function pointer deleter");
2743 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002744 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002745 : __ptr_(pointer())
2746 {
2747 static_assert(!is_pointer<deleter_type>::value,
2748 "unique_ptr constructed with null function pointer deleter");
2749 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002750#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002751 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002752 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002753 >
Howard Hinnant99968442011-11-29 18:15:50 +00002754 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002755 : __ptr_(__p)
2756 {
2757 static_assert(!is_pointer<deleter_type>::value,
2758 "unique_ptr constructed with null function pointer deleter");
2759 }
2760
Howard Hinnant99968442011-11-29 18:15:50 +00002761 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002762 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002763 >
Howard Hinnant99968442011-11-29 18:15:50 +00002764 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002765 is_reference<deleter_type>::value,
2766 deleter_type,
2767 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002768 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002769 : __ptr_(__p, __d) {}
2770
2771 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2772 is_reference<deleter_type>::value,
2773 deleter_type,
2774 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002775 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002776 : __ptr_(pointer(), __d) {}
2777
Howard Hinnant99968442011-11-29 18:15:50 +00002778 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002779 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002780 >
Howard Hinnant99968442011-11-29 18:15:50 +00002781 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002782 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002783 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002784 {
2785 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2786 }
2787
2788 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant1694d232011-05-28 14:41:13 +00002789 _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002790 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002791 {
2792 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2793 }
2794
Howard Hinnant1694d232011-05-28 14:41:13 +00002795 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:19 +00002796 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002797
Howard Hinnant1694d232011-05-28 14:41:13 +00002798 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002799 {
2800 reset(__u.release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002801 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002802 return *this;
2803 }
Howard Hinnant8e843502011-12-18 21:19:44 +00002804
2805 template <class _Up, class _Ep>
2806 _LIBCPP_INLINE_VISIBILITY
2807 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2808 typename enable_if
2809 <
2810 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002811 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant8e843502011-12-18 21:19:44 +00002812 && is_convertible<_Ep, deleter_type>::value &&
2813 (
2814 !is_reference<deleter_type>::value ||
2815 is_same<deleter_type, _Ep>::value
2816 ),
2817 __nat
2818 >::type = __nat()
2819 ) _NOEXCEPT
2820 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2821
2822
2823 template <class _Up, class _Ep>
2824 _LIBCPP_INLINE_VISIBILITY
2825 typename enable_if
2826 <
2827 is_array<_Up>::value &&
Howard Hinnant57199402012-01-02 17:56:02 +00002828 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2829 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant8e843502011-12-18 21:19:44 +00002830 unique_ptr&
2831 >::type
2832 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2833 {
2834 reset(__u.release());
2835 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2836 return *this;
2837 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002838#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002839
2840 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2841 : __ptr_(__p)
2842 {
2843 static_assert(!is_pointer<deleter_type>::value,
2844 "unique_ptr constructed with null function pointer deleter");
2845 }
2846
2847 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002848 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002849
2850 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002851 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002852
2853 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2854 {
2855 return __rv<unique_ptr>(*this);
2856 }
2857
2858 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002859 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002860
2861 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2862 {
2863 reset(__u->release());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002864 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002865 return *this;
2866 }
2867
Howard Hinnant73d21a42010-09-04 23:28:19 +00002868#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002869 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2870
Howard Hinnant1694d232011-05-28 14:41:13 +00002871 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002872 {
2873 reset();
2874 return *this;
2875 }
2876
2877 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2878 {return __ptr_.first()[__i];}
Howard Hinnant1694d232011-05-28 14:41:13 +00002879 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2880 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2881 {return __ptr_.second();}
2882 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2883 {return __ptr_.second();}
2884 _LIBCPP_INLINE_VISIBILITY operator int __nat::*() const _NOEXCEPT
2885 {return __ptr_.first() ? &__nat::__for_bool_ : 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002886
Howard Hinnant1694d232011-05-28 14:41:13 +00002887 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002888 {
2889 pointer __t = __ptr_.first();
2890 __ptr_.first() = pointer();
2891 return __t;
2892 }
2893
Howard Hinnant73d21a42010-09-04 23:28:19 +00002894#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50 +00002895 template <class _Pp,
Howard Hinnant57199402012-01-02 17:56:02 +00002896 class = typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002897 >
Howard Hinnant99968442011-11-29 18:15:50 +00002898 _LIBCPP_INLINE_VISIBILITY void reset(_Pp __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002899 {
2900 pointer __tmp = __ptr_.first();
2901 __ptr_.first() = __p;
2902 if (__tmp)
2903 __ptr_.second()(__tmp);
2904 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002905 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002906 {
2907 pointer __tmp = __ptr_.first();
2908 __ptr_.first() = nullptr;
2909 if (__tmp)
2910 __ptr_.second()(__tmp);
2911 }
Howard Hinnant1694d232011-05-28 14:41:13 +00002912 _LIBCPP_INLINE_VISIBILITY void reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002913 {
2914 pointer __tmp = __ptr_.first();
2915 __ptr_.first() = nullptr;
2916 if (__tmp)
2917 __ptr_.second()(__tmp);
2918 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002919#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002920 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer())
2921 {
2922 pointer __tmp = __ptr_.first();
2923 __ptr_.first() = __p;
2924 if (__tmp)
2925 __ptr_.second()(__tmp);
2926 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00002927#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002928
2929 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2930private:
Howard Hinnant324bb032010-08-22 00:02:43 +00002931
Howard Hinnant73d21a42010-09-04 23:28:19 +00002932#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002933 template <class _Up>
2934 explicit unique_ptr(_Up);
2935 template <class _Up>
2936 unique_ptr(_Up __u,
2937 typename conditional<
2938 is_reference<deleter_type>::value,
2939 deleter_type,
2940 typename add_lvalue_reference<const deleter_type>::type>::type,
2941 typename enable_if
2942 <
2943 is_convertible<_Up, pointer>::value,
2944 __nat
2945 >::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00002946#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002947};
2948
2949template <class _Tp, class _Dp>
2950inline _LIBCPP_INLINE_VISIBILITY
2951void
Howard Hinnant1694d232011-05-28 14:41:13 +00002952swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002953
2954template <class _T1, class _D1, class _T2, class _D2>
2955inline _LIBCPP_INLINE_VISIBILITY
2956bool
2957operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2958
2959template <class _T1, class _D1, class _T2, class _D2>
2960inline _LIBCPP_INLINE_VISIBILITY
2961bool
2962operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2963
2964template <class _T1, class _D1, class _T2, class _D2>
2965inline _LIBCPP_INLINE_VISIBILITY
2966bool
Howard Hinnant3fadda32012-02-21 21:02:58 +00002967operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2968{
2969 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2970 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
2971 typedef typename common_type<_P1, _P2>::type _V;
2972 return less<_V>()(__x.get(), __y.get());
2973}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002974
2975template <class _T1, class _D1, class _T2, class _D2>
2976inline _LIBCPP_INLINE_VISIBILITY
2977bool
2978operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2979
2980template <class _T1, class _D1, class _T2, class _D2>
2981inline _LIBCPP_INLINE_VISIBILITY
2982bool
2983operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2984
2985template <class _T1, class _D1, class _T2, class _D2>
2986inline _LIBCPP_INLINE_VISIBILITY
2987bool
2988operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2989
Howard Hinnant3fadda32012-02-21 21:02:58 +00002990template <class _T1, class _D1>
2991inline _LIBCPP_INLINE_VISIBILITY
2992bool
2993operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2994{
2995 return !__x;
2996}
2997
2998template <class _T1, class _D1>
2999inline _LIBCPP_INLINE_VISIBILITY
3000bool
3001operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3002{
3003 return !__x;
3004}
3005
3006template <class _T1, class _D1>
3007inline _LIBCPP_INLINE_VISIBILITY
3008bool
3009operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3010{
3011 return static_cast<bool>(__x);
3012}
3013
3014template <class _T1, class _D1>
3015inline _LIBCPP_INLINE_VISIBILITY
3016bool
3017operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3018{
3019 return static_cast<bool>(__x);
3020}
3021
3022template <class _T1, class _D1>
3023inline _LIBCPP_INLINE_VISIBILITY
3024bool
3025operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3026{
3027 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3028 return less<_P1>()(__x.get(), nullptr);
3029}
3030
3031template <class _T1, class _D1>
3032inline _LIBCPP_INLINE_VISIBILITY
3033bool
3034operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3035{
3036 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3037 return less<_P1>()(nullptr, __x.get());
3038}
3039
3040template <class _T1, class _D1>
3041inline _LIBCPP_INLINE_VISIBILITY
3042bool
3043operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3044{
3045 return nullptr < __x;
3046}
3047
3048template <class _T1, class _D1>
3049inline _LIBCPP_INLINE_VISIBILITY
3050bool
3051operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3052{
3053 return __x < nullptr;
3054}
3055
3056template <class _T1, class _D1>
3057inline _LIBCPP_INLINE_VISIBILITY
3058bool
3059operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3060{
3061 return !(nullptr < __x);
3062}
3063
3064template <class _T1, class _D1>
3065inline _LIBCPP_INLINE_VISIBILITY
3066bool
3067operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3068{
3069 return !(__x < nullptr);
3070}
3071
3072template <class _T1, class _D1>
3073inline _LIBCPP_INLINE_VISIBILITY
3074bool
3075operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3076{
3077 return !(__x < nullptr);
3078}
3079
3080template <class _T1, class _D1>
3081inline _LIBCPP_INLINE_VISIBILITY
3082bool
3083operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3084{
3085 return !(nullptr < __x);
3086}
3087
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00003088template <class _Tp> struct hash;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003089
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003090// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
3091// is 64 bits. This is because cityhash64 uses 64bit x 64bit
3092// multiplication, which can be very slow on 32-bit systems.
Howard Hinnant40c13d32011-12-05 00:08:45 +00003093template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003094struct __murmur2_or_cityhash;
Howard Hinnant40c13d32011-12-05 00:08:45 +00003095
3096template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003097struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003098{
3099 _Size operator()(const void* __key, _Size __len);
3100};
3101
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003102// murmur2
Howard Hinnant40c13d32011-12-05 00:08:45 +00003103template <class _Size>
3104_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003105__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003106{
3107 const _Size __m = 0x5bd1e995;
3108 const _Size __r = 24;
3109 _Size __h = __len;
3110 const unsigned char* __data = static_cast<const unsigned char*>(__key);
3111 for (; __len >= 4; __data += 4, __len -= 4)
3112 {
3113 _Size __k = *(const _Size*)__data;
3114 __k *= __m;
3115 __k ^= __k >> __r;
3116 __k *= __m;
3117 __h *= __m;
3118 __h ^= __k;
3119 }
3120 switch (__len)
3121 {
3122 case 3:
3123 __h ^= __data[2] << 16;
3124 case 2:
3125 __h ^= __data[1] << 8;
3126 case 1:
3127 __h ^= __data[0];
3128 __h *= __m;
3129 }
3130 __h ^= __h >> 13;
3131 __h *= __m;
3132 __h ^= __h >> 15;
3133 return __h;
3134}
3135
3136template <class _Size>
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003137struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnant40c13d32011-12-05 00:08:45 +00003138{
3139 _Size operator()(const void* __key, _Size __len);
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003140
3141 private:
3142 // Some primes between 2^63 and 2^64.
3143 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
3144 static const _Size __k1 = 0xb492b66fbe98f273ULL;
3145 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
3146 static const _Size __k3 = 0xc949d7c7509e6557ULL;
3147
3148 static _Size __rotate(_Size __val, int __shift) {
3149 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
3150 }
3151
3152 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
3153 return (__val >> __shift) | (__val << (64 - __shift));
3154 }
3155
3156 static _Size __shift_mix(_Size __val) {
3157 return __val ^ (__val >> 47);
3158 }
3159
3160 static _Size __hash_len_16(_Size __u, _Size __v) {
3161 const _Size __mul = 0x9ddfea08eb382d69ULL;
3162 _Size __a = (__u ^ __v) * __mul;
3163 __a ^= (__a >> 47);
3164 _Size __b = (__v ^ __a) * __mul;
3165 __b ^= (__b >> 47);
3166 __b *= __mul;
3167 return __b;
3168 }
3169
3170 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
3171 if (__len > 8) {
3172 const _Size __a = *(const _Size*)__s;
3173 const _Size __b = *(const _Size*)(__s + __len - 8);
3174 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
3175 }
3176 if (__len >= 4) {
3177 const uint32_t __a = *(const uint32_t*)(__s);
3178 const uint32_t __b = *(const uint32_t*)(__s + __len - 4);
3179 return __hash_len_16(__len + (__a << 3), __b);
3180 }
3181 if (__len > 0) {
3182 const unsigned char __a = __s[0];
3183 const unsigned char __b = __s[__len >> 1];
3184 const unsigned char __c = __s[__len - 1];
3185 const uint32_t __y = static_cast<uint32_t>(__a) +
3186 (static_cast<uint32_t>(__b) << 8);
3187 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3188 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3189 }
3190 return __k2;
3191 }
3192
3193 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
3194 const _Size __a = *(const _Size*)(__s) * __k1;
3195 const _Size __b = *(const _Size*)(__s + 8);
3196 const _Size __c = *(const _Size*)(__s + __len - 8) * __k2;
3197 const _Size __d = *(const _Size*)(__s + __len - 16) * __k0;
3198 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3199 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3200 }
3201
3202 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3203 // Callers do best to use "random-looking" values for a and b.
3204 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3205 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3206 __a += __w;
3207 __b = __rotate(__b + __a + __z, 21);
3208 const _Size __c = __a;
3209 __a += __x;
3210 __a += __y;
3211 __b += __rotate(__a, 44);
3212 return pair<_Size, _Size>(__a + __z, __b + __c);
3213 }
3214
3215 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3216 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3217 const char* __s, _Size __a, _Size __b) {
3218 return __weak_hash_len_32_with_seeds(*(const _Size*)(__s),
3219 *(const _Size*)(__s + 8),
3220 *(const _Size*)(__s + 16),
3221 *(const _Size*)(__s + 24),
3222 __a,
3223 __b);
3224 }
3225
3226 // Return an 8-byte hash for 33 to 64 bytes.
3227 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
3228 _Size __z = *(const _Size*)(__s + 24);
3229 _Size __a = *(const _Size*)(__s) +
3230 (__len + *(const _Size*)(__s + __len - 16)) * __k0;
3231 _Size __b = __rotate(__a + __z, 52);
3232 _Size __c = __rotate(__a, 37);
3233 __a += *(const _Size*)(__s + 8);
3234 __c += __rotate(__a, 7);
3235 __a += *(const _Size*)(__s + 16);
3236 _Size __vf = __a + __z;
3237 _Size __vs = __b + __rotate(__a, 31) + __c;
3238 __a = *(const _Size*)(__s + 16) + *(const _Size*)(__s + __len - 32);
3239 __z += *(const _Size*)(__s + __len - 8);
3240 __b = __rotate(__a + __z, 52);
3241 __c = __rotate(__a, 37);
3242 __a += *(const _Size*)(__s + __len - 24);
3243 __c += __rotate(__a, 7);
3244 __a += *(const _Size*)(__s + __len - 16);
3245 _Size __wf = __a + __z;
3246 _Size __ws = __b + __rotate(__a, 31) + __c;
3247 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3248 return __shift_mix(__r * __k0 + __vs) * __k2;
3249 }
Howard Hinnant40c13d32011-12-05 00:08:45 +00003250};
3251
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003252// cityhash64
Howard Hinnant40c13d32011-12-05 00:08:45 +00003253template <class _Size>
3254_Size
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003255__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)
Howard Hinnant40c13d32011-12-05 00:08:45 +00003256{
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003257 const char* __s = static_cast<const char*>(__key);
3258 if (__len <= 32) {
3259 if (__len <= 16) {
3260 return __hash_len_0_to_16(__s, __len);
3261 } else {
3262 return __hash_len_17_to_32(__s, __len);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003263 }
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003264 } else if (__len <= 64) {
3265 return __hash_len_33_to_64(__s, __len);
3266 }
3267
3268 // For strings over 64 bytes we hash the end first, and then as we
3269 // loop we keep 56 bytes of state: v, w, x, y, and z.
3270 _Size __x = *(const _Size*)(__s + __len - 40);
3271 _Size __y = *(const _Size*)(__s + __len - 16) +
3272 *(const _Size*)(__s + __len - 56);
3273 _Size __z = __hash_len_16(*(const _Size*)(__s + __len - 48) + __len,
3274 *(const _Size*)(__s + __len - 24));
3275 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3276 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
3277 __x = __x * __k1 + *(const _Size*)(__s);
3278
3279 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3280 __len = (__len - 1) & ~static_cast<_Size>(63);
3281 do {
3282 __x = __rotate(__x + __y + __v.first + *(const _Size*)(__s + 8), 37) * __k1;
3283 __y = __rotate(__y + __v.second + *(const _Size*)(__s + 48), 42) * __k1;
3284 __x ^= __w.second;
3285 __y += __v.first + *(const _Size*)(__s + 40);
3286 __z = __rotate(__z + __w.first, 33) * __k1;
3287 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3288 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
3289 __y + *(const _Size*)(__s + 16));
3290 std::swap(__z, __x);
3291 __s += 64;
3292 __len -= 64;
3293 } while (__len != 0);
3294 return __hash_len_16(
3295 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3296 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnant40c13d32011-12-05 00:08:45 +00003297}
3298
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003299template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3300struct __scalar_hash;
3301
3302template <class _Tp>
3303struct __scalar_hash<_Tp, 0>
3304 : public unary_function<_Tp, size_t>
Howard Hinnant21aefc32010-06-03 16:42:57 +00003305{
Howard Hinnant82894812010-09-22 16:48:34 +00003306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003307 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003308 {
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003309 union
3310 {
3311 _Tp __t;
3312 size_t __a;
3313 } __u;
3314 __u.__a = 0;
3315 __u.__t = __v;
3316 return __u.__a;
Howard Hinnant21aefc32010-06-03 16:42:57 +00003317 }
3318};
3319
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003320template <class _Tp>
3321struct __scalar_hash<_Tp, 1>
3322 : public unary_function<_Tp, size_t>
3323{
3324 _LIBCPP_INLINE_VISIBILITY
3325 size_t operator()(_Tp __v) const _NOEXCEPT
3326 {
3327 union
3328 {
3329 _Tp __t;
3330 size_t __a;
3331 } __u;
3332 __u.__t = __v;
3333 return __u.__a;
3334 }
3335};
3336
3337template <class _Tp>
3338struct __scalar_hash<_Tp, 2>
3339 : public unary_function<_Tp, size_t>
3340{
3341 _LIBCPP_INLINE_VISIBILITY
3342 size_t operator()(_Tp __v) const _NOEXCEPT
3343 {
3344 union
3345 {
3346 _Tp __t;
3347 struct
3348 {
3349 size_t __a;
3350 size_t __b;
3351 };
3352 } __u;
3353 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003354 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003355 }
3356};
3357
3358template <class _Tp>
3359struct __scalar_hash<_Tp, 3>
3360 : public unary_function<_Tp, size_t>
3361{
3362 _LIBCPP_INLINE_VISIBILITY
3363 size_t operator()(_Tp __v) const _NOEXCEPT
3364 {
3365 union
3366 {
3367 _Tp __t;
3368 struct
3369 {
3370 size_t __a;
3371 size_t __b;
3372 size_t __c;
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 __scalar_hash<_Tp, 4>
3382 : public unary_function<_Tp, size_t>
3383{
3384 _LIBCPP_INLINE_VISIBILITY
3385 size_t operator()(_Tp __v) const _NOEXCEPT
3386 {
3387 union
3388 {
3389 _Tp __t;
3390 struct
3391 {
3392 size_t __a;
3393 size_t __b;
3394 size_t __c;
3395 size_t __d;
3396 };
3397 } __u;
3398 __u.__t = __v;
Howard Hinnantc00f75d2011-12-10 20:28:56 +00003399 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnantcf2654b2011-12-03 21:11:36 +00003400 }
3401};
3402
3403template<class _Tp>
3404struct _LIBCPP_VISIBLE hash<_Tp*>
3405 : public __scalar_hash<_Tp*>
3406{
3407};
3408
Howard Hinnant21aefc32010-06-03 16:42:57 +00003409template <class _Tp, class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00003410struct _LIBCPP_VISIBLE hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00003411{
3412 typedef unique_ptr<_Tp, _Dp> argument_type;
3413 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00003414 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003415 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00003416 {
3417 typedef typename argument_type::pointer pointer;
3418 return hash<pointer>()(__ptr.get());
3419 }
3420};
3421
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003422struct __destruct_n
3423{
3424private:
3425 size_t size;
3426
3427 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003428 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003429 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3430
3431 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003432 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003433 {}
3434
Howard Hinnant1694d232011-05-28 14:41:13 +00003435 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003436 {++size;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003437 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003438 {}
3439
Howard Hinnant1694d232011-05-28 14:41:13 +00003440 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003441 {size = __s;}
Howard Hinnant1694d232011-05-28 14:41:13 +00003442 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003443 {}
3444public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003445 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3446 : size(__s) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003447
3448 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003449 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003450 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003451
3452 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003453 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003454 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003455
3456 template <class _Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00003457 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnant1468b662010-11-19 22:17:28 +00003458 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003459};
3460
3461template <class _Alloc>
3462class __allocator_destructor
3463{
3464 typedef allocator_traits<_Alloc> __alloc_traits;
3465public:
3466 typedef typename __alloc_traits::pointer pointer;
3467 typedef typename __alloc_traits::size_type size_type;
3468private:
3469 _Alloc& __alloc_;
3470 size_type __s_;
3471public:
3472 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant1694d232011-05-28 14:41:13 +00003473 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003474 : __alloc_(__a), __s_(__s) {}
Howard Hinnant82894812010-09-22 16:48:34 +00003475 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003476 void operator()(pointer __p) _NOEXCEPT
3477 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003478};
3479
3480template <class _InputIterator, class _ForwardIterator>
3481_ForwardIterator
3482uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3483{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003484 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003485#ifndef _LIBCPP_NO_EXCEPTIONS
3486 _ForwardIterator __s = __r;
3487 try
3488 {
3489#endif
3490 for (; __f != __l; ++__f, ++__r)
3491 ::new(&*__r) value_type(*__f);
3492#ifndef _LIBCPP_NO_EXCEPTIONS
3493 }
3494 catch (...)
3495 {
3496 for (; __s != __r; ++__s)
3497 __s->~value_type();
3498 throw;
3499 }
3500#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003501 return __r;
3502}
3503
3504template <class _InputIterator, class _Size, class _ForwardIterator>
3505_ForwardIterator
3506uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3507{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003508 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003509#ifndef _LIBCPP_NO_EXCEPTIONS
3510 _ForwardIterator __s = __r;
3511 try
3512 {
3513#endif
3514 for (; __n > 0; ++__f, ++__r, --__n)
3515 ::new(&*__r) value_type(*__f);
3516#ifndef _LIBCPP_NO_EXCEPTIONS
3517 }
3518 catch (...)
3519 {
3520 for (; __s != __r; ++__s)
3521 __s->~value_type();
3522 throw;
3523 }
3524#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003525 return __r;
3526}
3527
3528template <class _ForwardIterator, class _Tp>
3529void
3530uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, 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 (; __f != __l; ++__f)
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 Hinnantbc8d3f92010-05-11 19:42:16 +00003549}
3550
3551template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003552_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003553uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3554{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003555 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant8292d742011-12-29 17:45:35 +00003556#ifndef _LIBCPP_NO_EXCEPTIONS
3557 _ForwardIterator __s = __f;
3558 try
3559 {
3560#endif
3561 for (; __n > 0; ++__f, --__n)
3562 ::new(&*__f) value_type(__x);
3563#ifndef _LIBCPP_NO_EXCEPTIONS
3564 }
3565 catch (...)
3566 {
3567 for (; __s != __f; ++__s)
3568 __s->~value_type();
3569 throw;
3570 }
3571#endif
Howard Hinnant2f6a6272010-11-18 16:13:03 +00003572 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003573}
3574
Howard Hinnant82894812010-09-22 16:48:34 +00003575class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003576 : public std::exception
3577{
3578public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003579 virtual ~bad_weak_ptr() _NOEXCEPT;
3580 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003581};
3582
3583template<class _Tp> class weak_ptr;
3584
3585class __shared_count
3586{
3587 __shared_count(const __shared_count&);
3588 __shared_count& operator=(const __shared_count&);
3589
3590protected:
3591 long __shared_owners_;
3592 virtual ~__shared_count();
3593private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003594 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003595
3596public:
Howard Hinnant82894812010-09-22 16:48:34 +00003597 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003598 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003599 : __shared_owners_(__refs) {}
3600
Howard Hinnant1694d232011-05-28 14:41:13 +00003601 void __add_shared() _NOEXCEPT;
3602 bool __release_shared() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003603 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003604 long use_count() const _NOEXCEPT {return __shared_owners_ + 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003605};
3606
3607class __shared_weak_count
3608 : private __shared_count
3609{
3610 long __shared_weak_owners_;
3611
3612public:
Howard Hinnant82894812010-09-22 16:48:34 +00003613 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003614 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003615 : __shared_count(__refs),
3616 __shared_weak_owners_(__refs) {}
3617protected:
3618 virtual ~__shared_weak_count();
3619
3620public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003621 void __add_shared() _NOEXCEPT;
3622 void __add_weak() _NOEXCEPT;
3623 void __release_shared() _NOEXCEPT;
3624 void __release_weak() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00003625 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003626 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3627 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003628
Howard Hinnant1694d232011-05-28 14:41:13 +00003629 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003630private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003631 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003632};
3633
3634template <class _Tp, class _Dp, class _Alloc>
3635class __shared_ptr_pointer
3636 : public __shared_weak_count
3637{
3638 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3639public:
Howard Hinnant82894812010-09-22 16:48:34 +00003640 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003641 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003642 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003643
Howard Hinnantd4444702010-08-11 17:04:31 +00003644#ifndef _LIBCPP_NO_RTTI
Howard Hinnant1694d232011-05-28 14:41:13 +00003645 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnantd4444702010-08-11 17:04:31 +00003646#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003647
3648private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003649 virtual void __on_zero_shared() _NOEXCEPT;
3650 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003651};
3652
Howard Hinnantd4444702010-08-11 17:04:31 +00003653#ifndef _LIBCPP_NO_RTTI
3654
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003655template <class _Tp, class _Dp, class _Alloc>
3656const void*
Howard Hinnant1694d232011-05-28 14:41:13 +00003657__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003658{
3659 return __t == typeid(_Dp) ? &__data_.first().second() : 0;
3660}
3661
Howard Hinnant324bb032010-08-22 00:02:43 +00003662#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00003663
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003664template <class _Tp, class _Dp, class _Alloc>
3665void
Howard Hinnant1694d232011-05-28 14:41:13 +00003666__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003667{
3668 __data_.first().second()(__data_.first().first());
3669 __data_.first().second().~_Dp();
3670}
3671
3672template <class _Tp, class _Dp, class _Alloc>
3673void
Howard Hinnant1694d232011-05-28 14:41:13 +00003674__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003675{
3676 typename _Alloc::template rebind<__shared_ptr_pointer>::other __a(__data_.second());
3677 __data_.second().~_Alloc();
3678 __a.deallocate(this, 1);
3679}
3680
3681template <class _Tp, class _Alloc>
3682class __shared_ptr_emplace
3683 : public __shared_weak_count
3684{
3685 __compressed_pair<_Alloc, _Tp> __data_;
3686public:
3687#ifndef _LIBCPP_HAS_NO_VARIADICS
3688
Howard Hinnant82894812010-09-22 16:48:34 +00003689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003690 __shared_ptr_emplace(_Alloc __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003691 : __data_(_VSTD::move(__a)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003692
3693 template <class ..._Args>
Howard Hinnant82894812010-09-22 16:48:34 +00003694 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003695 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant6cc99fa2011-12-19 17:58:44 +00003696 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3697 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003698
3699#else // _LIBCPP_HAS_NO_VARIADICS
3700
Howard Hinnant82894812010-09-22 16:48:34 +00003701 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003702 __shared_ptr_emplace(_Alloc __a)
3703 : __data_(__a) {}
3704
3705 template <class _A0>
Howard Hinnant82894812010-09-22 16:48:34 +00003706 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003707 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3708 : __data_(__a, _Tp(__a0)) {}
3709
3710 template <class _A0, class _A1>
Howard Hinnant82894812010-09-22 16:48:34 +00003711 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003712 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3713 : __data_(__a, _Tp(__a0, __a1)) {}
3714
3715 template <class _A0, class _A1, class _A2>
Howard Hinnant82894812010-09-22 16:48:34 +00003716 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003717 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3718 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3719
3720#endif // _LIBCPP_HAS_NO_VARIADICS
3721
3722private:
Howard Hinnant1694d232011-05-28 14:41:13 +00003723 virtual void __on_zero_shared() _NOEXCEPT;
3724 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003725public:
Howard Hinnant82894812010-09-22 16:48:34 +00003726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003727 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003728};
3729
3730template <class _Tp, class _Alloc>
3731void
Howard Hinnant1694d232011-05-28 14:41:13 +00003732__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003733{
3734 __data_.second().~_Tp();
3735}
3736
3737template <class _Tp, class _Alloc>
3738void
Howard Hinnant1694d232011-05-28 14:41:13 +00003739__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003740{
3741 typename _Alloc::template rebind<__shared_ptr_emplace>::other __a(__data_.first());
3742 __data_.first().~_Alloc();
3743 __a.deallocate(this, 1);
3744}
3745
3746template<class _Tp> class enable_shared_from_this;
3747
3748template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00003749class _LIBCPP_VISIBLE shared_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003750{
Howard Hinnant324bb032010-08-22 00:02:43 +00003751public:
3752 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003753private:
3754 element_type* __ptr_;
3755 __shared_weak_count* __cntrl_;
3756
3757 struct __nat {int __for_bool_;};
3758public:
Howard Hinnant1694d232011-05-28 14:41:13 +00003759 shared_ptr() _NOEXCEPT;
3760 shared_ptr(nullptr_t) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003761 template<class _Yp,
3762 class = typename enable_if
3763 <
3764 is_convertible<_Yp*, element_type*>::value
3765 >::type
3766 >
3767 explicit shared_ptr(_Yp* __p);
3768 template<class _Yp, class _Dp,
3769 class = typename enable_if
3770 <
3771 is_convertible<_Yp*, element_type*>::value
3772 >::type
3773 >
3774 shared_ptr(_Yp* __p, _Dp __d);
3775 template<class _Yp, class _Dp, class _Alloc,
3776 class = typename enable_if
3777 <
3778 is_convertible<_Yp*, element_type*>::value
3779 >::type
3780 >
3781 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003782 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3783 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Howard Hinnant1694d232011-05-28 14:41:13 +00003784 template<class _Yp> shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3785 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003786 template<class _Yp>
3787 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003788 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3789 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003790#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003791 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003792 template<class _Yp> shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00003793 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3794 _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003795#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003796 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant324bb032010-08-22 00:02:43 +00003797 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003798#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003799 template<class _Yp,
3800 class = typename enable_if
3801 <
3802 is_convertible<_Yp*, element_type*>::value
3803 >::type
3804 >
3805 shared_ptr(auto_ptr<_Yp>&& __r);
Howard Hinnant324bb032010-08-22 00:02:43 +00003806#else
Howard Hinnant57199402012-01-02 17:56:02 +00003807 template<class _Yp,
3808 class = typename enable_if
3809 <
3810 is_convertible<_Yp*, element_type*>::value
3811 >::type
3812 >
3813 shared_ptr(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003814#endif
Howard Hinnant73d21a42010-09-04 23:28:19 +00003815#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003816 template <class _Yp, class _Dp,
3817 class = typename enable_if
3818 <
3819 !is_array<_Yp>::value &&
3820 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3821 >::type
3822 >
3823 shared_ptr(unique_ptr<_Yp, _Dp>&&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003824 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant57199402012-01-02 17:56:02 +00003825 template <class _Yp, class _Dp,
3826 class = typename enable_if
3827 <
3828 !is_array<_Yp>::value &&
3829 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3830 >::type
3831 >
3832 shared_ptr(unique_ptr<_Yp, _Dp>&&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003833 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003834#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003835 template <class _Yp, class _Dp,
3836 class = typename enable_if
3837 <
3838 !is_array<_Yp>::value &&
3839 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3840 >::type
3841 > shared_ptr(unique_ptr<_Yp, _Dp>,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003842 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant57199402012-01-02 17:56:02 +00003843 template <class _Yp, class _Dp,
3844 class = typename enable_if
3845 <
3846 !is_array<_Yp>::value &&
3847 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value
3848 >::type
3849 >
3850 shared_ptr(unique_ptr<_Yp, _Dp>,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003851 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type = __nat());
Howard Hinnant73d21a42010-09-04 23:28:19 +00003852#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003853
3854 ~shared_ptr();
3855
Howard Hinnant1694d232011-05-28 14:41:13 +00003856 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003857 template<class _Yp>
3858 typename enable_if
3859 <
3860 is_convertible<_Yp*, element_type*>::value,
3861 shared_ptr&
3862 >::type
3863 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:19 +00003864#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant1694d232011-05-28 14:41:13 +00003865 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003866 template<class _Yp>
3867 typename enable_if
3868 <
3869 is_convertible<_Yp*, element_type*>::value,
3870 shared_ptr<_Tp>&
3871 >::type
3872 operator=(shared_ptr<_Yp>&& __r);
3873 template<class _Yp>
3874 typename enable_if
3875 <
3876 !is_array<_Yp>::value &&
3877 is_convertible<_Yp*, element_type*>::value,
3878 shared_ptr&
3879 >::type
3880 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003881#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003882 template<class _Yp>
3883 typename enable_if
3884 <
3885 !is_array<_Yp>::value &&
3886 is_convertible<_Yp*, element_type*>::value,
3887 shared_ptr&
3888 >::type
3889 operator=(auto_ptr<_Yp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003890#endif
Howard Hinnant57199402012-01-02 17:56:02 +00003891 template <class _Yp, class _Dp>
3892 typename enable_if
3893 <
3894 !is_array<_Yp>::value &&
3895 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3896 shared_ptr&
3897 >::type
Howard Hinnant73d21a42010-09-04 23:28:19 +00003898#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003899 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant73d21a42010-09-04 23:28:19 +00003900#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant57199402012-01-02 17:56:02 +00003901 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003902#endif
3903
Howard Hinnant1694d232011-05-28 14:41:13 +00003904 void swap(shared_ptr& __r) _NOEXCEPT;
3905 void reset() _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00003906 template<class _Yp>
3907 typename enable_if
3908 <
3909 is_convertible<_Yp*, element_type*>::value,
3910 void
3911 >::type
3912 reset(_Yp* __p);
3913 template<class _Yp, class _Dp>
3914 typename enable_if
3915 <
3916 is_convertible<_Yp*, element_type*>::value,
3917 void
3918 >::type
3919 reset(_Yp* __p, _Dp __d);
3920 template<class _Yp, class _Dp, class _Alloc>
3921 typename enable_if
3922 <
3923 is_convertible<_Yp*, element_type*>::value,
3924 void
3925 >::type
3926 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003927
Howard Hinnant82894812010-09-22 16:48:34 +00003928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003929 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003930 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003931 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3932 {return *__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003933 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003934 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +00003935 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003936 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00003937 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003938 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant82894812010-09-22 16:48:34 +00003939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003940 /*explicit*/ operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnant99968442011-11-29 18:15:50 +00003941 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00003942 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003943 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003944 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant99968442011-11-29 18:15:50 +00003945 template <class _Up>
Howard Hinnant82894812010-09-22 16:48:34 +00003946 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00003947 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003948 {return __cntrl_ < __p.__cntrl_;}
3949
Howard Hinnantd4444702010-08-11 17:04:31 +00003950#ifndef _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003951 template <class _Dp>
Howard Hinnant82894812010-09-22 16:48:34 +00003952 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00003953 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003954 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant324bb032010-08-22 00:02:43 +00003955#endif // _LIBCPP_NO_RTTI
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003956
3957#ifndef _LIBCPP_HAS_NO_VARIADICS
3958
3959 template<class ..._Args>
3960 static
3961 shared_ptr<_Tp>
3962 make_shared(_Args&& ...__args);
3963
3964 template<class _Alloc, class ..._Args>
3965 static
3966 shared_ptr<_Tp>
3967 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3968
3969#else // _LIBCPP_HAS_NO_VARIADICS
3970
3971 static shared_ptr<_Tp> make_shared();
3972
3973 template<class _A0>
3974 static shared_ptr<_Tp> make_shared(_A0&);
3975
3976 template<class _A0, class _A1>
3977 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3978
3979 template<class _A0, class _A1, class _A2>
3980 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3981
3982 template<class _Alloc>
3983 static shared_ptr<_Tp>
3984 allocate_shared(const _Alloc& __a);
3985
3986 template<class _Alloc, class _A0>
3987 static shared_ptr<_Tp>
3988 allocate_shared(const _Alloc& __a, _A0& __a0);
3989
3990 template<class _Alloc, class _A0, class _A1>
3991 static shared_ptr<_Tp>
3992 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3993
3994 template<class _Alloc, class _A0, class _A1, class _A2>
3995 static shared_ptr<_Tp>
3996 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3997
3998#endif // _LIBCPP_HAS_NO_VARIADICS
3999
4000private:
4001
4002 template <class _Yp>
Howard Hinnant82894812010-09-22 16:48:34 +00004003 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004004 void
Howard Hinnant1694d232011-05-28 14:41:13 +00004005 __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004006 {
4007 if (__e)
4008 __e->__weak_this_ = *this;
4009 }
4010
Howard Hinnant82894812010-09-22 16:48:34 +00004011 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004012 void __enable_weak_this(const void*) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004013
Howard Hinnant82894812010-09-22 16:48:34 +00004014 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
4015 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004016};
4017
4018template<class _Tp>
4019inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004020shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004021 : __ptr_(0),
4022 __cntrl_(0)
4023{
4024}
4025
4026template<class _Tp>
4027inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004028shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004029 : __ptr_(0),
4030 __cntrl_(0)
4031{
4032}
4033
4034template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004035template<class _Yp, class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004036shared_ptr<_Tp>::shared_ptr(_Yp* __p)
4037 : __ptr_(__p)
4038{
4039 unique_ptr<_Yp> __hold(__p);
4040 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4041 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
4042 __hold.release();
4043 __enable_weak_this(__p);
4044}
4045
4046template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004047template<class _Yp, class _Dp, class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004048shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d)
4049 : __ptr_(__p)
4050{
4051#ifndef _LIBCPP_NO_EXCEPTIONS
4052 try
4053 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004054#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004055 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4056 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
4057 __enable_weak_this(__p);
4058#ifndef _LIBCPP_NO_EXCEPTIONS
4059 }
4060 catch (...)
4061 {
4062 __d(__p);
4063 throw;
4064 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004065#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004066}
4067
4068template<class _Tp>
4069template<class _Dp>
4070shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4071 : __ptr_(0)
4072{
4073#ifndef _LIBCPP_NO_EXCEPTIONS
4074 try
4075 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004076#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004077 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4078 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4079#ifndef _LIBCPP_NO_EXCEPTIONS
4080 }
4081 catch (...)
4082 {
4083 __d(__p);
4084 throw;
4085 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004086#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004087}
4088
4089template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004090template<class _Yp, class _Dp, class _Alloc, class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004091shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a)
4092 : __ptr_(__p)
4093{
4094#ifndef _LIBCPP_NO_EXCEPTIONS
4095 try
4096 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004097#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004098 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
4099 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
4100 typedef __allocator_destructor<_A2> _D2;
4101 _A2 __a2(__a);
4102 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4103 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
4104 __cntrl_ = __hold2.release();
4105 __enable_weak_this(__p);
4106#ifndef _LIBCPP_NO_EXCEPTIONS
4107 }
4108 catch (...)
4109 {
4110 __d(__p);
4111 throw;
4112 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004113#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004114}
4115
4116template<class _Tp>
4117template<class _Dp, class _Alloc>
4118shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4119 : __ptr_(0)
4120{
4121#ifndef _LIBCPP_NO_EXCEPTIONS
4122 try
4123 {
Howard Hinnant324bb032010-08-22 00:02:43 +00004124#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004125 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
4126 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
4127 typedef __allocator_destructor<_A2> _D2;
4128 _A2 __a2(__a);
4129 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4130 ::new(__hold2.get()) _CntrlBlk(__p, __d, __a);
4131 __cntrl_ = __hold2.release();
4132#ifndef _LIBCPP_NO_EXCEPTIONS
4133 }
4134 catch (...)
4135 {
4136 __d(__p);
4137 throw;
4138 }
Howard Hinnant324bb032010-08-22 00:02:43 +00004139#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004140}
4141
4142template<class _Tp>
4143template<class _Yp>
4144inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004145shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004146 : __ptr_(__p),
4147 __cntrl_(__r.__cntrl_)
4148{
4149 if (__cntrl_)
4150 __cntrl_->__add_shared();
4151}
4152
4153template<class _Tp>
4154inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004155shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004156 : __ptr_(__r.__ptr_),
4157 __cntrl_(__r.__cntrl_)
4158{
4159 if (__cntrl_)
4160 __cntrl_->__add_shared();
4161}
4162
4163template<class _Tp>
4164template<class _Yp>
4165inline _LIBCPP_INLINE_VISIBILITY
4166shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4167 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004168 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004169 : __ptr_(__r.__ptr_),
4170 __cntrl_(__r.__cntrl_)
4171{
4172 if (__cntrl_)
4173 __cntrl_->__add_shared();
4174}
4175
Howard Hinnant73d21a42010-09-04 23:28:19 +00004176#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004177
4178template<class _Tp>
4179inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004180shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004181 : __ptr_(__r.__ptr_),
4182 __cntrl_(__r.__cntrl_)
4183{
4184 __r.__ptr_ = 0;
4185 __r.__cntrl_ = 0;
4186}
4187
4188template<class _Tp>
4189template<class _Yp>
4190inline _LIBCPP_INLINE_VISIBILITY
4191shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4192 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00004193 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004194 : __ptr_(__r.__ptr_),
4195 __cntrl_(__r.__cntrl_)
4196{
4197 __r.__ptr_ = 0;
4198 __r.__cntrl_ = 0;
4199}
4200
Howard Hinnant73d21a42010-09-04 23:28:19 +00004201#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004202
4203template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004204template<class _Yp, class>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004205#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004206shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r)
4207#else
Howard Hinnant92172b82010-08-21 21:14:53 +00004208shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004209#endif
4210 : __ptr_(__r.get())
4211{
4212 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4213 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
4214 __enable_weak_this(__r.get());
4215 __r.release();
4216}
4217
4218template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004219template <class _Yp, class _Dp, class>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004220#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004221shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4222#else
4223shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4224#endif
4225 typename enable_if<!is_lvalue_reference<_Dp>::value, __nat>::type)
4226 : __ptr_(__r.get())
4227{
4228 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4229 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
4230 __enable_weak_this(__r.get());
4231 __r.release();
4232}
4233
4234template<class _Tp>
Howard Hinnant57199402012-01-02 17:56:02 +00004235template <class _Yp, class _Dp, class>
Howard Hinnant73d21a42010-09-04 23:28:19 +00004236#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004237shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4238#else
4239shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4240#endif
4241 typename enable_if<is_lvalue_reference<_Dp>::value, __nat>::type)
4242 : __ptr_(__r.get())
4243{
4244 typedef __shared_ptr_pointer<_Yp*,
4245 reference_wrapper<typename remove_reference<_Dp>::type>,
4246 allocator<_Yp> > _CntrlBlk;
4247 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
4248 __enable_weak_this(__r.get());
4249 __r.release();
4250}
4251
4252#ifndef _LIBCPP_HAS_NO_VARIADICS
4253
4254template<class _Tp>
4255template<class ..._Args>
4256shared_ptr<_Tp>
4257shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4258{
4259 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4260 typedef allocator<_CntrlBlk> _A2;
4261 typedef __allocator_destructor<_A2> _D2;
4262 _A2 __a2;
4263 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004264 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004265 shared_ptr<_Tp> __r;
4266 __r.__ptr_ = __hold2.get()->get();
4267 __r.__cntrl_ = __hold2.release();
4268 __r.__enable_weak_this(__r.__ptr_);
4269 return __r;
4270}
4271
4272template<class _Tp>
4273template<class _Alloc, class ..._Args>
4274shared_ptr<_Tp>
4275shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4276{
4277 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4278 typedef typename _Alloc::template rebind<_CntrlBlk>::other _A2;
4279 typedef __allocator_destructor<_A2> _D2;
4280 _A2 __a2(__a);
4281 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004282 ::new(__hold2.get()) _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004283 shared_ptr<_Tp> __r;
4284 __r.__ptr_ = __hold2.get()->get();
4285 __r.__cntrl_ = __hold2.release();
4286 __r.__enable_weak_this(__r.__ptr_);
4287 return __r;
4288}
4289
4290#else // _LIBCPP_HAS_NO_VARIADICS
4291
4292template<class _Tp>
4293shared_ptr<_Tp>
4294shared_ptr<_Tp>::make_shared()
4295{
4296 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4297 typedef allocator<_CntrlBlk> _Alloc2;
4298 typedef __allocator_destructor<_Alloc2> _D2;
4299 _Alloc2 __alloc2;
4300 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4301 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4302 shared_ptr<_Tp> __r;
4303 __r.__ptr_ = __hold2.get()->get();
4304 __r.__cntrl_ = __hold2.release();
4305 __r.__enable_weak_this(__r.__ptr_);
4306 return __r;
4307}
4308
4309template<class _Tp>
4310template<class _A0>
4311shared_ptr<_Tp>
4312shared_ptr<_Tp>::make_shared(_A0& __a0)
4313{
4314 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4315 typedef allocator<_CntrlBlk> _Alloc2;
4316 typedef __allocator_destructor<_Alloc2> _D2;
4317 _Alloc2 __alloc2;
4318 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4319 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4320 shared_ptr<_Tp> __r;
4321 __r.__ptr_ = __hold2.get()->get();
4322 __r.__cntrl_ = __hold2.release();
4323 __r.__enable_weak_this(__r.__ptr_);
4324 return __r;
4325}
4326
4327template<class _Tp>
4328template<class _A0, class _A1>
4329shared_ptr<_Tp>
4330shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4331{
4332 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4333 typedef allocator<_CntrlBlk> _Alloc2;
4334 typedef __allocator_destructor<_Alloc2> _D2;
4335 _Alloc2 __alloc2;
4336 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4337 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4338 shared_ptr<_Tp> __r;
4339 __r.__ptr_ = __hold2.get()->get();
4340 __r.__cntrl_ = __hold2.release();
4341 __r.__enable_weak_this(__r.__ptr_);
4342 return __r;
4343}
4344
4345template<class _Tp>
4346template<class _A0, class _A1, class _A2>
4347shared_ptr<_Tp>
4348shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4349{
4350 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4351 typedef allocator<_CntrlBlk> _Alloc2;
4352 typedef __allocator_destructor<_Alloc2> _D2;
4353 _Alloc2 __alloc2;
4354 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4355 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4356 shared_ptr<_Tp> __r;
4357 __r.__ptr_ = __hold2.get()->get();
4358 __r.__cntrl_ = __hold2.release();
4359 __r.__enable_weak_this(__r.__ptr_);
4360 return __r;
4361}
4362
4363template<class _Tp>
4364template<class _Alloc>
4365shared_ptr<_Tp>
4366shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4367{
4368 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4369 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4370 typedef __allocator_destructor<_Alloc2> _D2;
4371 _Alloc2 __alloc2(__a);
4372 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4373 ::new(__hold2.get()) _CntrlBlk(__a);
4374 shared_ptr<_Tp> __r;
4375 __r.__ptr_ = __hold2.get()->get();
4376 __r.__cntrl_ = __hold2.release();
4377 __r.__enable_weak_this(__r.__ptr_);
4378 return __r;
4379}
4380
4381template<class _Tp>
4382template<class _Alloc, class _A0>
4383shared_ptr<_Tp>
4384shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4385{
4386 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4387 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4388 typedef __allocator_destructor<_Alloc2> _D2;
4389 _Alloc2 __alloc2(__a);
4390 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4391 ::new(__hold2.get()) _CntrlBlk(__a, __a0);
4392 shared_ptr<_Tp> __r;
4393 __r.__ptr_ = __hold2.get()->get();
4394 __r.__cntrl_ = __hold2.release();
4395 __r.__enable_weak_this(__r.__ptr_);
4396 return __r;
4397}
4398
4399template<class _Tp>
4400template<class _Alloc, class _A0, class _A1>
4401shared_ptr<_Tp>
4402shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4403{
4404 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4405 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4406 typedef __allocator_destructor<_Alloc2> _D2;
4407 _Alloc2 __alloc2(__a);
4408 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4409 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1);
4410 shared_ptr<_Tp> __r;
4411 __r.__ptr_ = __hold2.get()->get();
4412 __r.__cntrl_ = __hold2.release();
4413 __r.__enable_weak_this(__r.__ptr_);
4414 return __r;
4415}
4416
4417template<class _Tp>
4418template<class _Alloc, class _A0, class _A1, class _A2>
4419shared_ptr<_Tp>
4420shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4421{
4422 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4423 typedef typename _Alloc::template rebind<_CntrlBlk>::other _Alloc2;
4424 typedef __allocator_destructor<_Alloc2> _D2;
4425 _Alloc2 __alloc2(__a);
4426 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4427 ::new(__hold2.get()) _CntrlBlk(__a, __a0, __a1, __a2);
4428 shared_ptr<_Tp> __r;
4429 __r.__ptr_ = __hold2.get()->get();
4430 __r.__cntrl_ = __hold2.release();
4431 __r.__enable_weak_this(__r.__ptr_);
4432 return __r;
4433}
4434
4435#endif // _LIBCPP_HAS_NO_VARIADICS
4436
4437template<class _Tp>
4438shared_ptr<_Tp>::~shared_ptr()
4439{
4440 if (__cntrl_)
4441 __cntrl_->__release_shared();
4442}
4443
4444template<class _Tp>
4445inline _LIBCPP_INLINE_VISIBILITY
4446shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004447shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004448{
4449 shared_ptr(__r).swap(*this);
4450 return *this;
4451}
4452
4453template<class _Tp>
4454template<class _Yp>
4455inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004456typename enable_if
4457<
4458 is_convertible<_Yp*, _Tp*>::value,
4459 shared_ptr<_Tp>&
4460>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004461shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004462{
4463 shared_ptr(__r).swap(*this);
4464 return *this;
4465}
4466
Howard Hinnant73d21a42010-09-04 23:28:19 +00004467#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004468
4469template<class _Tp>
4470inline _LIBCPP_INLINE_VISIBILITY
4471shared_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00004472shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004473{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004474 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004475 return *this;
4476}
4477
4478template<class _Tp>
4479template<class _Yp>
4480inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004481typename enable_if
4482<
4483 is_convertible<_Yp*, _Tp*>::value,
4484 shared_ptr<_Tp>&
4485>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004486shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4487{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004488 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004489 return *this;
4490}
4491
4492template<class _Tp>
4493template<class _Yp>
4494inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004495typename enable_if
4496<
4497 !is_array<_Yp>::value &&
4498 is_convertible<_Yp*, _Tp*>::value,
4499 shared_ptr<_Tp>&
4500>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004501shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4502{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004503 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004504 return *this;
4505}
4506
4507template<class _Tp>
4508template <class _Yp, class _Dp>
4509inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004510typename enable_if
4511<
4512 !is_array<_Yp>::value &&
4513 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4514 shared_ptr<_Tp>&
4515>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004516shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4517{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004518 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004519 return *this;
4520}
4521
Howard Hinnant73d21a42010-09-04 23:28:19 +00004522#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004523
4524template<class _Tp>
4525template<class _Yp>
4526inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004527typename enable_if
4528<
4529 !is_array<_Yp>::value &&
4530 is_convertible<_Yp*, _Tp*>::value,
4531 shared_ptr<_Tp>&
4532>::type
Howard Hinnant324bb032010-08-22 00:02:43 +00004533shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004534{
4535 shared_ptr(__r).swap(*this);
4536 return *this;
4537}
4538
4539template<class _Tp>
4540template <class _Yp, class _Dp>
4541inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004542typename enable_if
4543<
4544 !is_array<_Yp>::value &&
4545 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4546 shared_ptr<_Tp>&
4547>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004548shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4549{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004550 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004551 return *this;
4552}
4553
Howard Hinnant73d21a42010-09-04 23:28:19 +00004554#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004555
4556template<class _Tp>
4557inline _LIBCPP_INLINE_VISIBILITY
4558void
Howard Hinnant1694d232011-05-28 14:41:13 +00004559shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004560{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004561 _VSTD::swap(__ptr_, __r.__ptr_);
4562 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004563}
4564
4565template<class _Tp>
4566inline _LIBCPP_INLINE_VISIBILITY
4567void
Howard Hinnant1694d232011-05-28 14:41:13 +00004568shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004569{
4570 shared_ptr().swap(*this);
4571}
4572
4573template<class _Tp>
4574template<class _Yp>
4575inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004576typename enable_if
4577<
4578 is_convertible<_Yp*, _Tp*>::value,
4579 void
4580>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004581shared_ptr<_Tp>::reset(_Yp* __p)
4582{
4583 shared_ptr(__p).swap(*this);
4584}
4585
4586template<class _Tp>
4587template<class _Yp, class _Dp>
4588inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004589typename enable_if
4590<
4591 is_convertible<_Yp*, _Tp*>::value,
4592 void
4593>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004594shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4595{
4596 shared_ptr(__p, __d).swap(*this);
4597}
4598
4599template<class _Tp>
4600template<class _Yp, class _Dp, class _Alloc>
4601inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004602typename enable_if
4603<
4604 is_convertible<_Yp*, _Tp*>::value,
4605 void
4606>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004607shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4608{
4609 shared_ptr(__p, __d, __a).swap(*this);
4610}
4611
4612#ifndef _LIBCPP_HAS_NO_VARIADICS
4613
Howard Hinnant324bb032010-08-22 00:02:43 +00004614template<class _Tp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004615inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004616typename enable_if
4617<
4618 !is_array<_Tp>::value,
4619 shared_ptr<_Tp>
4620>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004621make_shared(_Args&& ...__args)
4622{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004623 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004624}
4625
Howard Hinnant324bb032010-08-22 00:02:43 +00004626template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004627inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004628typename enable_if
4629<
4630 !is_array<_Tp>::value,
4631 shared_ptr<_Tp>
4632>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004633allocate_shared(const _Alloc& __a, _Args&& ...__args)
4634{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004635 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004636}
4637
4638#else // _LIBCPP_HAS_NO_VARIADICS
4639
4640template<class _Tp>
4641inline _LIBCPP_INLINE_VISIBILITY
4642shared_ptr<_Tp>
4643make_shared()
4644{
4645 return shared_ptr<_Tp>::make_shared();
4646}
4647
4648template<class _Tp, class _A0>
4649inline _LIBCPP_INLINE_VISIBILITY
4650shared_ptr<_Tp>
4651make_shared(_A0& __a0)
4652{
4653 return shared_ptr<_Tp>::make_shared(__a0);
4654}
4655
4656template<class _Tp, class _A0, class _A1>
4657inline _LIBCPP_INLINE_VISIBILITY
4658shared_ptr<_Tp>
4659make_shared(_A0& __a0, _A1& __a1)
4660{
4661 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4662}
4663
Howard Hinnant324bb032010-08-22 00:02:43 +00004664template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004665inline _LIBCPP_INLINE_VISIBILITY
4666shared_ptr<_Tp>
4667make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4668{
4669 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4670}
4671
4672template<class _Tp, class _Alloc>
4673inline _LIBCPP_INLINE_VISIBILITY
4674shared_ptr<_Tp>
4675allocate_shared(const _Alloc& __a)
4676{
4677 return shared_ptr<_Tp>::allocate_shared(__a);
4678}
4679
4680template<class _Tp, class _Alloc, class _A0>
4681inline _LIBCPP_INLINE_VISIBILITY
4682shared_ptr<_Tp>
4683allocate_shared(const _Alloc& __a, _A0& __a0)
4684{
4685 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4686}
4687
4688template<class _Tp, class _Alloc, class _A0, class _A1>
4689inline _LIBCPP_INLINE_VISIBILITY
4690shared_ptr<_Tp>
4691allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4692{
4693 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4694}
4695
4696template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4697inline _LIBCPP_INLINE_VISIBILITY
4698shared_ptr<_Tp>
4699allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4700{
4701 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4702}
4703
4704#endif // _LIBCPP_HAS_NO_VARIADICS
4705
4706template<class _Tp, class _Up>
4707inline _LIBCPP_INLINE_VISIBILITY
4708bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004709operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004710{
4711 return __x.get() == __y.get();
4712}
4713
4714template<class _Tp, class _Up>
4715inline _LIBCPP_INLINE_VISIBILITY
4716bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004717operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004718{
4719 return !(__x == __y);
4720}
4721
4722template<class _Tp, class _Up>
4723inline _LIBCPP_INLINE_VISIBILITY
4724bool
Howard Hinnant1694d232011-05-28 14:41:13 +00004725operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004726{
Howard Hinnant3fadda32012-02-21 21:02:58 +00004727 typedef typename common_type<_Tp*, _Up*>::type _V;
4728 return less<_V>()(__x.get(), __y.get());
4729}
4730
4731template<class _Tp, class _Up>
4732inline _LIBCPP_INLINE_VISIBILITY
4733bool
4734operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4735{
4736 return __y < __x;
4737}
4738
4739template<class _Tp, class _Up>
4740inline _LIBCPP_INLINE_VISIBILITY
4741bool
4742operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4743{
4744 return !(__y < __x);
4745}
4746
4747template<class _Tp, class _Up>
4748inline _LIBCPP_INLINE_VISIBILITY
4749bool
4750operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4751{
4752 return !(__x < __y);
4753}
4754
4755template<class _Tp>
4756inline _LIBCPP_INLINE_VISIBILITY
4757bool
4758operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4759{
4760 return !__x;
4761}
4762
4763template<class _Tp>
4764inline _LIBCPP_INLINE_VISIBILITY
4765bool
4766operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4767{
4768 return !__x;
4769}
4770
4771template<class _Tp>
4772inline _LIBCPP_INLINE_VISIBILITY
4773bool
4774operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4775{
4776 return static_cast<bool>(__x);
4777}
4778
4779template<class _Tp>
4780inline _LIBCPP_INLINE_VISIBILITY
4781bool
4782operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4783{
4784 return static_cast<bool>(__x);
4785}
4786
4787template<class _Tp>
4788inline _LIBCPP_INLINE_VISIBILITY
4789bool
4790operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4791{
4792 return less<_Tp*>()(__x.get(), nullptr);
4793}
4794
4795template<class _Tp>
4796inline _LIBCPP_INLINE_VISIBILITY
4797bool
4798operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4799{
4800 return less<_Tp*>()(nullptr, __x.get());
4801}
4802
4803template<class _Tp>
4804inline _LIBCPP_INLINE_VISIBILITY
4805bool
4806operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4807{
4808 return nullptr < __x;
4809}
4810
4811template<class _Tp>
4812inline _LIBCPP_INLINE_VISIBILITY
4813bool
4814operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4815{
4816 return __x < nullptr;
4817}
4818
4819template<class _Tp>
4820inline _LIBCPP_INLINE_VISIBILITY
4821bool
4822operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4823{
4824 return !(nullptr < __x);
4825}
4826
4827template<class _Tp>
4828inline _LIBCPP_INLINE_VISIBILITY
4829bool
4830operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4831{
4832 return !(__x < nullptr);
4833}
4834
4835template<class _Tp>
4836inline _LIBCPP_INLINE_VISIBILITY
4837bool
4838operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4839{
4840 return !(__x < nullptr);
4841}
4842
4843template<class _Tp>
4844inline _LIBCPP_INLINE_VISIBILITY
4845bool
4846operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4847{
4848 return !(nullptr < __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004849}
4850
4851template<class _Tp>
4852inline _LIBCPP_INLINE_VISIBILITY
4853void
Howard Hinnant1694d232011-05-28 14:41:13 +00004854swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004855{
4856 __x.swap(__y);
4857}
4858
4859template<class _Tp, class _Up>
4860inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004861typename enable_if
4862<
4863 !is_array<_Tp>::value && !is_array<_Up>::value,
4864 shared_ptr<_Tp>
4865>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004866static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004867{
4868 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4869}
4870
4871template<class _Tp, class _Up>
4872inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00004873typename enable_if
4874<
4875 !is_array<_Tp>::value && !is_array<_Up>::value,
4876 shared_ptr<_Tp>
4877>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004878dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004879{
4880 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4881 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4882}
4883
4884template<class _Tp, class _Up>
Howard Hinnant57199402012-01-02 17:56:02 +00004885typename enable_if
4886<
4887 is_array<_Tp>::value == is_array<_Up>::value,
4888 shared_ptr<_Tp>
4889>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00004890const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004891{
Howard Hinnant57199402012-01-02 17:56:02 +00004892 typedef typename remove_extent<_Tp>::type _RTp;
4893 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004894}
4895
Howard Hinnantd4444702010-08-11 17:04:31 +00004896#ifndef _LIBCPP_NO_RTTI
4897
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004898template<class _Dp, class _Tp>
4899inline _LIBCPP_INLINE_VISIBILITY
4900_Dp*
Howard Hinnant1694d232011-05-28 14:41:13 +00004901get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004902{
4903 return __p.template __get_deleter<_Dp>();
4904}
4905
Howard Hinnant324bb032010-08-22 00:02:43 +00004906#endif // _LIBCPP_NO_RTTI
Howard Hinnantd4444702010-08-11 17:04:31 +00004907
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004908template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00004909class _LIBCPP_VISIBLE weak_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004910{
Howard Hinnant324bb032010-08-22 00:02:43 +00004911public:
4912 typedef _Tp element_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004913private:
4914 element_type* __ptr_;
4915 __shared_weak_count* __cntrl_;
4916
Howard Hinnant324bb032010-08-22 00:02:43 +00004917public:
Howard Hinnant1694d232011-05-28 14:41:13 +00004918 weak_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004919 template<class _Yp> weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00004920 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4921 _NOEXCEPT;
4922 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004923 template<class _Yp> weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant1694d232011-05-28 14:41:13 +00004924 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4925 _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004926
Howard Hinnant57199402012-01-02 17:56:02 +00004927#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4928 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
4929 template<class _Yp> weak_ptr(weak_ptr<_Yp>&& __r,
4930 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4931 _NOEXCEPT;
4932#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant324bb032010-08-22 00:02:43 +00004933 ~weak_ptr();
4934
Howard Hinnant1694d232011-05-28 14:41:13 +00004935 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant57199402012-01-02 17:56:02 +00004936 template<class _Yp>
4937 typename enable_if
4938 <
4939 is_convertible<_Yp*, element_type*>::value,
4940 weak_ptr&
4941 >::type
4942 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4943
4944#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4945
4946 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4947 template<class _Yp>
4948 typename enable_if
4949 <
4950 is_convertible<_Yp*, element_type*>::value,
4951 weak_ptr&
4952 >::type
4953 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4954
4955#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4956
4957 template<class _Yp>
4958 typename enable_if
4959 <
4960 is_convertible<_Yp*, element_type*>::value,
4961 weak_ptr&
4962 >::type
4963 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004964
Howard Hinnant1694d232011-05-28 14:41:13 +00004965 void swap(weak_ptr& __r) _NOEXCEPT;
4966 void reset() _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00004967
Howard Hinnant82894812010-09-22 16:48:34 +00004968 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004969 long use_count() const _NOEXCEPT
4970 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00004971 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004972 bool expired() const _NOEXCEPT
4973 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4974 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +00004975 template<class _Up>
4976 _LIBCPP_INLINE_VISIBILITY
4977 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004978 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant82894812010-09-22 16:48:34 +00004979 template<class _Up>
4980 _LIBCPP_INLINE_VISIBILITY
4981 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004982 {return __cntrl_ < __r.__cntrl_;}
4983
Howard Hinnant82894812010-09-22 16:48:34 +00004984 template <class _Up> friend class _LIBCPP_VISIBLE weak_ptr;
4985 template <class _Up> friend class _LIBCPP_VISIBLE shared_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004986};
4987
4988template<class _Tp>
4989inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004990weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004991 : __ptr_(0),
4992 __cntrl_(0)
4993{
4994}
4995
4996template<class _Tp>
4997inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00004998weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004999 : __ptr_(__r.__ptr_),
5000 __cntrl_(__r.__cntrl_)
5001{
5002 if (__cntrl_)
5003 __cntrl_->__add_weak();
5004}
5005
5006template<class _Tp>
5007template<class _Yp>
5008inline _LIBCPP_INLINE_VISIBILITY
5009weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005010 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005011 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005012 : __ptr_(__r.__ptr_),
5013 __cntrl_(__r.__cntrl_)
5014{
5015 if (__cntrl_)
5016 __cntrl_->__add_weak();
5017}
5018
5019template<class _Tp>
5020template<class _Yp>
5021inline _LIBCPP_INLINE_VISIBILITY
5022weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnante003ce42011-05-22 00:09:02 +00005023 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant1694d232011-05-28 14:41:13 +00005024 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005025 : __ptr_(__r.__ptr_),
5026 __cntrl_(__r.__cntrl_)
5027{
5028 if (__cntrl_)
5029 __cntrl_->__add_weak();
5030}
5031
Howard Hinnant57199402012-01-02 17:56:02 +00005032#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5033
5034template<class _Tp>
5035inline _LIBCPP_INLINE_VISIBILITY
5036weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5037 : __ptr_(__r.__ptr_),
5038 __cntrl_(__r.__cntrl_)
5039{
5040 __r.__ptr_ = 0;
5041 __r.__cntrl_ = 0;
5042}
5043
5044template<class _Tp>
5045template<class _Yp>
5046inline _LIBCPP_INLINE_VISIBILITY
5047weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5048 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5049 _NOEXCEPT
5050 : __ptr_(__r.__ptr_),
5051 __cntrl_(__r.__cntrl_)
5052{
5053 __r.__ptr_ = 0;
5054 __r.__cntrl_ = 0;
5055}
5056
5057#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5058
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005059template<class _Tp>
5060weak_ptr<_Tp>::~weak_ptr()
5061{
5062 if (__cntrl_)
5063 __cntrl_->__release_weak();
5064}
5065
5066template<class _Tp>
5067inline _LIBCPP_INLINE_VISIBILITY
5068weak_ptr<_Tp>&
Howard Hinnant1694d232011-05-28 14:41:13 +00005069weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005070{
5071 weak_ptr(__r).swap(*this);
5072 return *this;
5073}
5074
5075template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005076template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005077inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005078typename enable_if
5079<
5080 is_convertible<_Yp*, _Tp*>::value,
5081 weak_ptr<_Tp>&
5082>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005083weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005084{
5085 weak_ptr(__r).swap(*this);
5086 return *this;
5087}
5088
Howard Hinnant57199402012-01-02 17:56:02 +00005089#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5090
5091template<class _Tp>
5092inline _LIBCPP_INLINE_VISIBILITY
5093weak_ptr<_Tp>&
5094weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5095{
5096 weak_ptr(_VSTD::move(__r)).swap(*this);
5097 return *this;
5098}
5099
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005100template<class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +00005101template<class _Yp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005102inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57199402012-01-02 17:56:02 +00005103typename enable_if
5104<
5105 is_convertible<_Yp*, _Tp*>::value,
5106 weak_ptr<_Tp>&
5107>::type
5108weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5109{
5110 weak_ptr(_VSTD::move(__r)).swap(*this);
5111 return *this;
5112}
5113
5114#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5115
5116template<class _Tp>
5117template<class _Yp>
5118inline _LIBCPP_INLINE_VISIBILITY
5119typename enable_if
5120<
5121 is_convertible<_Yp*, _Tp*>::value,
5122 weak_ptr<_Tp>&
5123>::type
Howard Hinnant1694d232011-05-28 14:41:13 +00005124weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005125{
5126 weak_ptr(__r).swap(*this);
5127 return *this;
5128}
5129
5130template<class _Tp>
5131inline _LIBCPP_INLINE_VISIBILITY
5132void
Howard Hinnant1694d232011-05-28 14:41:13 +00005133weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005134{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005135 _VSTD::swap(__ptr_, __r.__ptr_);
5136 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005137}
5138
5139template<class _Tp>
5140inline _LIBCPP_INLINE_VISIBILITY
5141void
Howard Hinnant1694d232011-05-28 14:41:13 +00005142swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005143{
5144 __x.swap(__y);
5145}
5146
5147template<class _Tp>
5148inline _LIBCPP_INLINE_VISIBILITY
5149void
Howard Hinnant1694d232011-05-28 14:41:13 +00005150weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005151{
5152 weak_ptr().swap(*this);
5153}
5154
5155template<class _Tp>
5156template<class _Yp>
5157shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5158 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
5159 : __ptr_(__r.__ptr_),
5160 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5161{
5162 if (__cntrl_ == 0)
5163#ifndef _LIBCPP_NO_EXCEPTIONS
5164 throw bad_weak_ptr();
5165#else
5166 assert(!"bad_weak_ptr");
5167#endif
5168}
5169
5170template<class _Tp>
5171shared_ptr<_Tp>
Howard Hinnant1694d232011-05-28 14:41:13 +00005172weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005173{
5174 shared_ptr<_Tp> __r;
5175 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5176 if (__r.__cntrl_)
5177 __r.__ptr_ = __ptr_;
5178 return __r;
5179}
5180
Howard Hinnant324bb032010-08-22 00:02:43 +00005181template <class _Tp> struct owner_less;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005182
5183template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00005184struct _LIBCPP_VISIBLE owner_less<shared_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005185 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant324bb032010-08-22 00:02:43 +00005186{
5187 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005189 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5190 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005191 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005192 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5193 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005195 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5196 {return __x.owner_before(__y);}
Howard Hinnant324bb032010-08-22 00:02:43 +00005197};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005198
5199template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00005200struct _LIBCPP_VISIBLE owner_less<weak_ptr<_Tp> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005201 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5202{
Howard Hinnant324bb032010-08-22 00:02:43 +00005203 typedef bool result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005204 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005205 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5206 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005207 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005208 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5209 {return __x.owner_before(__y);}
Howard Hinnant82894812010-09-22 16:48:34 +00005210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005211 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5212 {return __x.owner_before(__y);}
5213};
5214
5215template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00005216class _LIBCPP_VISIBLE enable_shared_from_this
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005217{
5218 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant324bb032010-08-22 00:02:43 +00005219protected:
Howard Hinnant82894812010-09-22 16:48:34 +00005220 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005221 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005222 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005223 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant82894812010-09-22 16:48:34 +00005224 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005225 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5226 {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +00005227 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005228 ~enable_shared_from_this() {}
Howard Hinnant324bb032010-08-22 00:02:43 +00005229public:
Howard Hinnant82894812010-09-22 16:48:34 +00005230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005231 shared_ptr<_Tp> shared_from_this()
5232 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant82894812010-09-22 16:48:34 +00005233 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005234 shared_ptr<_Tp const> shared_from_this() const
5235 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005236
5237 template <class _Up> friend class shared_ptr;
5238};
5239
Howard Hinnant21aefc32010-06-03 16:42:57 +00005240template <class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +00005241struct _LIBCPP_VISIBLE hash<shared_ptr<_Tp> >
Howard Hinnant21aefc32010-06-03 16:42:57 +00005242{
5243 typedef shared_ptr<_Tp> argument_type;
5244 typedef size_t result_type;
Howard Hinnant82894812010-09-22 16:48:34 +00005245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1694d232011-05-28 14:41:13 +00005246 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant21aefc32010-06-03 16:42:57 +00005247 {
5248 return hash<_Tp*>()(__ptr.get());
5249 }
5250};
5251
Howard Hinnant99968442011-11-29 18:15:50 +00005252template<class _CharT, class _Traits, class _Yp>
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005253inline _LIBCPP_INLINE_VISIBILITY
5254basic_ostream<_CharT, _Traits>&
Howard Hinnant99968442011-11-29 18:15:50 +00005255operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnant464aa5c2011-07-18 15:51:59 +00005256
Howard Hinnant324bb032010-08-22 00:02:43 +00005257//enum class
Howard Hinnant82894812010-09-22 16:48:34 +00005258struct _LIBCPP_VISIBLE pointer_safety
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005259{
5260 enum _
5261 {
5262 relaxed,
5263 preferred,
5264 strict
5265 };
5266
5267 _ __v_;
5268
Howard Hinnant82894812010-09-22 16:48:34 +00005269 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005270 pointer_safety(_ __v) : __v_(__v) {}
Howard Hinnant82894812010-09-22 16:48:34 +00005271 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005272 operator int() const {return __v_;}
5273};
5274
5275void declare_reachable(void* __p);
5276void declare_no_pointers(char* __p, size_t __n);
5277void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnant1694d232011-05-28 14:41:13 +00005278pointer_safety get_pointer_safety() _NOEXCEPT;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00005279void* __undeclare_reachable(void* __p);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005280
5281template <class _Tp>
5282inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005283_Tp*
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005284undeclare_reachable(_Tp* __p)
5285{
5286 return static_cast<_Tp*>(__undeclare_reachable(__p));
5287}
5288
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00005289void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005290
5291_LIBCPP_END_NAMESPACE_STD
5292
5293#endif // _LIBCPP_MEMORY