blob: 8cb094e6b6af77da0e705db0a69d72e4c0b4ba2f [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-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 Hinnantc51e1022010-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 Hinnant3b6579a2010-08-22 00:02:43 +000031
Howard Hinnantc51e1022010-05-11 19:42:16 +000032 template <class U> using rebind = <details>;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000033
Howard Hinnantc51e1022010-05-11 19:42:16 +000034 static pointer pointer_to(<details>);
35};
36
Howard Hinnant719bda32011-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 Hinnantc51e1022010-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 Hinnant8e4e6002010-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 Hinnantc51e1022010-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;
Marshall Clow0b587562015-06-02 16:34:03 +000078 typedef Alloc::is_always_equal
79 | is_empty is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +000080
81 template <class T> using rebind_alloc = Alloc::rebind<U>::other | Alloc<T, Args...>;
82 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
83
84 static pointer allocate(allocator_type& a, size_type n);
85 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint);
86
Howard Hinnant719bda32011-05-28 14:41:13 +000087 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
89 template <class T, class... Args>
90 static void construct(allocator_type& a, T* p, Args&&... args);
91
92 template <class T>
93 static void destroy(allocator_type& a, T* p);
94
Marshall Clow4f834b52013-08-27 20:22:15 +000095 static size_type max_size(const allocator_type& a); // noexcept in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000096
97 static allocator_type
98 select_on_container_copy_construction(const allocator_type& a);
99};
100
101template <>
102class allocator<void>
103{
104public:
105 typedef void* pointer;
106 typedef const void* const_pointer;
107 typedef void value_type;
108
109 template <class _Up> struct rebind {typedef allocator<_Up> other;};
110};
111
112template <class T>
113class allocator
114{
115public:
116 typedef size_t size_type;
117 typedef ptrdiff_t difference_type;
118 typedef T* pointer;
119 typedef const T* const_pointer;
120 typedef typename add_lvalue_reference<T>::type reference;
121 typedef typename add_lvalue_reference<const T>::type const_reference;
122 typedef T value_type;
123
124 template <class U> struct rebind {typedef allocator<U> other;};
125
Howard Hinnant719bda32011-05-28 14:41:13 +0000126 allocator() noexcept;
127 allocator(const allocator&) noexcept;
128 template <class U> allocator(const allocator<U>&) noexcept;
129 ~allocator();
130 pointer address(reference x) const noexcept;
131 const_pointer address(const_reference x) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
Howard Hinnant719bda32011-05-28 14:41:13 +0000133 void deallocate(pointer p, size_type n) noexcept;
134 size_type max_size() const noexcept;
135 template<class U, class... Args>
136 void construct(U* p, Args&&... args);
137 template <class U>
138 void destroy(U* p);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000139};
140
141template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000142bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000143
144template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000145bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000146
147template <class OutputIterator, class T>
148class raw_storage_iterator
149 : public iterator<output_iterator_tag,
150 T, // purposefully not C++03
151 ptrdiff_t, // purposefully not C++03
152 T*, // purposefully not C++03
153 raw_storage_iterator&> // purposefully not C++03
154{
155public:
156 explicit raw_storage_iterator(OutputIterator x);
157 raw_storage_iterator& operator*();
158 raw_storage_iterator& operator=(const T& element);
159 raw_storage_iterator& operator++();
160 raw_storage_iterator operator++(int);
161};
162
Howard Hinnant719bda32011-05-28 14:41:13 +0000163template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
164template <class T> void return_temporary_buffer(T* p) noexcept;
165
166template <class T> T* addressof(T& r) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000167
168template <class InputIterator, class ForwardIterator>
169ForwardIterator
170uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
171
Howard Hinnant719bda32011-05-28 14:41:13 +0000172template <class InputIterator, class Size, class ForwardIterator>
173ForwardIterator
174uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
175
Howard Hinnantc51e1022010-05-11 19:42:16 +0000176template <class ForwardIterator, class T>
177void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
178
179template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000180ForwardIterator
181uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000182
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000183template <class T>
184void destroy_at(T* location);
185
186template <class ForwardIterator>
187 void destroy(ForwardIterator first, ForwardIterator last);
188
189template <class ForwardIterator, class Size>
190 ForwardIterator destroy_n(ForwardIterator first, Size n);
191
192template <class InputIterator, class ForwardIterator>
193 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
194
195template <class InputIterator, class Size, class ForwardIterator>
196 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
197
198template <class ForwardIterator>
199 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
200
201template <class ForwardIterator, class Size>
202 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
203
204template <class ForwardIterator>
205 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
206
207template <class ForwardIterator, class Size>
208 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
209
Howard Hinnantc51e1022010-05-11 19:42:16 +0000210template <class Y> struct auto_ptr_ref {};
211
212template<class X>
213class auto_ptr
214{
215public:
216 typedef X element_type;
217
218 explicit auto_ptr(X* p =0) throw();
219 auto_ptr(auto_ptr&) throw();
220 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
221 auto_ptr& operator=(auto_ptr&) throw();
222 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
223 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
224 ~auto_ptr() throw();
225
226 typename add_lvalue_reference<X>::type operator*() const throw();
227 X* operator->() const throw();
228 X* get() const throw();
229 X* release() throw();
230 void reset(X* p =0) throw();
231
232 auto_ptr(auto_ptr_ref<X>) throw();
233 template<class Y> operator auto_ptr_ref<Y>() throw();
234 template<class Y> operator auto_ptr<Y>() throw();
235};
236
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000237template <class T>
238struct default_delete
239{
Howard Hinnant719bda32011-05-28 14:41:13 +0000240 constexpr default_delete() noexcept = default;
241 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000242
Howard Hinnant719bda32011-05-28 14:41:13 +0000243 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000244};
245
246template <class T>
247struct default_delete<T[]>
248{
Howard Hinnant719bda32011-05-28 14:41:13 +0000249 constexpr default_delete() noexcept = default;
250 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000251 template <class U> void operator()(U*) const = delete;
252};
253
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000254template <class T, class D = default_delete<T>>
255class unique_ptr
256{
257public:
258 typedef see below pointer;
259 typedef T element_type;
260 typedef D deleter_type;
261
262 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000263 constexpr unique_ptr() noexcept;
264 explicit unique_ptr(pointer p) noexcept;
265 unique_ptr(pointer p, see below d1) noexcept;
266 unique_ptr(pointer p, see below d2) noexcept;
267 unique_ptr(unique_ptr&& u) noexcept;
268 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000269 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000270 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000271 template <class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000272 unique_ptr(auto_ptr<U>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000273
274 // destructor
275 ~unique_ptr();
276
277 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000278 unique_ptr& operator=(unique_ptr&& u) noexcept;
279 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
280 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000281
282 // observers
283 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000284 pointer operator->() const noexcept;
285 pointer get() const noexcept;
286 deleter_type& get_deleter() noexcept;
287 const deleter_type& get_deleter() const noexcept;
288 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000289
290 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000291 pointer release() noexcept;
292 void reset(pointer p = pointer()) noexcept;
293 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000294};
295
296template <class T, class D>
297class unique_ptr<T[], D>
298{
299public:
300 typedef implementation-defined pointer;
301 typedef T element_type;
302 typedef D deleter_type;
303
304 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000305 constexpr unique_ptr() noexcept;
306 explicit unique_ptr(pointer p) noexcept;
307 unique_ptr(pointer p, see below d) noexcept;
308 unique_ptr(pointer p, see below d) noexcept;
309 unique_ptr(unique_ptr&& u) noexcept;
310 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000311
312 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000313 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000314
315 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000316 unique_ptr& operator=(unique_ptr&& u) noexcept;
317 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000318
319 // observers
320 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000321 pointer get() const noexcept;
322 deleter_type& get_deleter() noexcept;
323 const deleter_type& get_deleter() const noexcept;
324 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000325
326 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000327 pointer release() noexcept;
328 void reset(pointer p = pointer()) noexcept;
329 void reset(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000330 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000331 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000332};
333
334template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000335 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000336
337template <class T1, class D1, class T2, class D2>
338 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
339template <class T1, class D1, class T2, class D2>
340 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
341template <class T1, class D1, class T2, class D2>
342 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
343template <class T1, class D1, class T2, class D2>
344 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
345template <class T1, class D1, class T2, class D2>
346 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
347template <class T1, class D1, class T2, class D2>
348 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
349
Howard Hinnant719bda32011-05-28 14:41:13 +0000350template <class T, class D>
351 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
352template <class T, class D>
353 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
354template <class T, class D>
355 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
356template <class T, class D>
357 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
358
359template <class T, class D>
360 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
361template <class T, class D>
362 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
363template <class T, class D>
364 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
365template <class T, class D>
366 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
367template <class T, class D>
368 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
369template <class T, class D>
370 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
371template <class T, class D>
372 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
373template <class T, class D>
374 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
375
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000376class bad_weak_ptr
377 : public std::exception
378{
Howard Hinnant719bda32011-05-28 14:41:13 +0000379 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000380};
381
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000382template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
383template<class T> unique_ptr<T> make_unique(size_t n); // C++14
384template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
385
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000386template<class T>
387class shared_ptr
388{
389public:
390 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000391 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000392
393 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000394 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000395 template<class Y> explicit shared_ptr(Y* p);
396 template<class Y, class D> shared_ptr(Y* p, D d);
397 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
398 template <class D> shared_ptr(nullptr_t p, D d);
399 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000400 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
401 shared_ptr(const shared_ptr& r) noexcept;
402 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
403 shared_ptr(shared_ptr&& r) noexcept;
404 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000405 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
406 template<class Y> shared_ptr(auto_ptr<Y>&& r);
407 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
408 shared_ptr(nullptr_t) : shared_ptr() { }
409
410 // destructor:
411 ~shared_ptr();
412
413 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000414 shared_ptr& operator=(const shared_ptr& r) noexcept;
415 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
416 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000417 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
418 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r);
419 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
420
421 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000422 void swap(shared_ptr& r) noexcept;
423 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000424 template<class Y> void reset(Y* p);
425 template<class Y, class D> void reset(Y* p, D d);
426 template<class Y, class D, class A> void reset(Y* p, D d, A a);
427
Howard Hinnant719bda32011-05-28 14:41:13 +0000428 // observers:
429 T* get() const noexcept;
430 T& operator*() const noexcept;
431 T* operator->() const noexcept;
432 long use_count() const noexcept;
433 bool unique() const noexcept;
434 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000435 template<class U> bool owner_before(shared_ptr<U> const& b) const;
436 template<class U> bool owner_before(weak_ptr<U> const& b) const;
437};
438
439// shared_ptr comparisons:
440template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000441 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000442template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000443 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000444template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000445 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000446template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000447 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000448template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000449 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000450template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000451 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
452
453template <class T>
454 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
455template <class T>
456 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
457template <class T>
458 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
459template <class T>
460 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
461template <class T>
462 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
463template <class T>
464bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
465template <class T>
466 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
467template <class T>
468 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
469template <class T>
470 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
471template <class T>
472 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
473template <class T>
474 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
475template <class T>
476 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000477
478// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000479template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000480
481// shared_ptr casts:
482template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000483 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000484template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000485 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000486template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000487 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000488
489// shared_ptr I/O:
490template<class E, class T, class Y>
491 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
492
493// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000494template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000495
496template<class T, class... Args>
497 shared_ptr<T> make_shared(Args&&... args);
498template<class T, class A, class... Args>
499 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
500
501template<class T>
502class weak_ptr
503{
504public:
505 typedef T element_type;
506
507 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000508 constexpr weak_ptr() noexcept;
509 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
510 weak_ptr(weak_ptr const& r) noexcept;
511 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000512 weak_ptr(weak_ptr&& r) noexcept; // C++14
513 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000514
515 // destructor
516 ~weak_ptr();
517
518 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000519 weak_ptr& operator=(weak_ptr const& r) noexcept;
520 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
521 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000522 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
523 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000524
525 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000526 void swap(weak_ptr& r) noexcept;
527 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000528
529 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000530 long use_count() const noexcept;
531 bool expired() const noexcept;
532 shared_ptr<T> lock() const noexcept;
Marshall Clow495e4a62013-09-03 14:37:50 +0000533 template<class U> bool owner_before(shared_ptr<U> const& b) const;
534 template<class U> bool owner_before(weak_ptr<U> const& b) const;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000535};
536
537// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000538template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000539
540// class owner_less:
541template<class T> struct owner_less;
542
543template<class T>
544struct owner_less<shared_ptr<T>>
545 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
546{
547 typedef bool result_type;
548 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const;
549 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
550 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
551};
552
553template<class T>
554struct owner_less<weak_ptr<T>>
555 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
556{
557 typedef bool result_type;
558 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const;
559 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
560 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
561};
562
563template<class T>
564class enable_shared_from_this
565{
566protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000567 constexpr enable_shared_from_this() noexcept;
568 enable_shared_from_this(enable_shared_from_this const&) noexcept;
569 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000570 ~enable_shared_from_this();
571public:
572 shared_ptr<T> shared_from_this();
573 shared_ptr<T const> shared_from_this() const;
574};
575
576template<class T>
577 bool atomic_is_lock_free(const shared_ptr<T>* p);
578template<class T>
579 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
580template<class T>
581 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
582template<class T>
583 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
584template<class T>
585 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
586template<class T>
587 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
588template<class T>
589 shared_ptr<T>
590 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
591template<class T>
592 bool
593 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
594template<class T>
595 bool
596 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
597template<class T>
598 bool
599 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
600 shared_ptr<T> w, memory_order success,
601 memory_order failure);
602template<class T>
603 bool
604 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
605 shared_ptr<T> w, memory_order success,
606 memory_order failure);
607// Hash support
608template <class T> struct hash;
609template <class T, class D> struct hash<unique_ptr<T, D> >;
610template <class T> struct hash<shared_ptr<T> >;
611
612// Pointer safety
613enum class pointer_safety { relaxed, preferred, strict };
614void declare_reachable(void *p);
615template <class T> T *undeclare_reachable(T *p);
616void declare_no_pointers(char *p, size_t n);
617void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000618pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000619
Howard Hinnantc51e1022010-05-11 19:42:16 +0000620void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
621
622} // std
623
624*/
625
626#include <__config>
627#include <type_traits>
628#include <typeinfo>
629#include <cstddef>
630#include <cstdint>
631#include <new>
632#include <utility>
633#include <limits>
634#include <iterator>
635#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000636#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000637#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000638#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000639#include <cstring>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000640
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000641#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000642# include <atomic>
643#endif
644
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000645#include <__undef_min_max>
Saleem Abdulrasooldcf27a62015-02-13 22:15:32 +0000646#include <__undef___deallocate>
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000647
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000648#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000649#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000650#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000651
652_LIBCPP_BEGIN_NAMESPACE_STD
653
Eric Fiselier89659d12015-07-07 00:27:16 +0000654template <class _ValueType>
655inline _LIBCPP_ALWAYS_INLINE
656_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
657#if !defined(_LIBCPP_HAS_NO_THREADS) && \
658 defined(__ATOMIC_RELAXED) && \
659 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
660 return __atomic_load_n(__value, __ATOMIC_RELAXED);
661#else
662 return *__value;
663#endif
664}
665
Kuba Breckade9d6792016-09-04 09:55:12 +0000666template <class _ValueType>
667inline _LIBCPP_ALWAYS_INLINE
668_ValueType __libcpp_acquire_load(_ValueType const* __value) {
669#if !defined(_LIBCPP_HAS_NO_THREADS) && \
670 defined(__ATOMIC_ACQUIRE) && \
671 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
672 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
673#else
674 return *__value;
675#endif
676}
677
Howard Hinnant8bea6da2013-08-08 18:38:55 +0000678// addressof moved to <__functional_base>
Douglas Gregor13034442011-06-22 22:17:44 +0000679
Howard Hinnantc51e1022010-05-11 19:42:16 +0000680template <class _Tp> class allocator;
681
682template <>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000683class _LIBCPP_TYPE_VIS_ONLY allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000684{
685public:
686 typedef void* pointer;
687 typedef const void* const_pointer;
688 typedef void value_type;
689
690 template <class _Up> struct rebind {typedef allocator<_Up> other;};
691};
692
Howard Hinnant9f771052012-01-19 23:15:22 +0000693template <>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000694class _LIBCPP_TYPE_VIS_ONLY allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000695{
696public:
697 typedef const void* pointer;
698 typedef const void* const_pointer;
699 typedef const void value_type;
700
701 template <class _Up> struct rebind {typedef allocator<_Up> other;};
702};
703
Howard Hinnantc51e1022010-05-11 19:42:16 +0000704// pointer_traits
705
706template <class _Tp>
707struct __has_element_type
708{
709private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000710 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000711 template <class _Up> static __two __test(...);
712 template <class _Up> static char __test(typename _Up::element_type* = 0);
713public:
714 static const bool value = sizeof(__test<_Tp>(0)) == 1;
715};
716
717template <class _Ptr, bool = __has_element_type<_Ptr>::value>
718struct __pointer_traits_element_type;
719
720template <class _Ptr>
721struct __pointer_traits_element_type<_Ptr, true>
722{
723 typedef typename _Ptr::element_type type;
724};
725
726#ifndef _LIBCPP_HAS_NO_VARIADICS
727
728template <template <class, class...> class _Sp, class _Tp, class ..._Args>
729struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
730{
731 typedef typename _Sp<_Tp, _Args...>::element_type type;
732};
733
734template <template <class, class...> class _Sp, class _Tp, class ..._Args>
735struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
736{
737 typedef _Tp type;
738};
739
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000740#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000741
742template <template <class> class _Sp, class _Tp>
743struct __pointer_traits_element_type<_Sp<_Tp>, true>
744{
745 typedef typename _Sp<_Tp>::element_type type;
746};
747
748template <template <class> class _Sp, class _Tp>
749struct __pointer_traits_element_type<_Sp<_Tp>, false>
750{
751 typedef _Tp type;
752};
753
754template <template <class, class> class _Sp, class _Tp, class _A0>
755struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
756{
757 typedef typename _Sp<_Tp, _A0>::element_type type;
758};
759
760template <template <class, class> class _Sp, class _Tp, class _A0>
761struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
762{
763 typedef _Tp type;
764};
765
766template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
767struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
768{
769 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
770};
771
772template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
773struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
774{
775 typedef _Tp type;
776};
777
778template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
779 class _A1, class _A2>
780struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
781{
782 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
783};
784
785template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
786 class _A1, class _A2>
787struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
788{
789 typedef _Tp type;
790};
791
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000792#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000793
794template <class _Tp>
795struct __has_difference_type
796{
797private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000798 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000799 template <class _Up> static __two __test(...);
800 template <class _Up> static char __test(typename _Up::difference_type* = 0);
801public:
802 static const bool value = sizeof(__test<_Tp>(0)) == 1;
803};
804
805template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
806struct __pointer_traits_difference_type
807{
808 typedef ptrdiff_t type;
809};
810
811template <class _Ptr>
812struct __pointer_traits_difference_type<_Ptr, true>
813{
814 typedef typename _Ptr::difference_type type;
815};
816
817template <class _Tp, class _Up>
818struct __has_rebind
819{
820private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000821 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000822 template <class _Xp> static __two __test(...);
823 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
824public:
825 static const bool value = sizeof(__test<_Tp>(0)) == 1;
826};
827
828template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
829struct __pointer_traits_rebind
830{
831#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
832 typedef typename _Tp::template rebind<_Up> type;
833#else
834 typedef typename _Tp::template rebind<_Up>::other type;
835#endif
836};
837
838#ifndef _LIBCPP_HAS_NO_VARIADICS
839
840template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
841struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
842{
843#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
844 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
845#else
846 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
847#endif
848};
849
850template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
851struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
852{
853 typedef _Sp<_Up, _Args...> type;
854};
855
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000856#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000857
858template <template <class> class _Sp, class _Tp, class _Up>
859struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
860{
861#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
862 typedef typename _Sp<_Tp>::template rebind<_Up> type;
863#else
864 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
865#endif
866};
867
868template <template <class> class _Sp, class _Tp, class _Up>
869struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
870{
871 typedef _Sp<_Up> type;
872};
873
874template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
875struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
876{
877#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
878 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
879#else
880 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
881#endif
882};
883
884template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
885struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
886{
887 typedef _Sp<_Up, _A0> type;
888};
889
890template <template <class, class, class> class _Sp, class _Tp, class _A0,
891 class _A1, class _Up>
892struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
893{
894#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
895 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
896#else
897 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
898#endif
899};
900
901template <template <class, class, class> class _Sp, class _Tp, class _A0,
902 class _A1, class _Up>
903struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
904{
905 typedef _Sp<_Up, _A0, _A1> type;
906};
907
908template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
909 class _A1, class _A2, class _Up>
910struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
911{
912#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
913 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
914#else
915 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
916#endif
917};
918
919template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
920 class _A1, class _A2, class _Up>
921struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
922{
923 typedef _Sp<_Up, _A0, _A1, _A2> type;
924};
925
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000926#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000927
928template <class _Ptr>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000929struct _LIBCPP_TYPE_VIS_ONLY pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000930{
931 typedef _Ptr pointer;
932 typedef typename __pointer_traits_element_type<pointer>::type element_type;
933 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
934
935#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantf09283d2011-05-11 20:21:19 +0000936 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000937#else
938 template <class _Up> struct rebind
939 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000940#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000941
942private:
943 struct __nat {};
944public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000945 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946 static pointer pointer_to(typename conditional<is_void<element_type>::value,
947 __nat, element_type>::type& __r)
948 {return pointer::pointer_to(__r);}
949};
950
951template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000952struct _LIBCPP_TYPE_VIS_ONLY pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000953{
954 typedef _Tp* pointer;
955 typedef _Tp element_type;
956 typedef ptrdiff_t difference_type;
957
958#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
959 template <class _Up> using rebind = _Up*;
960#else
961 template <class _Up> struct rebind {typedef _Up* other;};
962#endif
963
964private:
965 struct __nat {};
966public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000969 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000970 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971};
972
Eric Fiselierae3ab842015-08-23 02:56:05 +0000973template <class _From, class _To>
974struct __rebind_pointer {
975#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
976 typedef typename pointer_traits<_From>::template rebind<_To> type;
977#else
978 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
979#endif
980};
981
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982// allocator_traits
983
984namespace __has_pointer_type_imp
985{
Howard Hinnant6e260172013-09-21 01:45:05 +0000986 template <class _Up> static __two __test(...);
987 template <class _Up> static char __test(typename _Up::pointer* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988}
989
990template <class _Tp>
991struct __has_pointer_type
Howard Hinnant6e260172013-09-21 01:45:05 +0000992 : public integral_constant<bool, sizeof(__has_pointer_type_imp::__test<_Tp>(0)) == 1>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000993{
994};
995
996namespace __pointer_type_imp
997{
998
999template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1000struct __pointer_type
1001{
1002 typedef typename _Dp::pointer type;
1003};
1004
1005template <class _Tp, class _Dp>
1006struct __pointer_type<_Tp, _Dp, false>
1007{
1008 typedef _Tp* type;
1009};
1010
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001011} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001012
1013template <class _Tp, class _Dp>
1014struct __pointer_type
1015{
1016 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
1017};
1018
1019template <class _Tp>
1020struct __has_const_pointer
1021{
1022private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001023 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001024 template <class _Up> static __two __test(...);
1025 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
1026public:
1027 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1028};
1029
1030template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1031struct __const_pointer
1032{
1033 typedef typename _Alloc::const_pointer type;
1034};
1035
1036template <class _Tp, class _Ptr, class _Alloc>
1037struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1038{
1039#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1040 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1041#else
1042 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1043#endif
1044};
1045
1046template <class _Tp>
1047struct __has_void_pointer
1048{
1049private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001050 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051 template <class _Up> static __two __test(...);
1052 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1053public:
1054 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1055};
1056
1057template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1058struct __void_pointer
1059{
1060 typedef typename _Alloc::void_pointer type;
1061};
1062
1063template <class _Ptr, class _Alloc>
1064struct __void_pointer<_Ptr, _Alloc, false>
1065{
1066#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1067 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1068#else
1069 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1070#endif
1071};
1072
1073template <class _Tp>
1074struct __has_const_void_pointer
1075{
1076private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001077 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078 template <class _Up> static __two __test(...);
1079 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1080public:
1081 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1082};
1083
1084template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1085struct __const_void_pointer
1086{
1087 typedef typename _Alloc::const_void_pointer type;
1088};
1089
1090template <class _Ptr, class _Alloc>
1091struct __const_void_pointer<_Ptr, _Alloc, false>
1092{
1093#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1094 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1095#else
1096 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1097#endif
1098};
1099
Howard Hinnantc834c512011-11-29 18:15:50 +00001100template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001102_Tp*
1103__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104{
1105 return __p;
1106}
1107
1108template <class _Pointer>
1109inline _LIBCPP_INLINE_VISIBILITY
1110typename pointer_traits<_Pointer>::element_type*
Howard Hinnant719bda32011-05-28 14:41:13 +00001111__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001112{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001113 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114}
1115
1116template <class _Tp>
1117struct __has_size_type
1118{
1119private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001120 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121 template <class _Up> static __two __test(...);
1122 template <class _Up> static char __test(typename _Up::size_type* = 0);
1123public:
1124 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1125};
1126
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001127template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001128struct __size_type
1129{
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001130 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131};
1132
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001133template <class _Alloc, class _DiffType>
1134struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001135{
1136 typedef typename _Alloc::size_type type;
1137};
1138
1139template <class _Tp>
1140struct __has_propagate_on_container_copy_assignment
1141{
1142private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001143 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144 template <class _Up> static __two __test(...);
1145 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1146public:
1147 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1148};
1149
1150template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1151struct __propagate_on_container_copy_assignment
1152{
1153 typedef false_type type;
1154};
1155
1156template <class _Alloc>
1157struct __propagate_on_container_copy_assignment<_Alloc, true>
1158{
1159 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1160};
1161
1162template <class _Tp>
1163struct __has_propagate_on_container_move_assignment
1164{
1165private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001166 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167 template <class _Up> static __two __test(...);
1168 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1169public:
1170 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1171};
1172
1173template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1174struct __propagate_on_container_move_assignment
1175{
1176 typedef false_type type;
1177};
1178
1179template <class _Alloc>
1180struct __propagate_on_container_move_assignment<_Alloc, true>
1181{
1182 typedef typename _Alloc::propagate_on_container_move_assignment type;
1183};
1184
1185template <class _Tp>
1186struct __has_propagate_on_container_swap
1187{
1188private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001189 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190 template <class _Up> static __two __test(...);
1191 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1192public:
1193 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1194};
1195
1196template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1197struct __propagate_on_container_swap
1198{
1199 typedef false_type type;
1200};
1201
1202template <class _Alloc>
1203struct __propagate_on_container_swap<_Alloc, true>
1204{
1205 typedef typename _Alloc::propagate_on_container_swap type;
1206};
1207
Marshall Clow0b587562015-06-02 16:34:03 +00001208template <class _Tp>
1209struct __has_is_always_equal
1210{
1211private:
1212 struct __two {char __lx; char __lxx;};
1213 template <class _Up> static __two __test(...);
1214 template <class _Up> static char __test(typename _Up::is_always_equal* = 0);
1215public:
1216 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1217};
1218
1219template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1220struct __is_always_equal
1221{
1222 typedef typename _VSTD::is_empty<_Alloc>::type type;
1223};
1224
1225template <class _Alloc>
1226struct __is_always_equal<_Alloc, true>
1227{
1228 typedef typename _Alloc::is_always_equal type;
1229};
1230
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1232struct __has_rebind_other
1233{
1234private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001235 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236 template <class _Xp> static __two __test(...);
1237 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1238public:
1239 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1240};
1241
1242template <class _Tp, class _Up>
1243struct __has_rebind_other<_Tp, _Up, false>
1244{
1245 static const bool value = false;
1246};
1247
1248template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1249struct __allocator_traits_rebind
1250{
1251 typedef typename _Tp::template rebind<_Up>::other type;
1252};
1253
1254#ifndef _LIBCPP_HAS_NO_VARIADICS
1255
1256template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1257struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1258{
1259 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1260};
1261
1262template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1263struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1264{
1265 typedef _Alloc<_Up, _Args...> type;
1266};
1267
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001268#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001269
1270template <template <class> class _Alloc, class _Tp, class _Up>
1271struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1272{
1273 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1274};
1275
1276template <template <class> class _Alloc, class _Tp, class _Up>
1277struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1278{
1279 typedef _Alloc<_Up> type;
1280};
1281
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1283struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1284{
1285 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1286};
1287
1288template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1289struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1290{
1291 typedef _Alloc<_Up, _A0> type;
1292};
1293
Howard Hinnantc51e1022010-05-11 19:42:16 +00001294template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1295 class _A1, class _Up>
1296struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1297{
1298 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1299};
1300
1301template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1302 class _A1, class _Up>
1303struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1304{
1305 typedef _Alloc<_Up, _A0, _A1> type;
1306};
1307
Howard Hinnantc51e1022010-05-11 19:42:16 +00001308template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1309 class _A1, class _A2, class _Up>
1310struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1311{
1312 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1313};
1314
1315template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1316 class _A1, class _A2, class _Up>
1317struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1318{
1319 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1320};
1321
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001322#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323
1324#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1325
1326template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1327auto
1328__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1329 -> decltype(__a.allocate(__sz, __p), true_type());
1330
1331template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1332auto
1333__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1334 -> false_type;
1335
1336template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1337struct __has_allocate_hint
1338 : integral_constant<bool,
1339 is_same<
1340 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1341 declval<_SizeType>(),
1342 declval<_ConstVoidPtr>())),
1343 true_type>::value>
1344{
1345};
1346
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001347#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001348
1349template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1350struct __has_allocate_hint
1351 : true_type
1352{
1353};
1354
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001355#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001356
Howard Hinnant986832c2011-07-29 21:35:53 +00001357#if !defined(_LIBCPP_HAS_NO_ADVANCED_SFINAE) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358
1359template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001360decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1361 _VSTD::declval<_Args>()...),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001362 true_type())
1363__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1364
1365template <class _Alloc, class _Pointer, class ..._Args>
1366false_type
1367__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1368
1369template <class _Alloc, class _Pointer, class ..._Args>
1370struct __has_construct
1371 : integral_constant<bool,
1372 is_same<
1373 decltype(__has_construct_test(declval<_Alloc>(),
1374 declval<_Pointer>(),
1375 declval<_Args>()...)),
1376 true_type>::value>
1377{
1378};
1379
1380template <class _Alloc, class _Pointer>
1381auto
1382__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1383 -> decltype(__a.destroy(__p), true_type());
1384
1385template <class _Alloc, class _Pointer>
1386auto
1387__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1388 -> false_type;
1389
1390template <class _Alloc, class _Pointer>
1391struct __has_destroy
1392 : integral_constant<bool,
1393 is_same<
1394 decltype(__has_destroy_test(declval<_Alloc>(),
1395 declval<_Pointer>())),
1396 true_type>::value>
1397{
1398};
1399
1400template <class _Alloc>
1401auto
1402__has_max_size_test(_Alloc&& __a)
1403 -> decltype(__a.max_size(), true_type());
1404
1405template <class _Alloc>
1406auto
1407__has_max_size_test(const volatile _Alloc& __a)
1408 -> false_type;
1409
1410template <class _Alloc>
1411struct __has_max_size
1412 : integral_constant<bool,
1413 is_same<
1414 decltype(__has_max_size_test(declval<_Alloc&>())),
1415 true_type>::value>
1416{
1417};
1418
1419template <class _Alloc>
1420auto
1421__has_select_on_container_copy_construction_test(_Alloc&& __a)
1422 -> decltype(__a.select_on_container_copy_construction(), true_type());
1423
1424template <class _Alloc>
1425auto
1426__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1427 -> false_type;
1428
1429template <class _Alloc>
1430struct __has_select_on_container_copy_construction
1431 : integral_constant<bool,
1432 is_same<
1433 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1434 true_type>::value>
1435{
1436};
1437
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001438#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439
1440#ifndef _LIBCPP_HAS_NO_VARIADICS
1441
1442template <class _Alloc, class _Pointer, class ..._Args>
1443struct __has_construct
1444 : false_type
1445{
1446};
1447
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001448#else // _LIBCPP_HAS_NO_VARIADICS
1449
1450template <class _Alloc, class _Pointer, class _Args>
1451struct __has_construct
1452 : false_type
1453{
1454};
1455
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001456#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457
1458template <class _Alloc, class _Pointer>
1459struct __has_destroy
1460 : false_type
1461{
1462};
1463
1464template <class _Alloc>
1465struct __has_max_size
1466 : true_type
1467{
1468};
1469
1470template <class _Alloc>
1471struct __has_select_on_container_copy_construction
1472 : false_type
1473{
1474};
1475
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001476#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001478template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1479struct __alloc_traits_difference_type
1480{
1481 typedef typename pointer_traits<_Ptr>::difference_type type;
1482};
1483
1484template <class _Alloc, class _Ptr>
1485struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1486{
1487 typedef typename _Alloc::difference_type type;
1488};
1489
Howard Hinnantc51e1022010-05-11 19:42:16 +00001490template <class _Alloc>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001491struct _LIBCPP_TYPE_VIS_ONLY allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492{
1493 typedef _Alloc allocator_type;
1494 typedef typename allocator_type::value_type value_type;
1495
1496 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1497 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1498 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1499 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1500
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001501 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1502 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001503
1504 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1505 propagate_on_container_copy_assignment;
1506 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1507 propagate_on_container_move_assignment;
1508 typedef typename __propagate_on_container_swap<allocator_type>::type
1509 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001510 typedef typename __is_always_equal<allocator_type>::type
1511 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001512
1513#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1514 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001515 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001516 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001517#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001518 template <class _Tp> struct rebind_alloc
1519 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1520 template <class _Tp> struct rebind_traits
1521 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001522#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001523
Howard Hinnant756c69b2010-09-22 16:48:34 +00001524 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001525 static pointer allocate(allocator_type& __a, size_type __n)
1526 {return __a.allocate(__n);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001527 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001528 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1529 {return allocate(__a, __n, __hint,
1530 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1531
Howard Hinnant756c69b2010-09-22 16:48:34 +00001532 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001533 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534 {__a.deallocate(__p, __n);}
1535
1536#ifndef _LIBCPP_HAS_NO_VARIADICS
1537 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001539 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001540 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001541 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001542#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545 static void construct(allocator_type& __a, _Tp* __p)
1546 {
1547 ::new ((void*)__p) _Tp();
1548 }
1549 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001551 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1552 {
1553 ::new ((void*)__p) _Tp(__a0);
1554 }
1555 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001557 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1558 const _A1& __a1)
1559 {
1560 ::new ((void*)__p) _Tp(__a0, __a1);
1561 }
1562 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1565 const _A1& __a1, const _A2& __a2)
1566 {
1567 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1568 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001569#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570
1571 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001572 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 static void destroy(allocator_type& __a, _Tp* __p)
1574 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1575
Howard Hinnant756c69b2010-09-22 16:48:34 +00001576 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001577 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1579
Howard Hinnant756c69b2010-09-22 16:48:34 +00001580 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001581 static allocator_type
1582 select_on_container_copy_construction(const allocator_type& __a)
1583 {return select_on_container_copy_construction(
1584 __has_select_on_container_copy_construction<const allocator_type>(),
1585 __a);}
1586
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001587 template <class _Ptr>
1588 _LIBCPP_INLINE_VISIBILITY
1589 static
1590 void
1591 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1592 {
1593 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1594 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1595 }
1596
1597 template <class _Tp>
1598 _LIBCPP_INLINE_VISIBILITY
1599 static
1600 typename enable_if
1601 <
1602 (is_same<allocator_type, allocator<_Tp> >::value
1603 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1604 is_trivially_move_constructible<_Tp>::value,
1605 void
1606 >::type
1607 __construct_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1608 {
1609 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001610 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001611 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001612 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001613 __begin2 += _Np;
1614 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001615 }
1616
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001617 template <class _Iter, class _Ptr>
1618 _LIBCPP_INLINE_VISIBILITY
1619 static
1620 void
1621 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1622 {
1623 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1624 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1625 }
1626
1627 template <class _Tp>
1628 _LIBCPP_INLINE_VISIBILITY
1629 static
1630 typename enable_if
1631 <
1632 (is_same<allocator_type, allocator<_Tp> >::value
1633 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1634 is_trivially_move_constructible<_Tp>::value,
1635 void
1636 >::type
1637 __construct_range_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1638 {
1639 typedef typename remove_const<_Tp>::type _Vp;
1640 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001641 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001642 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001643 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001644 __begin2 += _Np;
1645 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001646 }
1647
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001648 template <class _Ptr>
1649 _LIBCPP_INLINE_VISIBILITY
1650 static
1651 void
1652 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1653 {
1654 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001655 {
1656 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1657 --__end2;
1658 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001659 }
1660
1661 template <class _Tp>
1662 _LIBCPP_INLINE_VISIBILITY
1663 static
1664 typename enable_if
1665 <
1666 (is_same<allocator_type, allocator<_Tp> >::value
1667 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1668 is_trivially_move_constructible<_Tp>::value,
1669 void
1670 >::type
1671 __construct_backward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
1672 {
1673 ptrdiff_t _Np = __end1 - __begin1;
1674 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001675 if (_Np > 0)
1676 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001677 }
1678
Howard Hinnantc51e1022010-05-11 19:42:16 +00001679private:
1680
Howard Hinnant756c69b2010-09-22 16:48:34 +00001681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001682 static pointer allocate(allocator_type& __a, size_type __n,
1683 const_void_pointer __hint, true_type)
1684 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001685 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001686 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001687 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001688 {return __a.allocate(__n);}
1689
1690#ifndef _LIBCPP_HAS_NO_VARIADICS
1691 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001693 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001694 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001696 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001697 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1698 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001699 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001701#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001702
1703 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001704 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001705 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1706 {__a.destroy(__p);}
1707 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001708 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001709 static void __destroy(false_type, allocator_type&, _Tp* __p)
1710 {
1711 __p->~_Tp();
1712 }
1713
Howard Hinnant756c69b2010-09-22 16:48:34 +00001714 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715 static size_type __max_size(true_type, const allocator_type& __a)
1716 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001717 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718 static size_type __max_size(false_type, const allocator_type&)
Marshall Clow33daa162015-10-25 19:34:04 +00001719 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720
Howard Hinnant756c69b2010-09-22 16:48:34 +00001721 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001722 static allocator_type
1723 select_on_container_copy_construction(true_type, const allocator_type& __a)
1724 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001726 static allocator_type
1727 select_on_container_copy_construction(false_type, const allocator_type& __a)
1728 {return __a;}
1729};
1730
Marshall Clow940e01c2015-04-07 05:21:38 +00001731template <class _Traits, class _Tp>
1732struct __rebind_alloc_helper
1733{
1734#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Marshall Clow35cde742015-05-10 13:59:45 +00001735 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001736#else
1737 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1738#endif
1739};
1740
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741// allocator
1742
1743template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001744class _LIBCPP_TYPE_VIS_ONLY allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001745{
1746public:
1747 typedef size_t size_type;
1748 typedef ptrdiff_t difference_type;
1749 typedef _Tp* pointer;
1750 typedef const _Tp* const_pointer;
1751 typedef _Tp& reference;
1752 typedef const _Tp& const_reference;
1753 typedef _Tp value_type;
1754
Howard Hinnant4931e092011-06-02 21:38:57 +00001755 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001756 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001757
Howard Hinnantc51e1022010-05-11 19:42:16 +00001758 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1759
Howard Hinnant719bda32011-05-28 14:41:13 +00001760 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1761 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1762 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001763 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001764 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001765 {return _VSTD::addressof(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001766 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001767 {
1768 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001769 __throw_length_error("allocator<T>::allocate(size_t n)"
1770 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001771 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
1772 }
Howard Hinnant719bda32011-05-28 14:41:13 +00001773 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith1f1c1472014-06-04 19:54:15 +00001774 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001775 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1776 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001777#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001778 template <class _Up, class... _Args>
1779 _LIBCPP_INLINE_VISIBILITY
1780 void
1781 construct(_Up* __p, _Args&&... __args)
1782 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001783 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001784 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001785#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786 _LIBCPP_INLINE_VISIBILITY
1787 void
1788 construct(pointer __p)
1789 {
1790 ::new((void*)__p) _Tp();
1791 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001792# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001793
Howard Hinnantc51e1022010-05-11 19:42:16 +00001794 template <class _A0>
1795 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001796 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001797 construct(pointer __p, _A0& __a0)
1798 {
1799 ::new((void*)__p) _Tp(__a0);
1800 }
1801 template <class _A0>
1802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001803 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001804 construct(pointer __p, const _A0& __a0)
1805 {
1806 ::new((void*)__p) _Tp(__a0);
1807 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001808# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001809 template <class _A0, class _A1>
1810 _LIBCPP_INLINE_VISIBILITY
1811 void
1812 construct(pointer __p, _A0& __a0, _A1& __a1)
1813 {
1814 ::new((void*)__p) _Tp(__a0, __a1);
1815 }
1816 template <class _A0, class _A1>
1817 _LIBCPP_INLINE_VISIBILITY
1818 void
1819 construct(pointer __p, const _A0& __a0, _A1& __a1)
1820 {
1821 ::new((void*)__p) _Tp(__a0, __a1);
1822 }
1823 template <class _A0, class _A1>
1824 _LIBCPP_INLINE_VISIBILITY
1825 void
1826 construct(pointer __p, _A0& __a0, const _A1& __a1)
1827 {
1828 ::new((void*)__p) _Tp(__a0, __a1);
1829 }
1830 template <class _A0, class _A1>
1831 _LIBCPP_INLINE_VISIBILITY
1832 void
1833 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1834 {
1835 ::new((void*)__p) _Tp(__a0, __a1);
1836 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001837#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1839};
1840
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001841template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001842class _LIBCPP_TYPE_VIS_ONLY allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001843{
1844public:
1845 typedef size_t size_type;
1846 typedef ptrdiff_t difference_type;
1847 typedef const _Tp* pointer;
1848 typedef const _Tp* const_pointer;
1849 typedef const _Tp& reference;
1850 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001851 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001852
1853 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001854 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001855
1856 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1857
1858 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1859 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1860 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1861 {return _VSTD::addressof(__x);}
1862 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001863 {
1864 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001865 __throw_length_error("allocator<const T>::allocate(size_t n)"
1866 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001867 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001868 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001869 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith1f1c1472014-06-04 19:54:15 +00001870 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001871 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1872 {return size_type(~0) / sizeof(_Tp);}
1873#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1874 template <class _Up, class... _Args>
1875 _LIBCPP_INLINE_VISIBILITY
1876 void
1877 construct(_Up* __p, _Args&&... __args)
1878 {
1879 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1880 }
1881#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1882 _LIBCPP_INLINE_VISIBILITY
1883 void
1884 construct(pointer __p)
1885 {
1886 ::new((void*)__p) _Tp();
1887 }
1888# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001889
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001890 template <class _A0>
1891 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001892 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001893 construct(pointer __p, _A0& __a0)
1894 {
1895 ::new((void*)__p) _Tp(__a0);
1896 }
1897 template <class _A0>
1898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001899 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001900 construct(pointer __p, const _A0& __a0)
1901 {
1902 ::new((void*)__p) _Tp(__a0);
1903 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001904# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1905 template <class _A0, class _A1>
1906 _LIBCPP_INLINE_VISIBILITY
1907 void
1908 construct(pointer __p, _A0& __a0, _A1& __a1)
1909 {
1910 ::new((void*)__p) _Tp(__a0, __a1);
1911 }
1912 template <class _A0, class _A1>
1913 _LIBCPP_INLINE_VISIBILITY
1914 void
1915 construct(pointer __p, const _A0& __a0, _A1& __a1)
1916 {
1917 ::new((void*)__p) _Tp(__a0, __a1);
1918 }
1919 template <class _A0, class _A1>
1920 _LIBCPP_INLINE_VISIBILITY
1921 void
1922 construct(pointer __p, _A0& __a0, const _A1& __a1)
1923 {
1924 ::new((void*)__p) _Tp(__a0, __a1);
1925 }
1926 template <class _A0, class _A1>
1927 _LIBCPP_INLINE_VISIBILITY
1928 void
1929 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1930 {
1931 ::new((void*)__p) _Tp(__a0, __a1);
1932 }
1933#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1934 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1935};
1936
Howard Hinnantc51e1022010-05-11 19:42:16 +00001937template <class _Tp, class _Up>
1938inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001939bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940
1941template <class _Tp, class _Up>
1942inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001943bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001944
1945template <class _OutputIterator, class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001946class _LIBCPP_TYPE_VIS_ONLY raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001947 : public iterator<output_iterator_tag,
1948 _Tp, // purposefully not C++03
1949 ptrdiff_t, // purposefully not C++03
1950 _Tp*, // purposefully not C++03
1951 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1952{
1953private:
1954 _OutputIterator __x_;
1955public:
1956 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1957 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1958 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1959 {::new(&*__x_) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001960#if _LIBCPP_STD_VER >= 14
1961 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1962 {::new(&*__x_) _Tp(_VSTD::move(__element)); return *this;}
1963#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001964 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1965 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1966 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001967#if _LIBCPP_STD_VER >= 14
1968 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1969#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001970};
1971
1972template <class _Tp>
1973pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001974get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001975{
1976 pair<_Tp*, ptrdiff_t> __r(0, 0);
1977 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1978 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1979 / sizeof(_Tp);
1980 if (__n > __m)
1981 __n = __m;
1982 while (__n > 0)
1983 {
1984 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1985 if (__r.first)
1986 {
1987 __r.second = __n;
1988 break;
1989 }
1990 __n /= 2;
1991 }
1992 return __r;
1993}
1994
1995template <class _Tp>
1996inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001997void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001998
1999template <class _Tp>
2000struct auto_ptr_ref
2001{
2002 _Tp* __ptr_;
2003};
2004
2005template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002006class _LIBCPP_TYPE_VIS_ONLY auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002007{
2008private:
2009 _Tp* __ptr_;
2010public:
2011 typedef _Tp element_type;
2012
2013 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2014 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2015 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2016 : __ptr_(__p.release()) {}
2017 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2018 {reset(__p.release()); return *this;}
2019 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2020 {reset(__p.release()); return *this;}
2021 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2022 {reset(__p.__ptr_); return *this;}
2023 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2024
2025 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2026 {return *__ptr_;}
2027 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2028 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2029 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2030 {
2031 _Tp* __t = __ptr_;
2032 __ptr_ = 0;
2033 return __t;
2034 }
2035 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2036 {
2037 if (__ptr_ != __p)
2038 delete __ptr_;
2039 __ptr_ = __p;
2040 }
2041
2042 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2043 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2044 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2045 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2046 {return auto_ptr<_Up>(release());}
2047};
2048
2049template <>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002050class _LIBCPP_TYPE_VIS_ONLY auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002051{
2052public:
2053 typedef void element_type;
2054};
2055
Howard Hinnantc51e1022010-05-11 19:42:16 +00002056template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
2057 typename remove_cv<_T2>::type>::value,
Howard Hinnantd0aabf82011-12-11 20:31:33 +00002058 bool = is_empty<_T1>::value
Eric Fiselierf7394302015-06-13 07:08:02 +00002059 && !__libcpp_is_final<_T1>::value,
Howard Hinnantd0aabf82011-12-11 20:31:33 +00002060 bool = is_empty<_T2>::value
Eric Fiselierf7394302015-06-13 07:08:02 +00002061 && !__libcpp_is_final<_T2>::value
Howard Hinnantd0aabf82011-12-11 20:31:33 +00002062 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00002063struct __libcpp_compressed_pair_switch;
2064
2065template <class _T1, class _T2, bool IsSame>
2066struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
2067
2068template <class _T1, class _T2, bool IsSame>
2069struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
2070
2071template <class _T1, class _T2, bool IsSame>
2072struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
2073
2074template <class _T1, class _T2>
2075struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
2076
2077template <class _T1, class _T2>
2078struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
2079
2080template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
2081class __libcpp_compressed_pair_imp;
2082
2083template <class _T1, class _T2>
2084class __libcpp_compressed_pair_imp<_T1, _T2, 0>
2085{
2086private:
2087 _T1 __first_;
2088 _T2 __second_;
2089public:
2090 typedef _T1 _T1_param;
2091 typedef _T2 _T2_param;
2092
2093 typedef typename remove_reference<_T1>::type& _T1_reference;
2094 typedef typename remove_reference<_T2>::type& _T2_reference;
2095
2096 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2097 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2098
Marshall Clowac92bfe2015-07-16 03:05:06 +00002099 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_(), __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002100 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002101 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002102 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002103 : __first_(), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002104 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002105 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002106
Howard Hinnant59f73012013-11-13 00:39:22 +00002107#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002108
2109 _LIBCPP_INLINE_VISIBILITY
2110 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2111 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2112 is_nothrow_copy_constructible<_T2>::value)
2113 : __first_(__p.first()),
2114 __second_(__p.second()) {}
2115
2116 _LIBCPP_INLINE_VISIBILITY
2117 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2118 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2119 is_nothrow_copy_assignable<_T2>::value)
2120 {
2121 __first_ = __p.first();
2122 __second_ = __p.second();
2123 return *this;
2124 }
2125
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002126 _LIBCPP_INLINE_VISIBILITY
2127 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant719bda32011-05-28 14:41:13 +00002128 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2129 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002130 : __first_(_VSTD::forward<_T1>(__p.first())),
2131 __second_(_VSTD::forward<_T2>(__p.second())) {}
2132
2133 _LIBCPP_INLINE_VISIBILITY
2134 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2135 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2136 is_nothrow_move_assignable<_T2>::value)
2137 {
2138 __first_ = _VSTD::forward<_T1>(__p.first());
2139 __second_ = _VSTD::forward<_T2>(__p.second());
2140 return *this;
2141 }
2142
Howard Hinnant59f73012013-11-13 00:39:22 +00002143#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002144
Howard Hinnant83b1c052011-12-19 17:58:44 +00002145#ifndef _LIBCPP_HAS_NO_VARIADICS
2146
2147 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2148 _LIBCPP_INLINE_VISIBILITY
2149 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2150 tuple<_Args1...> __first_args,
2151 tuple<_Args2...> __second_args,
2152 __tuple_indices<_I1...>,
2153 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002154 : __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2155 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002156 {}
2157
2158#endif // _LIBCPP_HAS_NO_VARIADICS
2159
Howard Hinnant719bda32011-05-28 14:41:13 +00002160 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2161 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002162
Howard Hinnant719bda32011-05-28 14:41:13 +00002163 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2164 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002165
2166 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002167 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002168 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002169 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002170 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002171 swap(__first_, __x.__first_);
2172 swap(__second_, __x.__second_);
2173 }
2174};
2175
2176template <class _T1, class _T2>
2177class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2178 : private _T1
2179{
2180private:
2181 _T2 __second_;
2182public:
2183 typedef _T1 _T1_param;
2184 typedef _T2 _T2_param;
2185
2186 typedef _T1& _T1_reference;
2187 typedef typename remove_reference<_T2>::type& _T2_reference;
2188
2189 typedef const _T1& _T1_const_reference;
2190 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2191
Marshall Clowac92bfe2015-07-16 03:05:06 +00002192 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002193 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002194 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002195 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002196 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002198 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002199
Howard Hinnant59f73012013-11-13 00:39:22 +00002200#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002201
2202 _LIBCPP_INLINE_VISIBILITY
2203 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2204 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2205 is_nothrow_copy_constructible<_T2>::value)
2206 : _T1(__p.first()), __second_(__p.second()) {}
2207
2208 _LIBCPP_INLINE_VISIBILITY
2209 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2210 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2211 is_nothrow_copy_assignable<_T2>::value)
2212 {
2213 _T1::operator=(__p.first());
2214 __second_ = __p.second();
2215 return *this;
2216 }
2217
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002218 _LIBCPP_INLINE_VISIBILITY
2219 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant719bda32011-05-28 14:41:13 +00002220 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2221 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002222 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002223
2224 _LIBCPP_INLINE_VISIBILITY
2225 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2226 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2227 is_nothrow_move_assignable<_T2>::value)
2228 {
2229 _T1::operator=(_VSTD::move(__p.first()));
2230 __second_ = _VSTD::forward<_T2>(__p.second());
2231 return *this;
2232 }
2233
Howard Hinnant59f73012013-11-13 00:39:22 +00002234#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002235
Howard Hinnant83b1c052011-12-19 17:58:44 +00002236#ifndef _LIBCPP_HAS_NO_VARIADICS
2237
2238 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2239 _LIBCPP_INLINE_VISIBILITY
2240 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2241 tuple<_Args1...> __first_args,
2242 tuple<_Args2...> __second_args,
2243 __tuple_indices<_I1...>,
2244 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002245 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2246 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002247 {}
2248
2249#endif // _LIBCPP_HAS_NO_VARIADICS
2250
Howard Hinnant719bda32011-05-28 14:41:13 +00002251 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2252 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002253
Howard Hinnant719bda32011-05-28 14:41:13 +00002254 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2255 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002256
2257 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002258 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002259 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002260 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002261 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262 swap(__second_, __x.__second_);
2263 }
2264};
2265
2266template <class _T1, class _T2>
2267class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2268 : private _T2
2269{
2270private:
2271 _T1 __first_;
2272public:
2273 typedef _T1 _T1_param;
2274 typedef _T2 _T2_param;
2275
2276 typedef typename remove_reference<_T1>::type& _T1_reference;
2277 typedef _T2& _T2_reference;
2278
2279 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2280 typedef const _T2& _T2_const_reference;
2281
Marshall Clowac92bfe2015-07-16 03:05:06 +00002282 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002284 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002285 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002286 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002287 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant719bda32011-05-28 14:41:13 +00002288 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2289 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002290 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002291
Howard Hinnant59f73012013-11-13 00:39:22 +00002292#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002293
2294 _LIBCPP_INLINE_VISIBILITY
2295 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2296 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2297 is_nothrow_copy_constructible<_T2>::value)
2298 : _T2(__p.second()), __first_(__p.first()) {}
2299
2300 _LIBCPP_INLINE_VISIBILITY
2301 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2302 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2303 is_nothrow_copy_assignable<_T2>::value)
2304 {
2305 _T2::operator=(__p.second());
2306 __first_ = __p.first();
2307 return *this;
2308 }
2309
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002311 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002312 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2313 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002314 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002315
2316 _LIBCPP_INLINE_VISIBILITY
2317 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2318 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2319 is_nothrow_move_assignable<_T2>::value)
2320 {
2321 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2322 __first_ = _VSTD::move(__p.first());
2323 return *this;
2324 }
2325
Howard Hinnant59f73012013-11-13 00:39:22 +00002326#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002327
Howard Hinnant83b1c052011-12-19 17:58:44 +00002328#ifndef _LIBCPP_HAS_NO_VARIADICS
2329
2330 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2331 _LIBCPP_INLINE_VISIBILITY
2332 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2333 tuple<_Args1...> __first_args,
2334 tuple<_Args2...> __second_args,
2335 __tuple_indices<_I1...>,
2336 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002337 : _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...),
2338 __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002339
2340 {}
2341
2342#endif // _LIBCPP_HAS_NO_VARIADICS
2343
Howard Hinnant719bda32011-05-28 14:41:13 +00002344 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2345 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002346
Howard Hinnant719bda32011-05-28 14:41:13 +00002347 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2348 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002349
2350 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002351 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002352 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002353 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002354 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002355 swap(__first_, __x.__first_);
2356 }
2357};
2358
2359template <class _T1, class _T2>
2360class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2361 : private _T1,
2362 private _T2
2363{
2364public:
2365 typedef _T1 _T1_param;
2366 typedef _T2 _T2_param;
2367
2368 typedef _T1& _T1_reference;
2369 typedef _T2& _T2_reference;
2370
2371 typedef const _T1& _T1_const_reference;
2372 typedef const _T2& _T2_const_reference;
2373
2374 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2375 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002376 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002377 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002378 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002379 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002380 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002381
Howard Hinnant59f73012013-11-13 00:39:22 +00002382#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002383
2384 _LIBCPP_INLINE_VISIBILITY
2385 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2386 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2387 is_nothrow_copy_constructible<_T2>::value)
2388 : _T1(__p.first()), _T2(__p.second()) {}
2389
2390 _LIBCPP_INLINE_VISIBILITY
2391 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2392 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2393 is_nothrow_copy_assignable<_T2>::value)
2394 {
2395 _T1::operator=(__p.first());
2396 _T2::operator=(__p.second());
2397 return *this;
2398 }
2399
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002400 _LIBCPP_INLINE_VISIBILITY
2401 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant719bda32011-05-28 14:41:13 +00002402 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2403 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002404 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002405
2406 _LIBCPP_INLINE_VISIBILITY
2407 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2408 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2409 is_nothrow_move_assignable<_T2>::value)
2410 {
2411 _T1::operator=(_VSTD::move(__p.first()));
2412 _T2::operator=(_VSTD::move(__p.second()));
2413 return *this;
2414 }
2415
Howard Hinnant59f73012013-11-13 00:39:22 +00002416#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002417
Howard Hinnant83b1c052011-12-19 17:58:44 +00002418#ifndef _LIBCPP_HAS_NO_VARIADICS
2419
2420 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2421 _LIBCPP_INLINE_VISIBILITY
2422 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2423 tuple<_Args1...> __first_args,
2424 tuple<_Args2...> __second_args,
2425 __tuple_indices<_I1...>,
2426 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002427 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2428 _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002429 {}
2430
2431#endif // _LIBCPP_HAS_NO_VARIADICS
2432
Howard Hinnant719bda32011-05-28 14:41:13 +00002433 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2434 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002435
Howard Hinnant719bda32011-05-28 14:41:13 +00002436 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2437 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002438
Howard Hinnant28b24882011-12-01 20:21:04 +00002439 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant719bda32011-05-28 14:41:13 +00002440 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002441 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002442 {
2443 }
2444};
2445
2446template <class _T1, class _T2>
2447class __compressed_pair
2448 : private __libcpp_compressed_pair_imp<_T1, _T2>
2449{
2450 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2451public:
2452 typedef typename base::_T1_param _T1_param;
2453 typedef typename base::_T2_param _T2_param;
2454
2455 typedef typename base::_T1_reference _T1_reference;
2456 typedef typename base::_T2_reference _T2_reference;
2457
2458 typedef typename base::_T1_const_reference _T1_const_reference;
2459 typedef typename base::_T2_const_reference _T2_const_reference;
2460
2461 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002462 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002463 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002464 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002465 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002466 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002467 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468
Howard Hinnant59f73012013-11-13 00:39:22 +00002469#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002470
2471 _LIBCPP_INLINE_VISIBILITY
2472 __compressed_pair(const __compressed_pair& __p)
2473 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2474 is_nothrow_copy_constructible<_T2>::value)
2475 : base(__p) {}
2476
2477 _LIBCPP_INLINE_VISIBILITY
2478 __compressed_pair& operator=(const __compressed_pair& __p)
2479 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2480 is_nothrow_copy_assignable<_T2>::value)
2481 {
2482 base::operator=(__p);
2483 return *this;
2484 }
2485
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002486 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant719bda32011-05-28 14:41:13 +00002488 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2489 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002490 : base(_VSTD::move(__p)) {}
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002491
2492 _LIBCPP_INLINE_VISIBILITY
2493 __compressed_pair& operator=(__compressed_pair&& __p)
2494 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2495 is_nothrow_move_assignable<_T2>::value)
2496 {
2497 base::operator=(_VSTD::move(__p));
2498 return *this;
2499 }
Howard Hinnant83b1c052011-12-19 17:58:44 +00002500
Howard Hinnant59f73012013-11-13 00:39:22 +00002501#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002502
Howard Hinnant83b1c052011-12-19 17:58:44 +00002503#ifndef _LIBCPP_HAS_NO_VARIADICS
2504
2505 template <class... _Args1, class... _Args2>
2506 _LIBCPP_INLINE_VISIBILITY
2507 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2508 tuple<_Args2...> __second_args)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002509 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant83b1c052011-12-19 17:58:44 +00002510 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2511 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2512 {}
2513
2514#endif // _LIBCPP_HAS_NO_VARIADICS
2515
Howard Hinnant719bda32011-05-28 14:41:13 +00002516 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2517 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518
Howard Hinnant719bda32011-05-28 14:41:13 +00002519 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2520 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002521
Howard Hinnant719bda32011-05-28 14:41:13 +00002522 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2523 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002524 __is_nothrow_swappable<_T2>::value)
Howard Hinnant719bda32011-05-28 14:41:13 +00002525 {base::swap(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002526};
2527
2528template <class _T1, class _T2>
2529inline _LIBCPP_INLINE_VISIBILITY
2530void
2531swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant719bda32011-05-28 14:41:13 +00002532 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002533 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002534 {__x.swap(__y);}
2535
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002536// __same_or_less_cv_qualified
2537
2538template <class _Ptr1, class _Ptr2,
2539 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2540 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2541 >::value
2542 >
2543struct __same_or_less_cv_qualified_imp
2544 : is_convertible<_Ptr1, _Ptr2> {};
2545
2546template <class _Ptr1, class _Ptr2>
2547struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2548 : false_type {};
2549
Marshall Clowdf296162014-04-26 05:19:48 +00002550template <class _Ptr1, class _Ptr2, bool = is_pointer<_Ptr1>::value ||
2551 is_same<_Ptr1, _Ptr2>::value ||
2552 __has_element_type<_Ptr1>::value>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002553struct __same_or_less_cv_qualified
2554 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2555
2556template <class _Ptr1, class _Ptr2>
Marshall Clowdf296162014-04-26 05:19:48 +00002557struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, false>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002558 : false_type {};
2559
2560// default_delete
2561
Howard Hinnantc51e1022010-05-11 19:42:16 +00002562template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002563struct _LIBCPP_TYPE_VIS_ONLY default_delete
Howard Hinnantc51e1022010-05-11 19:42:16 +00002564{
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002565#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2566 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2567#else
2568 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2569#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002570 template <class _Up>
2571 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant719bda32011-05-28 14:41:13 +00002572 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2573 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002574 {
2575 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant14015fd2013-04-24 19:44:26 +00002576 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002577 delete __ptr;
2578 }
2579};
2580
2581template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002582struct _LIBCPP_TYPE_VIS_ONLY default_delete<_Tp[]>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002583{
Howard Hinnant4500ca52011-12-18 21:19:44 +00002584public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002585#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2586 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2587#else
2588 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2589#endif
Howard Hinnant4500ca52011-12-18 21:19:44 +00002590 template <class _Up>
2591 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002592 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant4500ca52011-12-18 21:19:44 +00002593 template <class _Up>
2594 _LIBCPP_INLINE_VISIBILITY
2595 void operator() (_Up* __ptr,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002596 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597 {
2598 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Marshall Clowff0e4132016-02-25 16:50:51 +00002599 static_assert(!is_void<_Tp>::value, "default_delete can not delete void type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002600 delete [] __ptr;
2601 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002602};
2603
2604template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002605class _LIBCPP_TYPE_VIS_ONLY unique_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002606{
2607public:
2608 typedef _Tp element_type;
2609 typedef _Dp deleter_type;
2610 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2611private:
2612 __compressed_pair<pointer, deleter_type> __ptr_;
2613
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002614#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002615 unique_ptr(unique_ptr&);
2616 template <class _Up, class _Ep>
2617 unique_ptr(unique_ptr<_Up, _Ep>&);
2618 unique_ptr& operator=(unique_ptr&);
2619 template <class _Up, class _Ep>
2620 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant74279a52010-09-04 23:28:19 +00002621#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002622
2623 struct __nat {int __for_bool_;};
2624
2625 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2626 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2627public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002628 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002629 : __ptr_(pointer())
2630 {
2631 static_assert(!is_pointer<deleter_type>::value,
2632 "unique_ptr constructed with null function pointer deleter");
2633 }
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002634 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002635 : __ptr_(pointer())
2636 {
2637 static_assert(!is_pointer<deleter_type>::value,
2638 "unique_ptr constructed with null function pointer deleter");
2639 }
Howard Hinnant719bda32011-05-28 14:41:13 +00002640 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002641 : __ptr_(_VSTD::move(__p))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002642 {
2643 static_assert(!is_pointer<deleter_type>::value,
2644 "unique_ptr constructed with null function pointer deleter");
2645 }
2646
Howard Hinnant74279a52010-09-04 23:28:19 +00002647#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002648 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2649 is_reference<deleter_type>::value,
2650 deleter_type,
2651 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002652 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002653 : __ptr_(__p, __d) {}
2654
2655 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002656 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002657 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002658 {
2659 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2660 }
Howard Hinnant719bda32011-05-28 14:41:13 +00002661 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002662 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002663 template <class _Up, class _Ep>
2664 _LIBCPP_INLINE_VISIBILITY
2665 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2666 typename enable_if
2667 <
2668 !is_array<_Up>::value &&
2669 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2670 is_convertible<_Ep, deleter_type>::value &&
2671 (
2672 !is_reference<deleter_type>::value ||
2673 is_same<deleter_type, _Ep>::value
2674 ),
2675 __nat
Howard Hinnant719bda32011-05-28 14:41:13 +00002676 >::type = __nat()) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002677 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678
2679 template <class _Up>
2680 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2681 typename enable_if<
2682 is_convertible<_Up*, _Tp*>::value &&
2683 is_same<_Dp, default_delete<_Tp> >::value,
2684 __nat
Howard Hinnant719bda32011-05-28 14:41:13 +00002685 >::type = __nat()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002686 : __ptr_(__p.release())
2687 {
2688 }
2689
Howard Hinnant719bda32011-05-28 14:41:13 +00002690 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691 {
2692 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002693 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002694 return *this;
2695 }
2696
2697 template <class _Up, class _Ep>
2698 _LIBCPP_INLINE_VISIBILITY
2699 typename enable_if
2700 <
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002701 !is_array<_Up>::value &&
2702 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2703 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704 unique_ptr&
2705 >::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002706 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002707 {
2708 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002709 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002710 return *this;
2711 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002712#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002713
2714 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2715 {
2716 return __rv<unique_ptr>(*this);
2717 }
2718
2719 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002720 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002721
2722 template <class _Up, class _Ep>
Eric Fiselier6d8c8ed2015-08-28 05:07:06 +00002723 _LIBCPP_INLINE_VISIBILITY
2724 typename enable_if<
2725 !is_array<_Up>::value &&
2726 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2727 is_assignable<deleter_type&, _Ep&>::value,
2728 unique_ptr&
2729 >::type
2730 operator=(unique_ptr<_Up, _Ep> __u)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002731 {
2732 reset(__u.release());
Eric Fiselier6d8c8ed2015-08-28 05:07:06 +00002733 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002734 return *this;
2735 }
2736
2737 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002738 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002739
2740 template <class _Up>
2741 _LIBCPP_INLINE_VISIBILITY
2742 typename enable_if<
2743 is_convertible<_Up*, _Tp*>::value &&
2744 is_same<_Dp, default_delete<_Tp> >::value,
2745 unique_ptr&
2746 >::type
2747 operator=(auto_ptr<_Up> __p)
2748 {reset(__p.release()); return *this;}
2749
Howard Hinnant74279a52010-09-04 23:28:19 +00002750#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002751 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2752
Howard Hinnant719bda32011-05-28 14:41:13 +00002753 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754 {
2755 reset();
2756 return *this;
2757 }
2758
2759 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2760 {return *__ptr_.first();}
Howard Hinnant719bda32011-05-28 14:41:13 +00002761 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2762 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2763 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2764 {return __ptr_.second();}
2765 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2766 {return __ptr_.second();}
Howard Hinnant86a291f2012-02-21 21:46:43 +00002767 _LIBCPP_INLINE_VISIBILITY
2768 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2769 {return __ptr_.first() != nullptr;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002770
Howard Hinnant719bda32011-05-28 14:41:13 +00002771 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002772 {
2773 pointer __t = __ptr_.first();
2774 __ptr_.first() = pointer();
2775 return __t;
2776 }
2777
Howard Hinnant719bda32011-05-28 14:41:13 +00002778 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002779 {
2780 pointer __tmp = __ptr_.first();
2781 __ptr_.first() = __p;
2782 if (__tmp)
2783 __ptr_.second()(__tmp);
2784 }
2785
Howard Hinnant719bda32011-05-28 14:41:13 +00002786 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2787 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002788};
2789
2790template <class _Tp, class _Dp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002791class _LIBCPP_TYPE_VIS_ONLY unique_ptr<_Tp[], _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002792{
2793public:
2794 typedef _Tp element_type;
2795 typedef _Dp deleter_type;
2796 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2797private:
2798 __compressed_pair<pointer, deleter_type> __ptr_;
2799
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002800#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002801 unique_ptr(unique_ptr&);
2802 template <class _Up>
2803 unique_ptr(unique_ptr<_Up>&);
2804 unique_ptr& operator=(unique_ptr&);
2805 template <class _Up>
2806 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant74279a52010-09-04 23:28:19 +00002807#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002808
2809 struct __nat {int __for_bool_;};
2810
2811 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2812 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2813public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002814 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002815 : __ptr_(pointer())
2816 {
2817 static_assert(!is_pointer<deleter_type>::value,
2818 "unique_ptr constructed with null function pointer deleter");
2819 }
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002820 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002821 : __ptr_(pointer())
2822 {
2823 static_assert(!is_pointer<deleter_type>::value,
2824 "unique_ptr constructed with null function pointer deleter");
2825 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002826#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00002827 template <class _Pp>
2828 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p,
2829 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830 : __ptr_(__p)
2831 {
2832 static_assert(!is_pointer<deleter_type>::value,
2833 "unique_ptr constructed with null function pointer deleter");
2834 }
2835
Logan Chiend435f8b2014-01-31 09:30:46 +00002836 template <class _Pp>
Howard Hinnantc834c512011-11-29 18:15:50 +00002837 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantc51e1022010-05-11 19:42:16 +00002838 is_reference<deleter_type>::value,
2839 deleter_type,
Logan Chiend435f8b2014-01-31 09:30:46 +00002840 typename add_lvalue_reference<const deleter_type>::type>::type __d,
2841 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002842 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002843 : __ptr_(__p, __d) {}
2844
2845 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2846 is_reference<deleter_type>::value,
2847 deleter_type,
2848 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002849 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002850 : __ptr_(pointer(), __d) {}
2851
Logan Chiend435f8b2014-01-31 09:30:46 +00002852 template <class _Pp>
2853 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p,
2854 typename remove_reference<deleter_type>::type&& __d,
2855 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002856 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002857 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002858 {
2859 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2860 }
2861
2862 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002863 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002864 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002865 {
2866 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2867 }
2868
Howard Hinnant719bda32011-05-28 14:41:13 +00002869 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002870 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002871
Howard Hinnant719bda32011-05-28 14:41:13 +00002872 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873 {
2874 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002875 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002876 return *this;
2877 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002878
2879 template <class _Up, class _Ep>
2880 _LIBCPP_INLINE_VISIBILITY
2881 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2882 typename enable_if
2883 <
2884 is_array<_Up>::value &&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002885 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant4500ca52011-12-18 21:19:44 +00002886 && is_convertible<_Ep, deleter_type>::value &&
2887 (
2888 !is_reference<deleter_type>::value ||
2889 is_same<deleter_type, _Ep>::value
2890 ),
2891 __nat
2892 >::type = __nat()
2893 ) _NOEXCEPT
2894 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2895
2896
2897 template <class _Up, class _Ep>
2898 _LIBCPP_INLINE_VISIBILITY
2899 typename enable_if
2900 <
2901 is_array<_Up>::value &&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002902 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2903 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant4500ca52011-12-18 21:19:44 +00002904 unique_ptr&
2905 >::type
2906 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2907 {
2908 reset(__u.release());
2909 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2910 return *this;
2911 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002912#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002913
2914 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2915 : __ptr_(__p)
2916 {
2917 static_assert(!is_pointer<deleter_type>::value,
2918 "unique_ptr constructed with null function pointer deleter");
2919 }
2920
2921 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002922 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002923
2924 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002925 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002926
2927 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2928 {
2929 return __rv<unique_ptr>(*this);
2930 }
2931
2932 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002933 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002934
2935 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2936 {
2937 reset(__u->release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002938 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002939 return *this;
2940 }
2941
Howard Hinnant74279a52010-09-04 23:28:19 +00002942#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002943 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2944
Howard Hinnant719bda32011-05-28 14:41:13 +00002945 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002946 {
2947 reset();
2948 return *this;
2949 }
2950
2951 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2952 {return __ptr_.first()[__i];}
Howard Hinnant719bda32011-05-28 14:41:13 +00002953 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2954 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2955 {return __ptr_.second();}
2956 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2957 {return __ptr_.second();}
Howard Hinnant86a291f2012-02-21 21:46:43 +00002958 _LIBCPP_INLINE_VISIBILITY
2959 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2960 {return __ptr_.first() != nullptr;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002961
Howard Hinnant719bda32011-05-28 14:41:13 +00002962 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002963 {
2964 pointer __t = __ptr_.first();
2965 __ptr_.first() = pointer();
2966 return __t;
2967 }
2968
Logan Chiend435f8b2014-01-31 09:30:46 +00002969 template <class _Pp>
2970 _LIBCPP_INLINE_VISIBILITY
2971 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, void>::type
2972 reset(_Pp __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002973 {
2974 pointer __tmp = __ptr_.first();
2975 __ptr_.first() = __p;
2976 if (__tmp)
2977 __ptr_.second()(__tmp);
2978 }
Marshall Clowff0e4132016-02-25 16:50:51 +00002979 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t = nullptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002980 {
2981 pointer __tmp = __ptr_.first();
2982 __ptr_.first() = nullptr;
2983 if (__tmp)
2984 __ptr_.second()(__tmp);
2985 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002986
2987 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2988private:
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002989
Howard Hinnant74279a52010-09-04 23:28:19 +00002990#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002991 template <class _Up>
2992 explicit unique_ptr(_Up);
2993 template <class _Up>
2994 unique_ptr(_Up __u,
2995 typename conditional<
2996 is_reference<deleter_type>::value,
2997 deleter_type,
2998 typename add_lvalue_reference<const deleter_type>::type>::type,
2999 typename enable_if
3000 <
3001 is_convertible<_Up, pointer>::value,
3002 __nat
3003 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003004#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003005};
3006
3007template <class _Tp, class _Dp>
3008inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00003009typename enable_if<
3010 __is_swappable<_Dp>::value,
3011 void
3012>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003013swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003014
3015template <class _T1, class _D1, class _T2, class _D2>
3016inline _LIBCPP_INLINE_VISIBILITY
3017bool
3018operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
3019
3020template <class _T1, class _D1, class _T2, class _D2>
3021inline _LIBCPP_INLINE_VISIBILITY
3022bool
3023operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
3024
3025template <class _T1, class _D1, class _T2, class _D2>
3026inline _LIBCPP_INLINE_VISIBILITY
3027bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00003028operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
3029{
3030 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3031 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003032 typedef typename common_type<_P1, _P2>::type _Vp;
3033 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00003034}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003035
3036template <class _T1, class _D1, class _T2, class _D2>
3037inline _LIBCPP_INLINE_VISIBILITY
3038bool
3039operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
3040
3041template <class _T1, class _D1, class _T2, class _D2>
3042inline _LIBCPP_INLINE_VISIBILITY
3043bool
3044operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
3045
3046template <class _T1, class _D1, class _T2, class _D2>
3047inline _LIBCPP_INLINE_VISIBILITY
3048bool
3049operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
3050
Howard Hinnantb17caf92012-02-21 21:02:58 +00003051template <class _T1, class _D1>
3052inline _LIBCPP_INLINE_VISIBILITY
3053bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003054operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003055{
3056 return !__x;
3057}
3058
3059template <class _T1, class _D1>
3060inline _LIBCPP_INLINE_VISIBILITY
3061bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003062operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003063{
3064 return !__x;
3065}
3066
3067template <class _T1, class _D1>
3068inline _LIBCPP_INLINE_VISIBILITY
3069bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003070operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003071{
3072 return static_cast<bool>(__x);
3073}
3074
3075template <class _T1, class _D1>
3076inline _LIBCPP_INLINE_VISIBILITY
3077bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003078operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003079{
3080 return static_cast<bool>(__x);
3081}
3082
3083template <class _T1, class _D1>
3084inline _LIBCPP_INLINE_VISIBILITY
3085bool
3086operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3087{
3088 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3089 return less<_P1>()(__x.get(), nullptr);
3090}
3091
3092template <class _T1, class _D1>
3093inline _LIBCPP_INLINE_VISIBILITY
3094bool
3095operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3096{
3097 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3098 return less<_P1>()(nullptr, __x.get());
3099}
3100
3101template <class _T1, class _D1>
3102inline _LIBCPP_INLINE_VISIBILITY
3103bool
3104operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3105{
3106 return nullptr < __x;
3107}
3108
3109template <class _T1, class _D1>
3110inline _LIBCPP_INLINE_VISIBILITY
3111bool
3112operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3113{
3114 return __x < nullptr;
3115}
3116
3117template <class _T1, class _D1>
3118inline _LIBCPP_INLINE_VISIBILITY
3119bool
3120operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3121{
3122 return !(nullptr < __x);
3123}
3124
3125template <class _T1, class _D1>
3126inline _LIBCPP_INLINE_VISIBILITY
3127bool
3128operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3129{
3130 return !(__x < nullptr);
3131}
3132
3133template <class _T1, class _D1>
3134inline _LIBCPP_INLINE_VISIBILITY
3135bool
3136operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3137{
3138 return !(__x < nullptr);
3139}
3140
3141template <class _T1, class _D1>
3142inline _LIBCPP_INLINE_VISIBILITY
3143bool
3144operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3145{
3146 return !(nullptr < __x);
3147}
3148
Howard Hinnant9e028f12012-05-01 15:37:54 +00003149#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3150
3151template <class _Tp, class _Dp>
3152inline _LIBCPP_INLINE_VISIBILITY
3153unique_ptr<_Tp, _Dp>
3154move(unique_ptr<_Tp, _Dp>& __t)
3155{
3156 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3157}
3158
3159#endif
3160
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003161#if _LIBCPP_STD_VER > 11
3162
3163template<class _Tp>
3164struct __unique_if
3165{
3166 typedef unique_ptr<_Tp> __unique_single;
3167};
3168
3169template<class _Tp>
3170struct __unique_if<_Tp[]>
3171{
3172 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3173};
3174
3175template<class _Tp, size_t _Np>
3176struct __unique_if<_Tp[_Np]>
3177{
3178 typedef void __unique_array_known_bound;
3179};
3180
3181template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003182inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003183typename __unique_if<_Tp>::__unique_single
3184make_unique(_Args&&... __args)
3185{
3186 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3187}
3188
3189template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003190inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003191typename __unique_if<_Tp>::__unique_array_unknown_bound
3192make_unique(size_t __n)
3193{
3194 typedef typename remove_extent<_Tp>::type _Up;
3195 return unique_ptr<_Tp>(new _Up[__n]());
3196}
3197
3198template<class _Tp, class... _Args>
3199 typename __unique_if<_Tp>::__unique_array_known_bound
3200 make_unique(_Args&&...) = delete;
3201
3202#endif // _LIBCPP_STD_VER > 11
3203
Howard Hinnant6a855442013-07-03 17:39:28 +00003204template <class _Size>
3205inline _LIBCPP_INLINE_VISIBILITY
3206_Size
3207__loadword(const void* __p)
3208{
3209 _Size __r;
3210 std::memcpy(&__r, __p, sizeof(__r));
3211 return __r;
3212}
3213
Howard Hinnantf1973402011-12-10 20:28:56 +00003214// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
3215// is 64 bits. This is because cityhash64 uses 64bit x 64bit
3216// multiplication, which can be very slow on 32-bit systems.
Howard Hinnantad71c232011-12-05 00:08:45 +00003217template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantf1973402011-12-10 20:28:56 +00003218struct __murmur2_or_cityhash;
Howard Hinnantad71c232011-12-05 00:08:45 +00003219
3220template <class _Size>
Howard Hinnantf1973402011-12-10 20:28:56 +00003221struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnantad71c232011-12-05 00:08:45 +00003222{
3223 _Size operator()(const void* __key, _Size __len);
3224};
3225
Howard Hinnantf1973402011-12-10 20:28:56 +00003226// murmur2
Howard Hinnantad71c232011-12-05 00:08:45 +00003227template <class _Size>
3228_Size
Marshall Clow91c7f642016-01-11 19:27:10 +00003229__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantad71c232011-12-05 00:08:45 +00003230{
3231 const _Size __m = 0x5bd1e995;
3232 const _Size __r = 24;
3233 _Size __h = __len;
3234 const unsigned char* __data = static_cast<const unsigned char*>(__key);
3235 for (; __len >= 4; __data += 4, __len -= 4)
3236 {
Howard Hinnant6a855442013-07-03 17:39:28 +00003237 _Size __k = __loadword<_Size>(__data);
Howard Hinnantad71c232011-12-05 00:08:45 +00003238 __k *= __m;
3239 __k ^= __k >> __r;
3240 __k *= __m;
3241 __h *= __m;
3242 __h ^= __k;
3243 }
3244 switch (__len)
3245 {
3246 case 3:
3247 __h ^= __data[2] << 16;
3248 case 2:
3249 __h ^= __data[1] << 8;
3250 case 1:
3251 __h ^= __data[0];
3252 __h *= __m;
3253 }
3254 __h ^= __h >> 13;
3255 __h *= __m;
3256 __h ^= __h >> 15;
3257 return __h;
3258}
3259
3260template <class _Size>
Howard Hinnantf1973402011-12-10 20:28:56 +00003261struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnantad71c232011-12-05 00:08:45 +00003262{
3263 _Size operator()(const void* __key, _Size __len);
Howard Hinnantf1973402011-12-10 20:28:56 +00003264
3265 private:
3266 // Some primes between 2^63 and 2^64.
3267 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
3268 static const _Size __k1 = 0xb492b66fbe98f273ULL;
3269 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
3270 static const _Size __k3 = 0xc949d7c7509e6557ULL;
3271
3272 static _Size __rotate(_Size __val, int __shift) {
3273 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
3274 }
3275
3276 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
3277 return (__val >> __shift) | (__val << (64 - __shift));
3278 }
3279
3280 static _Size __shift_mix(_Size __val) {
3281 return __val ^ (__val >> 47);
3282 }
3283
3284 static _Size __hash_len_16(_Size __u, _Size __v) {
3285 const _Size __mul = 0x9ddfea08eb382d69ULL;
3286 _Size __a = (__u ^ __v) * __mul;
3287 __a ^= (__a >> 47);
3288 _Size __b = (__v ^ __a) * __mul;
3289 __b ^= (__b >> 47);
3290 __b *= __mul;
3291 return __b;
3292 }
3293
3294 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
3295 if (__len > 8) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003296 const _Size __a = __loadword<_Size>(__s);
3297 const _Size __b = __loadword<_Size>(__s + __len - 8);
Howard Hinnantf1973402011-12-10 20:28:56 +00003298 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
3299 }
3300 if (__len >= 4) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003301 const uint32_t __a = __loadword<uint32_t>(__s);
3302 const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
Howard Hinnantf1973402011-12-10 20:28:56 +00003303 return __hash_len_16(__len + (__a << 3), __b);
3304 }
3305 if (__len > 0) {
3306 const unsigned char __a = __s[0];
3307 const unsigned char __b = __s[__len >> 1];
3308 const unsigned char __c = __s[__len - 1];
3309 const uint32_t __y = static_cast<uint32_t>(__a) +
3310 (static_cast<uint32_t>(__b) << 8);
3311 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3312 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3313 }
3314 return __k2;
3315 }
3316
3317 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003318 const _Size __a = __loadword<_Size>(__s) * __k1;
3319 const _Size __b = __loadword<_Size>(__s + 8);
3320 const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;
3321 const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;
Howard Hinnantf1973402011-12-10 20:28:56 +00003322 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3323 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3324 }
3325
3326 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3327 // Callers do best to use "random-looking" values for a and b.
3328 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3329 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3330 __a += __w;
3331 __b = __rotate(__b + __a + __z, 21);
3332 const _Size __c = __a;
3333 __a += __x;
3334 __a += __y;
3335 __b += __rotate(__a, 44);
3336 return pair<_Size, _Size>(__a + __z, __b + __c);
3337 }
3338
3339 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3340 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3341 const char* __s, _Size __a, _Size __b) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003342 return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),
3343 __loadword<_Size>(__s + 8),
3344 __loadword<_Size>(__s + 16),
3345 __loadword<_Size>(__s + 24),
Howard Hinnantf1973402011-12-10 20:28:56 +00003346 __a,
3347 __b);
3348 }
3349
3350 // Return an 8-byte hash for 33 to 64 bytes.
3351 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003352 _Size __z = __loadword<_Size>(__s + 24);
3353 _Size __a = __loadword<_Size>(__s) +
3354 (__len + __loadword<_Size>(__s + __len - 16)) * __k0;
Howard Hinnantf1973402011-12-10 20:28:56 +00003355 _Size __b = __rotate(__a + __z, 52);
3356 _Size __c = __rotate(__a, 37);
Howard Hinnant6a855442013-07-03 17:39:28 +00003357 __a += __loadword<_Size>(__s + 8);
Howard Hinnantf1973402011-12-10 20:28:56 +00003358 __c += __rotate(__a, 7);
Howard Hinnant6a855442013-07-03 17:39:28 +00003359 __a += __loadword<_Size>(__s + 16);
Howard Hinnantf1973402011-12-10 20:28:56 +00003360 _Size __vf = __a + __z;
3361 _Size __vs = __b + __rotate(__a, 31) + __c;
Howard Hinnant6a855442013-07-03 17:39:28 +00003362 __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);
3363 __z += __loadword<_Size>(__s + __len - 8);
Howard Hinnantf1973402011-12-10 20:28:56 +00003364 __b = __rotate(__a + __z, 52);
3365 __c = __rotate(__a, 37);
Howard Hinnant6a855442013-07-03 17:39:28 +00003366 __a += __loadword<_Size>(__s + __len - 24);
Howard Hinnantf1973402011-12-10 20:28:56 +00003367 __c += __rotate(__a, 7);
Howard Hinnant6a855442013-07-03 17:39:28 +00003368 __a += __loadword<_Size>(__s + __len - 16);
Howard Hinnantf1973402011-12-10 20:28:56 +00003369 _Size __wf = __a + __z;
3370 _Size __ws = __b + __rotate(__a, 31) + __c;
3371 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3372 return __shift_mix(__r * __k0 + __vs) * __k2;
3373 }
Howard Hinnantad71c232011-12-05 00:08:45 +00003374};
3375
Howard Hinnantf1973402011-12-10 20:28:56 +00003376// cityhash64
Howard Hinnantad71c232011-12-05 00:08:45 +00003377template <class _Size>
3378_Size
Marshall Clow91c7f642016-01-11 19:27:10 +00003379__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantad71c232011-12-05 00:08:45 +00003380{
Howard Hinnantf1973402011-12-10 20:28:56 +00003381 const char* __s = static_cast<const char*>(__key);
3382 if (__len <= 32) {
3383 if (__len <= 16) {
3384 return __hash_len_0_to_16(__s, __len);
3385 } else {
3386 return __hash_len_17_to_32(__s, __len);
Howard Hinnantad71c232011-12-05 00:08:45 +00003387 }
Howard Hinnantf1973402011-12-10 20:28:56 +00003388 } else if (__len <= 64) {
3389 return __hash_len_33_to_64(__s, __len);
3390 }
3391
3392 // For strings over 64 bytes we hash the end first, and then as we
3393 // loop we keep 56 bytes of state: v, w, x, y, and z.
Howard Hinnant6a855442013-07-03 17:39:28 +00003394 _Size __x = __loadword<_Size>(__s + __len - 40);
3395 _Size __y = __loadword<_Size>(__s + __len - 16) +
3396 __loadword<_Size>(__s + __len - 56);
3397 _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,
3398 __loadword<_Size>(__s + __len - 24));
Howard Hinnantf1973402011-12-10 20:28:56 +00003399 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3400 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
Howard Hinnant6a855442013-07-03 17:39:28 +00003401 __x = __x * __k1 + __loadword<_Size>(__s);
Howard Hinnantf1973402011-12-10 20:28:56 +00003402
3403 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3404 __len = (__len - 1) & ~static_cast<_Size>(63);
3405 do {
Howard Hinnant6a855442013-07-03 17:39:28 +00003406 __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;
3407 __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;
Howard Hinnantf1973402011-12-10 20:28:56 +00003408 __x ^= __w.second;
Howard Hinnant6a855442013-07-03 17:39:28 +00003409 __y += __v.first + __loadword<_Size>(__s + 40);
Howard Hinnantf1973402011-12-10 20:28:56 +00003410 __z = __rotate(__z + __w.first, 33) * __k1;
3411 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3412 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
Howard Hinnant6a855442013-07-03 17:39:28 +00003413 __y + __loadword<_Size>(__s + 16));
Howard Hinnantf1973402011-12-10 20:28:56 +00003414 std::swap(__z, __x);
3415 __s += 64;
3416 __len -= 64;
3417 } while (__len != 0);
3418 return __hash_len_16(
3419 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3420 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnantad71c232011-12-05 00:08:45 +00003421}
3422
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003423template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3424struct __scalar_hash;
3425
3426template <class _Tp>
3427struct __scalar_hash<_Tp, 0>
3428 : public unary_function<_Tp, size_t>
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003429{
Howard Hinnant756c69b2010-09-22 16:48:34 +00003430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003431 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003432 {
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003433 union
3434 {
3435 _Tp __t;
3436 size_t __a;
3437 } __u;
3438 __u.__a = 0;
3439 __u.__t = __v;
3440 return __u.__a;
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003441 }
3442};
3443
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003444template <class _Tp>
3445struct __scalar_hash<_Tp, 1>
3446 : public unary_function<_Tp, size_t>
3447{
3448 _LIBCPP_INLINE_VISIBILITY
3449 size_t operator()(_Tp __v) const _NOEXCEPT
3450 {
3451 union
3452 {
3453 _Tp __t;
3454 size_t __a;
3455 } __u;
3456 __u.__t = __v;
3457 return __u.__a;
3458 }
3459};
3460
3461template <class _Tp>
3462struct __scalar_hash<_Tp, 2>
3463 : public unary_function<_Tp, size_t>
3464{
3465 _LIBCPP_INLINE_VISIBILITY
3466 size_t operator()(_Tp __v) const _NOEXCEPT
3467 {
3468 union
3469 {
3470 _Tp __t;
3471 struct
3472 {
3473 size_t __a;
3474 size_t __b;
Eric Fiseliere012bf22015-07-18 20:40:46 +00003475 } __s;
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003476 } __u;
3477 __u.__t = __v;
Howard Hinnantf1973402011-12-10 20:28:56 +00003478 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003479 }
3480};
3481
3482template <class _Tp>
3483struct __scalar_hash<_Tp, 3>
3484 : public unary_function<_Tp, size_t>
3485{
3486 _LIBCPP_INLINE_VISIBILITY
3487 size_t operator()(_Tp __v) const _NOEXCEPT
3488 {
3489 union
3490 {
3491 _Tp __t;
3492 struct
3493 {
3494 size_t __a;
3495 size_t __b;
3496 size_t __c;
Eric Fiseliere012bf22015-07-18 20:40:46 +00003497 } __s;
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003498 } __u;
3499 __u.__t = __v;
Howard Hinnantf1973402011-12-10 20:28:56 +00003500 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003501 }
3502};
3503
3504template <class _Tp>
3505struct __scalar_hash<_Tp, 4>
3506 : public unary_function<_Tp, size_t>
3507{
3508 _LIBCPP_INLINE_VISIBILITY
3509 size_t operator()(_Tp __v) const _NOEXCEPT
3510 {
3511 union
3512 {
3513 _Tp __t;
3514 struct
3515 {
3516 size_t __a;
3517 size_t __b;
3518 size_t __c;
3519 size_t __d;
Eric Fiseliere012bf22015-07-18 20:40:46 +00003520 } __s;
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003521 } __u;
3522 __u.__t = __v;
Howard Hinnantf1973402011-12-10 20:28:56 +00003523 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003524 }
3525};
3526
3527template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003528struct _LIBCPP_TYPE_VIS_ONLY hash<_Tp*>
Howard Hinnant9fa30202012-07-30 01:40:57 +00003529 : public unary_function<_Tp*, size_t>
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003530{
Howard Hinnant9fa30202012-07-30 01:40:57 +00003531 _LIBCPP_INLINE_VISIBILITY
3532 size_t operator()(_Tp* __v) const _NOEXCEPT
3533 {
3534 union
3535 {
3536 _Tp* __t;
3537 size_t __a;
3538 } __u;
3539 __u.__t = __v;
3540 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
3541 }
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003542};
3543
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003544template <class _Tp, class _Dp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003545struct _LIBCPP_TYPE_VIS_ONLY hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003546{
3547 typedef unique_ptr<_Tp, _Dp> argument_type;
3548 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003549 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003550 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003551 {
3552 typedef typename argument_type::pointer pointer;
3553 return hash<pointer>()(__ptr.get());
3554 }
3555};
3556
Howard Hinnantc51e1022010-05-11 19:42:16 +00003557struct __destruct_n
3558{
3559private:
3560 size_t size;
3561
3562 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003563 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003564 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3565
3566 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003567 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003568 {}
3569
Howard Hinnant719bda32011-05-28 14:41:13 +00003570 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003571 {++size;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003572 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003573 {}
3574
Howard Hinnant719bda32011-05-28 14:41:13 +00003575 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003576 {size = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003577 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003578 {}
3579public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003580 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3581 : size(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003582
3583 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003584 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003585 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003586
3587 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003588 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003589 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003590
3591 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003592 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003593 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003594};
3595
3596template <class _Alloc>
3597class __allocator_destructor
3598{
3599 typedef allocator_traits<_Alloc> __alloc_traits;
3600public:
3601 typedef typename __alloc_traits::pointer pointer;
3602 typedef typename __alloc_traits::size_type size_type;
3603private:
3604 _Alloc& __alloc_;
3605 size_type __s_;
3606public:
3607 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003608 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003609 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003610 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003611 void operator()(pointer __p) _NOEXCEPT
3612 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003613};
3614
3615template <class _InputIterator, class _ForwardIterator>
3616_ForwardIterator
3617uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3618{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003619 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003620#ifndef _LIBCPP_NO_EXCEPTIONS
3621 _ForwardIterator __s = __r;
3622 try
3623 {
3624#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003625 for (; __f != __l; ++__f, (void) ++__r)
3626 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003627#ifndef _LIBCPP_NO_EXCEPTIONS
3628 }
3629 catch (...)
3630 {
3631 for (; __s != __r; ++__s)
3632 __s->~value_type();
3633 throw;
3634 }
3635#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003636 return __r;
3637}
3638
3639template <class _InputIterator, class _Size, class _ForwardIterator>
3640_ForwardIterator
3641uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3642{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003643 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003644#ifndef _LIBCPP_NO_EXCEPTIONS
3645 _ForwardIterator __s = __r;
3646 try
3647 {
3648#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003649 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3650 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003651#ifndef _LIBCPP_NO_EXCEPTIONS
3652 }
3653 catch (...)
3654 {
3655 for (; __s != __r; ++__s)
3656 __s->~value_type();
3657 throw;
3658 }
3659#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003660 return __r;
3661}
3662
3663template <class _ForwardIterator, class _Tp>
3664void
3665uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3666{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003667 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003668#ifndef _LIBCPP_NO_EXCEPTIONS
3669 _ForwardIterator __s = __f;
3670 try
3671 {
3672#endif
3673 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003674 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003675#ifndef _LIBCPP_NO_EXCEPTIONS
3676 }
3677 catch (...)
3678 {
3679 for (; __s != __f; ++__s)
3680 __s->~value_type();
3681 throw;
3682 }
3683#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003684}
3685
3686template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003687_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003688uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3689{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003690 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003691#ifndef _LIBCPP_NO_EXCEPTIONS
3692 _ForwardIterator __s = __f;
3693 try
3694 {
3695#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003696 for (; __n > 0; ++__f, (void) --__n)
3697 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003698#ifndef _LIBCPP_NO_EXCEPTIONS
3699 }
3700 catch (...)
3701 {
3702 for (; __s != __f; ++__s)
3703 __s->~value_type();
3704 throw;
3705 }
3706#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003707 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003708}
3709
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003710#if _LIBCPP_STD_VER > 14
3711
3712template <class _ForwardIterator>
3713inline _LIBCPP_INLINE_VISIBILITY
3714void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3715 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3716 for (; __first != __last; ++__first)
3717 ::new((void*)_VSTD::addressof(*__first)) _Vt;
3718}
3719
3720template <class _ForwardIterator, class _Size>
3721inline _LIBCPP_INLINE_VISIBILITY
3722_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3723 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3724 for (; __n > 0; (void)++__first, --__n)
3725 ::new((void*)_VSTD::addressof(*__first)) _Vt;
3726 return __first;
3727}
3728
3729
3730template <class _ForwardIterator>
3731inline _LIBCPP_INLINE_VISIBILITY
3732void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3733 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3734 for (; __first != __last; ++__first)
3735 ::new((void*)_VSTD::addressof(*__first)) _Vt();
3736}
3737
3738template <class _ForwardIterator, class _Size>
3739inline _LIBCPP_INLINE_VISIBILITY
3740_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3741 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3742 for (; __n > 0; (void)++__first, --__n)
3743 ::new((void*)_VSTD::addressof(*__first)) _Vt();
3744 return __first;
3745}
3746
3747
3748template <class _InputIt, class _ForwardIt>
3749inline _LIBCPP_INLINE_VISIBILITY
3750_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __res) {
3751 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3752 for (; __first != __last; (void)++__res, ++__first)
3753 ::new((void*)_VSTD::addressof(*__res)) _Vt(std::move(*__first));
3754 return __res;
3755}
3756
3757template <class _InputIt, class _Size, class _ForwardIt>
3758inline _LIBCPP_INLINE_VISIBILITY
3759pair<_InputIt, _ForwardIt>
3760uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __res) {
3761 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3762 for (; __n > 0; ++__res, (void)++__first, --__n)
3763 ::new((void*)_VSTD::addressof(*__res)) _Vt(std::move(*__first));
3764 return {__first, __res};
3765}
3766
3767template <class _Tp>
3768inline _LIBCPP_INLINE_VISIBILITY
3769void destroy_at(_Tp* __loc) {
3770 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3771 __loc->~_Tp();
3772}
3773
3774template <class _ForwardIterator>
3775inline _LIBCPP_INLINE_VISIBILITY
3776void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3777 for (; __first != __last; ++__first)
3778 _VSTD::destroy_at(_VSTD::addressof(*__first));
3779}
3780
3781template <class _ForwardIterator, class _Size>
3782inline _LIBCPP_INLINE_VISIBILITY
3783_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3784 for (; __n > 0; (void)++__first, --__n)
3785 _VSTD::destroy_at(_VSTD::addressof(*__first));
3786 return __first;
3787}
3788
3789#endif // _LIBCPP_STD_VER > 14
3790
Howard Hinnant756c69b2010-09-22 16:48:34 +00003791class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003792 : public std::exception
3793{
3794public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003795 virtual ~bad_weak_ptr() _NOEXCEPT;
3796 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003797};
3798
Marshall Clow8fea1612016-08-25 15:09:01 +00003799_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
3800void __throw_bad_weak_ptr()
3801{
3802#ifndef _LIBCPP_NO_EXCEPTIONS
3803 throw bad_weak_ptr();
3804#else
3805 _VSTD::abort();
3806#endif
3807}
3808
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003809template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003810
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003811class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003812{
3813 __shared_count(const __shared_count&);
3814 __shared_count& operator=(const __shared_count&);
3815
3816protected:
3817 long __shared_owners_;
3818 virtual ~__shared_count();
3819private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003820 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003821
3822public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003824 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003825 : __shared_owners_(__refs) {}
3826
Howard Hinnant719bda32011-05-28 14:41:13 +00003827 void __add_shared() _NOEXCEPT;
3828 bool __release_shared() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003829 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003830 long use_count() const _NOEXCEPT {
3831 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3832 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003833};
3834
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003835class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003836 : private __shared_count
3837{
3838 long __shared_weak_owners_;
3839
3840public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003841 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003842 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003843 : __shared_count(__refs),
3844 __shared_weak_owners_(__refs) {}
3845protected:
3846 virtual ~__shared_weak_count();
3847
3848public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003849 void __add_shared() _NOEXCEPT;
3850 void __add_weak() _NOEXCEPT;
3851 void __release_shared() _NOEXCEPT;
3852 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003853 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003854 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3855 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003856
Howard Hinnant807d6332013-02-25 15:50:36 +00003857 // Define the function out only if we build static libc++ without RTTI.
3858 // Otherwise we may break clients who need to compile their projects with
3859 // -fno-rtti and yet link against a libc++.dylib compiled
3860 // without -fno-rtti.
3861#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003862 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003863#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003864private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003865 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003866};
3867
3868template <class _Tp, class _Dp, class _Alloc>
3869class __shared_ptr_pointer
3870 : public __shared_weak_count
3871{
3872 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3873public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003875 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003876 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003877
Howard Hinnant72f73582010-08-11 17:04:31 +00003878#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003879 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003880#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003881
3882private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003883 virtual void __on_zero_shared() _NOEXCEPT;
3884 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003885};
3886
Howard Hinnant72f73582010-08-11 17:04:31 +00003887#ifndef _LIBCPP_NO_RTTI
3888
Howard Hinnantc51e1022010-05-11 19:42:16 +00003889template <class _Tp, class _Dp, class _Alloc>
3890const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003891__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003892{
Marshall Clowa8dfc942014-11-17 19:05:50 +00003893 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003894}
3895
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003896#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003897
Howard Hinnantc51e1022010-05-11 19:42:16 +00003898template <class _Tp, class _Dp, class _Alloc>
3899void
Howard Hinnant719bda32011-05-28 14:41:13 +00003900__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003901{
3902 __data_.first().second()(__data_.first().first());
3903 __data_.first().second().~_Dp();
3904}
3905
3906template <class _Tp, class _Dp, class _Alloc>
3907void
Howard Hinnant719bda32011-05-28 14:41:13 +00003908__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003909{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003910 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3911 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003912 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3913
Eric Fiselierf8898c82015-02-05 23:01:40 +00003914 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003915 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003916 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003917}
3918
3919template <class _Tp, class _Alloc>
3920class __shared_ptr_emplace
3921 : public __shared_weak_count
3922{
3923 __compressed_pair<_Alloc, _Tp> __data_;
3924public:
3925#ifndef _LIBCPP_HAS_NO_VARIADICS
3926
Howard Hinnant756c69b2010-09-22 16:48:34 +00003927 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003928 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003929 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003930
3931 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003932 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003933 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003934 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3935 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003936
3937#else // _LIBCPP_HAS_NO_VARIADICS
3938
Howard Hinnant756c69b2010-09-22 16:48:34 +00003939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003940 __shared_ptr_emplace(_Alloc __a)
3941 : __data_(__a) {}
3942
3943 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003944 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003945 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3946 : __data_(__a, _Tp(__a0)) {}
3947
3948 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003949 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003950 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3951 : __data_(__a, _Tp(__a0, __a1)) {}
3952
3953 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003955 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3956 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3957
3958#endif // _LIBCPP_HAS_NO_VARIADICS
3959
3960private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003961 virtual void __on_zero_shared() _NOEXCEPT;
3962 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003963public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003964 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003965 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003966};
3967
3968template <class _Tp, class _Alloc>
3969void
Howard Hinnant719bda32011-05-28 14:41:13 +00003970__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003971{
3972 __data_.second().~_Tp();
3973}
3974
3975template <class _Tp, class _Alloc>
3976void
Howard Hinnant719bda32011-05-28 14:41:13 +00003977__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003978{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003979 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3980 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003981 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003982 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003983 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003984 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003985}
3986
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003987template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003988
3989template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003990class _LIBCPP_TYPE_VIS_ONLY shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003991{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003992public:
3993 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00003994#if _LIBCPP_STD_VER > 14
3995 typedef weak_ptr<_Tp> weak_type;
3996#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003997private:
3998 element_type* __ptr_;
3999 __shared_weak_count* __cntrl_;
4000
4001 struct __nat {int __for_bool_;};
4002public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004003 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004004 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004005 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004006 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00004007 template<class _Yp>
4008 explicit shared_ptr(_Yp* __p,
4009 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
4010 template<class _Yp, class _Dp>
4011 shared_ptr(_Yp* __p, _Dp __d,
4012 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
4013 template<class _Yp, class _Dp, class _Alloc>
4014 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4015 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004016 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
4017 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004018 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
4019 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004020 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004021 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004022 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004023 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004024 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
4025 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00004026#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004027 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004028 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004029 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004030 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
4031 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00004032#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004033 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004034 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00004035#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004036 template<class _Yp>
4037 shared_ptr(auto_ptr<_Yp>&& __r,
4038 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004039#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004040 template<class _Yp>
4041 shared_ptr(auto_ptr<_Yp> __r,
4042 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004043#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00004044#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004045 template <class _Yp, class _Dp>
4046 shared_ptr(unique_ptr<_Yp, _Dp>&&,
4047 typename enable_if
4048 <
4049 !is_lvalue_reference<_Dp>::value &&
4050 !is_array<_Yp>::value &&
4051 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4052 __nat
4053 >::type = __nat());
4054 template <class _Yp, class _Dp>
4055 shared_ptr(unique_ptr<_Yp, _Dp>&&,
4056 typename enable_if
4057 <
4058 is_lvalue_reference<_Dp>::value &&
4059 !is_array<_Yp>::value &&
4060 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4061 __nat
4062 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00004063#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004064 template <class _Yp, class _Dp>
4065 shared_ptr(unique_ptr<_Yp, _Dp>,
4066 typename enable_if
4067 <
4068 !is_lvalue_reference<_Dp>::value &&
4069 !is_array<_Yp>::value &&
4070 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4071 __nat
4072 >::type = __nat());
4073 template <class _Yp, class _Dp>
4074 shared_ptr(unique_ptr<_Yp, _Dp>,
4075 typename enable_if
4076 <
4077 is_lvalue_reference<_Dp>::value &&
4078 !is_array<_Yp>::value &&
4079 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4080 __nat
4081 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00004082#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004083
4084 ~shared_ptr();
4085
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004086 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004087 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004088 template<class _Yp>
4089 typename enable_if
4090 <
4091 is_convertible<_Yp*, element_type*>::value,
4092 shared_ptr&
4093 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004094 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004095 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00004096#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004097 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004098 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004099 template<class _Yp>
4100 typename enable_if
4101 <
4102 is_convertible<_Yp*, element_type*>::value,
4103 shared_ptr<_Tp>&
4104 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004105 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004106 operator=(shared_ptr<_Yp>&& __r);
4107 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00004108 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004109 typename enable_if
4110 <
4111 !is_array<_Yp>::value &&
4112 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004113 shared_ptr
4114 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004115 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00004116#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004117 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00004118 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004119 typename enable_if
4120 <
4121 !is_array<_Yp>::value &&
4122 is_convertible<_Yp*, element_type*>::value,
4123 shared_ptr&
4124 >::type
4125 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004126#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004127 template <class _Yp, class _Dp>
4128 typename enable_if
4129 <
4130 !is_array<_Yp>::value &&
4131 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4132 shared_ptr&
4133 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00004134#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004135 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004136 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00004137#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00004138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004139 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004140#endif
4141
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004143 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004144 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004145 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004146 template<class _Yp>
4147 typename enable_if
4148 <
4149 is_convertible<_Yp*, element_type*>::value,
4150 void
4151 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004153 reset(_Yp* __p);
4154 template<class _Yp, class _Dp>
4155 typename enable_if
4156 <
4157 is_convertible<_Yp*, element_type*>::value,
4158 void
4159 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004161 reset(_Yp* __p, _Dp __d);
4162 template<class _Yp, class _Dp, class _Alloc>
4163 typename enable_if
4164 <
4165 is_convertible<_Yp*, element_type*>::value,
4166 void
4167 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004168 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004169 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004170
Howard Hinnant756c69b2010-09-22 16:48:34 +00004171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004172 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004174 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
4175 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004176 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004177 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004178 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004179 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004181 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004182 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00004183 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00004184 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004185 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00004186 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00004187 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00004188 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004189 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00004190 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00004191 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00004192 _LIBCPP_INLINE_VISIBILITY
4193 bool
4194 __owner_equivalent(const shared_ptr& __p) const
4195 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004196
Howard Hinnant72f73582010-08-11 17:04:31 +00004197#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004198 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004200 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004201 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004202#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004203
4204#ifndef _LIBCPP_HAS_NO_VARIADICS
4205
4206 template<class ..._Args>
4207 static
4208 shared_ptr<_Tp>
4209 make_shared(_Args&& ...__args);
4210
4211 template<class _Alloc, class ..._Args>
4212 static
4213 shared_ptr<_Tp>
4214 allocate_shared(const _Alloc& __a, _Args&& ...__args);
4215
4216#else // _LIBCPP_HAS_NO_VARIADICS
4217
4218 static shared_ptr<_Tp> make_shared();
4219
4220 template<class _A0>
4221 static shared_ptr<_Tp> make_shared(_A0&);
4222
4223 template<class _A0, class _A1>
4224 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
4225
4226 template<class _A0, class _A1, class _A2>
4227 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
4228
4229 template<class _Alloc>
4230 static shared_ptr<_Tp>
4231 allocate_shared(const _Alloc& __a);
4232
4233 template<class _Alloc, class _A0>
4234 static shared_ptr<_Tp>
4235 allocate_shared(const _Alloc& __a, _A0& __a0);
4236
4237 template<class _Alloc, class _A0, class _A1>
4238 static shared_ptr<_Tp>
4239 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
4240
4241 template<class _Alloc, class _A0, class _A1, class _A2>
4242 static shared_ptr<_Tp>
4243 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
4244
4245#endif // _LIBCPP_HAS_NO_VARIADICS
4246
4247private:
4248
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004249 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004251 void
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004252 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
4253 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004254 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004255 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00004256 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00004257 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004258 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
4259 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00004260 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004261 }
4262
Howard Hinnant756c69b2010-09-22 16:48:34 +00004263 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004264 void __enable_weak_this(const volatile void*, const volatile void*) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004265
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004266 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
4267 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004268};
4269
4270template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004271inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004272_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004273shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004274 : __ptr_(0),
4275 __cntrl_(0)
4276{
4277}
4278
4279template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004280inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004281_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004282shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004283 : __ptr_(0),
4284 __cntrl_(0)
4285{
4286}
4287
4288template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004289template<class _Yp>
4290shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4291 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004292 : __ptr_(__p)
4293{
4294 unique_ptr<_Yp> __hold(__p);
4295 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4296 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
4297 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004298 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004299}
4300
4301template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004302template<class _Yp, class _Dp>
4303shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4304 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004305 : __ptr_(__p)
4306{
4307#ifndef _LIBCPP_NO_EXCEPTIONS
4308 try
4309 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004310#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004311 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4312 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004313 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004314#ifndef _LIBCPP_NO_EXCEPTIONS
4315 }
4316 catch (...)
4317 {
4318 __d(__p);
4319 throw;
4320 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004321#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004322}
4323
4324template<class _Tp>
4325template<class _Dp>
4326shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4327 : __ptr_(0)
4328{
4329#ifndef _LIBCPP_NO_EXCEPTIONS
4330 try
4331 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004332#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004333 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4334 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4335#ifndef _LIBCPP_NO_EXCEPTIONS
4336 }
4337 catch (...)
4338 {
4339 __d(__p);
4340 throw;
4341 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004342#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004343}
4344
4345template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004346template<class _Yp, class _Dp, class _Alloc>
4347shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4348 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004349 : __ptr_(__p)
4350{
4351#ifndef _LIBCPP_NO_EXCEPTIONS
4352 try
4353 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004354#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004355 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004356 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004357 typedef __allocator_destructor<_A2> _D2;
4358 _A2 __a2(__a);
4359 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004360 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4361 _CntrlBlk(__p, __d, __a);
4362 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004363 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004364#ifndef _LIBCPP_NO_EXCEPTIONS
4365 }
4366 catch (...)
4367 {
4368 __d(__p);
4369 throw;
4370 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004371#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004372}
4373
4374template<class _Tp>
4375template<class _Dp, class _Alloc>
4376shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4377 : __ptr_(0)
4378{
4379#ifndef _LIBCPP_NO_EXCEPTIONS
4380 try
4381 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004382#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004383 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004384 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004385 typedef __allocator_destructor<_A2> _D2;
4386 _A2 __a2(__a);
4387 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004388 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4389 _CntrlBlk(__p, __d, __a);
4390 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004391#ifndef _LIBCPP_NO_EXCEPTIONS
4392 }
4393 catch (...)
4394 {
4395 __d(__p);
4396 throw;
4397 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004398#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004399}
4400
4401template<class _Tp>
4402template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004403inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004404shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004405 : __ptr_(__p),
4406 __cntrl_(__r.__cntrl_)
4407{
4408 if (__cntrl_)
4409 __cntrl_->__add_shared();
4410}
4411
4412template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004413inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004414shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004415 : __ptr_(__r.__ptr_),
4416 __cntrl_(__r.__cntrl_)
4417{
4418 if (__cntrl_)
4419 __cntrl_->__add_shared();
4420}
4421
4422template<class _Tp>
4423template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004424inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004425shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4426 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004427 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004428 : __ptr_(__r.__ptr_),
4429 __cntrl_(__r.__cntrl_)
4430{
4431 if (__cntrl_)
4432 __cntrl_->__add_shared();
4433}
4434
Howard Hinnant74279a52010-09-04 23:28:19 +00004435#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004436
4437template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004438inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004439shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004440 : __ptr_(__r.__ptr_),
4441 __cntrl_(__r.__cntrl_)
4442{
4443 __r.__ptr_ = 0;
4444 __r.__cntrl_ = 0;
4445}
4446
4447template<class _Tp>
4448template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004449inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004450shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4451 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004452 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004453 : __ptr_(__r.__ptr_),
4454 __cntrl_(__r.__cntrl_)
4455{
4456 __r.__ptr_ = 0;
4457 __r.__cntrl_ = 0;
4458}
4459
Howard Hinnant74279a52010-09-04 23:28:19 +00004460#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004461
4462template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004463template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004464#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004465shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004466#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004467shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004468#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004469 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004470 : __ptr_(__r.get())
4471{
4472 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4473 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004474 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004475 __r.release();
4476}
4477
4478template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004479template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004480#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004481shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4482#else
4483shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4484#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004485 typename enable_if
4486 <
4487 !is_lvalue_reference<_Dp>::value &&
4488 !is_array<_Yp>::value &&
4489 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4490 __nat
4491 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004492 : __ptr_(__r.get())
4493{
Marshall Clow35cde742015-05-10 13:59:45 +00004494#if _LIBCPP_STD_VER > 11
4495 if (__ptr_ == nullptr)
4496 __cntrl_ = nullptr;
4497 else
4498#endif
4499 {
4500 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4501 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004502 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004503 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004504 __r.release();
4505}
4506
4507template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004508template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004509#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004510shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4511#else
4512shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4513#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004514 typename enable_if
4515 <
4516 is_lvalue_reference<_Dp>::value &&
4517 !is_array<_Yp>::value &&
4518 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4519 __nat
4520 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004521 : __ptr_(__r.get())
4522{
Marshall Clow35cde742015-05-10 13:59:45 +00004523#if _LIBCPP_STD_VER > 11
4524 if (__ptr_ == nullptr)
4525 __cntrl_ = nullptr;
4526 else
4527#endif
4528 {
4529 typedef __shared_ptr_pointer<_Yp*,
4530 reference_wrapper<typename remove_reference<_Dp>::type>,
4531 allocator<_Yp> > _CntrlBlk;
4532 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004533 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004534 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004535 __r.release();
4536}
4537
4538#ifndef _LIBCPP_HAS_NO_VARIADICS
4539
4540template<class _Tp>
4541template<class ..._Args>
4542shared_ptr<_Tp>
4543shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4544{
4545 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4546 typedef allocator<_CntrlBlk> _A2;
4547 typedef __allocator_destructor<_A2> _D2;
4548 _A2 __a2;
4549 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004550 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004551 shared_ptr<_Tp> __r;
4552 __r.__ptr_ = __hold2.get()->get();
4553 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004554 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004555 return __r;
4556}
4557
4558template<class _Tp>
4559template<class _Alloc, class ..._Args>
4560shared_ptr<_Tp>
4561shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4562{
4563 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004564 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004565 typedef __allocator_destructor<_A2> _D2;
4566 _A2 __a2(__a);
4567 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004568 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4569 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004570 shared_ptr<_Tp> __r;
4571 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004572 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004573 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004574 return __r;
4575}
4576
4577#else // _LIBCPP_HAS_NO_VARIADICS
4578
4579template<class _Tp>
4580shared_ptr<_Tp>
4581shared_ptr<_Tp>::make_shared()
4582{
4583 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4584 typedef allocator<_CntrlBlk> _Alloc2;
4585 typedef __allocator_destructor<_Alloc2> _D2;
4586 _Alloc2 __alloc2;
4587 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4588 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4589 shared_ptr<_Tp> __r;
4590 __r.__ptr_ = __hold2.get()->get();
4591 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004592 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004593 return __r;
4594}
4595
4596template<class _Tp>
4597template<class _A0>
4598shared_ptr<_Tp>
4599shared_ptr<_Tp>::make_shared(_A0& __a0)
4600{
4601 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4602 typedef allocator<_CntrlBlk> _Alloc2;
4603 typedef __allocator_destructor<_Alloc2> _D2;
4604 _Alloc2 __alloc2;
4605 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4606 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4607 shared_ptr<_Tp> __r;
4608 __r.__ptr_ = __hold2.get()->get();
4609 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004610 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004611 return __r;
4612}
4613
4614template<class _Tp>
4615template<class _A0, class _A1>
4616shared_ptr<_Tp>
4617shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4618{
4619 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4620 typedef allocator<_CntrlBlk> _Alloc2;
4621 typedef __allocator_destructor<_Alloc2> _D2;
4622 _Alloc2 __alloc2;
4623 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4624 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4625 shared_ptr<_Tp> __r;
4626 __r.__ptr_ = __hold2.get()->get();
4627 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004628 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004629 return __r;
4630}
4631
4632template<class _Tp>
4633template<class _A0, class _A1, class _A2>
4634shared_ptr<_Tp>
4635shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4636{
4637 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4638 typedef allocator<_CntrlBlk> _Alloc2;
4639 typedef __allocator_destructor<_Alloc2> _D2;
4640 _Alloc2 __alloc2;
4641 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4642 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4643 shared_ptr<_Tp> __r;
4644 __r.__ptr_ = __hold2.get()->get();
4645 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004646 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004647 return __r;
4648}
4649
4650template<class _Tp>
4651template<class _Alloc>
4652shared_ptr<_Tp>
4653shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4654{
4655 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004656 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004657 typedef __allocator_destructor<_Alloc2> _D2;
4658 _Alloc2 __alloc2(__a);
4659 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004660 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4661 _CntrlBlk(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004662 shared_ptr<_Tp> __r;
4663 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004664 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004665 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004666 return __r;
4667}
4668
4669template<class _Tp>
4670template<class _Alloc, class _A0>
4671shared_ptr<_Tp>
4672shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4673{
4674 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004675 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004676 typedef __allocator_destructor<_Alloc2> _D2;
4677 _Alloc2 __alloc2(__a);
4678 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004679 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4680 _CntrlBlk(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004681 shared_ptr<_Tp> __r;
4682 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004683 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004684 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004685 return __r;
4686}
4687
4688template<class _Tp>
4689template<class _Alloc, class _A0, class _A1>
4690shared_ptr<_Tp>
4691shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4692{
4693 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004694 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004695 typedef __allocator_destructor<_Alloc2> _D2;
4696 _Alloc2 __alloc2(__a);
4697 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004698 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4699 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004700 shared_ptr<_Tp> __r;
4701 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004702 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004703 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004704 return __r;
4705}
4706
4707template<class _Tp>
4708template<class _Alloc, class _A0, class _A1, class _A2>
4709shared_ptr<_Tp>
4710shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4711{
4712 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004713 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004714 typedef __allocator_destructor<_Alloc2> _D2;
4715 _Alloc2 __alloc2(__a);
4716 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004717 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4718 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004719 shared_ptr<_Tp> __r;
4720 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004721 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004722 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004723 return __r;
4724}
4725
4726#endif // _LIBCPP_HAS_NO_VARIADICS
4727
4728template<class _Tp>
4729shared_ptr<_Tp>::~shared_ptr()
4730{
4731 if (__cntrl_)
4732 __cntrl_->__release_shared();
4733}
4734
4735template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004736inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004737shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004738shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004739{
4740 shared_ptr(__r).swap(*this);
4741 return *this;
4742}
4743
4744template<class _Tp>
4745template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004746inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004747typename enable_if
4748<
4749 is_convertible<_Yp*, _Tp*>::value,
4750 shared_ptr<_Tp>&
4751>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004752shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004753{
4754 shared_ptr(__r).swap(*this);
4755 return *this;
4756}
4757
Howard Hinnant74279a52010-09-04 23:28:19 +00004758#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004759
4760template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004761inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004762shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004763shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004764{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004765 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004766 return *this;
4767}
4768
4769template<class _Tp>
4770template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004771inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004772typename enable_if
4773<
4774 is_convertible<_Yp*, _Tp*>::value,
4775 shared_ptr<_Tp>&
4776>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004777shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4778{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004779 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004780 return *this;
4781}
4782
4783template<class _Tp>
4784template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004785inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004786typename enable_if
4787<
4788 !is_array<_Yp>::value &&
4789 is_convertible<_Yp*, _Tp*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004790 shared_ptr<_Tp>
4791>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004792shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4793{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004794 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004795 return *this;
4796}
4797
4798template<class _Tp>
4799template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004800inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004801typename enable_if
4802<
4803 !is_array<_Yp>::value &&
4804 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4805 shared_ptr<_Tp>&
4806>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004807shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4808{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004809 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004810 return *this;
4811}
4812
Howard Hinnant74279a52010-09-04 23:28:19 +00004813#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004814
4815template<class _Tp>
4816template<class _Yp>
4817inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004818typename enable_if
4819<
4820 !is_array<_Yp>::value &&
4821 is_convertible<_Yp*, _Tp*>::value,
4822 shared_ptr<_Tp>&
4823>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004824shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004825{
4826 shared_ptr(__r).swap(*this);
4827 return *this;
4828}
4829
4830template<class _Tp>
4831template <class _Yp, class _Dp>
4832inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004833typename enable_if
4834<
4835 !is_array<_Yp>::value &&
4836 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4837 shared_ptr<_Tp>&
4838>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004839shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4840{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004841 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004842 return *this;
4843}
4844
Howard Hinnant74279a52010-09-04 23:28:19 +00004845#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004846
4847template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004848inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004849void
Howard Hinnant719bda32011-05-28 14:41:13 +00004850shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004851{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004852 _VSTD::swap(__ptr_, __r.__ptr_);
4853 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004854}
4855
4856template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004857inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004858void
Howard Hinnant719bda32011-05-28 14:41:13 +00004859shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004860{
4861 shared_ptr().swap(*this);
4862}
4863
4864template<class _Tp>
4865template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004866inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004867typename enable_if
4868<
4869 is_convertible<_Yp*, _Tp*>::value,
4870 void
4871>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004872shared_ptr<_Tp>::reset(_Yp* __p)
4873{
4874 shared_ptr(__p).swap(*this);
4875}
4876
4877template<class _Tp>
4878template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004879inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004880typename enable_if
4881<
4882 is_convertible<_Yp*, _Tp*>::value,
4883 void
4884>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004885shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4886{
4887 shared_ptr(__p, __d).swap(*this);
4888}
4889
4890template<class _Tp>
4891template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004892inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004893typename enable_if
4894<
4895 is_convertible<_Yp*, _Tp*>::value,
4896 void
4897>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004898shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4899{
4900 shared_ptr(__p, __d, __a).swap(*this);
4901}
4902
4903#ifndef _LIBCPP_HAS_NO_VARIADICS
4904
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004905template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004906inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004907typename enable_if
4908<
4909 !is_array<_Tp>::value,
4910 shared_ptr<_Tp>
4911>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004912make_shared(_Args&& ...__args)
4913{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004914 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004915}
4916
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004917template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004918inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004919typename enable_if
4920<
4921 !is_array<_Tp>::value,
4922 shared_ptr<_Tp>
4923>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004924allocate_shared(const _Alloc& __a, _Args&& ...__args)
4925{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004926 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004927}
4928
4929#else // _LIBCPP_HAS_NO_VARIADICS
4930
4931template<class _Tp>
4932inline _LIBCPP_INLINE_VISIBILITY
4933shared_ptr<_Tp>
4934make_shared()
4935{
4936 return shared_ptr<_Tp>::make_shared();
4937}
4938
4939template<class _Tp, class _A0>
4940inline _LIBCPP_INLINE_VISIBILITY
4941shared_ptr<_Tp>
4942make_shared(_A0& __a0)
4943{
4944 return shared_ptr<_Tp>::make_shared(__a0);
4945}
4946
4947template<class _Tp, class _A0, class _A1>
4948inline _LIBCPP_INLINE_VISIBILITY
4949shared_ptr<_Tp>
4950make_shared(_A0& __a0, _A1& __a1)
4951{
4952 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4953}
4954
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004955template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004956inline _LIBCPP_INLINE_VISIBILITY
4957shared_ptr<_Tp>
4958make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4959{
4960 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4961}
4962
4963template<class _Tp, class _Alloc>
4964inline _LIBCPP_INLINE_VISIBILITY
4965shared_ptr<_Tp>
4966allocate_shared(const _Alloc& __a)
4967{
4968 return shared_ptr<_Tp>::allocate_shared(__a);
4969}
4970
4971template<class _Tp, class _Alloc, class _A0>
4972inline _LIBCPP_INLINE_VISIBILITY
4973shared_ptr<_Tp>
4974allocate_shared(const _Alloc& __a, _A0& __a0)
4975{
4976 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4977}
4978
4979template<class _Tp, class _Alloc, class _A0, class _A1>
4980inline _LIBCPP_INLINE_VISIBILITY
4981shared_ptr<_Tp>
4982allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4983{
4984 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4985}
4986
4987template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4988inline _LIBCPP_INLINE_VISIBILITY
4989shared_ptr<_Tp>
4990allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4991{
4992 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4993}
4994
4995#endif // _LIBCPP_HAS_NO_VARIADICS
4996
4997template<class _Tp, class _Up>
4998inline _LIBCPP_INLINE_VISIBILITY
4999bool
Howard Hinnant719bda32011-05-28 14:41:13 +00005000operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005001{
5002 return __x.get() == __y.get();
5003}
5004
5005template<class _Tp, class _Up>
5006inline _LIBCPP_INLINE_VISIBILITY
5007bool
Howard Hinnant719bda32011-05-28 14:41:13 +00005008operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005009{
5010 return !(__x == __y);
5011}
5012
5013template<class _Tp, class _Up>
5014inline _LIBCPP_INLINE_VISIBILITY
5015bool
Howard Hinnant719bda32011-05-28 14:41:13 +00005016operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005017{
Eric Fiselierf8898c82015-02-05 23:01:40 +00005018 typedef typename common_type<_Tp*, _Up*>::type _Vp;
5019 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00005020}
5021
5022template<class _Tp, class _Up>
5023inline _LIBCPP_INLINE_VISIBILITY
5024bool
5025operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
5026{
5027 return __y < __x;
5028}
5029
5030template<class _Tp, class _Up>
5031inline _LIBCPP_INLINE_VISIBILITY
5032bool
5033operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
5034{
5035 return !(__y < __x);
5036}
5037
5038template<class _Tp, class _Up>
5039inline _LIBCPP_INLINE_VISIBILITY
5040bool
5041operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
5042{
5043 return !(__x < __y);
5044}
5045
5046template<class _Tp>
5047inline _LIBCPP_INLINE_VISIBILITY
5048bool
5049operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5050{
5051 return !__x;
5052}
5053
5054template<class _Tp>
5055inline _LIBCPP_INLINE_VISIBILITY
5056bool
5057operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5058{
5059 return !__x;
5060}
5061
5062template<class _Tp>
5063inline _LIBCPP_INLINE_VISIBILITY
5064bool
5065operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5066{
5067 return static_cast<bool>(__x);
5068}
5069
5070template<class _Tp>
5071inline _LIBCPP_INLINE_VISIBILITY
5072bool
5073operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5074{
5075 return static_cast<bool>(__x);
5076}
5077
5078template<class _Tp>
5079inline _LIBCPP_INLINE_VISIBILITY
5080bool
5081operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5082{
5083 return less<_Tp*>()(__x.get(), nullptr);
5084}
5085
5086template<class _Tp>
5087inline _LIBCPP_INLINE_VISIBILITY
5088bool
5089operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5090{
5091 return less<_Tp*>()(nullptr, __x.get());
5092}
5093
5094template<class _Tp>
5095inline _LIBCPP_INLINE_VISIBILITY
5096bool
5097operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5098{
5099 return nullptr < __x;
5100}
5101
5102template<class _Tp>
5103inline _LIBCPP_INLINE_VISIBILITY
5104bool
5105operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5106{
5107 return __x < nullptr;
5108}
5109
5110template<class _Tp>
5111inline _LIBCPP_INLINE_VISIBILITY
5112bool
5113operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5114{
5115 return !(nullptr < __x);
5116}
5117
5118template<class _Tp>
5119inline _LIBCPP_INLINE_VISIBILITY
5120bool
5121operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5122{
5123 return !(__x < nullptr);
5124}
5125
5126template<class _Tp>
5127inline _LIBCPP_INLINE_VISIBILITY
5128bool
5129operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5130{
5131 return !(__x < nullptr);
5132}
5133
5134template<class _Tp>
5135inline _LIBCPP_INLINE_VISIBILITY
5136bool
5137operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5138{
5139 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005140}
5141
5142template<class _Tp>
5143inline _LIBCPP_INLINE_VISIBILITY
5144void
Howard Hinnant719bda32011-05-28 14:41:13 +00005145swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005146{
5147 __x.swap(__y);
5148}
5149
5150template<class _Tp, class _Up>
5151inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005152typename enable_if
5153<
5154 !is_array<_Tp>::value && !is_array<_Up>::value,
5155 shared_ptr<_Tp>
5156>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005157static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005158{
5159 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
5160}
5161
5162template<class _Tp, class _Up>
5163inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005164typename enable_if
5165<
5166 !is_array<_Tp>::value && !is_array<_Up>::value,
5167 shared_ptr<_Tp>
5168>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005169dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005170{
5171 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
5172 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
5173}
5174
5175template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005176typename enable_if
5177<
5178 is_array<_Tp>::value == is_array<_Up>::value,
5179 shared_ptr<_Tp>
5180>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005181const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005182{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005183 typedef typename remove_extent<_Tp>::type _RTp;
5184 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00005185}
5186
Howard Hinnant72f73582010-08-11 17:04:31 +00005187#ifndef _LIBCPP_NO_RTTI
5188
Howard Hinnantc51e1022010-05-11 19:42:16 +00005189template<class _Dp, class _Tp>
5190inline _LIBCPP_INLINE_VISIBILITY
5191_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00005192get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005193{
5194 return __p.template __get_deleter<_Dp>();
5195}
5196
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005197#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00005198
Howard Hinnantc51e1022010-05-11 19:42:16 +00005199template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005200class _LIBCPP_TYPE_VIS_ONLY weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00005201{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005202public:
5203 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005204private:
5205 element_type* __ptr_;
5206 __shared_weak_count* __cntrl_;
5207
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005208public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005210 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005211 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00005212 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5213 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005214 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005215 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005216 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00005217 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5218 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005219
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005220#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005222 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005223 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005224 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5225 _NOEXCEPT;
5226#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005227 ~weak_ptr();
5228
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005229 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005230 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005231 template<class _Yp>
5232 typename enable_if
5233 <
5234 is_convertible<_Yp*, element_type*>::value,
5235 weak_ptr&
5236 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005238 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
5239
5240#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5241
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005243 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
5244 template<class _Yp>
5245 typename enable_if
5246 <
5247 is_convertible<_Yp*, element_type*>::value,
5248 weak_ptr&
5249 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005251 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
5252
5253#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5254
5255 template<class _Yp>
5256 typename enable_if
5257 <
5258 is_convertible<_Yp*, element_type*>::value,
5259 weak_ptr&
5260 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005262 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005263
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005264 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005265 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005266 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005267 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005268
Howard Hinnant756c69b2010-09-22 16:48:34 +00005269 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005270 long use_count() const _NOEXCEPT
5271 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005273 bool expired() const _NOEXCEPT
5274 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5275 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005276 template<class _Up>
5277 _LIBCPP_INLINE_VISIBILITY
5278 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00005279 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005280 template<class _Up>
5281 _LIBCPP_INLINE_VISIBILITY
5282 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00005283 {return __cntrl_ < __r.__cntrl_;}
5284
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005285 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
5286 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005287};
5288
5289template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005290inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005291_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005292weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005293 : __ptr_(0),
5294 __cntrl_(0)
5295{
5296}
5297
5298template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005299inline
Howard Hinnant719bda32011-05-28 14:41:13 +00005300weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005301 : __ptr_(__r.__ptr_),
5302 __cntrl_(__r.__cntrl_)
5303{
5304 if (__cntrl_)
5305 __cntrl_->__add_weak();
5306}
5307
5308template<class _Tp>
5309template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005310inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005311weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005312 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005313 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005314 : __ptr_(__r.__ptr_),
5315 __cntrl_(__r.__cntrl_)
5316{
5317 if (__cntrl_)
5318 __cntrl_->__add_weak();
5319}
5320
5321template<class _Tp>
5322template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005323inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005324weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005325 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005326 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005327 : __ptr_(__r.__ptr_),
5328 __cntrl_(__r.__cntrl_)
5329{
5330 if (__cntrl_)
5331 __cntrl_->__add_weak();
5332}
5333
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005334#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5335
5336template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005337inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005338weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5339 : __ptr_(__r.__ptr_),
5340 __cntrl_(__r.__cntrl_)
5341{
5342 __r.__ptr_ = 0;
5343 __r.__cntrl_ = 0;
5344}
5345
5346template<class _Tp>
5347template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005348inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005349weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5350 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5351 _NOEXCEPT
5352 : __ptr_(__r.__ptr_),
5353 __cntrl_(__r.__cntrl_)
5354{
5355 __r.__ptr_ = 0;
5356 __r.__cntrl_ = 0;
5357}
5358
5359#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5360
Howard Hinnantc51e1022010-05-11 19:42:16 +00005361template<class _Tp>
5362weak_ptr<_Tp>::~weak_ptr()
5363{
5364 if (__cntrl_)
5365 __cntrl_->__release_weak();
5366}
5367
5368template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005369inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005370weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005371weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005372{
5373 weak_ptr(__r).swap(*this);
5374 return *this;
5375}
5376
5377template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005378template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005379inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005380typename enable_if
5381<
5382 is_convertible<_Yp*, _Tp*>::value,
5383 weak_ptr<_Tp>&
5384>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005385weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005386{
5387 weak_ptr(__r).swap(*this);
5388 return *this;
5389}
5390
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005391#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5392
5393template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005394inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005395weak_ptr<_Tp>&
5396weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5397{
5398 weak_ptr(_VSTD::move(__r)).swap(*this);
5399 return *this;
5400}
5401
Howard Hinnantc51e1022010-05-11 19:42:16 +00005402template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005403template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005404inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005405typename enable_if
5406<
5407 is_convertible<_Yp*, _Tp*>::value,
5408 weak_ptr<_Tp>&
5409>::type
5410weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5411{
5412 weak_ptr(_VSTD::move(__r)).swap(*this);
5413 return *this;
5414}
5415
5416#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5417
5418template<class _Tp>
5419template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005420inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005421typename enable_if
5422<
5423 is_convertible<_Yp*, _Tp*>::value,
5424 weak_ptr<_Tp>&
5425>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005426weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005427{
5428 weak_ptr(__r).swap(*this);
5429 return *this;
5430}
5431
5432template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005433inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005434void
Howard Hinnant719bda32011-05-28 14:41:13 +00005435weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005436{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005437 _VSTD::swap(__ptr_, __r.__ptr_);
5438 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005439}
5440
5441template<class _Tp>
5442inline _LIBCPP_INLINE_VISIBILITY
5443void
Howard Hinnant719bda32011-05-28 14:41:13 +00005444swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005445{
5446 __x.swap(__y);
5447}
5448
5449template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005450inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005451void
Howard Hinnant719bda32011-05-28 14:41:13 +00005452weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005453{
5454 weak_ptr().swap(*this);
5455}
5456
5457template<class _Tp>
5458template<class _Yp>
5459shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5460 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
5461 : __ptr_(__r.__ptr_),
5462 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5463{
5464 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00005465 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00005466}
5467
5468template<class _Tp>
5469shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005470weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005471{
5472 shared_ptr<_Tp> __r;
5473 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5474 if (__r.__cntrl_)
5475 __r.__ptr_ = __ptr_;
5476 return __r;
5477}
5478
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005479#if _LIBCPP_STD_VER > 14
5480template <class _Tp = void> struct owner_less;
5481#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005482template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005483#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005484
5485template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005486struct _LIBCPP_TYPE_VIS_ONLY owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005487 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005488{
5489 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005490 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005491 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5492 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005493 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005494 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5495 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005496 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005497 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5498 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005499};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005500
5501template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005502struct _LIBCPP_TYPE_VIS_ONLY owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005503 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5504{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005505 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005506 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005507 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5508 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005509 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005510 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5511 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005512 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005513 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5514 {return __x.owner_before(__y);}
5515};
5516
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005517#if _LIBCPP_STD_VER > 14
5518template <>
5519struct _LIBCPP_TYPE_VIS_ONLY owner_less<void>
5520{
5521 template <class _Tp, class _Up>
5522 _LIBCPP_INLINE_VISIBILITY
5523 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const
5524 {return __x.owner_before(__y);}
5525 template <class _Tp, class _Up>
5526 _LIBCPP_INLINE_VISIBILITY
5527 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const
5528 {return __x.owner_before(__y);}
5529 template <class _Tp, class _Up>
5530 _LIBCPP_INLINE_VISIBILITY
5531 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const
5532 {return __x.owner_before(__y);}
5533 template <class _Tp, class _Up>
5534 _LIBCPP_INLINE_VISIBILITY
5535 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const
5536 {return __x.owner_before(__y);}
5537 typedef void is_transparent;
5538};
5539#endif
5540
Howard Hinnantc51e1022010-05-11 19:42:16 +00005541template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005542class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005543{
5544 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005545protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005546 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005547 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005549 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005551 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5552 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005554 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005555public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005557 shared_ptr<_Tp> shared_from_this()
5558 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005560 shared_ptr<_Tp const> shared_from_this() const
5561 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005562
Eric Fiselier84006862016-06-02 00:15:35 +00005563#if _LIBCPP_STD_VER > 14
5564 _LIBCPP_INLINE_VISIBILITY
5565 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5566 { return __weak_this_; }
5567
5568 _LIBCPP_INLINE_VISIBILITY
5569 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5570 { return __weak_this_; }
5571#endif // _LIBCPP_STD_VER > 14
5572
Howard Hinnantc51e1022010-05-11 19:42:16 +00005573 template <class _Up> friend class shared_ptr;
5574};
5575
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005576template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005577struct _LIBCPP_TYPE_VIS_ONLY hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005578{
5579 typedef shared_ptr<_Tp> argument_type;
5580 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005581 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005582 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005583 {
5584 return hash<_Tp*>()(__ptr.get());
5585 }
5586};
5587
Howard Hinnantc834c512011-11-29 18:15:50 +00005588template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005589inline _LIBCPP_INLINE_VISIBILITY
5590basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005591operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005592
Eric Fiselier9b492672016-06-18 02:12:53 +00005593
5594#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005595
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005596class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005597{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005598 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005599public:
5600 void lock() _NOEXCEPT;
5601 void unlock() _NOEXCEPT;
5602
5603private:
5604 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5605 __sp_mut(const __sp_mut&);
5606 __sp_mut& operator=(const __sp_mut&);
5607
Howard Hinnant8331b762013-03-06 23:30:19 +00005608 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005609};
5610
Howard Hinnant8331b762013-03-06 23:30:19 +00005611_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005612
5613template <class _Tp>
5614inline _LIBCPP_INLINE_VISIBILITY
5615bool
5616atomic_is_lock_free(const shared_ptr<_Tp>*)
5617{
5618 return false;
5619}
5620
5621template <class _Tp>
5622shared_ptr<_Tp>
5623atomic_load(const shared_ptr<_Tp>* __p)
5624{
5625 __sp_mut& __m = __get_sp_mut(__p);
5626 __m.lock();
5627 shared_ptr<_Tp> __q = *__p;
5628 __m.unlock();
5629 return __q;
5630}
5631
5632template <class _Tp>
5633inline _LIBCPP_INLINE_VISIBILITY
5634shared_ptr<_Tp>
5635atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5636{
5637 return atomic_load(__p);
5638}
5639
5640template <class _Tp>
5641void
5642atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5643{
5644 __sp_mut& __m = __get_sp_mut(__p);
5645 __m.lock();
5646 __p->swap(__r);
5647 __m.unlock();
5648}
5649
5650template <class _Tp>
5651inline _LIBCPP_INLINE_VISIBILITY
5652void
5653atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5654{
5655 atomic_store(__p, __r);
5656}
5657
5658template <class _Tp>
5659shared_ptr<_Tp>
5660atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5661{
5662 __sp_mut& __m = __get_sp_mut(__p);
5663 __m.lock();
5664 __p->swap(__r);
5665 __m.unlock();
5666 return __r;
5667}
5668
5669template <class _Tp>
5670inline _LIBCPP_INLINE_VISIBILITY
5671shared_ptr<_Tp>
5672atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5673{
5674 return atomic_exchange(__p, __r);
5675}
5676
5677template <class _Tp>
5678bool
5679atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5680{
Marshall Clow4201ee82016-05-18 17:50:13 +00005681 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005682 __sp_mut& __m = __get_sp_mut(__p);
5683 __m.lock();
5684 if (__p->__owner_equivalent(*__v))
5685 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005686 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005687 *__p = __w;
5688 __m.unlock();
5689 return true;
5690 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005691 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005692 *__v = *__p;
5693 __m.unlock();
5694 return false;
5695}
5696
5697template <class _Tp>
5698inline _LIBCPP_INLINE_VISIBILITY
5699bool
5700atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5701{
5702 return atomic_compare_exchange_strong(__p, __v, __w);
5703}
5704
5705template <class _Tp>
5706inline _LIBCPP_INLINE_VISIBILITY
5707bool
5708atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5709 shared_ptr<_Tp> __w, memory_order, memory_order)
5710{
5711 return atomic_compare_exchange_strong(__p, __v, __w);
5712}
5713
5714template <class _Tp>
5715inline _LIBCPP_INLINE_VISIBILITY
5716bool
5717atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5718 shared_ptr<_Tp> __w, memory_order, memory_order)
5719{
5720 return atomic_compare_exchange_weak(__p, __v, __w);
5721}
5722
Eric Fiselier9b492672016-06-18 02:12:53 +00005723#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005724
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005725//enum class
Howard Hinnant8331b762013-03-06 23:30:19 +00005726struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005727{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005728 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005729 {
5730 relaxed,
5731 preferred,
5732 strict
5733 };
5734
Howard Hinnant49e145e2012-10-30 19:06:59 +00005735 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005736
Howard Hinnant756c69b2010-09-22 16:48:34 +00005737 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005738 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005739 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005740 operator int() const {return __v_;}
5741};
5742
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005743_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5744_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5745_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
5746_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5747_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005748
5749template <class _Tp>
5750inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005751_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005752undeclare_reachable(_Tp* __p)
5753{
5754 return static_cast<_Tp*>(__undeclare_reachable(__p));
5755}
5756
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005757_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005758
Marshall Clow8982dcd2015-07-13 20:04:56 +00005759// --- Helper for container swap --
5760template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005761inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005762void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5763#if _LIBCPP_STD_VER >= 14
5764 _NOEXCEPT
5765#else
5766 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5767#endif
5768{
5769 __swap_allocator(__a1, __a2,
5770 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5771}
5772
5773template <typename _Alloc>
5774_LIBCPP_INLINE_VISIBILITY
5775void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5776#if _LIBCPP_STD_VER >= 14
5777 _NOEXCEPT
5778#else
5779 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5780#endif
5781{
5782 using _VSTD::swap;
5783 swap(__a1, __a2);
5784}
5785
5786template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005787inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005788void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5789
Marshall Clowff91de82015-08-18 19:51:37 +00005790template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005791struct __noexcept_move_assign_container : public integral_constant<bool,
5792 _Traits::propagate_on_container_move_assignment::value
5793#if _LIBCPP_STD_VER > 14
5794 || _Traits::is_always_equal::value
5795#else
5796 && is_nothrow_move_assignable<_Alloc>::value
5797#endif
5798 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005799
Marshall Clowa591b9a2016-07-11 21:38:08 +00005800
5801#ifndef _LIBCPP_HAS_NO_VARIADICS
5802template <class _Tp, class _Alloc>
5803struct __temp_value {
5804 typedef allocator_traits<_Alloc> _Traits;
5805
5806 typename aligned_storage<sizeof(_Tp), alignof(_Tp)>::type __v;
5807 _Alloc &__a;
5808
5809 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5810 _Tp & get() { return *__addr(); }
5811
5812 template<class... _Args>
5813 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc)
5814 { _Traits::construct(__a, __addr(), _VSTD::forward<_Args>(__args)...); }
5815
5816 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5817 };
5818#endif
5819
Howard Hinnantc51e1022010-05-11 19:42:16 +00005820_LIBCPP_END_NAMESPACE_STD
5821
5822#endif // _LIBCPP_MEMORY