blob: 5cdb60f5e70bf5328296f9f455a9ab72c16b813f [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#if defined(_LIBCPP_NO_EXCEPTIONS)
641 #include <cassert>
642#endif
643
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000644#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000645# include <atomic>
646#endif
647
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000648#include <__undef_min_max>
Saleem Abdulrasooldcf27a62015-02-13 22:15:32 +0000649#include <__undef___deallocate>
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000650
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000651#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000652#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000653#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000654
655_LIBCPP_BEGIN_NAMESPACE_STD
656
Eric Fiselier89659d12015-07-07 00:27:16 +0000657template <class _ValueType>
658inline _LIBCPP_ALWAYS_INLINE
659_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
660#if !defined(_LIBCPP_HAS_NO_THREADS) && \
661 defined(__ATOMIC_RELAXED) && \
662 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
663 return __atomic_load_n(__value, __ATOMIC_RELAXED);
664#else
665 return *__value;
666#endif
667}
668
Howard Hinnant8bea6da2013-08-08 18:38:55 +0000669// addressof moved to <__functional_base>
Douglas Gregor13034442011-06-22 22:17:44 +0000670
Howard Hinnantc51e1022010-05-11 19:42:16 +0000671template <class _Tp> class allocator;
672
673template <>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000674class _LIBCPP_TYPE_VIS_ONLY allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000675{
676public:
677 typedef void* pointer;
678 typedef const void* const_pointer;
679 typedef void value_type;
680
681 template <class _Up> struct rebind {typedef allocator<_Up> other;};
682};
683
Howard Hinnant9f771052012-01-19 23:15:22 +0000684template <>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000685class _LIBCPP_TYPE_VIS_ONLY allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000686{
687public:
688 typedef const void* pointer;
689 typedef const void* const_pointer;
690 typedef const void value_type;
691
692 template <class _Up> struct rebind {typedef allocator<_Up> other;};
693};
694
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695// pointer_traits
696
697template <class _Tp>
698struct __has_element_type
699{
700private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000701 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000702 template <class _Up> static __two __test(...);
703 template <class _Up> static char __test(typename _Up::element_type* = 0);
704public:
705 static const bool value = sizeof(__test<_Tp>(0)) == 1;
706};
707
708template <class _Ptr, bool = __has_element_type<_Ptr>::value>
709struct __pointer_traits_element_type;
710
711template <class _Ptr>
712struct __pointer_traits_element_type<_Ptr, true>
713{
714 typedef typename _Ptr::element_type type;
715};
716
717#ifndef _LIBCPP_HAS_NO_VARIADICS
718
719template <template <class, class...> class _Sp, class _Tp, class ..._Args>
720struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
721{
722 typedef typename _Sp<_Tp, _Args...>::element_type type;
723};
724
725template <template <class, class...> class _Sp, class _Tp, class ..._Args>
726struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
727{
728 typedef _Tp type;
729};
730
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000731#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000732
733template <template <class> class _Sp, class _Tp>
734struct __pointer_traits_element_type<_Sp<_Tp>, true>
735{
736 typedef typename _Sp<_Tp>::element_type type;
737};
738
739template <template <class> class _Sp, class _Tp>
740struct __pointer_traits_element_type<_Sp<_Tp>, false>
741{
742 typedef _Tp type;
743};
744
745template <template <class, class> class _Sp, class _Tp, class _A0>
746struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
747{
748 typedef typename _Sp<_Tp, _A0>::element_type type;
749};
750
751template <template <class, class> class _Sp, class _Tp, class _A0>
752struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
753{
754 typedef _Tp type;
755};
756
757template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
758struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
759{
760 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
761};
762
763template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
764struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
765{
766 typedef _Tp type;
767};
768
769template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
770 class _A1, class _A2>
771struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
772{
773 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
774};
775
776template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
777 class _A1, class _A2>
778struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
779{
780 typedef _Tp type;
781};
782
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000783#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000784
785template <class _Tp>
786struct __has_difference_type
787{
788private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000789 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000790 template <class _Up> static __two __test(...);
791 template <class _Up> static char __test(typename _Up::difference_type* = 0);
792public:
793 static const bool value = sizeof(__test<_Tp>(0)) == 1;
794};
795
796template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
797struct __pointer_traits_difference_type
798{
799 typedef ptrdiff_t type;
800};
801
802template <class _Ptr>
803struct __pointer_traits_difference_type<_Ptr, true>
804{
805 typedef typename _Ptr::difference_type type;
806};
807
808template <class _Tp, class _Up>
809struct __has_rebind
810{
811private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000812 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000813 template <class _Xp> static __two __test(...);
814 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
815public:
816 static const bool value = sizeof(__test<_Tp>(0)) == 1;
817};
818
819template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
820struct __pointer_traits_rebind
821{
822#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
823 typedef typename _Tp::template rebind<_Up> type;
824#else
825 typedef typename _Tp::template rebind<_Up>::other type;
826#endif
827};
828
829#ifndef _LIBCPP_HAS_NO_VARIADICS
830
831template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
832struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
833{
834#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
835 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
836#else
837 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
838#endif
839};
840
841template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
842struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
843{
844 typedef _Sp<_Up, _Args...> type;
845};
846
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000847#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000848
849template <template <class> class _Sp, class _Tp, class _Up>
850struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
851{
852#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
853 typedef typename _Sp<_Tp>::template rebind<_Up> type;
854#else
855 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
856#endif
857};
858
859template <template <class> class _Sp, class _Tp, class _Up>
860struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
861{
862 typedef _Sp<_Up> type;
863};
864
865template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
866struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
867{
868#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
869 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
870#else
871 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
872#endif
873};
874
875template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
876struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
877{
878 typedef _Sp<_Up, _A0> type;
879};
880
881template <template <class, class, class> class _Sp, class _Tp, class _A0,
882 class _A1, class _Up>
883struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
884{
885#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
886 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
887#else
888 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
889#endif
890};
891
892template <template <class, class, class> class _Sp, class _Tp, class _A0,
893 class _A1, class _Up>
894struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
895{
896 typedef _Sp<_Up, _A0, _A1> type;
897};
898
899template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
900 class _A1, class _A2, class _Up>
901struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
902{
903#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
904 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
905#else
906 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
907#endif
908};
909
910template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
911 class _A1, class _A2, class _Up>
912struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
913{
914 typedef _Sp<_Up, _A0, _A1, _A2> type;
915};
916
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000917#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000918
919template <class _Ptr>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000920struct _LIBCPP_TYPE_VIS_ONLY pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000921{
922 typedef _Ptr pointer;
923 typedef typename __pointer_traits_element_type<pointer>::type element_type;
924 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
925
926#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantf09283d2011-05-11 20:21:19 +0000927 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928#else
929 template <class _Up> struct rebind
930 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000931#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000932
933private:
934 struct __nat {};
935public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000936 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000937 static pointer pointer_to(typename conditional<is_void<element_type>::value,
938 __nat, element_type>::type& __r)
939 {return pointer::pointer_to(__r);}
940};
941
942template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000943struct _LIBCPP_TYPE_VIS_ONLY pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000944{
945 typedef _Tp* pointer;
946 typedef _Tp element_type;
947 typedef ptrdiff_t difference_type;
948
949#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
950 template <class _Up> using rebind = _Up*;
951#else
952 template <class _Up> struct rebind {typedef _Up* other;};
953#endif
954
955private:
956 struct __nat {};
957public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000958 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000959 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000960 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000961 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962};
963
Eric Fiselierae3ab842015-08-23 02:56:05 +0000964template <class _From, class _To>
965struct __rebind_pointer {
966#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
967 typedef typename pointer_traits<_From>::template rebind<_To> type;
968#else
969 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
970#endif
971};
972
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973// allocator_traits
974
975namespace __has_pointer_type_imp
976{
Howard Hinnant6e260172013-09-21 01:45:05 +0000977 template <class _Up> static __two __test(...);
978 template <class _Up> static char __test(typename _Up::pointer* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000979}
980
981template <class _Tp>
982struct __has_pointer_type
Howard Hinnant6e260172013-09-21 01:45:05 +0000983 : public integral_constant<bool, sizeof(__has_pointer_type_imp::__test<_Tp>(0)) == 1>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000984{
985};
986
987namespace __pointer_type_imp
988{
989
990template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
991struct __pointer_type
992{
993 typedef typename _Dp::pointer type;
994};
995
996template <class _Tp, class _Dp>
997struct __pointer_type<_Tp, _Dp, false>
998{
999 typedef _Tp* type;
1000};
1001
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001002} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001003
1004template <class _Tp, class _Dp>
1005struct __pointer_type
1006{
1007 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
1008};
1009
1010template <class _Tp>
1011struct __has_const_pointer
1012{
1013private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001014 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015 template <class _Up> static __two __test(...);
1016 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
1017public:
1018 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1019};
1020
1021template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1022struct __const_pointer
1023{
1024 typedef typename _Alloc::const_pointer type;
1025};
1026
1027template <class _Tp, class _Ptr, class _Alloc>
1028struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1029{
1030#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1031 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1032#else
1033 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1034#endif
1035};
1036
1037template <class _Tp>
1038struct __has_void_pointer
1039{
1040private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001041 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001042 template <class _Up> static __two __test(...);
1043 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1044public:
1045 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1046};
1047
1048template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1049struct __void_pointer
1050{
1051 typedef typename _Alloc::void_pointer type;
1052};
1053
1054template <class _Ptr, class _Alloc>
1055struct __void_pointer<_Ptr, _Alloc, false>
1056{
1057#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1058 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1059#else
1060 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1061#endif
1062};
1063
1064template <class _Tp>
1065struct __has_const_void_pointer
1066{
1067private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001068 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069 template <class _Up> static __two __test(...);
1070 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1071public:
1072 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1073};
1074
1075template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1076struct __const_void_pointer
1077{
1078 typedef typename _Alloc::const_void_pointer type;
1079};
1080
1081template <class _Ptr, class _Alloc>
1082struct __const_void_pointer<_Ptr, _Alloc, false>
1083{
1084#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1085 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1086#else
1087 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1088#endif
1089};
1090
Howard Hinnantc834c512011-11-29 18:15:50 +00001091template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001093_Tp*
1094__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095{
1096 return __p;
1097}
1098
1099template <class _Pointer>
1100inline _LIBCPP_INLINE_VISIBILITY
1101typename pointer_traits<_Pointer>::element_type*
Howard Hinnant719bda32011-05-28 14:41:13 +00001102__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001103{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001104 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001105}
1106
1107template <class _Tp>
1108struct __has_size_type
1109{
1110private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001111 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001112 template <class _Up> static __two __test(...);
1113 template <class _Up> static char __test(typename _Up::size_type* = 0);
1114public:
1115 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1116};
1117
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001118template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119struct __size_type
1120{
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001121 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122};
1123
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001124template <class _Alloc, class _DiffType>
1125struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126{
1127 typedef typename _Alloc::size_type type;
1128};
1129
1130template <class _Tp>
1131struct __has_propagate_on_container_copy_assignment
1132{
1133private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001134 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001135 template <class _Up> static __two __test(...);
1136 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1137public:
1138 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1139};
1140
1141template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1142struct __propagate_on_container_copy_assignment
1143{
1144 typedef false_type type;
1145};
1146
1147template <class _Alloc>
1148struct __propagate_on_container_copy_assignment<_Alloc, true>
1149{
1150 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1151};
1152
1153template <class _Tp>
1154struct __has_propagate_on_container_move_assignment
1155{
1156private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001157 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158 template <class _Up> static __two __test(...);
1159 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1160public:
1161 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1162};
1163
1164template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1165struct __propagate_on_container_move_assignment
1166{
1167 typedef false_type type;
1168};
1169
1170template <class _Alloc>
1171struct __propagate_on_container_move_assignment<_Alloc, true>
1172{
1173 typedef typename _Alloc::propagate_on_container_move_assignment type;
1174};
1175
1176template <class _Tp>
1177struct __has_propagate_on_container_swap
1178{
1179private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001180 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181 template <class _Up> static __two __test(...);
1182 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1183public:
1184 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1185};
1186
1187template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1188struct __propagate_on_container_swap
1189{
1190 typedef false_type type;
1191};
1192
1193template <class _Alloc>
1194struct __propagate_on_container_swap<_Alloc, true>
1195{
1196 typedef typename _Alloc::propagate_on_container_swap type;
1197};
1198
Marshall Clow0b587562015-06-02 16:34:03 +00001199template <class _Tp>
1200struct __has_is_always_equal
1201{
1202private:
1203 struct __two {char __lx; char __lxx;};
1204 template <class _Up> static __two __test(...);
1205 template <class _Up> static char __test(typename _Up::is_always_equal* = 0);
1206public:
1207 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1208};
1209
1210template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1211struct __is_always_equal
1212{
1213 typedef typename _VSTD::is_empty<_Alloc>::type type;
1214};
1215
1216template <class _Alloc>
1217struct __is_always_equal<_Alloc, true>
1218{
1219 typedef typename _Alloc::is_always_equal type;
1220};
1221
Howard Hinnantc51e1022010-05-11 19:42:16 +00001222template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1223struct __has_rebind_other
1224{
1225private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001226 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001227 template <class _Xp> static __two __test(...);
1228 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1229public:
1230 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1231};
1232
1233template <class _Tp, class _Up>
1234struct __has_rebind_other<_Tp, _Up, false>
1235{
1236 static const bool value = false;
1237};
1238
1239template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1240struct __allocator_traits_rebind
1241{
1242 typedef typename _Tp::template rebind<_Up>::other type;
1243};
1244
1245#ifndef _LIBCPP_HAS_NO_VARIADICS
1246
1247template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1248struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1249{
1250 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1251};
1252
1253template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1254struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1255{
1256 typedef _Alloc<_Up, _Args...> type;
1257};
1258
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001259#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260
1261template <template <class> class _Alloc, class _Tp, class _Up>
1262struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1263{
1264 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1265};
1266
1267template <template <class> class _Alloc, class _Tp, class _Up>
1268struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1269{
1270 typedef _Alloc<_Up> type;
1271};
1272
Howard Hinnantc51e1022010-05-11 19:42:16 +00001273template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1274struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1275{
1276 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1277};
1278
1279template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1280struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1281{
1282 typedef _Alloc<_Up, _A0> type;
1283};
1284
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1286 class _A1, class _Up>
1287struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1288{
1289 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1290};
1291
1292template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1293 class _A1, class _Up>
1294struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1295{
1296 typedef _Alloc<_Up, _A0, _A1> type;
1297};
1298
Howard Hinnantc51e1022010-05-11 19:42:16 +00001299template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1300 class _A1, class _A2, class _Up>
1301struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1302{
1303 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1304};
1305
1306template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1307 class _A1, class _A2, class _Up>
1308struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1309{
1310 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1311};
1312
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001313#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001314
1315#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
1316
1317template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1318auto
1319__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1320 -> decltype(__a.allocate(__sz, __p), true_type());
1321
1322template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1323auto
1324__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1325 -> false_type;
1326
1327template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1328struct __has_allocate_hint
1329 : integral_constant<bool,
1330 is_same<
1331 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1332 declval<_SizeType>(),
1333 declval<_ConstVoidPtr>())),
1334 true_type>::value>
1335{
1336};
1337
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001338#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001339
1340template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1341struct __has_allocate_hint
1342 : true_type
1343{
1344};
1345
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001346#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001347
Howard Hinnant986832c2011-07-29 21:35:53 +00001348#if !defined(_LIBCPP_HAS_NO_ADVANCED_SFINAE) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001349
1350template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001351decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1352 _VSTD::declval<_Args>()...),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001353 true_type())
1354__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1355
1356template <class _Alloc, class _Pointer, class ..._Args>
1357false_type
1358__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1359
1360template <class _Alloc, class _Pointer, class ..._Args>
1361struct __has_construct
1362 : integral_constant<bool,
1363 is_same<
1364 decltype(__has_construct_test(declval<_Alloc>(),
1365 declval<_Pointer>(),
1366 declval<_Args>()...)),
1367 true_type>::value>
1368{
1369};
1370
1371template <class _Alloc, class _Pointer>
1372auto
1373__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1374 -> decltype(__a.destroy(__p), true_type());
1375
1376template <class _Alloc, class _Pointer>
1377auto
1378__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1379 -> false_type;
1380
1381template <class _Alloc, class _Pointer>
1382struct __has_destroy
1383 : integral_constant<bool,
1384 is_same<
1385 decltype(__has_destroy_test(declval<_Alloc>(),
1386 declval<_Pointer>())),
1387 true_type>::value>
1388{
1389};
1390
1391template <class _Alloc>
1392auto
1393__has_max_size_test(_Alloc&& __a)
1394 -> decltype(__a.max_size(), true_type());
1395
1396template <class _Alloc>
1397auto
1398__has_max_size_test(const volatile _Alloc& __a)
1399 -> false_type;
1400
1401template <class _Alloc>
1402struct __has_max_size
1403 : integral_constant<bool,
1404 is_same<
1405 decltype(__has_max_size_test(declval<_Alloc&>())),
1406 true_type>::value>
1407{
1408};
1409
1410template <class _Alloc>
1411auto
1412__has_select_on_container_copy_construction_test(_Alloc&& __a)
1413 -> decltype(__a.select_on_container_copy_construction(), true_type());
1414
1415template <class _Alloc>
1416auto
1417__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1418 -> false_type;
1419
1420template <class _Alloc>
1421struct __has_select_on_container_copy_construction
1422 : integral_constant<bool,
1423 is_same<
1424 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1425 true_type>::value>
1426{
1427};
1428
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001429#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001430
1431#ifndef _LIBCPP_HAS_NO_VARIADICS
1432
1433template <class _Alloc, class _Pointer, class ..._Args>
1434struct __has_construct
1435 : false_type
1436{
1437};
1438
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001439#else // _LIBCPP_HAS_NO_VARIADICS
1440
1441template <class _Alloc, class _Pointer, class _Args>
1442struct __has_construct
1443 : false_type
1444{
1445};
1446
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001447#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448
1449template <class _Alloc, class _Pointer>
1450struct __has_destroy
1451 : false_type
1452{
1453};
1454
1455template <class _Alloc>
1456struct __has_max_size
1457 : true_type
1458{
1459};
1460
1461template <class _Alloc>
1462struct __has_select_on_container_copy_construction
1463 : false_type
1464{
1465};
1466
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001467#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
Howard Hinnantc51e1022010-05-11 19:42:16 +00001468
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001469template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1470struct __alloc_traits_difference_type
1471{
1472 typedef typename pointer_traits<_Ptr>::difference_type type;
1473};
1474
1475template <class _Alloc, class _Ptr>
1476struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1477{
1478 typedef typename _Alloc::difference_type type;
1479};
1480
Howard Hinnantc51e1022010-05-11 19:42:16 +00001481template <class _Alloc>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001482struct _LIBCPP_TYPE_VIS_ONLY allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001483{
1484 typedef _Alloc allocator_type;
1485 typedef typename allocator_type::value_type value_type;
1486
1487 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1488 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1489 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1490 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1491
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001492 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1493 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001494
1495 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1496 propagate_on_container_copy_assignment;
1497 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1498 propagate_on_container_move_assignment;
1499 typedef typename __propagate_on_container_swap<allocator_type>::type
1500 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001501 typedef typename __is_always_equal<allocator_type>::type
1502 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001503
1504#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1505 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001506 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001507 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001508#else // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001509 template <class _Tp> struct rebind_alloc
1510 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1511 template <class _Tp> struct rebind_traits
1512 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001513#endif // _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001514
Howard Hinnant756c69b2010-09-22 16:48:34 +00001515 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001516 static pointer allocate(allocator_type& __a, size_type __n)
1517 {return __a.allocate(__n);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001518 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001519 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1520 {return allocate(__a, __n, __hint,
1521 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1522
Howard Hinnant756c69b2010-09-22 16:48:34 +00001523 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001524 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001525 {__a.deallocate(__p, __n);}
1526
1527#ifndef _LIBCPP_HAS_NO_VARIADICS
1528 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001530 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001531 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001532 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001533#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001535 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001536 static void construct(allocator_type& __a, _Tp* __p)
1537 {
1538 ::new ((void*)__p) _Tp();
1539 }
1540 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001542 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1543 {
1544 ::new ((void*)__p) _Tp(__a0);
1545 }
1546 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1549 const _A1& __a1)
1550 {
1551 ::new ((void*)__p) _Tp(__a0, __a1);
1552 }
1553 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001555 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1556 const _A1& __a1, const _A2& __a2)
1557 {
1558 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1559 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001560#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561
1562 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564 static void destroy(allocator_type& __a, _Tp* __p)
1565 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1566
Howard Hinnant756c69b2010-09-22 16:48:34 +00001567 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001568 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001569 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1570
Howard Hinnant756c69b2010-09-22 16:48:34 +00001571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001572 static allocator_type
1573 select_on_container_copy_construction(const allocator_type& __a)
1574 {return select_on_container_copy_construction(
1575 __has_select_on_container_copy_construction<const allocator_type>(),
1576 __a);}
1577
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001578 template <class _Ptr>
1579 _LIBCPP_INLINE_VISIBILITY
1580 static
1581 void
1582 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1583 {
1584 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1585 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1586 }
1587
1588 template <class _Tp>
1589 _LIBCPP_INLINE_VISIBILITY
1590 static
1591 typename enable_if
1592 <
1593 (is_same<allocator_type, allocator<_Tp> >::value
1594 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1595 is_trivially_move_constructible<_Tp>::value,
1596 void
1597 >::type
1598 __construct_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1599 {
1600 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001601 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001602 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001603 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001604 __begin2 += _Np;
1605 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001606 }
1607
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001608 template <class _Iter, class _Ptr>
1609 _LIBCPP_INLINE_VISIBILITY
1610 static
1611 void
1612 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1613 {
1614 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1615 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1616 }
1617
1618 template <class _Tp>
1619 _LIBCPP_INLINE_VISIBILITY
1620 static
1621 typename enable_if
1622 <
1623 (is_same<allocator_type, allocator<_Tp> >::value
1624 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1625 is_trivially_move_constructible<_Tp>::value,
1626 void
1627 >::type
1628 __construct_range_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1629 {
1630 typedef typename remove_const<_Tp>::type _Vp;
1631 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001632 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001633 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001634 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001635 __begin2 += _Np;
1636 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001637 }
1638
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001639 template <class _Ptr>
1640 _LIBCPP_INLINE_VISIBILITY
1641 static
1642 void
1643 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1644 {
1645 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001646 {
1647 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1648 --__end2;
1649 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001650 }
1651
1652 template <class _Tp>
1653 _LIBCPP_INLINE_VISIBILITY
1654 static
1655 typename enable_if
1656 <
1657 (is_same<allocator_type, allocator<_Tp> >::value
1658 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1659 is_trivially_move_constructible<_Tp>::value,
1660 void
1661 >::type
1662 __construct_backward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
1663 {
1664 ptrdiff_t _Np = __end1 - __begin1;
1665 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001666 if (_Np > 0)
1667 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001668 }
1669
Howard Hinnantc51e1022010-05-11 19:42:16 +00001670private:
1671
Howard Hinnant756c69b2010-09-22 16:48:34 +00001672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001673 static pointer allocate(allocator_type& __a, size_type __n,
1674 const_void_pointer __hint, true_type)
1675 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001676 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001677 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001678 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001679 {return __a.allocate(__n);}
1680
1681#ifndef _LIBCPP_HAS_NO_VARIADICS
1682 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001684 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001685 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001686 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001687 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001688 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1689 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001690 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001691 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001692#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001693
1694 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001695 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001696 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1697 {__a.destroy(__p);}
1698 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001699 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700 static void __destroy(false_type, allocator_type&, _Tp* __p)
1701 {
1702 __p->~_Tp();
1703 }
1704
Howard Hinnant756c69b2010-09-22 16:48:34 +00001705 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001706 static size_type __max_size(true_type, const allocator_type& __a)
1707 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001708 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001709 static size_type __max_size(false_type, const allocator_type&)
Marshall Clow33daa162015-10-25 19:34:04 +00001710 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001711
Howard Hinnant756c69b2010-09-22 16:48:34 +00001712 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001713 static allocator_type
1714 select_on_container_copy_construction(true_type, const allocator_type& __a)
1715 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001716 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001717 static allocator_type
1718 select_on_container_copy_construction(false_type, const allocator_type& __a)
1719 {return __a;}
1720};
1721
Marshall Clow940e01c2015-04-07 05:21:38 +00001722template <class _Traits, class _Tp>
1723struct __rebind_alloc_helper
1724{
1725#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Marshall Clow35cde742015-05-10 13:59:45 +00001726 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001727#else
1728 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1729#endif
1730};
1731
Howard Hinnantc51e1022010-05-11 19:42:16 +00001732// allocator
1733
1734template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001735class _LIBCPP_TYPE_VIS_ONLY allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001736{
1737public:
1738 typedef size_t size_type;
1739 typedef ptrdiff_t difference_type;
1740 typedef _Tp* pointer;
1741 typedef const _Tp* const_pointer;
1742 typedef _Tp& reference;
1743 typedef const _Tp& const_reference;
1744 typedef _Tp value_type;
1745
Howard Hinnant4931e092011-06-02 21:38:57 +00001746 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001747 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001748
Howard Hinnantc51e1022010-05-11 19:42:16 +00001749 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1750
Howard Hinnant719bda32011-05-28 14:41:13 +00001751 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1752 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1753 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001754 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001755 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001756 {return _VSTD::addressof(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001757 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001758 {
1759 if (__n > max_size())
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001760 __libcpp_throw(length_error("allocator<T>::allocate(size_t n)"
1761 " 'n' exceeds maximum supported size"));
Marshall Clow813ffb32016-03-03 12:04:39 +00001762 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
1763 }
Howard Hinnant719bda32011-05-28 14:41:13 +00001764 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith1f1c1472014-06-04 19:54:15 +00001765 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001766 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1767 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001768#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001769 template <class _Up, class... _Args>
1770 _LIBCPP_INLINE_VISIBILITY
1771 void
1772 construct(_Up* __p, _Args&&... __args)
1773 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001774 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001776#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777 _LIBCPP_INLINE_VISIBILITY
1778 void
1779 construct(pointer __p)
1780 {
1781 ::new((void*)__p) _Tp();
1782 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001783# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001784
Howard Hinnantc51e1022010-05-11 19:42:16 +00001785 template <class _A0>
1786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001787 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001788 construct(pointer __p, _A0& __a0)
1789 {
1790 ::new((void*)__p) _Tp(__a0);
1791 }
1792 template <class _A0>
1793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001794 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001795 construct(pointer __p, const _A0& __a0)
1796 {
1797 ::new((void*)__p) _Tp(__a0);
1798 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001799# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001800 template <class _A0, class _A1>
1801 _LIBCPP_INLINE_VISIBILITY
1802 void
1803 construct(pointer __p, _A0& __a0, _A1& __a1)
1804 {
1805 ::new((void*)__p) _Tp(__a0, __a1);
1806 }
1807 template <class _A0, class _A1>
1808 _LIBCPP_INLINE_VISIBILITY
1809 void
1810 construct(pointer __p, const _A0& __a0, _A1& __a1)
1811 {
1812 ::new((void*)__p) _Tp(__a0, __a1);
1813 }
1814 template <class _A0, class _A1>
1815 _LIBCPP_INLINE_VISIBILITY
1816 void
1817 construct(pointer __p, _A0& __a0, const _A1& __a1)
1818 {
1819 ::new((void*)__p) _Tp(__a0, __a1);
1820 }
1821 template <class _A0, class _A1>
1822 _LIBCPP_INLINE_VISIBILITY
1823 void
1824 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1825 {
1826 ::new((void*)__p) _Tp(__a0, __a1);
1827 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001828#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001829 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1830};
1831
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001832template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001833class _LIBCPP_TYPE_VIS_ONLY allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001834{
1835public:
1836 typedef size_t size_type;
1837 typedef ptrdiff_t difference_type;
1838 typedef const _Tp* pointer;
1839 typedef const _Tp* const_pointer;
1840 typedef const _Tp& reference;
1841 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001842 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001843
1844 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001845 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001846
1847 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1848
1849 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1850 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1851 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1852 {return _VSTD::addressof(__x);}
1853 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001854 {
1855 if (__n > max_size())
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001856 __libcpp_throw(length_error("allocator<const T>::allocate(size_t n)"
1857 " 'n' exceeds maximum supported size"));
Marshall Clow813ffb32016-03-03 12:04:39 +00001858 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001859 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001860 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith1f1c1472014-06-04 19:54:15 +00001861 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001862 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1863 {return size_type(~0) / sizeof(_Tp);}
1864#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1865 template <class _Up, class... _Args>
1866 _LIBCPP_INLINE_VISIBILITY
1867 void
1868 construct(_Up* __p, _Args&&... __args)
1869 {
1870 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1871 }
1872#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1873 _LIBCPP_INLINE_VISIBILITY
1874 void
1875 construct(pointer __p)
1876 {
1877 ::new((void*)__p) _Tp();
1878 }
1879# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001880
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001881 template <class _A0>
1882 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001883 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001884 construct(pointer __p, _A0& __a0)
1885 {
1886 ::new((void*)__p) _Tp(__a0);
1887 }
1888 template <class _A0>
1889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001890 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001891 construct(pointer __p, const _A0& __a0)
1892 {
1893 ::new((void*)__p) _Tp(__a0);
1894 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001895# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1896 template <class _A0, class _A1>
1897 _LIBCPP_INLINE_VISIBILITY
1898 void
1899 construct(pointer __p, _A0& __a0, _A1& __a1)
1900 {
1901 ::new((void*)__p) _Tp(__a0, __a1);
1902 }
1903 template <class _A0, class _A1>
1904 _LIBCPP_INLINE_VISIBILITY
1905 void
1906 construct(pointer __p, const _A0& __a0, _A1& __a1)
1907 {
1908 ::new((void*)__p) _Tp(__a0, __a1);
1909 }
1910 template <class _A0, class _A1>
1911 _LIBCPP_INLINE_VISIBILITY
1912 void
1913 construct(pointer __p, _A0& __a0, const _A1& __a1)
1914 {
1915 ::new((void*)__p) _Tp(__a0, __a1);
1916 }
1917 template <class _A0, class _A1>
1918 _LIBCPP_INLINE_VISIBILITY
1919 void
1920 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1921 {
1922 ::new((void*)__p) _Tp(__a0, __a1);
1923 }
1924#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1925 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1926};
1927
Howard Hinnantc51e1022010-05-11 19:42:16 +00001928template <class _Tp, class _Up>
1929inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001930bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001931
1932template <class _Tp, class _Up>
1933inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001934bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001935
1936template <class _OutputIterator, class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001937class _LIBCPP_TYPE_VIS_ONLY raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001938 : public iterator<output_iterator_tag,
1939 _Tp, // purposefully not C++03
1940 ptrdiff_t, // purposefully not C++03
1941 _Tp*, // purposefully not C++03
1942 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1943{
1944private:
1945 _OutputIterator __x_;
1946public:
1947 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1948 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1949 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1950 {::new(&*__x_) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001951#if _LIBCPP_STD_VER >= 14
1952 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1953 {::new(&*__x_) _Tp(_VSTD::move(__element)); return *this;}
1954#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1956 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1957 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001958#if _LIBCPP_STD_VER >= 14
1959 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1960#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001961};
1962
1963template <class _Tp>
1964pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001965get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001966{
1967 pair<_Tp*, ptrdiff_t> __r(0, 0);
1968 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1969 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1970 / sizeof(_Tp);
1971 if (__n > __m)
1972 __n = __m;
1973 while (__n > 0)
1974 {
1975 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1976 if (__r.first)
1977 {
1978 __r.second = __n;
1979 break;
1980 }
1981 __n /= 2;
1982 }
1983 return __r;
1984}
1985
1986template <class _Tp>
1987inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001988void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001989
1990template <class _Tp>
1991struct auto_ptr_ref
1992{
1993 _Tp* __ptr_;
1994};
1995
1996template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001997class _LIBCPP_TYPE_VIS_ONLY auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00001998{
1999private:
2000 _Tp* __ptr_;
2001public:
2002 typedef _Tp element_type;
2003
2004 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2005 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2006 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2007 : __ptr_(__p.release()) {}
2008 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2009 {reset(__p.release()); return *this;}
2010 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2011 {reset(__p.release()); return *this;}
2012 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2013 {reset(__p.__ptr_); return *this;}
2014 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2015
2016 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2017 {return *__ptr_;}
2018 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2019 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2020 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2021 {
2022 _Tp* __t = __ptr_;
2023 __ptr_ = 0;
2024 return __t;
2025 }
2026 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2027 {
2028 if (__ptr_ != __p)
2029 delete __ptr_;
2030 __ptr_ = __p;
2031 }
2032
2033 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2034 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2035 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2036 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2037 {return auto_ptr<_Up>(release());}
2038};
2039
2040template <>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002041class _LIBCPP_TYPE_VIS_ONLY auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002042{
2043public:
2044 typedef void element_type;
2045};
2046
Howard Hinnantc51e1022010-05-11 19:42:16 +00002047template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
2048 typename remove_cv<_T2>::type>::value,
Howard Hinnantd0aabf82011-12-11 20:31:33 +00002049 bool = is_empty<_T1>::value
Eric Fiselierf7394302015-06-13 07:08:02 +00002050 && !__libcpp_is_final<_T1>::value,
Howard Hinnantd0aabf82011-12-11 20:31:33 +00002051 bool = is_empty<_T2>::value
Eric Fiselierf7394302015-06-13 07:08:02 +00002052 && !__libcpp_is_final<_T2>::value
Howard Hinnantd0aabf82011-12-11 20:31:33 +00002053 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00002054struct __libcpp_compressed_pair_switch;
2055
2056template <class _T1, class _T2, bool IsSame>
2057struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
2058
2059template <class _T1, class _T2, bool IsSame>
2060struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
2061
2062template <class _T1, class _T2, bool IsSame>
2063struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
2064
2065template <class _T1, class _T2>
2066struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
2067
2068template <class _T1, class _T2>
2069struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
2070
2071template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
2072class __libcpp_compressed_pair_imp;
2073
2074template <class _T1, class _T2>
2075class __libcpp_compressed_pair_imp<_T1, _T2, 0>
2076{
2077private:
2078 _T1 __first_;
2079 _T2 __second_;
2080public:
2081 typedef _T1 _T1_param;
2082 typedef _T2 _T2_param;
2083
2084 typedef typename remove_reference<_T1>::type& _T1_reference;
2085 typedef typename remove_reference<_T2>::type& _T2_reference;
2086
2087 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2088 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2089
Marshall Clowac92bfe2015-07-16 03:05:06 +00002090 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_(), __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002091 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002092 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002093 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002094 : __first_(), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002095 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002096 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002097
Howard Hinnant59f73012013-11-13 00:39:22 +00002098#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002099
2100 _LIBCPP_INLINE_VISIBILITY
2101 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2102 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2103 is_nothrow_copy_constructible<_T2>::value)
2104 : __first_(__p.first()),
2105 __second_(__p.second()) {}
2106
2107 _LIBCPP_INLINE_VISIBILITY
2108 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2109 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2110 is_nothrow_copy_assignable<_T2>::value)
2111 {
2112 __first_ = __p.first();
2113 __second_ = __p.second();
2114 return *this;
2115 }
2116
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002117 _LIBCPP_INLINE_VISIBILITY
2118 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant719bda32011-05-28 14:41:13 +00002119 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2120 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002121 : __first_(_VSTD::forward<_T1>(__p.first())),
2122 __second_(_VSTD::forward<_T2>(__p.second())) {}
2123
2124 _LIBCPP_INLINE_VISIBILITY
2125 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2126 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2127 is_nothrow_move_assignable<_T2>::value)
2128 {
2129 __first_ = _VSTD::forward<_T1>(__p.first());
2130 __second_ = _VSTD::forward<_T2>(__p.second());
2131 return *this;
2132 }
2133
Howard Hinnant59f73012013-11-13 00:39:22 +00002134#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002135
Howard Hinnant83b1c052011-12-19 17:58:44 +00002136#ifndef _LIBCPP_HAS_NO_VARIADICS
2137
2138 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2139 _LIBCPP_INLINE_VISIBILITY
2140 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2141 tuple<_Args1...> __first_args,
2142 tuple<_Args2...> __second_args,
2143 __tuple_indices<_I1...>,
2144 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002145 : __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2146 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002147 {}
2148
2149#endif // _LIBCPP_HAS_NO_VARIADICS
2150
Howard Hinnant719bda32011-05-28 14:41:13 +00002151 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2152 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002153
Howard Hinnant719bda32011-05-28 14:41:13 +00002154 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2155 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002156
2157 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002158 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002159 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002160 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002161 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002162 swap(__first_, __x.__first_);
2163 swap(__second_, __x.__second_);
2164 }
2165};
2166
2167template <class _T1, class _T2>
2168class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2169 : private _T1
2170{
2171private:
2172 _T2 __second_;
2173public:
2174 typedef _T1 _T1_param;
2175 typedef _T2 _T2_param;
2176
2177 typedef _T1& _T1_reference;
2178 typedef typename remove_reference<_T2>::type& _T2_reference;
2179
2180 typedef const _T1& _T1_const_reference;
2181 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2182
Marshall Clowac92bfe2015-07-16 03:05:06 +00002183 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002184 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002185 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002186 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002187 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002188 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002189 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002190
Howard Hinnant59f73012013-11-13 00:39:22 +00002191#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002192
2193 _LIBCPP_INLINE_VISIBILITY
2194 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2195 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2196 is_nothrow_copy_constructible<_T2>::value)
2197 : _T1(__p.first()), __second_(__p.second()) {}
2198
2199 _LIBCPP_INLINE_VISIBILITY
2200 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2201 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2202 is_nothrow_copy_assignable<_T2>::value)
2203 {
2204 _T1::operator=(__p.first());
2205 __second_ = __p.second();
2206 return *this;
2207 }
2208
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002209 _LIBCPP_INLINE_VISIBILITY
2210 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant719bda32011-05-28 14:41:13 +00002211 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2212 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002213 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002214
2215 _LIBCPP_INLINE_VISIBILITY
2216 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2217 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2218 is_nothrow_move_assignable<_T2>::value)
2219 {
2220 _T1::operator=(_VSTD::move(__p.first()));
2221 __second_ = _VSTD::forward<_T2>(__p.second());
2222 return *this;
2223 }
2224
Howard Hinnant59f73012013-11-13 00:39:22 +00002225#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002226
Howard Hinnant83b1c052011-12-19 17:58:44 +00002227#ifndef _LIBCPP_HAS_NO_VARIADICS
2228
2229 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2230 _LIBCPP_INLINE_VISIBILITY
2231 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2232 tuple<_Args1...> __first_args,
2233 tuple<_Args2...> __second_args,
2234 __tuple_indices<_I1...>,
2235 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002236 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2237 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002238 {}
2239
2240#endif // _LIBCPP_HAS_NO_VARIADICS
2241
Howard Hinnant719bda32011-05-28 14:41:13 +00002242 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2243 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244
Howard Hinnant719bda32011-05-28 14:41:13 +00002245 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2246 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002247
2248 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002249 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002250 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002251 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002252 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002253 swap(__second_, __x.__second_);
2254 }
2255};
2256
2257template <class _T1, class _T2>
2258class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2259 : private _T2
2260{
2261private:
2262 _T1 __first_;
2263public:
2264 typedef _T1 _T1_param;
2265 typedef _T2 _T2_param;
2266
2267 typedef typename remove_reference<_T1>::type& _T1_reference;
2268 typedef _T2& _T2_reference;
2269
2270 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2271 typedef const _T2& _T2_const_reference;
2272
Marshall Clowac92bfe2015-07-16 03:05:06 +00002273 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002274 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002275 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002276 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002277 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002278 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant719bda32011-05-28 14:41:13 +00002279 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2280 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002281 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002282
Howard Hinnant59f73012013-11-13 00:39:22 +00002283#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002284
2285 _LIBCPP_INLINE_VISIBILITY
2286 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2287 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2288 is_nothrow_copy_constructible<_T2>::value)
2289 : _T2(__p.second()), __first_(__p.first()) {}
2290
2291 _LIBCPP_INLINE_VISIBILITY
2292 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2293 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2294 is_nothrow_copy_assignable<_T2>::value)
2295 {
2296 _T2::operator=(__p.second());
2297 __first_ = __p.first();
2298 return *this;
2299 }
2300
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002302 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002303 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2304 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002305 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002306
2307 _LIBCPP_INLINE_VISIBILITY
2308 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2309 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2310 is_nothrow_move_assignable<_T2>::value)
2311 {
2312 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2313 __first_ = _VSTD::move(__p.first());
2314 return *this;
2315 }
2316
Howard Hinnant59f73012013-11-13 00:39:22 +00002317#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002318
Howard Hinnant83b1c052011-12-19 17:58:44 +00002319#ifndef _LIBCPP_HAS_NO_VARIADICS
2320
2321 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2322 _LIBCPP_INLINE_VISIBILITY
2323 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2324 tuple<_Args1...> __first_args,
2325 tuple<_Args2...> __second_args,
2326 __tuple_indices<_I1...>,
2327 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002328 : _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...),
2329 __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002330
2331 {}
2332
2333#endif // _LIBCPP_HAS_NO_VARIADICS
2334
Howard Hinnant719bda32011-05-28 14:41:13 +00002335 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2336 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002337
Howard Hinnant719bda32011-05-28 14:41:13 +00002338 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2339 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002340
2341 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002342 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002343 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002344 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002345 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002346 swap(__first_, __x.__first_);
2347 }
2348};
2349
2350template <class _T1, class _T2>
2351class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2352 : private _T1,
2353 private _T2
2354{
2355public:
2356 typedef _T1 _T1_param;
2357 typedef _T2 _T2_param;
2358
2359 typedef _T1& _T1_reference;
2360 typedef _T2& _T2_reference;
2361
2362 typedef const _T1& _T1_const_reference;
2363 typedef const _T2& _T2_const_reference;
2364
2365 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2366 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002367 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002368 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002369 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002370 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002371 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002372
Howard Hinnant59f73012013-11-13 00:39:22 +00002373#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002374
2375 _LIBCPP_INLINE_VISIBILITY
2376 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2377 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2378 is_nothrow_copy_constructible<_T2>::value)
2379 : _T1(__p.first()), _T2(__p.second()) {}
2380
2381 _LIBCPP_INLINE_VISIBILITY
2382 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2383 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2384 is_nothrow_copy_assignable<_T2>::value)
2385 {
2386 _T1::operator=(__p.first());
2387 _T2::operator=(__p.second());
2388 return *this;
2389 }
2390
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002391 _LIBCPP_INLINE_VISIBILITY
2392 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant719bda32011-05-28 14:41:13 +00002393 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2394 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002395 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002396
2397 _LIBCPP_INLINE_VISIBILITY
2398 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2399 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2400 is_nothrow_move_assignable<_T2>::value)
2401 {
2402 _T1::operator=(_VSTD::move(__p.first()));
2403 _T2::operator=(_VSTD::move(__p.second()));
2404 return *this;
2405 }
2406
Howard Hinnant59f73012013-11-13 00:39:22 +00002407#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002408
Howard Hinnant83b1c052011-12-19 17:58:44 +00002409#ifndef _LIBCPP_HAS_NO_VARIADICS
2410
2411 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2412 _LIBCPP_INLINE_VISIBILITY
2413 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2414 tuple<_Args1...> __first_args,
2415 tuple<_Args2...> __second_args,
2416 __tuple_indices<_I1...>,
2417 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002418 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2419 _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002420 {}
2421
2422#endif // _LIBCPP_HAS_NO_VARIADICS
2423
Howard Hinnant719bda32011-05-28 14:41:13 +00002424 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2425 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002426
Howard Hinnant719bda32011-05-28 14:41:13 +00002427 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2428 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002429
Howard Hinnant28b24882011-12-01 20:21:04 +00002430 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant719bda32011-05-28 14:41:13 +00002431 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002432 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002433 {
2434 }
2435};
2436
2437template <class _T1, class _T2>
2438class __compressed_pair
2439 : private __libcpp_compressed_pair_imp<_T1, _T2>
2440{
2441 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2442public:
2443 typedef typename base::_T1_param _T1_param;
2444 typedef typename base::_T2_param _T2_param;
2445
2446 typedef typename base::_T1_reference _T1_reference;
2447 typedef typename base::_T2_reference _T2_reference;
2448
2449 typedef typename base::_T1_const_reference _T1_const_reference;
2450 typedef typename base::_T2_const_reference _T2_const_reference;
2451
2452 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002453 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002454 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002455 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002456 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002457 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002458 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002459
Howard Hinnant59f73012013-11-13 00:39:22 +00002460#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002461
2462 _LIBCPP_INLINE_VISIBILITY
2463 __compressed_pair(const __compressed_pair& __p)
2464 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2465 is_nothrow_copy_constructible<_T2>::value)
2466 : base(__p) {}
2467
2468 _LIBCPP_INLINE_VISIBILITY
2469 __compressed_pair& operator=(const __compressed_pair& __p)
2470 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2471 is_nothrow_copy_assignable<_T2>::value)
2472 {
2473 base::operator=(__p);
2474 return *this;
2475 }
2476
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002478 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant719bda32011-05-28 14:41:13 +00002479 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2480 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002481 : base(_VSTD::move(__p)) {}
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002482
2483 _LIBCPP_INLINE_VISIBILITY
2484 __compressed_pair& operator=(__compressed_pair&& __p)
2485 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2486 is_nothrow_move_assignable<_T2>::value)
2487 {
2488 base::operator=(_VSTD::move(__p));
2489 return *this;
2490 }
Howard Hinnant83b1c052011-12-19 17:58:44 +00002491
Howard Hinnant59f73012013-11-13 00:39:22 +00002492#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002493
Howard Hinnant83b1c052011-12-19 17:58:44 +00002494#ifndef _LIBCPP_HAS_NO_VARIADICS
2495
2496 template <class... _Args1, class... _Args2>
2497 _LIBCPP_INLINE_VISIBILITY
2498 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2499 tuple<_Args2...> __second_args)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002500 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant83b1c052011-12-19 17:58:44 +00002501 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2502 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2503 {}
2504
2505#endif // _LIBCPP_HAS_NO_VARIADICS
2506
Howard Hinnant719bda32011-05-28 14:41:13 +00002507 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2508 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002509
Howard Hinnant719bda32011-05-28 14:41:13 +00002510 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2511 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002512
Howard Hinnant719bda32011-05-28 14:41:13 +00002513 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2514 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002515 __is_nothrow_swappable<_T2>::value)
Howard Hinnant719bda32011-05-28 14:41:13 +00002516 {base::swap(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002517};
2518
2519template <class _T1, class _T2>
2520inline _LIBCPP_INLINE_VISIBILITY
2521void
2522swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant719bda32011-05-28 14:41:13 +00002523 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002524 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002525 {__x.swap(__y);}
2526
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002527// __same_or_less_cv_qualified
2528
2529template <class _Ptr1, class _Ptr2,
2530 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2531 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2532 >::value
2533 >
2534struct __same_or_less_cv_qualified_imp
2535 : is_convertible<_Ptr1, _Ptr2> {};
2536
2537template <class _Ptr1, class _Ptr2>
2538struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2539 : false_type {};
2540
Marshall Clowdf296162014-04-26 05:19:48 +00002541template <class _Ptr1, class _Ptr2, bool = is_pointer<_Ptr1>::value ||
2542 is_same<_Ptr1, _Ptr2>::value ||
2543 __has_element_type<_Ptr1>::value>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002544struct __same_or_less_cv_qualified
2545 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2546
2547template <class _Ptr1, class _Ptr2>
Marshall Clowdf296162014-04-26 05:19:48 +00002548struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, false>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002549 : false_type {};
2550
2551// default_delete
2552
Howard Hinnantc51e1022010-05-11 19:42:16 +00002553template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002554struct _LIBCPP_TYPE_VIS_ONLY default_delete
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555{
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002556#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2557 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2558#else
2559 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2560#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002561 template <class _Up>
2562 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant719bda32011-05-28 14:41:13 +00002563 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2564 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002565 {
2566 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant14015fd2013-04-24 19:44:26 +00002567 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002568 delete __ptr;
2569 }
2570};
2571
2572template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002573struct _LIBCPP_TYPE_VIS_ONLY default_delete<_Tp[]>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002574{
Howard Hinnant4500ca52011-12-18 21:19:44 +00002575public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002576#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2577 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2578#else
2579 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2580#endif
Howard Hinnant4500ca52011-12-18 21:19:44 +00002581 template <class _Up>
2582 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002583 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant4500ca52011-12-18 21:19:44 +00002584 template <class _Up>
2585 _LIBCPP_INLINE_VISIBILITY
2586 void operator() (_Up* __ptr,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002587 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002588 {
2589 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Marshall Clowff0e4132016-02-25 16:50:51 +00002590 static_assert(!is_void<_Tp>::value, "default_delete can not delete void type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002591 delete [] __ptr;
2592 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002593};
2594
2595template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002596class _LIBCPP_TYPE_VIS_ONLY unique_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597{
2598public:
2599 typedef _Tp element_type;
2600 typedef _Dp deleter_type;
2601 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2602private:
2603 __compressed_pair<pointer, deleter_type> __ptr_;
2604
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002605#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002606 unique_ptr(unique_ptr&);
2607 template <class _Up, class _Ep>
2608 unique_ptr(unique_ptr<_Up, _Ep>&);
2609 unique_ptr& operator=(unique_ptr&);
2610 template <class _Up, class _Ep>
2611 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant74279a52010-09-04 23:28:19 +00002612#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002613
2614 struct __nat {int __for_bool_;};
2615
2616 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2617 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2618public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002619 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002620 : __ptr_(pointer())
2621 {
2622 static_assert(!is_pointer<deleter_type>::value,
2623 "unique_ptr constructed with null function pointer deleter");
2624 }
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002625 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002626 : __ptr_(pointer())
2627 {
2628 static_assert(!is_pointer<deleter_type>::value,
2629 "unique_ptr constructed with null function pointer deleter");
2630 }
Howard Hinnant719bda32011-05-28 14:41:13 +00002631 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002632 : __ptr_(_VSTD::move(__p))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002633 {
2634 static_assert(!is_pointer<deleter_type>::value,
2635 "unique_ptr constructed with null function pointer deleter");
2636 }
2637
Howard Hinnant74279a52010-09-04 23:28:19 +00002638#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002639 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2640 is_reference<deleter_type>::value,
2641 deleter_type,
2642 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002643 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002644 : __ptr_(__p, __d) {}
2645
2646 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002647 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002648 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002649 {
2650 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2651 }
Howard Hinnant719bda32011-05-28 14:41:13 +00002652 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002653 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002654 template <class _Up, class _Ep>
2655 _LIBCPP_INLINE_VISIBILITY
2656 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2657 typename enable_if
2658 <
2659 !is_array<_Up>::value &&
2660 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2661 is_convertible<_Ep, deleter_type>::value &&
2662 (
2663 !is_reference<deleter_type>::value ||
2664 is_same<deleter_type, _Ep>::value
2665 ),
2666 __nat
Howard Hinnant719bda32011-05-28 14:41:13 +00002667 >::type = __nat()) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002668 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002669
2670 template <class _Up>
2671 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2672 typename enable_if<
2673 is_convertible<_Up*, _Tp*>::value &&
2674 is_same<_Dp, default_delete<_Tp> >::value,
2675 __nat
Howard Hinnant719bda32011-05-28 14:41:13 +00002676 >::type = __nat()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002677 : __ptr_(__p.release())
2678 {
2679 }
2680
Howard Hinnant719bda32011-05-28 14:41:13 +00002681 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682 {
2683 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002684 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002685 return *this;
2686 }
2687
2688 template <class _Up, class _Ep>
2689 _LIBCPP_INLINE_VISIBILITY
2690 typename enable_if
2691 <
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002692 !is_array<_Up>::value &&
2693 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2694 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695 unique_ptr&
2696 >::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002697 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698 {
2699 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002700 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701 return *this;
2702 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002703#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704
2705 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2706 {
2707 return __rv<unique_ptr>(*this);
2708 }
2709
2710 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002711 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002712
2713 template <class _Up, class _Ep>
Eric Fiselier6d8c8ed2015-08-28 05:07:06 +00002714 _LIBCPP_INLINE_VISIBILITY
2715 typename enable_if<
2716 !is_array<_Up>::value &&
2717 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2718 is_assignable<deleter_type&, _Ep&>::value,
2719 unique_ptr&
2720 >::type
2721 operator=(unique_ptr<_Up, _Ep> __u)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002722 {
2723 reset(__u.release());
Eric Fiselier6d8c8ed2015-08-28 05:07:06 +00002724 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002725 return *this;
2726 }
2727
2728 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002729 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002730
2731 template <class _Up>
2732 _LIBCPP_INLINE_VISIBILITY
2733 typename enable_if<
2734 is_convertible<_Up*, _Tp*>::value &&
2735 is_same<_Dp, default_delete<_Tp> >::value,
2736 unique_ptr&
2737 >::type
2738 operator=(auto_ptr<_Up> __p)
2739 {reset(__p.release()); return *this;}
2740
Howard Hinnant74279a52010-09-04 23:28:19 +00002741#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002742 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2743
Howard Hinnant719bda32011-05-28 14:41:13 +00002744 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002745 {
2746 reset();
2747 return *this;
2748 }
2749
2750 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2751 {return *__ptr_.first();}
Howard Hinnant719bda32011-05-28 14:41:13 +00002752 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2753 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2754 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2755 {return __ptr_.second();}
2756 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2757 {return __ptr_.second();}
Howard Hinnant86a291f2012-02-21 21:46:43 +00002758 _LIBCPP_INLINE_VISIBILITY
2759 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2760 {return __ptr_.first() != nullptr;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002761
Howard Hinnant719bda32011-05-28 14:41:13 +00002762 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002763 {
2764 pointer __t = __ptr_.first();
2765 __ptr_.first() = pointer();
2766 return __t;
2767 }
2768
Howard Hinnant719bda32011-05-28 14:41:13 +00002769 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002770 {
2771 pointer __tmp = __ptr_.first();
2772 __ptr_.first() = __p;
2773 if (__tmp)
2774 __ptr_.second()(__tmp);
2775 }
2776
Howard Hinnant719bda32011-05-28 14:41:13 +00002777 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2778 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002779};
2780
2781template <class _Tp, class _Dp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002782class _LIBCPP_TYPE_VIS_ONLY unique_ptr<_Tp[], _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002783{
2784public:
2785 typedef _Tp element_type;
2786 typedef _Dp deleter_type;
2787 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2788private:
2789 __compressed_pair<pointer, deleter_type> __ptr_;
2790
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002791#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002792 unique_ptr(unique_ptr&);
2793 template <class _Up>
2794 unique_ptr(unique_ptr<_Up>&);
2795 unique_ptr& operator=(unique_ptr&);
2796 template <class _Up>
2797 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant74279a52010-09-04 23:28:19 +00002798#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002799
2800 struct __nat {int __for_bool_;};
2801
2802 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2803 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2804public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002805 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002806 : __ptr_(pointer())
2807 {
2808 static_assert(!is_pointer<deleter_type>::value,
2809 "unique_ptr constructed with null function pointer deleter");
2810 }
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002811 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002812 : __ptr_(pointer())
2813 {
2814 static_assert(!is_pointer<deleter_type>::value,
2815 "unique_ptr constructed with null function pointer deleter");
2816 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002817#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00002818 template <class _Pp>
2819 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p,
2820 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002821 : __ptr_(__p)
2822 {
2823 static_assert(!is_pointer<deleter_type>::value,
2824 "unique_ptr constructed with null function pointer deleter");
2825 }
2826
Logan Chiend435f8b2014-01-31 09:30:46 +00002827 template <class _Pp>
Howard Hinnantc834c512011-11-29 18:15:50 +00002828 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantc51e1022010-05-11 19:42:16 +00002829 is_reference<deleter_type>::value,
2830 deleter_type,
Logan Chiend435f8b2014-01-31 09:30:46 +00002831 typename add_lvalue_reference<const deleter_type>::type>::type __d,
2832 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002833 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002834 : __ptr_(__p, __d) {}
2835
2836 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2837 is_reference<deleter_type>::value,
2838 deleter_type,
2839 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002840 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002841 : __ptr_(pointer(), __d) {}
2842
Logan Chiend435f8b2014-01-31 09:30:46 +00002843 template <class _Pp>
2844 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p,
2845 typename remove_reference<deleter_type>::type&& __d,
2846 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002847 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002848 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002849 {
2850 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2851 }
2852
2853 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002854 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002855 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002856 {
2857 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2858 }
2859
Howard Hinnant719bda32011-05-28 14:41:13 +00002860 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002861 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002862
Howard Hinnant719bda32011-05-28 14:41:13 +00002863 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002864 {
2865 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002866 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002867 return *this;
2868 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002869
2870 template <class _Up, class _Ep>
2871 _LIBCPP_INLINE_VISIBILITY
2872 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2873 typename enable_if
2874 <
2875 is_array<_Up>::value &&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002876 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant4500ca52011-12-18 21:19:44 +00002877 && is_convertible<_Ep, deleter_type>::value &&
2878 (
2879 !is_reference<deleter_type>::value ||
2880 is_same<deleter_type, _Ep>::value
2881 ),
2882 __nat
2883 >::type = __nat()
2884 ) _NOEXCEPT
2885 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2886
2887
2888 template <class _Up, class _Ep>
2889 _LIBCPP_INLINE_VISIBILITY
2890 typename enable_if
2891 <
2892 is_array<_Up>::value &&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002893 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2894 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant4500ca52011-12-18 21:19:44 +00002895 unique_ptr&
2896 >::type
2897 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2898 {
2899 reset(__u.release());
2900 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2901 return *this;
2902 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002903#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002904
2905 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2906 : __ptr_(__p)
2907 {
2908 static_assert(!is_pointer<deleter_type>::value,
2909 "unique_ptr constructed with null function pointer deleter");
2910 }
2911
2912 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002913 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002914
2915 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002916 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002917
2918 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2919 {
2920 return __rv<unique_ptr>(*this);
2921 }
2922
2923 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002924 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002925
2926 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2927 {
2928 reset(__u->release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002929 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002930 return *this;
2931 }
2932
Howard Hinnant74279a52010-09-04 23:28:19 +00002933#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002934 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2935
Howard Hinnant719bda32011-05-28 14:41:13 +00002936 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002937 {
2938 reset();
2939 return *this;
2940 }
2941
2942 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2943 {return __ptr_.first()[__i];}
Howard Hinnant719bda32011-05-28 14:41:13 +00002944 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2945 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2946 {return __ptr_.second();}
2947 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2948 {return __ptr_.second();}
Howard Hinnant86a291f2012-02-21 21:46:43 +00002949 _LIBCPP_INLINE_VISIBILITY
2950 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2951 {return __ptr_.first() != nullptr;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002952
Howard Hinnant719bda32011-05-28 14:41:13 +00002953 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002954 {
2955 pointer __t = __ptr_.first();
2956 __ptr_.first() = pointer();
2957 return __t;
2958 }
2959
Logan Chiend435f8b2014-01-31 09:30:46 +00002960 template <class _Pp>
2961 _LIBCPP_INLINE_VISIBILITY
2962 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, void>::type
2963 reset(_Pp __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002964 {
2965 pointer __tmp = __ptr_.first();
2966 __ptr_.first() = __p;
2967 if (__tmp)
2968 __ptr_.second()(__tmp);
2969 }
Marshall Clowff0e4132016-02-25 16:50:51 +00002970 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t = nullptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002971 {
2972 pointer __tmp = __ptr_.first();
2973 __ptr_.first() = nullptr;
2974 if (__tmp)
2975 __ptr_.second()(__tmp);
2976 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002977
2978 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2979private:
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002980
Howard Hinnant74279a52010-09-04 23:28:19 +00002981#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002982 template <class _Up>
2983 explicit unique_ptr(_Up);
2984 template <class _Up>
2985 unique_ptr(_Up __u,
2986 typename conditional<
2987 is_reference<deleter_type>::value,
2988 deleter_type,
2989 typename add_lvalue_reference<const deleter_type>::type>::type,
2990 typename enable_if
2991 <
2992 is_convertible<_Up, pointer>::value,
2993 __nat
2994 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00002995#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002996};
2997
2998template <class _Tp, class _Dp>
2999inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00003000typename enable_if<
3001 __is_swappable<_Dp>::value,
3002 void
3003>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003004swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003005
3006template <class _T1, class _D1, class _T2, class _D2>
3007inline _LIBCPP_INLINE_VISIBILITY
3008bool
3009operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
3010
3011template <class _T1, class _D1, class _T2, class _D2>
3012inline _LIBCPP_INLINE_VISIBILITY
3013bool
3014operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
3015
3016template <class _T1, class _D1, class _T2, class _D2>
3017inline _LIBCPP_INLINE_VISIBILITY
3018bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00003019operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
3020{
3021 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3022 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003023 typedef typename common_type<_P1, _P2>::type _Vp;
3024 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00003025}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003026
3027template <class _T1, class _D1, class _T2, class _D2>
3028inline _LIBCPP_INLINE_VISIBILITY
3029bool
3030operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
3031
3032template <class _T1, class _D1, class _T2, class _D2>
3033inline _LIBCPP_INLINE_VISIBILITY
3034bool
3035operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
3036
3037template <class _T1, class _D1, class _T2, class _D2>
3038inline _LIBCPP_INLINE_VISIBILITY
3039bool
3040operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
3041
Howard Hinnantb17caf92012-02-21 21:02:58 +00003042template <class _T1, class _D1>
3043inline _LIBCPP_INLINE_VISIBILITY
3044bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003045operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003046{
3047 return !__x;
3048}
3049
3050template <class _T1, class _D1>
3051inline _LIBCPP_INLINE_VISIBILITY
3052bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003053operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003054{
3055 return !__x;
3056}
3057
3058template <class _T1, class _D1>
3059inline _LIBCPP_INLINE_VISIBILITY
3060bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003061operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003062{
3063 return static_cast<bool>(__x);
3064}
3065
3066template <class _T1, class _D1>
3067inline _LIBCPP_INLINE_VISIBILITY
3068bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003069operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003070{
3071 return static_cast<bool>(__x);
3072}
3073
3074template <class _T1, class _D1>
3075inline _LIBCPP_INLINE_VISIBILITY
3076bool
3077operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3078{
3079 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3080 return less<_P1>()(__x.get(), nullptr);
3081}
3082
3083template <class _T1, class _D1>
3084inline _LIBCPP_INLINE_VISIBILITY
3085bool
3086operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3087{
3088 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3089 return less<_P1>()(nullptr, __x.get());
3090}
3091
3092template <class _T1, class _D1>
3093inline _LIBCPP_INLINE_VISIBILITY
3094bool
3095operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3096{
3097 return nullptr < __x;
3098}
3099
3100template <class _T1, class _D1>
3101inline _LIBCPP_INLINE_VISIBILITY
3102bool
3103operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3104{
3105 return __x < nullptr;
3106}
3107
3108template <class _T1, class _D1>
3109inline _LIBCPP_INLINE_VISIBILITY
3110bool
3111operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3112{
3113 return !(nullptr < __x);
3114}
3115
3116template <class _T1, class _D1>
3117inline _LIBCPP_INLINE_VISIBILITY
3118bool
3119operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3120{
3121 return !(__x < nullptr);
3122}
3123
3124template <class _T1, class _D1>
3125inline _LIBCPP_INLINE_VISIBILITY
3126bool
3127operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3128{
3129 return !(__x < nullptr);
3130}
3131
3132template <class _T1, class _D1>
3133inline _LIBCPP_INLINE_VISIBILITY
3134bool
3135operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3136{
3137 return !(nullptr < __x);
3138}
3139
Howard Hinnant9e028f12012-05-01 15:37:54 +00003140#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3141
3142template <class _Tp, class _Dp>
3143inline _LIBCPP_INLINE_VISIBILITY
3144unique_ptr<_Tp, _Dp>
3145move(unique_ptr<_Tp, _Dp>& __t)
3146{
3147 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3148}
3149
3150#endif
3151
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003152#if _LIBCPP_STD_VER > 11
3153
3154template<class _Tp>
3155struct __unique_if
3156{
3157 typedef unique_ptr<_Tp> __unique_single;
3158};
3159
3160template<class _Tp>
3161struct __unique_if<_Tp[]>
3162{
3163 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3164};
3165
3166template<class _Tp, size_t _Np>
3167struct __unique_if<_Tp[_Np]>
3168{
3169 typedef void __unique_array_known_bound;
3170};
3171
3172template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003173inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003174typename __unique_if<_Tp>::__unique_single
3175make_unique(_Args&&... __args)
3176{
3177 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3178}
3179
3180template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003181inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003182typename __unique_if<_Tp>::__unique_array_unknown_bound
3183make_unique(size_t __n)
3184{
3185 typedef typename remove_extent<_Tp>::type _Up;
3186 return unique_ptr<_Tp>(new _Up[__n]());
3187}
3188
3189template<class _Tp, class... _Args>
3190 typename __unique_if<_Tp>::__unique_array_known_bound
3191 make_unique(_Args&&...) = delete;
3192
3193#endif // _LIBCPP_STD_VER > 11
3194
Howard Hinnant6a855442013-07-03 17:39:28 +00003195template <class _Size>
3196inline _LIBCPP_INLINE_VISIBILITY
3197_Size
3198__loadword(const void* __p)
3199{
3200 _Size __r;
3201 std::memcpy(&__r, __p, sizeof(__r));
3202 return __r;
3203}
3204
Howard Hinnantf1973402011-12-10 20:28:56 +00003205// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
3206// is 64 bits. This is because cityhash64 uses 64bit x 64bit
3207// multiplication, which can be very slow on 32-bit systems.
Howard Hinnantad71c232011-12-05 00:08:45 +00003208template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantf1973402011-12-10 20:28:56 +00003209struct __murmur2_or_cityhash;
Howard Hinnantad71c232011-12-05 00:08:45 +00003210
3211template <class _Size>
Howard Hinnantf1973402011-12-10 20:28:56 +00003212struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnantad71c232011-12-05 00:08:45 +00003213{
3214 _Size operator()(const void* __key, _Size __len);
3215};
3216
Howard Hinnantf1973402011-12-10 20:28:56 +00003217// murmur2
Howard Hinnantad71c232011-12-05 00:08:45 +00003218template <class _Size>
3219_Size
Marshall Clow91c7f642016-01-11 19:27:10 +00003220__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantad71c232011-12-05 00:08:45 +00003221{
3222 const _Size __m = 0x5bd1e995;
3223 const _Size __r = 24;
3224 _Size __h = __len;
3225 const unsigned char* __data = static_cast<const unsigned char*>(__key);
3226 for (; __len >= 4; __data += 4, __len -= 4)
3227 {
Howard Hinnant6a855442013-07-03 17:39:28 +00003228 _Size __k = __loadword<_Size>(__data);
Howard Hinnantad71c232011-12-05 00:08:45 +00003229 __k *= __m;
3230 __k ^= __k >> __r;
3231 __k *= __m;
3232 __h *= __m;
3233 __h ^= __k;
3234 }
3235 switch (__len)
3236 {
3237 case 3:
3238 __h ^= __data[2] << 16;
3239 case 2:
3240 __h ^= __data[1] << 8;
3241 case 1:
3242 __h ^= __data[0];
3243 __h *= __m;
3244 }
3245 __h ^= __h >> 13;
3246 __h *= __m;
3247 __h ^= __h >> 15;
3248 return __h;
3249}
3250
3251template <class _Size>
Howard Hinnantf1973402011-12-10 20:28:56 +00003252struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnantad71c232011-12-05 00:08:45 +00003253{
3254 _Size operator()(const void* __key, _Size __len);
Howard Hinnantf1973402011-12-10 20:28:56 +00003255
3256 private:
3257 // Some primes between 2^63 and 2^64.
3258 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
3259 static const _Size __k1 = 0xb492b66fbe98f273ULL;
3260 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
3261 static const _Size __k3 = 0xc949d7c7509e6557ULL;
3262
3263 static _Size __rotate(_Size __val, int __shift) {
3264 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
3265 }
3266
3267 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
3268 return (__val >> __shift) | (__val << (64 - __shift));
3269 }
3270
3271 static _Size __shift_mix(_Size __val) {
3272 return __val ^ (__val >> 47);
3273 }
3274
3275 static _Size __hash_len_16(_Size __u, _Size __v) {
3276 const _Size __mul = 0x9ddfea08eb382d69ULL;
3277 _Size __a = (__u ^ __v) * __mul;
3278 __a ^= (__a >> 47);
3279 _Size __b = (__v ^ __a) * __mul;
3280 __b ^= (__b >> 47);
3281 __b *= __mul;
3282 return __b;
3283 }
3284
3285 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
3286 if (__len > 8) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003287 const _Size __a = __loadword<_Size>(__s);
3288 const _Size __b = __loadword<_Size>(__s + __len - 8);
Howard Hinnantf1973402011-12-10 20:28:56 +00003289 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
3290 }
3291 if (__len >= 4) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003292 const uint32_t __a = __loadword<uint32_t>(__s);
3293 const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
Howard Hinnantf1973402011-12-10 20:28:56 +00003294 return __hash_len_16(__len + (__a << 3), __b);
3295 }
3296 if (__len > 0) {
3297 const unsigned char __a = __s[0];
3298 const unsigned char __b = __s[__len >> 1];
3299 const unsigned char __c = __s[__len - 1];
3300 const uint32_t __y = static_cast<uint32_t>(__a) +
3301 (static_cast<uint32_t>(__b) << 8);
3302 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3303 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3304 }
3305 return __k2;
3306 }
3307
3308 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003309 const _Size __a = __loadword<_Size>(__s) * __k1;
3310 const _Size __b = __loadword<_Size>(__s + 8);
3311 const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;
3312 const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;
Howard Hinnantf1973402011-12-10 20:28:56 +00003313 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3314 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3315 }
3316
3317 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3318 // Callers do best to use "random-looking" values for a and b.
3319 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3320 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3321 __a += __w;
3322 __b = __rotate(__b + __a + __z, 21);
3323 const _Size __c = __a;
3324 __a += __x;
3325 __a += __y;
3326 __b += __rotate(__a, 44);
3327 return pair<_Size, _Size>(__a + __z, __b + __c);
3328 }
3329
3330 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3331 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3332 const char* __s, _Size __a, _Size __b) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003333 return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),
3334 __loadword<_Size>(__s + 8),
3335 __loadword<_Size>(__s + 16),
3336 __loadword<_Size>(__s + 24),
Howard Hinnantf1973402011-12-10 20:28:56 +00003337 __a,
3338 __b);
3339 }
3340
3341 // Return an 8-byte hash for 33 to 64 bytes.
3342 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003343 _Size __z = __loadword<_Size>(__s + 24);
3344 _Size __a = __loadword<_Size>(__s) +
3345 (__len + __loadword<_Size>(__s + __len - 16)) * __k0;
Howard Hinnantf1973402011-12-10 20:28:56 +00003346 _Size __b = __rotate(__a + __z, 52);
3347 _Size __c = __rotate(__a, 37);
Howard Hinnant6a855442013-07-03 17:39:28 +00003348 __a += __loadword<_Size>(__s + 8);
Howard Hinnantf1973402011-12-10 20:28:56 +00003349 __c += __rotate(__a, 7);
Howard Hinnant6a855442013-07-03 17:39:28 +00003350 __a += __loadword<_Size>(__s + 16);
Howard Hinnantf1973402011-12-10 20:28:56 +00003351 _Size __vf = __a + __z;
3352 _Size __vs = __b + __rotate(__a, 31) + __c;
Howard Hinnant6a855442013-07-03 17:39:28 +00003353 __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);
3354 __z += __loadword<_Size>(__s + __len - 8);
Howard Hinnantf1973402011-12-10 20:28:56 +00003355 __b = __rotate(__a + __z, 52);
3356 __c = __rotate(__a, 37);
Howard Hinnant6a855442013-07-03 17:39:28 +00003357 __a += __loadword<_Size>(__s + __len - 24);
Howard Hinnantf1973402011-12-10 20:28:56 +00003358 __c += __rotate(__a, 7);
Howard Hinnant6a855442013-07-03 17:39:28 +00003359 __a += __loadword<_Size>(__s + __len - 16);
Howard Hinnantf1973402011-12-10 20:28:56 +00003360 _Size __wf = __a + __z;
3361 _Size __ws = __b + __rotate(__a, 31) + __c;
3362 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3363 return __shift_mix(__r * __k0 + __vs) * __k2;
3364 }
Howard Hinnantad71c232011-12-05 00:08:45 +00003365};
3366
Howard Hinnantf1973402011-12-10 20:28:56 +00003367// cityhash64
Howard Hinnantad71c232011-12-05 00:08:45 +00003368template <class _Size>
3369_Size
Marshall Clow91c7f642016-01-11 19:27:10 +00003370__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantad71c232011-12-05 00:08:45 +00003371{
Howard Hinnantf1973402011-12-10 20:28:56 +00003372 const char* __s = static_cast<const char*>(__key);
3373 if (__len <= 32) {
3374 if (__len <= 16) {
3375 return __hash_len_0_to_16(__s, __len);
3376 } else {
3377 return __hash_len_17_to_32(__s, __len);
Howard Hinnantad71c232011-12-05 00:08:45 +00003378 }
Howard Hinnantf1973402011-12-10 20:28:56 +00003379 } else if (__len <= 64) {
3380 return __hash_len_33_to_64(__s, __len);
3381 }
3382
3383 // For strings over 64 bytes we hash the end first, and then as we
3384 // loop we keep 56 bytes of state: v, w, x, y, and z.
Howard Hinnant6a855442013-07-03 17:39:28 +00003385 _Size __x = __loadword<_Size>(__s + __len - 40);
3386 _Size __y = __loadword<_Size>(__s + __len - 16) +
3387 __loadword<_Size>(__s + __len - 56);
3388 _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,
3389 __loadword<_Size>(__s + __len - 24));
Howard Hinnantf1973402011-12-10 20:28:56 +00003390 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3391 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
Howard Hinnant6a855442013-07-03 17:39:28 +00003392 __x = __x * __k1 + __loadword<_Size>(__s);
Howard Hinnantf1973402011-12-10 20:28:56 +00003393
3394 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3395 __len = (__len - 1) & ~static_cast<_Size>(63);
3396 do {
Howard Hinnant6a855442013-07-03 17:39:28 +00003397 __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;
3398 __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;
Howard Hinnantf1973402011-12-10 20:28:56 +00003399 __x ^= __w.second;
Howard Hinnant6a855442013-07-03 17:39:28 +00003400 __y += __v.first + __loadword<_Size>(__s + 40);
Howard Hinnantf1973402011-12-10 20:28:56 +00003401 __z = __rotate(__z + __w.first, 33) * __k1;
3402 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3403 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
Howard Hinnant6a855442013-07-03 17:39:28 +00003404 __y + __loadword<_Size>(__s + 16));
Howard Hinnantf1973402011-12-10 20:28:56 +00003405 std::swap(__z, __x);
3406 __s += 64;
3407 __len -= 64;
3408 } while (__len != 0);
3409 return __hash_len_16(
3410 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3411 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnantad71c232011-12-05 00:08:45 +00003412}
3413
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003414template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3415struct __scalar_hash;
3416
3417template <class _Tp>
3418struct __scalar_hash<_Tp, 0>
3419 : public unary_function<_Tp, size_t>
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003420{
Howard Hinnant756c69b2010-09-22 16:48:34 +00003421 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003422 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003423 {
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003424 union
3425 {
3426 _Tp __t;
3427 size_t __a;
3428 } __u;
3429 __u.__a = 0;
3430 __u.__t = __v;
3431 return __u.__a;
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003432 }
3433};
3434
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003435template <class _Tp>
3436struct __scalar_hash<_Tp, 1>
3437 : public unary_function<_Tp, size_t>
3438{
3439 _LIBCPP_INLINE_VISIBILITY
3440 size_t operator()(_Tp __v) const _NOEXCEPT
3441 {
3442 union
3443 {
3444 _Tp __t;
3445 size_t __a;
3446 } __u;
3447 __u.__t = __v;
3448 return __u.__a;
3449 }
3450};
3451
3452template <class _Tp>
3453struct __scalar_hash<_Tp, 2>
3454 : public unary_function<_Tp, size_t>
3455{
3456 _LIBCPP_INLINE_VISIBILITY
3457 size_t operator()(_Tp __v) const _NOEXCEPT
3458 {
3459 union
3460 {
3461 _Tp __t;
3462 struct
3463 {
3464 size_t __a;
3465 size_t __b;
Eric Fiseliere012bf22015-07-18 20:40:46 +00003466 } __s;
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003467 } __u;
3468 __u.__t = __v;
Howard Hinnantf1973402011-12-10 20:28:56 +00003469 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003470 }
3471};
3472
3473template <class _Tp>
3474struct __scalar_hash<_Tp, 3>
3475 : public unary_function<_Tp, size_t>
3476{
3477 _LIBCPP_INLINE_VISIBILITY
3478 size_t operator()(_Tp __v) const _NOEXCEPT
3479 {
3480 union
3481 {
3482 _Tp __t;
3483 struct
3484 {
3485 size_t __a;
3486 size_t __b;
3487 size_t __c;
Eric Fiseliere012bf22015-07-18 20:40:46 +00003488 } __s;
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003489 } __u;
3490 __u.__t = __v;
Howard Hinnantf1973402011-12-10 20:28:56 +00003491 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003492 }
3493};
3494
3495template <class _Tp>
3496struct __scalar_hash<_Tp, 4>
3497 : public unary_function<_Tp, size_t>
3498{
3499 _LIBCPP_INLINE_VISIBILITY
3500 size_t operator()(_Tp __v) const _NOEXCEPT
3501 {
3502 union
3503 {
3504 _Tp __t;
3505 struct
3506 {
3507 size_t __a;
3508 size_t __b;
3509 size_t __c;
3510 size_t __d;
Eric Fiseliere012bf22015-07-18 20:40:46 +00003511 } __s;
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003512 } __u;
3513 __u.__t = __v;
Howard Hinnantf1973402011-12-10 20:28:56 +00003514 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003515 }
3516};
3517
3518template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003519struct _LIBCPP_TYPE_VIS_ONLY hash<_Tp*>
Howard Hinnant9fa30202012-07-30 01:40:57 +00003520 : public unary_function<_Tp*, size_t>
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003521{
Howard Hinnant9fa30202012-07-30 01:40:57 +00003522 _LIBCPP_INLINE_VISIBILITY
3523 size_t operator()(_Tp* __v) const _NOEXCEPT
3524 {
3525 union
3526 {
3527 _Tp* __t;
3528 size_t __a;
3529 } __u;
3530 __u.__t = __v;
3531 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
3532 }
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003533};
3534
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003535template <class _Tp, class _Dp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003536struct _LIBCPP_TYPE_VIS_ONLY hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003537{
3538 typedef unique_ptr<_Tp, _Dp> argument_type;
3539 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003540 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003541 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003542 {
3543 typedef typename argument_type::pointer pointer;
3544 return hash<pointer>()(__ptr.get());
3545 }
3546};
3547
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548struct __destruct_n
3549{
3550private:
3551 size_t size;
3552
3553 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003554 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003555 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3556
3557 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003558 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559 {}
3560
Howard Hinnant719bda32011-05-28 14:41:13 +00003561 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003562 {++size;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003563 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003564 {}
3565
Howard Hinnant719bda32011-05-28 14:41:13 +00003566 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003567 {size = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003568 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003569 {}
3570public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003571 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3572 : size(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003573
3574 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003575 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003576 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003577
3578 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003579 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003580 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003581
3582 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003583 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003584 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003585};
3586
3587template <class _Alloc>
3588class __allocator_destructor
3589{
3590 typedef allocator_traits<_Alloc> __alloc_traits;
3591public:
3592 typedef typename __alloc_traits::pointer pointer;
3593 typedef typename __alloc_traits::size_type size_type;
3594private:
3595 _Alloc& __alloc_;
3596 size_type __s_;
3597public:
3598 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003599 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003600 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003602 void operator()(pointer __p) _NOEXCEPT
3603 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003604};
3605
3606template <class _InputIterator, class _ForwardIterator>
3607_ForwardIterator
3608uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3609{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003610 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003611#ifndef _LIBCPP_NO_EXCEPTIONS
3612 _ForwardIterator __s = __r;
3613 try
3614 {
3615#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003616 for (; __f != __l; ++__f, (void) ++__r)
3617 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003618#ifndef _LIBCPP_NO_EXCEPTIONS
3619 }
3620 catch (...)
3621 {
3622 for (; __s != __r; ++__s)
3623 __s->~value_type();
3624 throw;
3625 }
3626#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003627 return __r;
3628}
3629
3630template <class _InputIterator, class _Size, class _ForwardIterator>
3631_ForwardIterator
3632uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3633{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003634 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003635#ifndef _LIBCPP_NO_EXCEPTIONS
3636 _ForwardIterator __s = __r;
3637 try
3638 {
3639#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003640 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3641 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003642#ifndef _LIBCPP_NO_EXCEPTIONS
3643 }
3644 catch (...)
3645 {
3646 for (; __s != __r; ++__s)
3647 __s->~value_type();
3648 throw;
3649 }
3650#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003651 return __r;
3652}
3653
3654template <class _ForwardIterator, class _Tp>
3655void
3656uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3657{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003658 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003659#ifndef _LIBCPP_NO_EXCEPTIONS
3660 _ForwardIterator __s = __f;
3661 try
3662 {
3663#endif
3664 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003665 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003666#ifndef _LIBCPP_NO_EXCEPTIONS
3667 }
3668 catch (...)
3669 {
3670 for (; __s != __f; ++__s)
3671 __s->~value_type();
3672 throw;
3673 }
3674#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003675}
3676
3677template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003678_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003679uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3680{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003681 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003682#ifndef _LIBCPP_NO_EXCEPTIONS
3683 _ForwardIterator __s = __f;
3684 try
3685 {
3686#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003687 for (; __n > 0; ++__f, (void) --__n)
3688 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003689#ifndef _LIBCPP_NO_EXCEPTIONS
3690 }
3691 catch (...)
3692 {
3693 for (; __s != __f; ++__s)
3694 __s->~value_type();
3695 throw;
3696 }
3697#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003698 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003699}
3700
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003701#if _LIBCPP_STD_VER > 14
3702
3703template <class _ForwardIterator>
3704inline _LIBCPP_INLINE_VISIBILITY
3705void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3706 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3707 for (; __first != __last; ++__first)
3708 ::new((void*)_VSTD::addressof(*__first)) _Vt;
3709}
3710
3711template <class _ForwardIterator, class _Size>
3712inline _LIBCPP_INLINE_VISIBILITY
3713_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3714 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3715 for (; __n > 0; (void)++__first, --__n)
3716 ::new((void*)_VSTD::addressof(*__first)) _Vt;
3717 return __first;
3718}
3719
3720
3721template <class _ForwardIterator>
3722inline _LIBCPP_INLINE_VISIBILITY
3723void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3724 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3725 for (; __first != __last; ++__first)
3726 ::new((void*)_VSTD::addressof(*__first)) _Vt();
3727}
3728
3729template <class _ForwardIterator, class _Size>
3730inline _LIBCPP_INLINE_VISIBILITY
3731_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3732 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3733 for (; __n > 0; (void)++__first, --__n)
3734 ::new((void*)_VSTD::addressof(*__first)) _Vt();
3735 return __first;
3736}
3737
3738
3739template <class _InputIt, class _ForwardIt>
3740inline _LIBCPP_INLINE_VISIBILITY
3741_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __res) {
3742 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3743 for (; __first != __last; (void)++__res, ++__first)
3744 ::new((void*)_VSTD::addressof(*__res)) _Vt(std::move(*__first));
3745 return __res;
3746}
3747
3748template <class _InputIt, class _Size, class _ForwardIt>
3749inline _LIBCPP_INLINE_VISIBILITY
3750pair<_InputIt, _ForwardIt>
3751uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __res) {
3752 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3753 for (; __n > 0; ++__res, (void)++__first, --__n)
3754 ::new((void*)_VSTD::addressof(*__res)) _Vt(std::move(*__first));
3755 return {__first, __res};
3756}
3757
3758template <class _Tp>
3759inline _LIBCPP_INLINE_VISIBILITY
3760void destroy_at(_Tp* __loc) {
3761 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3762 __loc->~_Tp();
3763}
3764
3765template <class _ForwardIterator>
3766inline _LIBCPP_INLINE_VISIBILITY
3767void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3768 for (; __first != __last; ++__first)
3769 _VSTD::destroy_at(_VSTD::addressof(*__first));
3770}
3771
3772template <class _ForwardIterator, class _Size>
3773inline _LIBCPP_INLINE_VISIBILITY
3774_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3775 for (; __n > 0; (void)++__first, --__n)
3776 _VSTD::destroy_at(_VSTD::addressof(*__first));
3777 return __first;
3778}
3779
3780#endif // _LIBCPP_STD_VER > 14
3781
Howard Hinnant756c69b2010-09-22 16:48:34 +00003782class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003783 : public std::exception
3784{
3785public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003786 virtual ~bad_weak_ptr() _NOEXCEPT;
3787 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003788};
3789
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003790template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003791
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003792class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003793{
3794 __shared_count(const __shared_count&);
3795 __shared_count& operator=(const __shared_count&);
3796
3797protected:
3798 long __shared_owners_;
3799 virtual ~__shared_count();
3800private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003801 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003802
3803public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003805 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003806 : __shared_owners_(__refs) {}
3807
Howard Hinnant719bda32011-05-28 14:41:13 +00003808 void __add_shared() _NOEXCEPT;
3809 bool __release_shared() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003810 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003811 long use_count() const _NOEXCEPT {
3812 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3813 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003814};
3815
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003816class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003817 : private __shared_count
3818{
3819 long __shared_weak_owners_;
3820
3821public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003822 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003823 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824 : __shared_count(__refs),
3825 __shared_weak_owners_(__refs) {}
3826protected:
3827 virtual ~__shared_weak_count();
3828
3829public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003830 void __add_shared() _NOEXCEPT;
3831 void __add_weak() _NOEXCEPT;
3832 void __release_shared() _NOEXCEPT;
3833 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003834 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003835 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3836 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003837
Howard Hinnant807d6332013-02-25 15:50:36 +00003838 // Define the function out only if we build static libc++ without RTTI.
3839 // Otherwise we may break clients who need to compile their projects with
3840 // -fno-rtti and yet link against a libc++.dylib compiled
3841 // without -fno-rtti.
3842#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003843 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003844#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003845private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003846 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003847};
3848
3849template <class _Tp, class _Dp, class _Alloc>
3850class __shared_ptr_pointer
3851 : public __shared_weak_count
3852{
3853 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3854public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003855 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003856 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003857 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003858
Howard Hinnant72f73582010-08-11 17:04:31 +00003859#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003860 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003861#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003862
3863private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003864 virtual void __on_zero_shared() _NOEXCEPT;
3865 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003866};
3867
Howard Hinnant72f73582010-08-11 17:04:31 +00003868#ifndef _LIBCPP_NO_RTTI
3869
Howard Hinnantc51e1022010-05-11 19:42:16 +00003870template <class _Tp, class _Dp, class _Alloc>
3871const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003872__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003873{
Marshall Clowa8dfc942014-11-17 19:05:50 +00003874 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003875}
3876
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003877#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003878
Howard Hinnantc51e1022010-05-11 19:42:16 +00003879template <class _Tp, class _Dp, class _Alloc>
3880void
Howard Hinnant719bda32011-05-28 14:41:13 +00003881__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003882{
3883 __data_.first().second()(__data_.first().first());
3884 __data_.first().second().~_Dp();
3885}
3886
3887template <class _Tp, class _Dp, class _Alloc>
3888void
Howard Hinnant719bda32011-05-28 14:41:13 +00003889__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003890{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003891 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3892 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003893 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3894
Eric Fiselierf8898c82015-02-05 23:01:40 +00003895 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003896 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003897 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003898}
3899
3900template <class _Tp, class _Alloc>
3901class __shared_ptr_emplace
3902 : public __shared_weak_count
3903{
3904 __compressed_pair<_Alloc, _Tp> __data_;
3905public:
3906#ifndef _LIBCPP_HAS_NO_VARIADICS
3907
Howard Hinnant756c69b2010-09-22 16:48:34 +00003908 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003909 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003910 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003911
3912 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003914 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003915 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3916 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003917
3918#else // _LIBCPP_HAS_NO_VARIADICS
3919
Howard Hinnant756c69b2010-09-22 16:48:34 +00003920 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003921 __shared_ptr_emplace(_Alloc __a)
3922 : __data_(__a) {}
3923
3924 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003925 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003926 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3927 : __data_(__a, _Tp(__a0)) {}
3928
3929 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003930 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003931 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3932 : __data_(__a, _Tp(__a0, __a1)) {}
3933
3934 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003935 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003936 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3937 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3938
3939#endif // _LIBCPP_HAS_NO_VARIADICS
3940
3941private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003942 virtual void __on_zero_shared() _NOEXCEPT;
3943 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003944public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003945 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003946 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003947};
3948
3949template <class _Tp, class _Alloc>
3950void
Howard Hinnant719bda32011-05-28 14:41:13 +00003951__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003952{
3953 __data_.second().~_Tp();
3954}
3955
3956template <class _Tp, class _Alloc>
3957void
Howard Hinnant719bda32011-05-28 14:41:13 +00003958__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003959{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003960 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3961 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003962 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003963 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003964 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003965 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003966}
3967
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003968template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003969
3970template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003971class _LIBCPP_TYPE_VIS_ONLY shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003972{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003973public:
3974 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00003975#if _LIBCPP_STD_VER > 14
3976 typedef weak_ptr<_Tp> weak_type;
3977#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003978private:
3979 element_type* __ptr_;
3980 __shared_weak_count* __cntrl_;
3981
3982 struct __nat {int __for_bool_;};
3983public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003984 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003985 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003986 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003987 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003988 template<class _Yp>
3989 explicit shared_ptr(_Yp* __p,
3990 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3991 template<class _Yp, class _Dp>
3992 shared_ptr(_Yp* __p, _Dp __d,
3993 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3994 template<class _Yp, class _Dp, class _Alloc>
3995 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3996 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003997 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3998 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003999 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
4000 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004001 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004002 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004003 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004004 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004005 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
4006 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00004007#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004008 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004009 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004010 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004011 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
4012 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00004013#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004014 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004015 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00004016#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004017 template<class _Yp>
4018 shared_ptr(auto_ptr<_Yp>&& __r,
4019 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004020#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004021 template<class _Yp>
4022 shared_ptr(auto_ptr<_Yp> __r,
4023 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004024#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00004025#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004026 template <class _Yp, class _Dp>
4027 shared_ptr(unique_ptr<_Yp, _Dp>&&,
4028 typename enable_if
4029 <
4030 !is_lvalue_reference<_Dp>::value &&
4031 !is_array<_Yp>::value &&
4032 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4033 __nat
4034 >::type = __nat());
4035 template <class _Yp, class _Dp>
4036 shared_ptr(unique_ptr<_Yp, _Dp>&&,
4037 typename enable_if
4038 <
4039 is_lvalue_reference<_Dp>::value &&
4040 !is_array<_Yp>::value &&
4041 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4042 __nat
4043 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00004044#else // _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#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004064
4065 ~shared_ptr();
4066
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004068 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004069 template<class _Yp>
4070 typename enable_if
4071 <
4072 is_convertible<_Yp*, element_type*>::value,
4073 shared_ptr&
4074 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004075 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004076 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00004077#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004078 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004079 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004080 template<class _Yp>
4081 typename enable_if
4082 <
4083 is_convertible<_Yp*, element_type*>::value,
4084 shared_ptr<_Tp>&
4085 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004086 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004087 operator=(shared_ptr<_Yp>&& __r);
4088 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00004089 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004090 typename enable_if
4091 <
4092 !is_array<_Yp>::value &&
4093 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004094 shared_ptr
4095 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004096 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00004097#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004098 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00004099 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004100 typename enable_if
4101 <
4102 !is_array<_Yp>::value &&
4103 is_convertible<_Yp*, element_type*>::value,
4104 shared_ptr&
4105 >::type
4106 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004107#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004108 template <class _Yp, class _Dp>
4109 typename enable_if
4110 <
4111 !is_array<_Yp>::value &&
4112 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4113 shared_ptr&
4114 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00004115#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004116 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004117 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00004118#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00004119 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004120 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004121#endif
4122
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004123 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004124 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004125 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004126 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004127 template<class _Yp>
4128 typename enable_if
4129 <
4130 is_convertible<_Yp*, element_type*>::value,
4131 void
4132 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004133 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004134 reset(_Yp* __p);
4135 template<class _Yp, class _Dp>
4136 typename enable_if
4137 <
4138 is_convertible<_Yp*, element_type*>::value,
4139 void
4140 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004141 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004142 reset(_Yp* __p, _Dp __d);
4143 template<class _Yp, class _Dp, class _Alloc>
4144 typename enable_if
4145 <
4146 is_convertible<_Yp*, element_type*>::value,
4147 void
4148 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004149 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004150 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004151
Howard Hinnant756c69b2010-09-22 16:48:34 +00004152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004153 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004154 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004155 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
4156 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004157 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004158 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004159 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004160 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004162 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004163 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00004164 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00004165 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004166 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00004167 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00004168 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00004169 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00004171 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00004172 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00004173 _LIBCPP_INLINE_VISIBILITY
4174 bool
4175 __owner_equivalent(const shared_ptr& __p) const
4176 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004177
Howard Hinnant72f73582010-08-11 17:04:31 +00004178#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004179 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004181 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004182 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004183#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004184
4185#ifndef _LIBCPP_HAS_NO_VARIADICS
4186
4187 template<class ..._Args>
4188 static
4189 shared_ptr<_Tp>
4190 make_shared(_Args&& ...__args);
4191
4192 template<class _Alloc, class ..._Args>
4193 static
4194 shared_ptr<_Tp>
4195 allocate_shared(const _Alloc& __a, _Args&& ...__args);
4196
4197#else // _LIBCPP_HAS_NO_VARIADICS
4198
4199 static shared_ptr<_Tp> make_shared();
4200
4201 template<class _A0>
4202 static shared_ptr<_Tp> make_shared(_A0&);
4203
4204 template<class _A0, class _A1>
4205 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
4206
4207 template<class _A0, class _A1, class _A2>
4208 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
4209
4210 template<class _Alloc>
4211 static shared_ptr<_Tp>
4212 allocate_shared(const _Alloc& __a);
4213
4214 template<class _Alloc, class _A0>
4215 static shared_ptr<_Tp>
4216 allocate_shared(const _Alloc& __a, _A0& __a0);
4217
4218 template<class _Alloc, class _A0, class _A1>
4219 static shared_ptr<_Tp>
4220 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
4221
4222 template<class _Alloc, class _A0, class _A1, class _A2>
4223 static shared_ptr<_Tp>
4224 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
4225
4226#endif // _LIBCPP_HAS_NO_VARIADICS
4227
4228private:
4229
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004230 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004231 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004232 void
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004233 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
4234 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004235 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004236 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00004237 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00004238 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004239 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
4240 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00004241 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004242 }
4243
Howard Hinnant756c69b2010-09-22 16:48:34 +00004244 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004245 void __enable_weak_this(const volatile void*, const volatile void*) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004246
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004247 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
4248 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004249};
4250
4251template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004252inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004253_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004254shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004255 : __ptr_(0),
4256 __cntrl_(0)
4257{
4258}
4259
4260template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004261inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004262_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004263shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004264 : __ptr_(0),
4265 __cntrl_(0)
4266{
4267}
4268
4269template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004270template<class _Yp>
4271shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4272 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004273 : __ptr_(__p)
4274{
4275 unique_ptr<_Yp> __hold(__p);
4276 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4277 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
4278 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004279 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004280}
4281
4282template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004283template<class _Yp, class _Dp>
4284shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4285 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004286 : __ptr_(__p)
4287{
4288#ifndef _LIBCPP_NO_EXCEPTIONS
4289 try
4290 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004291#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004292 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4293 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004294 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004295#ifndef _LIBCPP_NO_EXCEPTIONS
4296 }
4297 catch (...)
4298 {
4299 __d(__p);
4300 throw;
4301 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004302#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004303}
4304
4305template<class _Tp>
4306template<class _Dp>
4307shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4308 : __ptr_(0)
4309{
4310#ifndef _LIBCPP_NO_EXCEPTIONS
4311 try
4312 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004313#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004314 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4315 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4316#ifndef _LIBCPP_NO_EXCEPTIONS
4317 }
4318 catch (...)
4319 {
4320 __d(__p);
4321 throw;
4322 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004323#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004324}
4325
4326template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004327template<class _Yp, class _Dp, class _Alloc>
4328shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4329 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004330 : __ptr_(__p)
4331{
4332#ifndef _LIBCPP_NO_EXCEPTIONS
4333 try
4334 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004335#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004336 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004337 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004338 typedef __allocator_destructor<_A2> _D2;
4339 _A2 __a2(__a);
4340 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004341 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4342 _CntrlBlk(__p, __d, __a);
4343 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004344 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004345#ifndef _LIBCPP_NO_EXCEPTIONS
4346 }
4347 catch (...)
4348 {
4349 __d(__p);
4350 throw;
4351 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004352#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004353}
4354
4355template<class _Tp>
4356template<class _Dp, class _Alloc>
4357shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4358 : __ptr_(0)
4359{
4360#ifndef _LIBCPP_NO_EXCEPTIONS
4361 try
4362 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004363#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004364 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004365 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004366 typedef __allocator_destructor<_A2> _D2;
4367 _A2 __a2(__a);
4368 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004369 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4370 _CntrlBlk(__p, __d, __a);
4371 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004372#ifndef _LIBCPP_NO_EXCEPTIONS
4373 }
4374 catch (...)
4375 {
4376 __d(__p);
4377 throw;
4378 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004379#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004380}
4381
4382template<class _Tp>
4383template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004384inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004385shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004386 : __ptr_(__p),
4387 __cntrl_(__r.__cntrl_)
4388{
4389 if (__cntrl_)
4390 __cntrl_->__add_shared();
4391}
4392
4393template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004394inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004395shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004396 : __ptr_(__r.__ptr_),
4397 __cntrl_(__r.__cntrl_)
4398{
4399 if (__cntrl_)
4400 __cntrl_->__add_shared();
4401}
4402
4403template<class _Tp>
4404template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004405inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004406shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4407 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004408 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004409 : __ptr_(__r.__ptr_),
4410 __cntrl_(__r.__cntrl_)
4411{
4412 if (__cntrl_)
4413 __cntrl_->__add_shared();
4414}
4415
Howard Hinnant74279a52010-09-04 23:28:19 +00004416#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004417
4418template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004419inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004420shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004421 : __ptr_(__r.__ptr_),
4422 __cntrl_(__r.__cntrl_)
4423{
4424 __r.__ptr_ = 0;
4425 __r.__cntrl_ = 0;
4426}
4427
4428template<class _Tp>
4429template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004430inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004431shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4432 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004433 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004434 : __ptr_(__r.__ptr_),
4435 __cntrl_(__r.__cntrl_)
4436{
4437 __r.__ptr_ = 0;
4438 __r.__cntrl_ = 0;
4439}
4440
Howard Hinnant74279a52010-09-04 23:28:19 +00004441#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004442
4443template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004444template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004445#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004446shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004447#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004448shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004449#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004450 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004451 : __ptr_(__r.get())
4452{
4453 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4454 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004455 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004456 __r.release();
4457}
4458
4459template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004460template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004461#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004462shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4463#else
4464shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4465#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004466 typename enable_if
4467 <
4468 !is_lvalue_reference<_Dp>::value &&
4469 !is_array<_Yp>::value &&
4470 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4471 __nat
4472 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004473 : __ptr_(__r.get())
4474{
Marshall Clow35cde742015-05-10 13:59:45 +00004475#if _LIBCPP_STD_VER > 11
4476 if (__ptr_ == nullptr)
4477 __cntrl_ = nullptr;
4478 else
4479#endif
4480 {
4481 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4482 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004483 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004484 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004485 __r.release();
4486}
4487
4488template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004489template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004490#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004491shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4492#else
4493shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4494#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004495 typename enable_if
4496 <
4497 is_lvalue_reference<_Dp>::value &&
4498 !is_array<_Yp>::value &&
4499 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4500 __nat
4501 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004502 : __ptr_(__r.get())
4503{
Marshall Clow35cde742015-05-10 13:59:45 +00004504#if _LIBCPP_STD_VER > 11
4505 if (__ptr_ == nullptr)
4506 __cntrl_ = nullptr;
4507 else
4508#endif
4509 {
4510 typedef __shared_ptr_pointer<_Yp*,
4511 reference_wrapper<typename remove_reference<_Dp>::type>,
4512 allocator<_Yp> > _CntrlBlk;
4513 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004514 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004515 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004516 __r.release();
4517}
4518
4519#ifndef _LIBCPP_HAS_NO_VARIADICS
4520
4521template<class _Tp>
4522template<class ..._Args>
4523shared_ptr<_Tp>
4524shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4525{
4526 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4527 typedef allocator<_CntrlBlk> _A2;
4528 typedef __allocator_destructor<_A2> _D2;
4529 _A2 __a2;
4530 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004531 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004532 shared_ptr<_Tp> __r;
4533 __r.__ptr_ = __hold2.get()->get();
4534 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004535 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004536 return __r;
4537}
4538
4539template<class _Tp>
4540template<class _Alloc, class ..._Args>
4541shared_ptr<_Tp>
4542shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4543{
4544 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004545 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004546 typedef __allocator_destructor<_A2> _D2;
4547 _A2 __a2(__a);
4548 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004549 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4550 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004551 shared_ptr<_Tp> __r;
4552 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004553 __r.__cntrl_ = _VSTD::addressof(*__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
4558#else // _LIBCPP_HAS_NO_VARIADICS
4559
4560template<class _Tp>
4561shared_ptr<_Tp>
4562shared_ptr<_Tp>::make_shared()
4563{
4564 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4565 typedef allocator<_CntrlBlk> _Alloc2;
4566 typedef __allocator_destructor<_Alloc2> _D2;
4567 _Alloc2 __alloc2;
4568 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4569 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4570 shared_ptr<_Tp> __r;
4571 __r.__ptr_ = __hold2.get()->get();
4572 __r.__cntrl_ = __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
4577template<class _Tp>
4578template<class _A0>
4579shared_ptr<_Tp>
4580shared_ptr<_Tp>::make_shared(_A0& __a0)
4581{
4582 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4583 typedef allocator<_CntrlBlk> _Alloc2;
4584 typedef __allocator_destructor<_Alloc2> _D2;
4585 _Alloc2 __alloc2;
4586 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4587 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4588 shared_ptr<_Tp> __r;
4589 __r.__ptr_ = __hold2.get()->get();
4590 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004591 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004592 return __r;
4593}
4594
4595template<class _Tp>
4596template<class _A0, class _A1>
4597shared_ptr<_Tp>
4598shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4599{
4600 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4601 typedef allocator<_CntrlBlk> _Alloc2;
4602 typedef __allocator_destructor<_Alloc2> _D2;
4603 _Alloc2 __alloc2;
4604 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4605 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4606 shared_ptr<_Tp> __r;
4607 __r.__ptr_ = __hold2.get()->get();
4608 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004609 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004610 return __r;
4611}
4612
4613template<class _Tp>
4614template<class _A0, class _A1, class _A2>
4615shared_ptr<_Tp>
4616shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4617{
4618 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4619 typedef allocator<_CntrlBlk> _Alloc2;
4620 typedef __allocator_destructor<_Alloc2> _D2;
4621 _Alloc2 __alloc2;
4622 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4623 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4624 shared_ptr<_Tp> __r;
4625 __r.__ptr_ = __hold2.get()->get();
4626 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004627 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004628 return __r;
4629}
4630
4631template<class _Tp>
4632template<class _Alloc>
4633shared_ptr<_Tp>
4634shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4635{
4636 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004637 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004638 typedef __allocator_destructor<_Alloc2> _D2;
4639 _Alloc2 __alloc2(__a);
4640 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004641 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4642 _CntrlBlk(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004643 shared_ptr<_Tp> __r;
4644 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004645 __r.__cntrl_ = _VSTD::addressof(*__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, class _A0>
4652shared_ptr<_Tp>
4653shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
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, __a0);
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, class _A1>
4671shared_ptr<_Tp>
4672shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
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, __a1);
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, class _A2>
4690shared_ptr<_Tp>
4691shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
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, __a2);
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
4707#endif // _LIBCPP_HAS_NO_VARIADICS
4708
4709template<class _Tp>
4710shared_ptr<_Tp>::~shared_ptr()
4711{
4712 if (__cntrl_)
4713 __cntrl_->__release_shared();
4714}
4715
4716template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004717inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004718shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004719shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004720{
4721 shared_ptr(__r).swap(*this);
4722 return *this;
4723}
4724
4725template<class _Tp>
4726template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004727inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004728typename enable_if
4729<
4730 is_convertible<_Yp*, _Tp*>::value,
4731 shared_ptr<_Tp>&
4732>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004733shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004734{
4735 shared_ptr(__r).swap(*this);
4736 return *this;
4737}
4738
Howard Hinnant74279a52010-09-04 23:28:19 +00004739#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004740
4741template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004742inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004743shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004744shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004745{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004746 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004747 return *this;
4748}
4749
4750template<class _Tp>
4751template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004752inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004753typename enable_if
4754<
4755 is_convertible<_Yp*, _Tp*>::value,
4756 shared_ptr<_Tp>&
4757>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004758shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4759{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004760 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004761 return *this;
4762}
4763
4764template<class _Tp>
4765template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004766inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004767typename enable_if
4768<
4769 !is_array<_Yp>::value &&
4770 is_convertible<_Yp*, _Tp*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004771 shared_ptr<_Tp>
4772>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004773shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4774{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004775 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004776 return *this;
4777}
4778
4779template<class _Tp>
4780template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004781inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004782typename enable_if
4783<
4784 !is_array<_Yp>::value &&
4785 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4786 shared_ptr<_Tp>&
4787>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004788shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4789{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004790 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004791 return *this;
4792}
4793
Howard Hinnant74279a52010-09-04 23:28:19 +00004794#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004795
4796template<class _Tp>
4797template<class _Yp>
4798inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004799typename enable_if
4800<
4801 !is_array<_Yp>::value &&
4802 is_convertible<_Yp*, _Tp*>::value,
4803 shared_ptr<_Tp>&
4804>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004805shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004806{
4807 shared_ptr(__r).swap(*this);
4808 return *this;
4809}
4810
4811template<class _Tp>
4812template <class _Yp, class _Dp>
4813inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004814typename enable_if
4815<
4816 !is_array<_Yp>::value &&
4817 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4818 shared_ptr<_Tp>&
4819>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004820shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4821{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004822 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004823 return *this;
4824}
4825
Howard Hinnant74279a52010-09-04 23:28:19 +00004826#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004827
4828template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004829inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004830void
Howard Hinnant719bda32011-05-28 14:41:13 +00004831shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004832{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004833 _VSTD::swap(__ptr_, __r.__ptr_);
4834 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004835}
4836
4837template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004838inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004839void
Howard Hinnant719bda32011-05-28 14:41:13 +00004840shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004841{
4842 shared_ptr().swap(*this);
4843}
4844
4845template<class _Tp>
4846template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004847inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004848typename enable_if
4849<
4850 is_convertible<_Yp*, _Tp*>::value,
4851 void
4852>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004853shared_ptr<_Tp>::reset(_Yp* __p)
4854{
4855 shared_ptr(__p).swap(*this);
4856}
4857
4858template<class _Tp>
4859template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004860inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004861typename enable_if
4862<
4863 is_convertible<_Yp*, _Tp*>::value,
4864 void
4865>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004866shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4867{
4868 shared_ptr(__p, __d).swap(*this);
4869}
4870
4871template<class _Tp>
4872template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004873inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004874typename enable_if
4875<
4876 is_convertible<_Yp*, _Tp*>::value,
4877 void
4878>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004879shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4880{
4881 shared_ptr(__p, __d, __a).swap(*this);
4882}
4883
4884#ifndef _LIBCPP_HAS_NO_VARIADICS
4885
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004886template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004887inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004888typename enable_if
4889<
4890 !is_array<_Tp>::value,
4891 shared_ptr<_Tp>
4892>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004893make_shared(_Args&& ...__args)
4894{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004895 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004896}
4897
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004898template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004899inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004900typename enable_if
4901<
4902 !is_array<_Tp>::value,
4903 shared_ptr<_Tp>
4904>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004905allocate_shared(const _Alloc& __a, _Args&& ...__args)
4906{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004907 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004908}
4909
4910#else // _LIBCPP_HAS_NO_VARIADICS
4911
4912template<class _Tp>
4913inline _LIBCPP_INLINE_VISIBILITY
4914shared_ptr<_Tp>
4915make_shared()
4916{
4917 return shared_ptr<_Tp>::make_shared();
4918}
4919
4920template<class _Tp, class _A0>
4921inline _LIBCPP_INLINE_VISIBILITY
4922shared_ptr<_Tp>
4923make_shared(_A0& __a0)
4924{
4925 return shared_ptr<_Tp>::make_shared(__a0);
4926}
4927
4928template<class _Tp, class _A0, class _A1>
4929inline _LIBCPP_INLINE_VISIBILITY
4930shared_ptr<_Tp>
4931make_shared(_A0& __a0, _A1& __a1)
4932{
4933 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4934}
4935
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004936template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004937inline _LIBCPP_INLINE_VISIBILITY
4938shared_ptr<_Tp>
4939make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4940{
4941 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4942}
4943
4944template<class _Tp, class _Alloc>
4945inline _LIBCPP_INLINE_VISIBILITY
4946shared_ptr<_Tp>
4947allocate_shared(const _Alloc& __a)
4948{
4949 return shared_ptr<_Tp>::allocate_shared(__a);
4950}
4951
4952template<class _Tp, class _Alloc, class _A0>
4953inline _LIBCPP_INLINE_VISIBILITY
4954shared_ptr<_Tp>
4955allocate_shared(const _Alloc& __a, _A0& __a0)
4956{
4957 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4958}
4959
4960template<class _Tp, class _Alloc, class _A0, class _A1>
4961inline _LIBCPP_INLINE_VISIBILITY
4962shared_ptr<_Tp>
4963allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4964{
4965 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4966}
4967
4968template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4969inline _LIBCPP_INLINE_VISIBILITY
4970shared_ptr<_Tp>
4971allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4972{
4973 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4974}
4975
4976#endif // _LIBCPP_HAS_NO_VARIADICS
4977
4978template<class _Tp, class _Up>
4979inline _LIBCPP_INLINE_VISIBILITY
4980bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004981operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004982{
4983 return __x.get() == __y.get();
4984}
4985
4986template<class _Tp, class _Up>
4987inline _LIBCPP_INLINE_VISIBILITY
4988bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004989operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004990{
4991 return !(__x == __y);
4992}
4993
4994template<class _Tp, class _Up>
4995inline _LIBCPP_INLINE_VISIBILITY
4996bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004997operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004998{
Eric Fiselierf8898c82015-02-05 23:01:40 +00004999 typedef typename common_type<_Tp*, _Up*>::type _Vp;
5000 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00005001}
5002
5003template<class _Tp, class _Up>
5004inline _LIBCPP_INLINE_VISIBILITY
5005bool
5006operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
5007{
5008 return __y < __x;
5009}
5010
5011template<class _Tp, class _Up>
5012inline _LIBCPP_INLINE_VISIBILITY
5013bool
5014operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
5015{
5016 return !(__y < __x);
5017}
5018
5019template<class _Tp, class _Up>
5020inline _LIBCPP_INLINE_VISIBILITY
5021bool
5022operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
5023{
5024 return !(__x < __y);
5025}
5026
5027template<class _Tp>
5028inline _LIBCPP_INLINE_VISIBILITY
5029bool
5030operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5031{
5032 return !__x;
5033}
5034
5035template<class _Tp>
5036inline _LIBCPP_INLINE_VISIBILITY
5037bool
5038operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5039{
5040 return !__x;
5041}
5042
5043template<class _Tp>
5044inline _LIBCPP_INLINE_VISIBILITY
5045bool
5046operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5047{
5048 return static_cast<bool>(__x);
5049}
5050
5051template<class _Tp>
5052inline _LIBCPP_INLINE_VISIBILITY
5053bool
5054operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5055{
5056 return static_cast<bool>(__x);
5057}
5058
5059template<class _Tp>
5060inline _LIBCPP_INLINE_VISIBILITY
5061bool
5062operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5063{
5064 return less<_Tp*>()(__x.get(), nullptr);
5065}
5066
5067template<class _Tp>
5068inline _LIBCPP_INLINE_VISIBILITY
5069bool
5070operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5071{
5072 return less<_Tp*>()(nullptr, __x.get());
5073}
5074
5075template<class _Tp>
5076inline _LIBCPP_INLINE_VISIBILITY
5077bool
5078operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5079{
5080 return nullptr < __x;
5081}
5082
5083template<class _Tp>
5084inline _LIBCPP_INLINE_VISIBILITY
5085bool
5086operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5087{
5088 return __x < nullptr;
5089}
5090
5091template<class _Tp>
5092inline _LIBCPP_INLINE_VISIBILITY
5093bool
5094operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5095{
5096 return !(nullptr < __x);
5097}
5098
5099template<class _Tp>
5100inline _LIBCPP_INLINE_VISIBILITY
5101bool
5102operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5103{
5104 return !(__x < nullptr);
5105}
5106
5107template<class _Tp>
5108inline _LIBCPP_INLINE_VISIBILITY
5109bool
5110operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5111{
5112 return !(__x < nullptr);
5113}
5114
5115template<class _Tp>
5116inline _LIBCPP_INLINE_VISIBILITY
5117bool
5118operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5119{
5120 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005121}
5122
5123template<class _Tp>
5124inline _LIBCPP_INLINE_VISIBILITY
5125void
Howard Hinnant719bda32011-05-28 14:41:13 +00005126swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005127{
5128 __x.swap(__y);
5129}
5130
5131template<class _Tp, class _Up>
5132inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005133typename enable_if
5134<
5135 !is_array<_Tp>::value && !is_array<_Up>::value,
5136 shared_ptr<_Tp>
5137>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005138static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005139{
5140 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
5141}
5142
5143template<class _Tp, class _Up>
5144inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005145typename enable_if
5146<
5147 !is_array<_Tp>::value && !is_array<_Up>::value,
5148 shared_ptr<_Tp>
5149>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005150dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005151{
5152 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
5153 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
5154}
5155
5156template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005157typename enable_if
5158<
5159 is_array<_Tp>::value == is_array<_Up>::value,
5160 shared_ptr<_Tp>
5161>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005162const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005163{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005164 typedef typename remove_extent<_Tp>::type _RTp;
5165 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00005166}
5167
Howard Hinnant72f73582010-08-11 17:04:31 +00005168#ifndef _LIBCPP_NO_RTTI
5169
Howard Hinnantc51e1022010-05-11 19:42:16 +00005170template<class _Dp, class _Tp>
5171inline _LIBCPP_INLINE_VISIBILITY
5172_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00005173get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005174{
5175 return __p.template __get_deleter<_Dp>();
5176}
5177
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005178#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00005179
Howard Hinnantc51e1022010-05-11 19:42:16 +00005180template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005181class _LIBCPP_TYPE_VIS_ONLY weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00005182{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005183public:
5184 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005185private:
5186 element_type* __ptr_;
5187 __shared_weak_count* __cntrl_;
5188
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005189public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005191 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005192 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00005193 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5194 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005195 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005196 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005197 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00005198 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5199 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005200
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005201#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005203 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005204 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005205 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5206 _NOEXCEPT;
5207#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005208 ~weak_ptr();
5209
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005211 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005212 template<class _Yp>
5213 typename enable_if
5214 <
5215 is_convertible<_Yp*, element_type*>::value,
5216 weak_ptr&
5217 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005219 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
5220
5221#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5222
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005223 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005224 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
5225 template<class _Yp>
5226 typename enable_if
5227 <
5228 is_convertible<_Yp*, element_type*>::value,
5229 weak_ptr&
5230 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005231 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005232 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
5233
5234#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5235
5236 template<class _Yp>
5237 typename enable_if
5238 <
5239 is_convertible<_Yp*, element_type*>::value,
5240 weak_ptr&
5241 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005243 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005244
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005246 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005248 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005249
Howard Hinnant756c69b2010-09-22 16:48:34 +00005250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005251 long use_count() const _NOEXCEPT
5252 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005254 bool expired() const _NOEXCEPT
5255 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5256 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005257 template<class _Up>
5258 _LIBCPP_INLINE_VISIBILITY
5259 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00005260 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005261 template<class _Up>
5262 _LIBCPP_INLINE_VISIBILITY
5263 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00005264 {return __cntrl_ < __r.__cntrl_;}
5265
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005266 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
5267 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005268};
5269
5270template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005271inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005272_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005273weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005274 : __ptr_(0),
5275 __cntrl_(0)
5276{
5277}
5278
5279template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005280inline
Howard Hinnant719bda32011-05-28 14:41:13 +00005281weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005282 : __ptr_(__r.__ptr_),
5283 __cntrl_(__r.__cntrl_)
5284{
5285 if (__cntrl_)
5286 __cntrl_->__add_weak();
5287}
5288
5289template<class _Tp>
5290template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005291inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005292weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005293 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005294 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005295 : __ptr_(__r.__ptr_),
5296 __cntrl_(__r.__cntrl_)
5297{
5298 if (__cntrl_)
5299 __cntrl_->__add_weak();
5300}
5301
5302template<class _Tp>
5303template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005304inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005305weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005306 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005307 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005308 : __ptr_(__r.__ptr_),
5309 __cntrl_(__r.__cntrl_)
5310{
5311 if (__cntrl_)
5312 __cntrl_->__add_weak();
5313}
5314
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005315#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5316
5317template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005318inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005319weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5320 : __ptr_(__r.__ptr_),
5321 __cntrl_(__r.__cntrl_)
5322{
5323 __r.__ptr_ = 0;
5324 __r.__cntrl_ = 0;
5325}
5326
5327template<class _Tp>
5328template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005329inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005330weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5331 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5332 _NOEXCEPT
5333 : __ptr_(__r.__ptr_),
5334 __cntrl_(__r.__cntrl_)
5335{
5336 __r.__ptr_ = 0;
5337 __r.__cntrl_ = 0;
5338}
5339
5340#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5341
Howard Hinnantc51e1022010-05-11 19:42:16 +00005342template<class _Tp>
5343weak_ptr<_Tp>::~weak_ptr()
5344{
5345 if (__cntrl_)
5346 __cntrl_->__release_weak();
5347}
5348
5349template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005350inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005351weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005352weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005353{
5354 weak_ptr(__r).swap(*this);
5355 return *this;
5356}
5357
5358template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005359template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005360inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005361typename enable_if
5362<
5363 is_convertible<_Yp*, _Tp*>::value,
5364 weak_ptr<_Tp>&
5365>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005366weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005367{
5368 weak_ptr(__r).swap(*this);
5369 return *this;
5370}
5371
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005372#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5373
5374template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005375inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005376weak_ptr<_Tp>&
5377weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5378{
5379 weak_ptr(_VSTD::move(__r)).swap(*this);
5380 return *this;
5381}
5382
Howard Hinnantc51e1022010-05-11 19:42:16 +00005383template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005384template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005385inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005386typename enable_if
5387<
5388 is_convertible<_Yp*, _Tp*>::value,
5389 weak_ptr<_Tp>&
5390>::type
5391weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5392{
5393 weak_ptr(_VSTD::move(__r)).swap(*this);
5394 return *this;
5395}
5396
5397#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5398
5399template<class _Tp>
5400template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005401inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005402typename enable_if
5403<
5404 is_convertible<_Yp*, _Tp*>::value,
5405 weak_ptr<_Tp>&
5406>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005407weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005408{
5409 weak_ptr(__r).swap(*this);
5410 return *this;
5411}
5412
5413template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005414inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005415void
Howard Hinnant719bda32011-05-28 14:41:13 +00005416weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005417{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005418 _VSTD::swap(__ptr_, __r.__ptr_);
5419 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005420}
5421
5422template<class _Tp>
5423inline _LIBCPP_INLINE_VISIBILITY
5424void
Howard Hinnant719bda32011-05-28 14:41:13 +00005425swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005426{
5427 __x.swap(__y);
5428}
5429
5430template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005431inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005432void
Howard Hinnant719bda32011-05-28 14:41:13 +00005433weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005434{
5435 weak_ptr().swap(*this);
5436}
5437
5438template<class _Tp>
5439template<class _Yp>
5440shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5441 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
5442 : __ptr_(__r.__ptr_),
5443 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5444{
5445 if (__cntrl_ == 0)
5446#ifndef _LIBCPP_NO_EXCEPTIONS
5447 throw bad_weak_ptr();
5448#else
5449 assert(!"bad_weak_ptr");
5450#endif
5451}
5452
5453template<class _Tp>
5454shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005455weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005456{
5457 shared_ptr<_Tp> __r;
5458 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5459 if (__r.__cntrl_)
5460 __r.__ptr_ = __ptr_;
5461 return __r;
5462}
5463
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005464#if _LIBCPP_STD_VER > 14
5465template <class _Tp = void> struct owner_less;
5466#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005467template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005468#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005469
5470template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005471struct _LIBCPP_TYPE_VIS_ONLY owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005472 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005473{
5474 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005475 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005476 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5477 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005478 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005479 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5480 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005481 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005482 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5483 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005484};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005485
5486template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005487struct _LIBCPP_TYPE_VIS_ONLY owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005488 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5489{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005490 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005491 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005492 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5493 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005494 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005495 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5496 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005497 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005498 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5499 {return __x.owner_before(__y);}
5500};
5501
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005502#if _LIBCPP_STD_VER > 14
5503template <>
5504struct _LIBCPP_TYPE_VIS_ONLY owner_less<void>
5505{
5506 template <class _Tp, class _Up>
5507 _LIBCPP_INLINE_VISIBILITY
5508 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const
5509 {return __x.owner_before(__y);}
5510 template <class _Tp, class _Up>
5511 _LIBCPP_INLINE_VISIBILITY
5512 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const
5513 {return __x.owner_before(__y);}
5514 template <class _Tp, class _Up>
5515 _LIBCPP_INLINE_VISIBILITY
5516 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const
5517 {return __x.owner_before(__y);}
5518 template <class _Tp, class _Up>
5519 _LIBCPP_INLINE_VISIBILITY
5520 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const
5521 {return __x.owner_before(__y);}
5522 typedef void is_transparent;
5523};
5524#endif
5525
Howard Hinnantc51e1022010-05-11 19:42:16 +00005526template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005527class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005528{
5529 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005530protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005531 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005532 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005534 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005535 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005536 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5537 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005539 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005540public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005542 shared_ptr<_Tp> shared_from_this()
5543 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005545 shared_ptr<_Tp const> shared_from_this() const
5546 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005547
Eric Fiselier84006862016-06-02 00:15:35 +00005548#if _LIBCPP_STD_VER > 14
5549 _LIBCPP_INLINE_VISIBILITY
5550 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5551 { return __weak_this_; }
5552
5553 _LIBCPP_INLINE_VISIBILITY
5554 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5555 { return __weak_this_; }
5556#endif // _LIBCPP_STD_VER > 14
5557
Howard Hinnantc51e1022010-05-11 19:42:16 +00005558 template <class _Up> friend class shared_ptr;
5559};
5560
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005561template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005562struct _LIBCPP_TYPE_VIS_ONLY hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005563{
5564 typedef shared_ptr<_Tp> argument_type;
5565 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005566 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005567 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005568 {
5569 return hash<_Tp*>()(__ptr.get());
5570 }
5571};
5572
Howard Hinnantc834c512011-11-29 18:15:50 +00005573template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005574inline _LIBCPP_INLINE_VISIBILITY
5575basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005576operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005577
Eric Fiselier9b492672016-06-18 02:12:53 +00005578
5579#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005580
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005581class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005582{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005583 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005584public:
5585 void lock() _NOEXCEPT;
5586 void unlock() _NOEXCEPT;
5587
5588private:
5589 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5590 __sp_mut(const __sp_mut&);
5591 __sp_mut& operator=(const __sp_mut&);
5592
Howard Hinnant8331b762013-03-06 23:30:19 +00005593 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005594};
5595
Howard Hinnant8331b762013-03-06 23:30:19 +00005596_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005597
5598template <class _Tp>
5599inline _LIBCPP_INLINE_VISIBILITY
5600bool
5601atomic_is_lock_free(const shared_ptr<_Tp>*)
5602{
5603 return false;
5604}
5605
5606template <class _Tp>
5607shared_ptr<_Tp>
5608atomic_load(const shared_ptr<_Tp>* __p)
5609{
5610 __sp_mut& __m = __get_sp_mut(__p);
5611 __m.lock();
5612 shared_ptr<_Tp> __q = *__p;
5613 __m.unlock();
5614 return __q;
5615}
5616
5617template <class _Tp>
5618inline _LIBCPP_INLINE_VISIBILITY
5619shared_ptr<_Tp>
5620atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5621{
5622 return atomic_load(__p);
5623}
5624
5625template <class _Tp>
5626void
5627atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5628{
5629 __sp_mut& __m = __get_sp_mut(__p);
5630 __m.lock();
5631 __p->swap(__r);
5632 __m.unlock();
5633}
5634
5635template <class _Tp>
5636inline _LIBCPP_INLINE_VISIBILITY
5637void
5638atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5639{
5640 atomic_store(__p, __r);
5641}
5642
5643template <class _Tp>
5644shared_ptr<_Tp>
5645atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5646{
5647 __sp_mut& __m = __get_sp_mut(__p);
5648 __m.lock();
5649 __p->swap(__r);
5650 __m.unlock();
5651 return __r;
5652}
5653
5654template <class _Tp>
5655inline _LIBCPP_INLINE_VISIBILITY
5656shared_ptr<_Tp>
5657atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5658{
5659 return atomic_exchange(__p, __r);
5660}
5661
5662template <class _Tp>
5663bool
5664atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5665{
Marshall Clow4201ee82016-05-18 17:50:13 +00005666 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005667 __sp_mut& __m = __get_sp_mut(__p);
5668 __m.lock();
5669 if (__p->__owner_equivalent(*__v))
5670 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005671 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005672 *__p = __w;
5673 __m.unlock();
5674 return true;
5675 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005676 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005677 *__v = *__p;
5678 __m.unlock();
5679 return false;
5680}
5681
5682template <class _Tp>
5683inline _LIBCPP_INLINE_VISIBILITY
5684bool
5685atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5686{
5687 return atomic_compare_exchange_strong(__p, __v, __w);
5688}
5689
5690template <class _Tp>
5691inline _LIBCPP_INLINE_VISIBILITY
5692bool
5693atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5694 shared_ptr<_Tp> __w, memory_order, memory_order)
5695{
5696 return atomic_compare_exchange_strong(__p, __v, __w);
5697}
5698
5699template <class _Tp>
5700inline _LIBCPP_INLINE_VISIBILITY
5701bool
5702atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5703 shared_ptr<_Tp> __w, memory_order, memory_order)
5704{
5705 return atomic_compare_exchange_weak(__p, __v, __w);
5706}
5707
Eric Fiselier9b492672016-06-18 02:12:53 +00005708#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005709
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005710//enum class
Howard Hinnant8331b762013-03-06 23:30:19 +00005711struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005712{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005713 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005714 {
5715 relaxed,
5716 preferred,
5717 strict
5718 };
5719
Howard Hinnant49e145e2012-10-30 19:06:59 +00005720 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005721
Howard Hinnant756c69b2010-09-22 16:48:34 +00005722 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005723 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005724 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005725 operator int() const {return __v_;}
5726};
5727
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005728_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5729_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5730_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
5731_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5732_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005733
5734template <class _Tp>
5735inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005736_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005737undeclare_reachable(_Tp* __p)
5738{
5739 return static_cast<_Tp*>(__undeclare_reachable(__p));
5740}
5741
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005742_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005743
Marshall Clow8982dcd2015-07-13 20:04:56 +00005744// --- Helper for container swap --
5745template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005746inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005747void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5748#if _LIBCPP_STD_VER >= 14
5749 _NOEXCEPT
5750#else
5751 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5752#endif
5753{
5754 __swap_allocator(__a1, __a2,
5755 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5756}
5757
5758template <typename _Alloc>
5759_LIBCPP_INLINE_VISIBILITY
5760void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5761#if _LIBCPP_STD_VER >= 14
5762 _NOEXCEPT
5763#else
5764 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5765#endif
5766{
5767 using _VSTD::swap;
5768 swap(__a1, __a2);
5769}
5770
5771template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005772inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005773void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5774
Marshall Clowff91de82015-08-18 19:51:37 +00005775template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005776struct __noexcept_move_assign_container : public integral_constant<bool,
5777 _Traits::propagate_on_container_move_assignment::value
5778#if _LIBCPP_STD_VER > 14
5779 || _Traits::is_always_equal::value
5780#else
5781 && is_nothrow_move_assignable<_Alloc>::value
5782#endif
5783 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005784
Marshall Clowa591b9a2016-07-11 21:38:08 +00005785
5786#ifndef _LIBCPP_HAS_NO_VARIADICS
5787template <class _Tp, class _Alloc>
5788struct __temp_value {
5789 typedef allocator_traits<_Alloc> _Traits;
5790
5791 typename aligned_storage<sizeof(_Tp), alignof(_Tp)>::type __v;
5792 _Alloc &__a;
5793
5794 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5795 _Tp & get() { return *__addr(); }
5796
5797 template<class... _Args>
5798 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc)
5799 { _Traits::construct(__a, __addr(), _VSTD::forward<_Args>(__args)...); }
5800
5801 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5802 };
5803#endif
5804
Howard Hinnantc51e1022010-05-11 19:42:16 +00005805_LIBCPP_END_NAMESPACE_STD
5806
5807#endif // _LIBCPP_MEMORY