blob: 3055a3ad5c28f749013df8b6a332d35dbb879d67 [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;
Marshall Clow78dbe462016-11-14 18:22:19 +0000167template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000168
169template <class InputIterator, class ForwardIterator>
170ForwardIterator
171uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
172
Howard Hinnant719bda32011-05-28 14:41:13 +0000173template <class InputIterator, class Size, class ForwardIterator>
174ForwardIterator
175uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
176
Howard Hinnantc51e1022010-05-11 19:42:16 +0000177template <class ForwardIterator, class T>
178void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
179
180template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000181ForwardIterator
182uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000183
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000184template <class T>
185void destroy_at(T* location);
186
187template <class ForwardIterator>
188 void destroy(ForwardIterator first, ForwardIterator last);
189
190template <class ForwardIterator, class Size>
191 ForwardIterator destroy_n(ForwardIterator first, Size n);
192
193template <class InputIterator, class ForwardIterator>
194 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
195
196template <class InputIterator, class Size, class ForwardIterator>
197 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
198
199template <class ForwardIterator>
200 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
201
202template <class ForwardIterator, class Size>
203 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
204
205template <class ForwardIterator>
206 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
207
208template <class ForwardIterator, class Size>
209 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
210
Howard Hinnantc51e1022010-05-11 19:42:16 +0000211template <class Y> struct auto_ptr_ref {};
212
213template<class X>
214class auto_ptr
215{
216public:
217 typedef X element_type;
218
219 explicit auto_ptr(X* p =0) throw();
220 auto_ptr(auto_ptr&) throw();
221 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
222 auto_ptr& operator=(auto_ptr&) throw();
223 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
224 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
225 ~auto_ptr() throw();
226
227 typename add_lvalue_reference<X>::type operator*() const throw();
228 X* operator->() const throw();
229 X* get() const throw();
230 X* release() throw();
231 void reset(X* p =0) throw();
232
233 auto_ptr(auto_ptr_ref<X>) throw();
234 template<class Y> operator auto_ptr_ref<Y>() throw();
235 template<class Y> operator auto_ptr<Y>() throw();
236};
237
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000238template <class T>
239struct default_delete
240{
Howard Hinnant719bda32011-05-28 14:41:13 +0000241 constexpr default_delete() noexcept = default;
242 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000243
Howard Hinnant719bda32011-05-28 14:41:13 +0000244 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000245};
246
247template <class T>
248struct default_delete<T[]>
249{
Howard Hinnant719bda32011-05-28 14:41:13 +0000250 constexpr default_delete() noexcept = default;
251 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000252 template <class U> void operator()(U*) const = delete;
253};
254
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000255template <class T, class D = default_delete<T>>
256class unique_ptr
257{
258public:
259 typedef see below pointer;
260 typedef T element_type;
261 typedef D deleter_type;
262
263 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000264 constexpr unique_ptr() noexcept;
265 explicit unique_ptr(pointer p) noexcept;
266 unique_ptr(pointer p, see below d1) noexcept;
267 unique_ptr(pointer p, see below d2) noexcept;
268 unique_ptr(unique_ptr&& u) noexcept;
269 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000270 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000271 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000272 template <class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000273 unique_ptr(auto_ptr<U>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000274
275 // destructor
276 ~unique_ptr();
277
278 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000279 unique_ptr& operator=(unique_ptr&& u) noexcept;
280 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
281 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000282
283 // observers
284 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000285 pointer operator->() const noexcept;
286 pointer get() const noexcept;
287 deleter_type& get_deleter() noexcept;
288 const deleter_type& get_deleter() const noexcept;
289 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000290
291 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000292 pointer release() noexcept;
293 void reset(pointer p = pointer()) noexcept;
294 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000295};
296
297template <class T, class D>
298class unique_ptr<T[], D>
299{
300public:
301 typedef implementation-defined pointer;
302 typedef T element_type;
303 typedef D deleter_type;
304
305 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000306 constexpr unique_ptr() noexcept;
307 explicit unique_ptr(pointer p) noexcept;
308 unique_ptr(pointer p, see below d) noexcept;
309 unique_ptr(pointer p, see below d) noexcept;
310 unique_ptr(unique_ptr&& u) noexcept;
311 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000312
313 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000314 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000315
316 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000317 unique_ptr& operator=(unique_ptr&& u) noexcept;
318 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000319
320 // observers
321 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000322 pointer get() const noexcept;
323 deleter_type& get_deleter() noexcept;
324 const deleter_type& get_deleter() const noexcept;
325 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000326
327 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000328 pointer release() noexcept;
329 void reset(pointer p = pointer()) noexcept;
330 void reset(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000331 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000332 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000333};
334
335template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000336 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000337
338template <class T1, class D1, class T2, class D2>
339 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
340template <class T1, class D1, class T2, class D2>
341 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
342template <class T1, class D1, class T2, class D2>
343 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
344template <class T1, class D1, class T2, class D2>
345 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
346template <class T1, class D1, class T2, class D2>
347 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
348template <class T1, class D1, class T2, class D2>
349 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
350
Howard Hinnant719bda32011-05-28 14:41:13 +0000351template <class T, class D>
352 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
353template <class T, class D>
354 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
355template <class T, class D>
356 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
357template <class T, class D>
358 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
359
360template <class T, class D>
361 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
362template <class T, class D>
363 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
364template <class T, class D>
365 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
366template <class T, class D>
367 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
368template <class T, class D>
369 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
370template <class T, class D>
371 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
372template <class T, class D>
373 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
374template <class T, class D>
375 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
376
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000377class bad_weak_ptr
378 : public std::exception
379{
Howard Hinnant719bda32011-05-28 14:41:13 +0000380 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000381};
382
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000383template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
384template<class T> unique_ptr<T> make_unique(size_t n); // C++14
385template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
386
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000387template<class T>
388class shared_ptr
389{
390public:
391 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000392 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000393
394 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000395 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000396 template<class Y> explicit shared_ptr(Y* p);
397 template<class Y, class D> shared_ptr(Y* p, D d);
398 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
399 template <class D> shared_ptr(nullptr_t p, D d);
400 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000401 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
402 shared_ptr(const shared_ptr& r) noexcept;
403 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
404 shared_ptr(shared_ptr&& r) noexcept;
405 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000406 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
407 template<class Y> shared_ptr(auto_ptr<Y>&& r);
408 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
409 shared_ptr(nullptr_t) : shared_ptr() { }
410
411 // destructor:
412 ~shared_ptr();
413
414 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000415 shared_ptr& operator=(const shared_ptr& r) noexcept;
416 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
417 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000418 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
419 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r);
420 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
421
422 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000423 void swap(shared_ptr& r) noexcept;
424 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000425 template<class Y> void reset(Y* p);
426 template<class Y, class D> void reset(Y* p, D d);
427 template<class Y, class D, class A> void reset(Y* p, D d, A a);
428
Howard Hinnant719bda32011-05-28 14:41:13 +0000429 // observers:
430 T* get() const noexcept;
431 T& operator*() const noexcept;
432 T* operator->() const noexcept;
433 long use_count() const noexcept;
434 bool unique() const noexcept;
435 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000436 template<class U> bool owner_before(shared_ptr<U> const& b) const;
437 template<class U> bool owner_before(weak_ptr<U> const& b) const;
438};
439
440// shared_ptr comparisons:
441template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000442 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000443template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000444 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000445template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000446 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000447template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000448 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000449template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000450 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000451template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000452 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
453
454template <class T>
455 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
456template <class T>
457 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
458template <class T>
459 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
460template <class T>
461 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
462template <class T>
463 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
464template <class T>
465bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
466template <class T>
467 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
468template <class T>
469 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
470template <class T>
471 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
472template <class T>
473 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
474template <class T>
475 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
476template <class T>
477 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000478
479// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000480template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000481
482// shared_ptr casts:
483template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000484 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000485template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000486 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000487template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000488 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000489
490// shared_ptr I/O:
491template<class E, class T, class Y>
492 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
493
494// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000495template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000496
497template<class T, class... Args>
498 shared_ptr<T> make_shared(Args&&... args);
499template<class T, class A, class... Args>
500 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
501
502template<class T>
503class weak_ptr
504{
505public:
506 typedef T element_type;
507
508 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000509 constexpr weak_ptr() noexcept;
510 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
511 weak_ptr(weak_ptr const& r) noexcept;
512 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000513 weak_ptr(weak_ptr&& r) noexcept; // C++14
514 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000515
516 // destructor
517 ~weak_ptr();
518
519 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000520 weak_ptr& operator=(weak_ptr const& r) noexcept;
521 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
522 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000523 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
524 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000525
526 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000527 void swap(weak_ptr& r) noexcept;
528 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000529
530 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000531 long use_count() const noexcept;
532 bool expired() const noexcept;
533 shared_ptr<T> lock() const noexcept;
Marshall Clow495e4a62013-09-03 14:37:50 +0000534 template<class U> bool owner_before(shared_ptr<U> const& b) const;
535 template<class U> bool owner_before(weak_ptr<U> const& b) const;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000536};
537
538// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000539template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000540
541// class owner_less:
542template<class T> struct owner_less;
543
544template<class T>
545struct owner_less<shared_ptr<T>>
546 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
547{
548 typedef bool result_type;
549 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const;
550 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
551 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
552};
553
554template<class T>
555struct owner_less<weak_ptr<T>>
556 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
557{
558 typedef bool result_type;
559 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const;
560 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
561 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
562};
563
564template<class T>
565class enable_shared_from_this
566{
567protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000568 constexpr enable_shared_from_this() noexcept;
569 enable_shared_from_this(enable_shared_from_this const&) noexcept;
570 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000571 ~enable_shared_from_this();
572public:
573 shared_ptr<T> shared_from_this();
574 shared_ptr<T const> shared_from_this() const;
575};
576
577template<class T>
578 bool atomic_is_lock_free(const shared_ptr<T>* p);
579template<class T>
580 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
581template<class T>
582 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
583template<class T>
584 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
585template<class T>
586 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
587template<class T>
588 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
589template<class T>
590 shared_ptr<T>
591 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
592template<class T>
593 bool
594 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
595template<class T>
596 bool
597 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
598template<class T>
599 bool
600 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
601 shared_ptr<T> w, memory_order success,
602 memory_order failure);
603template<class T>
604 bool
605 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
606 shared_ptr<T> w, memory_order success,
607 memory_order failure);
608// Hash support
609template <class T> struct hash;
610template <class T, class D> struct hash<unique_ptr<T, D> >;
611template <class T> struct hash<shared_ptr<T> >;
612
613// Pointer safety
614enum class pointer_safety { relaxed, preferred, strict };
615void declare_reachable(void *p);
616template <class T> T *undeclare_reachable(T *p);
617void declare_no_pointers(char *p, size_t n);
618void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000619pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000620
Howard Hinnantc51e1022010-05-11 19:42:16 +0000621void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
622
623} // std
624
625*/
626
627#include <__config>
628#include <type_traits>
629#include <typeinfo>
630#include <cstddef>
631#include <cstdint>
632#include <new>
633#include <utility>
634#include <limits>
635#include <iterator>
636#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000637#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000638#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000639#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000640#include <cstring>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000641
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000642#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000643# include <atomic>
644#endif
645
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000646#include <__undef_min_max>
647
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000648#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000649#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000650#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000651
652_LIBCPP_BEGIN_NAMESPACE_STD
653
Eric Fiselier89659d12015-07-07 00:27:16 +0000654template <class _ValueType>
655inline _LIBCPP_ALWAYS_INLINE
656_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
657#if !defined(_LIBCPP_HAS_NO_THREADS) && \
658 defined(__ATOMIC_RELAXED) && \
659 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
660 return __atomic_load_n(__value, __ATOMIC_RELAXED);
661#else
662 return *__value;
663#endif
664}
665
Kuba Breckade9d6792016-09-04 09:55:12 +0000666template <class _ValueType>
667inline _LIBCPP_ALWAYS_INLINE
668_ValueType __libcpp_acquire_load(_ValueType const* __value) {
669#if !defined(_LIBCPP_HAS_NO_THREADS) && \
670 defined(__ATOMIC_ACQUIRE) && \
671 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
672 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
673#else
674 return *__value;
675#endif
676}
677
Marshall Clow78dbe462016-11-14 18:22:19 +0000678// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000679
Howard Hinnantc51e1022010-05-11 19:42:16 +0000680template <class _Tp> class allocator;
681
682template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000683class _LIBCPP_TEMPLATE_VIS allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000684{
685public:
686 typedef void* pointer;
687 typedef const void* const_pointer;
688 typedef void value_type;
689
690 template <class _Up> struct rebind {typedef allocator<_Up> other;};
691};
692
Howard Hinnant9f771052012-01-19 23:15:22 +0000693template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000694class _LIBCPP_TEMPLATE_VIS allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000695{
696public:
697 typedef const void* pointer;
698 typedef const void* const_pointer;
699 typedef const void value_type;
700
701 template <class _Up> struct rebind {typedef allocator<_Up> other;};
702};
703
Howard Hinnantc51e1022010-05-11 19:42:16 +0000704// pointer_traits
705
706template <class _Tp>
707struct __has_element_type
708{
709private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000710 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000711 template <class _Up> static __two __test(...);
712 template <class _Up> static char __test(typename _Up::element_type* = 0);
713public:
714 static const bool value = sizeof(__test<_Tp>(0)) == 1;
715};
716
717template <class _Ptr, bool = __has_element_type<_Ptr>::value>
718struct __pointer_traits_element_type;
719
720template <class _Ptr>
721struct __pointer_traits_element_type<_Ptr, true>
722{
723 typedef typename _Ptr::element_type type;
724};
725
726#ifndef _LIBCPP_HAS_NO_VARIADICS
727
728template <template <class, class...> class _Sp, class _Tp, class ..._Args>
729struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
730{
731 typedef typename _Sp<_Tp, _Args...>::element_type type;
732};
733
734template <template <class, class...> class _Sp, class _Tp, class ..._Args>
735struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
736{
737 typedef _Tp type;
738};
739
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000740#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000741
742template <template <class> class _Sp, class _Tp>
743struct __pointer_traits_element_type<_Sp<_Tp>, true>
744{
745 typedef typename _Sp<_Tp>::element_type type;
746};
747
748template <template <class> class _Sp, class _Tp>
749struct __pointer_traits_element_type<_Sp<_Tp>, false>
750{
751 typedef _Tp type;
752};
753
754template <template <class, class> class _Sp, class _Tp, class _A0>
755struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
756{
757 typedef typename _Sp<_Tp, _A0>::element_type type;
758};
759
760template <template <class, class> class _Sp, class _Tp, class _A0>
761struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
762{
763 typedef _Tp type;
764};
765
766template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
767struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
768{
769 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
770};
771
772template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
773struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
774{
775 typedef _Tp type;
776};
777
778template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
779 class _A1, class _A2>
780struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
781{
782 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
783};
784
785template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
786 class _A1, class _A2>
787struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
788{
789 typedef _Tp type;
790};
791
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000792#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000793
794template <class _Tp>
795struct __has_difference_type
796{
797private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000798 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000799 template <class _Up> static __two __test(...);
800 template <class _Up> static char __test(typename _Up::difference_type* = 0);
801public:
802 static const bool value = sizeof(__test<_Tp>(0)) == 1;
803};
804
805template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
806struct __pointer_traits_difference_type
807{
808 typedef ptrdiff_t type;
809};
810
811template <class _Ptr>
812struct __pointer_traits_difference_type<_Ptr, true>
813{
814 typedef typename _Ptr::difference_type type;
815};
816
817template <class _Tp, class _Up>
818struct __has_rebind
819{
820private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000821 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000822 template <class _Xp> static __two __test(...);
823 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
824public:
825 static const bool value = sizeof(__test<_Tp>(0)) == 1;
826};
827
828template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
829struct __pointer_traits_rebind
830{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000831#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000832 typedef typename _Tp::template rebind<_Up> type;
833#else
834 typedef typename _Tp::template rebind<_Up>::other type;
835#endif
836};
837
838#ifndef _LIBCPP_HAS_NO_VARIADICS
839
840template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
841struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
842{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000843#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000844 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
845#else
846 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
847#endif
848};
849
850template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
851struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
852{
853 typedef _Sp<_Up, _Args...> type;
854};
855
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000856#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000857
858template <template <class> class _Sp, class _Tp, class _Up>
859struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
860{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000861#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862 typedef typename _Sp<_Tp>::template rebind<_Up> type;
863#else
864 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
865#endif
866};
867
868template <template <class> class _Sp, class _Tp, class _Up>
869struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
870{
871 typedef _Sp<_Up> type;
872};
873
874template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
875struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
876{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000877#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000878 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
879#else
880 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
881#endif
882};
883
884template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
885struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
886{
887 typedef _Sp<_Up, _A0> type;
888};
889
890template <template <class, class, class> class _Sp, class _Tp, class _A0,
891 class _A1, class _Up>
892struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
893{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000894#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000895 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
896#else
897 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
898#endif
899};
900
901template <template <class, class, class> class _Sp, class _Tp, class _A0,
902 class _A1, class _Up>
903struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
904{
905 typedef _Sp<_Up, _A0, _A1> type;
906};
907
908template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
909 class _A1, class _A2, class _Up>
910struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
911{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000912#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000913 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
914#else
915 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
916#endif
917};
918
919template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
920 class _A1, class _A2, class _Up>
921struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
922{
923 typedef _Sp<_Up, _A0, _A1, _A2> type;
924};
925
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000926#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000927
928template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000929struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000930{
931 typedef _Ptr pointer;
932 typedef typename __pointer_traits_element_type<pointer>::type element_type;
933 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
934
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000935#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000936 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000937#else
938 template <class _Up> struct rebind
939 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000940#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000941
942private:
943 struct __nat {};
944public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000945 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946 static pointer pointer_to(typename conditional<is_void<element_type>::value,
947 __nat, element_type>::type& __r)
948 {return pointer::pointer_to(__r);}
949};
950
951template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000952struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000953{
954 typedef _Tp* pointer;
955 typedef _Tp element_type;
956 typedef ptrdiff_t difference_type;
957
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000958#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000959 template <class _Up> using rebind = _Up*;
960#else
961 template <class _Up> struct rebind {typedef _Up* other;};
962#endif
963
964private:
965 struct __nat {};
966public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000969 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000970 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971};
972
Eric Fiselierae3ab842015-08-23 02:56:05 +0000973template <class _From, class _To>
974struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000975#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000976 typedef typename pointer_traits<_From>::template rebind<_To> type;
977#else
978 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
979#endif
980};
981
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982// allocator_traits
983
984namespace __has_pointer_type_imp
985{
Howard Hinnant6e260172013-09-21 01:45:05 +0000986 template <class _Up> static __two __test(...);
987 template <class _Up> static char __test(typename _Up::pointer* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988}
989
990template <class _Tp>
991struct __has_pointer_type
Howard Hinnant6e260172013-09-21 01:45:05 +0000992 : public integral_constant<bool, sizeof(__has_pointer_type_imp::__test<_Tp>(0)) == 1>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000993{
994};
995
996namespace __pointer_type_imp
997{
998
999template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1000struct __pointer_type
1001{
1002 typedef typename _Dp::pointer type;
1003};
1004
1005template <class _Tp, class _Dp>
1006struct __pointer_type<_Tp, _Dp, false>
1007{
1008 typedef _Tp* type;
1009};
1010
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001011} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001012
1013template <class _Tp, class _Dp>
1014struct __pointer_type
1015{
1016 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
1017};
1018
1019template <class _Tp>
1020struct __has_const_pointer
1021{
1022private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001023 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001024 template <class _Up> static __two __test(...);
1025 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
1026public:
1027 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1028};
1029
1030template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1031struct __const_pointer
1032{
1033 typedef typename _Alloc::const_pointer type;
1034};
1035
1036template <class _Tp, class _Ptr, class _Alloc>
1037struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1038{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001039#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1041#else
1042 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1043#endif
1044};
1045
1046template <class _Tp>
1047struct __has_void_pointer
1048{
1049private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001050 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051 template <class _Up> static __two __test(...);
1052 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1053public:
1054 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1055};
1056
1057template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1058struct __void_pointer
1059{
1060 typedef typename _Alloc::void_pointer type;
1061};
1062
1063template <class _Ptr, class _Alloc>
1064struct __void_pointer<_Ptr, _Alloc, false>
1065{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001066#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1068#else
1069 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1070#endif
1071};
1072
1073template <class _Tp>
1074struct __has_const_void_pointer
1075{
1076private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001077 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078 template <class _Up> static __two __test(...);
1079 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1080public:
1081 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1082};
1083
1084template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1085struct __const_void_pointer
1086{
1087 typedef typename _Alloc::const_void_pointer type;
1088};
1089
1090template <class _Ptr, class _Alloc>
1091struct __const_void_pointer<_Ptr, _Alloc, false>
1092{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001093#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1095#else
1096 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1097#endif
1098};
1099
Howard Hinnantc834c512011-11-29 18:15:50 +00001100template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001102_Tp*
1103__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104{
1105 return __p;
1106}
1107
1108template <class _Pointer>
1109inline _LIBCPP_INLINE_VISIBILITY
1110typename pointer_traits<_Pointer>::element_type*
Howard Hinnant719bda32011-05-28 14:41:13 +00001111__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001112{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001113 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114}
1115
1116template <class _Tp>
1117struct __has_size_type
1118{
1119private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001120 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121 template <class _Up> static __two __test(...);
1122 template <class _Up> static char __test(typename _Up::size_type* = 0);
1123public:
1124 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1125};
1126
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001127template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001128struct __size_type
1129{
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001130 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131};
1132
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001133template <class _Alloc, class _DiffType>
1134struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001135{
1136 typedef typename _Alloc::size_type type;
1137};
1138
1139template <class _Tp>
1140struct __has_propagate_on_container_copy_assignment
1141{
1142private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001143 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144 template <class _Up> static __two __test(...);
1145 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1146public:
1147 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1148};
1149
1150template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1151struct __propagate_on_container_copy_assignment
1152{
1153 typedef false_type type;
1154};
1155
1156template <class _Alloc>
1157struct __propagate_on_container_copy_assignment<_Alloc, true>
1158{
1159 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1160};
1161
1162template <class _Tp>
1163struct __has_propagate_on_container_move_assignment
1164{
1165private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001166 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167 template <class _Up> static __two __test(...);
1168 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1169public:
1170 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1171};
1172
1173template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1174struct __propagate_on_container_move_assignment
1175{
1176 typedef false_type type;
1177};
1178
1179template <class _Alloc>
1180struct __propagate_on_container_move_assignment<_Alloc, true>
1181{
1182 typedef typename _Alloc::propagate_on_container_move_assignment type;
1183};
1184
1185template <class _Tp>
1186struct __has_propagate_on_container_swap
1187{
1188private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001189 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190 template <class _Up> static __two __test(...);
1191 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1192public:
1193 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1194};
1195
1196template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1197struct __propagate_on_container_swap
1198{
1199 typedef false_type type;
1200};
1201
1202template <class _Alloc>
1203struct __propagate_on_container_swap<_Alloc, true>
1204{
1205 typedef typename _Alloc::propagate_on_container_swap type;
1206};
1207
Marshall Clow0b587562015-06-02 16:34:03 +00001208template <class _Tp>
1209struct __has_is_always_equal
1210{
1211private:
1212 struct __two {char __lx; char __lxx;};
1213 template <class _Up> static __two __test(...);
1214 template <class _Up> static char __test(typename _Up::is_always_equal* = 0);
1215public:
1216 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1217};
1218
1219template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1220struct __is_always_equal
1221{
1222 typedef typename _VSTD::is_empty<_Alloc>::type type;
1223};
1224
1225template <class _Alloc>
1226struct __is_always_equal<_Alloc, true>
1227{
1228 typedef typename _Alloc::is_always_equal type;
1229};
1230
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1232struct __has_rebind_other
1233{
1234private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001235 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236 template <class _Xp> static __two __test(...);
1237 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1238public:
1239 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1240};
1241
1242template <class _Tp, class _Up>
1243struct __has_rebind_other<_Tp, _Up, false>
1244{
1245 static const bool value = false;
1246};
1247
1248template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1249struct __allocator_traits_rebind
1250{
1251 typedef typename _Tp::template rebind<_Up>::other type;
1252};
1253
1254#ifndef _LIBCPP_HAS_NO_VARIADICS
1255
1256template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1257struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1258{
1259 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1260};
1261
1262template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1263struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1264{
1265 typedef _Alloc<_Up, _Args...> type;
1266};
1267
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001268#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001269
1270template <template <class> class _Alloc, class _Tp, class _Up>
1271struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1272{
1273 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1274};
1275
1276template <template <class> class _Alloc, class _Tp, class _Up>
1277struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1278{
1279 typedef _Alloc<_Up> type;
1280};
1281
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1283struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1284{
1285 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1286};
1287
1288template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1289struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1290{
1291 typedef _Alloc<_Up, _A0> type;
1292};
1293
Howard Hinnantc51e1022010-05-11 19:42:16 +00001294template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1295 class _A1, class _Up>
1296struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1297{
1298 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1299};
1300
1301template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1302 class _A1, class _Up>
1303struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1304{
1305 typedef _Alloc<_Up, _A0, _A1> type;
1306};
1307
Howard Hinnantc51e1022010-05-11 19:42:16 +00001308template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1309 class _A1, class _A2, class _Up>
1310struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1311{
1312 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1313};
1314
1315template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1316 class _A1, class _A2, class _Up>
1317struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1318{
1319 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1320};
1321
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001322#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001324#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325
1326template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1327auto
1328__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1329 -> decltype(__a.allocate(__sz, __p), true_type());
1330
1331template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1332auto
1333__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1334 -> false_type;
1335
1336template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1337struct __has_allocate_hint
1338 : integral_constant<bool,
1339 is_same<
1340 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1341 declval<_SizeType>(),
1342 declval<_ConstVoidPtr>())),
1343 true_type>::value>
1344{
1345};
1346
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001347#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001348
1349template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1350struct __has_allocate_hint
1351 : true_type
1352{
1353};
1354
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001355#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001356
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001357#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358
1359template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001360decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1361 _VSTD::declval<_Args>()...),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001362 true_type())
1363__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1364
1365template <class _Alloc, class _Pointer, class ..._Args>
1366false_type
1367__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1368
1369template <class _Alloc, class _Pointer, class ..._Args>
1370struct __has_construct
1371 : integral_constant<bool,
1372 is_same<
1373 decltype(__has_construct_test(declval<_Alloc>(),
1374 declval<_Pointer>(),
1375 declval<_Args>()...)),
1376 true_type>::value>
1377{
1378};
1379
1380template <class _Alloc, class _Pointer>
1381auto
1382__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1383 -> decltype(__a.destroy(__p), true_type());
1384
1385template <class _Alloc, class _Pointer>
1386auto
1387__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1388 -> false_type;
1389
1390template <class _Alloc, class _Pointer>
1391struct __has_destroy
1392 : integral_constant<bool,
1393 is_same<
1394 decltype(__has_destroy_test(declval<_Alloc>(),
1395 declval<_Pointer>())),
1396 true_type>::value>
1397{
1398};
1399
1400template <class _Alloc>
1401auto
1402__has_max_size_test(_Alloc&& __a)
1403 -> decltype(__a.max_size(), true_type());
1404
1405template <class _Alloc>
1406auto
1407__has_max_size_test(const volatile _Alloc& __a)
1408 -> false_type;
1409
1410template <class _Alloc>
1411struct __has_max_size
1412 : integral_constant<bool,
1413 is_same<
1414 decltype(__has_max_size_test(declval<_Alloc&>())),
1415 true_type>::value>
1416{
1417};
1418
1419template <class _Alloc>
1420auto
1421__has_select_on_container_copy_construction_test(_Alloc&& __a)
1422 -> decltype(__a.select_on_container_copy_construction(), true_type());
1423
1424template <class _Alloc>
1425auto
1426__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1427 -> false_type;
1428
1429template <class _Alloc>
1430struct __has_select_on_container_copy_construction
1431 : integral_constant<bool,
1432 is_same<
1433 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1434 true_type>::value>
1435{
1436};
1437
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001438#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439
1440#ifndef _LIBCPP_HAS_NO_VARIADICS
1441
1442template <class _Alloc, class _Pointer, class ..._Args>
1443struct __has_construct
1444 : false_type
1445{
1446};
1447
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001448#else // _LIBCPP_HAS_NO_VARIADICS
1449
1450template <class _Alloc, class _Pointer, class _Args>
1451struct __has_construct
1452 : false_type
1453{
1454};
1455
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001456#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457
1458template <class _Alloc, class _Pointer>
1459struct __has_destroy
1460 : false_type
1461{
1462};
1463
1464template <class _Alloc>
1465struct __has_max_size
1466 : true_type
1467{
1468};
1469
1470template <class _Alloc>
1471struct __has_select_on_container_copy_construction
1472 : false_type
1473{
1474};
1475
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001476#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001478template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1479struct __alloc_traits_difference_type
1480{
1481 typedef typename pointer_traits<_Ptr>::difference_type type;
1482};
1483
1484template <class _Alloc, class _Ptr>
1485struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1486{
1487 typedef typename _Alloc::difference_type type;
1488};
1489
Howard Hinnantc51e1022010-05-11 19:42:16 +00001490template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001491struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492{
1493 typedef _Alloc allocator_type;
1494 typedef typename allocator_type::value_type value_type;
1495
1496 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1497 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1498 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1499 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1500
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001501 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1502 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001503
1504 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1505 propagate_on_container_copy_assignment;
1506 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1507 propagate_on_container_move_assignment;
1508 typedef typename __propagate_on_container_swap<allocator_type>::type
1509 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001510 typedef typename __is_always_equal<allocator_type>::type
1511 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001512
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001513#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001514 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001515 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001516 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001517#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001518 template <class _Tp> struct rebind_alloc
1519 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1520 template <class _Tp> struct rebind_traits
1521 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001522#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001523
Howard Hinnant756c69b2010-09-22 16:48:34 +00001524 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001525 static pointer allocate(allocator_type& __a, size_type __n)
1526 {return __a.allocate(__n);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001527 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001528 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1529 {return allocate(__a, __n, __hint,
1530 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1531
Howard Hinnant756c69b2010-09-22 16:48:34 +00001532 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001533 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534 {__a.deallocate(__p, __n);}
1535
1536#ifndef _LIBCPP_HAS_NO_VARIADICS
1537 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001538 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001539 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001540 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001541 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001542#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545 static void construct(allocator_type& __a, _Tp* __p)
1546 {
1547 ::new ((void*)__p) _Tp();
1548 }
1549 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001551 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1552 {
1553 ::new ((void*)__p) _Tp(__a0);
1554 }
1555 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001557 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1558 const _A1& __a1)
1559 {
1560 ::new ((void*)__p) _Tp(__a0, __a1);
1561 }
1562 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1565 const _A1& __a1, const _A2& __a2)
1566 {
1567 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1568 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001569#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570
1571 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001572 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 static void destroy(allocator_type& __a, _Tp* __p)
1574 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1575
Howard Hinnant756c69b2010-09-22 16:48:34 +00001576 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001577 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1579
Howard Hinnant756c69b2010-09-22 16:48:34 +00001580 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001581 static allocator_type
1582 select_on_container_copy_construction(const allocator_type& __a)
1583 {return select_on_container_copy_construction(
1584 __has_select_on_container_copy_construction<const allocator_type>(),
1585 __a);}
1586
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001587 template <class _Ptr>
1588 _LIBCPP_INLINE_VISIBILITY
1589 static
1590 void
1591 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1592 {
1593 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1594 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1595 }
1596
1597 template <class _Tp>
1598 _LIBCPP_INLINE_VISIBILITY
1599 static
1600 typename enable_if
1601 <
1602 (is_same<allocator_type, allocator<_Tp> >::value
1603 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1604 is_trivially_move_constructible<_Tp>::value,
1605 void
1606 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001607 __construct_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001608 {
1609 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001610 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001611 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001612 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001613 __begin2 += _Np;
1614 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001615 }
1616
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001617 template <class _Iter, class _Ptr>
1618 _LIBCPP_INLINE_VISIBILITY
1619 static
1620 void
1621 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1622 {
1623 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1624 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1625 }
1626
1627 template <class _Tp>
1628 _LIBCPP_INLINE_VISIBILITY
1629 static
1630 typename enable_if
1631 <
1632 (is_same<allocator_type, allocator<_Tp> >::value
1633 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1634 is_trivially_move_constructible<_Tp>::value,
1635 void
1636 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001637 __construct_range_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001638 {
1639 typedef typename remove_const<_Tp>::type _Vp;
1640 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001641 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001642 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001643 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001644 __begin2 += _Np;
1645 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001646 }
1647
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001648 template <class _Ptr>
1649 _LIBCPP_INLINE_VISIBILITY
1650 static
1651 void
1652 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1653 {
1654 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001655 {
1656 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1657 --__end2;
1658 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001659 }
1660
1661 template <class _Tp>
1662 _LIBCPP_INLINE_VISIBILITY
1663 static
1664 typename enable_if
1665 <
1666 (is_same<allocator_type, allocator<_Tp> >::value
1667 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1668 is_trivially_move_constructible<_Tp>::value,
1669 void
1670 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001671 __construct_backward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001672 {
1673 ptrdiff_t _Np = __end1 - __begin1;
1674 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001675 if (_Np > 0)
1676 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001677 }
1678
Howard Hinnantc51e1022010-05-11 19:42:16 +00001679private:
1680
Howard Hinnant756c69b2010-09-22 16:48:34 +00001681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001682 static pointer allocate(allocator_type& __a, size_type __n,
1683 const_void_pointer __hint, true_type)
1684 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001685 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001686 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001687 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001688 {return __a.allocate(__n);}
1689
1690#ifndef _LIBCPP_HAS_NO_VARIADICS
1691 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001693 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001694 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001696 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001697 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1698 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001699 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001701#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001702
1703 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001704 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001705 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1706 {__a.destroy(__p);}
1707 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001708 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001709 static void __destroy(false_type, allocator_type&, _Tp* __p)
1710 {
1711 __p->~_Tp();
1712 }
1713
Howard Hinnant756c69b2010-09-22 16:48:34 +00001714 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715 static size_type __max_size(true_type, const allocator_type& __a)
1716 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001717 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718 static size_type __max_size(false_type, const allocator_type&)
Marshall Clow33daa162015-10-25 19:34:04 +00001719 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720
Howard Hinnant756c69b2010-09-22 16:48:34 +00001721 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001722 static allocator_type
1723 select_on_container_copy_construction(true_type, const allocator_type& __a)
1724 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001726 static allocator_type
1727 select_on_container_copy_construction(false_type, const allocator_type& __a)
1728 {return __a;}
1729};
1730
Marshall Clow940e01c2015-04-07 05:21:38 +00001731template <class _Traits, class _Tp>
1732struct __rebind_alloc_helper
1733{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001734#ifndef _LIBCPP_CXX03_LANG
Marshall Clow35cde742015-05-10 13:59:45 +00001735 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001736#else
1737 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1738#endif
1739};
1740
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741// allocator
1742
1743template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001744class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001745{
1746public:
1747 typedef size_t size_type;
1748 typedef ptrdiff_t difference_type;
1749 typedef _Tp* pointer;
1750 typedef const _Tp* const_pointer;
1751 typedef _Tp& reference;
1752 typedef const _Tp& const_reference;
1753 typedef _Tp value_type;
1754
Howard Hinnant4931e092011-06-02 21:38:57 +00001755 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001756 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001757
Howard Hinnantc51e1022010-05-11 19:42:16 +00001758 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1759
Howard Hinnant719bda32011-05-28 14:41:13 +00001760 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1761 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1762 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001763 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001764 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001765 {return _VSTD::addressof(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001766 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001767 {
1768 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001769 __throw_length_error("allocator<T>::allocate(size_t n)"
1770 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001771 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
1772 }
Howard Hinnant719bda32011-05-28 14:41:13 +00001773 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Eric Fiselier8122fda2017-01-07 03:01:24 +00001774 {_VSTD::__libcpp_deallocate((void*)__p);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001775 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1776 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001777#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001778 template <class _Up, class... _Args>
1779 _LIBCPP_INLINE_VISIBILITY
1780 void
1781 construct(_Up* __p, _Args&&... __args)
1782 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001783 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001784 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001785#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786 _LIBCPP_INLINE_VISIBILITY
1787 void
1788 construct(pointer __p)
1789 {
1790 ::new((void*)__p) _Tp();
1791 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001792# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001793
Howard Hinnantc51e1022010-05-11 19:42:16 +00001794 template <class _A0>
1795 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001796 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001797 construct(pointer __p, _A0& __a0)
1798 {
1799 ::new((void*)__p) _Tp(__a0);
1800 }
1801 template <class _A0>
1802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001803 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001804 construct(pointer __p, const _A0& __a0)
1805 {
1806 ::new((void*)__p) _Tp(__a0);
1807 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001808# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001809 template <class _A0, class _A1>
1810 _LIBCPP_INLINE_VISIBILITY
1811 void
1812 construct(pointer __p, _A0& __a0, _A1& __a1)
1813 {
1814 ::new((void*)__p) _Tp(__a0, __a1);
1815 }
1816 template <class _A0, class _A1>
1817 _LIBCPP_INLINE_VISIBILITY
1818 void
1819 construct(pointer __p, const _A0& __a0, _A1& __a1)
1820 {
1821 ::new((void*)__p) _Tp(__a0, __a1);
1822 }
1823 template <class _A0, class _A1>
1824 _LIBCPP_INLINE_VISIBILITY
1825 void
1826 construct(pointer __p, _A0& __a0, const _A1& __a1)
1827 {
1828 ::new((void*)__p) _Tp(__a0, __a1);
1829 }
1830 template <class _A0, class _A1>
1831 _LIBCPP_INLINE_VISIBILITY
1832 void
1833 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1834 {
1835 ::new((void*)__p) _Tp(__a0, __a1);
1836 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001837#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1839};
1840
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001841template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001842class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001843{
1844public:
1845 typedef size_t size_type;
1846 typedef ptrdiff_t difference_type;
1847 typedef const _Tp* pointer;
1848 typedef const _Tp* const_pointer;
1849 typedef const _Tp& reference;
1850 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001851 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001852
1853 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001854 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001855
1856 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1857
1858 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1859 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1860 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1861 {return _VSTD::addressof(__x);}
1862 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001863 {
1864 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001865 __throw_length_error("allocator<const T>::allocate(size_t n)"
1866 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001867 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001868 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001869 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Eric Fiselier8122fda2017-01-07 03:01:24 +00001870 {_VSTD::__libcpp_deallocate((void*)__p);}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001871 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1872 {return size_type(~0) / sizeof(_Tp);}
1873#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1874 template <class _Up, class... _Args>
1875 _LIBCPP_INLINE_VISIBILITY
1876 void
1877 construct(_Up* __p, _Args&&... __args)
1878 {
1879 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1880 }
1881#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1882 _LIBCPP_INLINE_VISIBILITY
1883 void
1884 construct(pointer __p)
1885 {
1886 ::new((void*)__p) _Tp();
1887 }
1888# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001889
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001890 template <class _A0>
1891 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001892 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001893 construct(pointer __p, _A0& __a0)
1894 {
1895 ::new((void*)__p) _Tp(__a0);
1896 }
1897 template <class _A0>
1898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001899 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001900 construct(pointer __p, const _A0& __a0)
1901 {
1902 ::new((void*)__p) _Tp(__a0);
1903 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001904# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1905 template <class _A0, class _A1>
1906 _LIBCPP_INLINE_VISIBILITY
1907 void
1908 construct(pointer __p, _A0& __a0, _A1& __a1)
1909 {
1910 ::new((void*)__p) _Tp(__a0, __a1);
1911 }
1912 template <class _A0, class _A1>
1913 _LIBCPP_INLINE_VISIBILITY
1914 void
1915 construct(pointer __p, const _A0& __a0, _A1& __a1)
1916 {
1917 ::new((void*)__p) _Tp(__a0, __a1);
1918 }
1919 template <class _A0, class _A1>
1920 _LIBCPP_INLINE_VISIBILITY
1921 void
1922 construct(pointer __p, _A0& __a0, const _A1& __a1)
1923 {
1924 ::new((void*)__p) _Tp(__a0, __a1);
1925 }
1926 template <class _A0, class _A1>
1927 _LIBCPP_INLINE_VISIBILITY
1928 void
1929 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1930 {
1931 ::new((void*)__p) _Tp(__a0, __a1);
1932 }
1933#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1934 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1935};
1936
Howard Hinnantc51e1022010-05-11 19:42:16 +00001937template <class _Tp, class _Up>
1938inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001939bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940
1941template <class _Tp, class _Up>
1942inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001943bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001944
1945template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001946class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001947 : public iterator<output_iterator_tag,
1948 _Tp, // purposefully not C++03
1949 ptrdiff_t, // purposefully not C++03
1950 _Tp*, // purposefully not C++03
1951 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1952{
1953private:
1954 _OutputIterator __x_;
1955public:
1956 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1957 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1958 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1959 {::new(&*__x_) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001960#if _LIBCPP_STD_VER >= 14
1961 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1962 {::new(&*__x_) _Tp(_VSTD::move(__element)); return *this;}
1963#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001964 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1965 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1966 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001967#if _LIBCPP_STD_VER >= 14
1968 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1969#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001970};
1971
1972template <class _Tp>
1973pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001974get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001975{
1976 pair<_Tp*, ptrdiff_t> __r(0, 0);
1977 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1978 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1979 / sizeof(_Tp);
1980 if (__n > __m)
1981 __n = __m;
1982 while (__n > 0)
1983 {
1984 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1985 if (__r.first)
1986 {
1987 __r.second = __n;
1988 break;
1989 }
1990 __n /= 2;
1991 }
1992 return __r;
1993}
1994
1995template <class _Tp>
1996inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001997void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001998
1999template <class _Tp>
2000struct auto_ptr_ref
2001{
2002 _Tp* __ptr_;
2003};
2004
2005template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002006class _LIBCPP_TEMPLATE_VIS auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002007{
2008private:
2009 _Tp* __ptr_;
2010public:
2011 typedef _Tp element_type;
2012
2013 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2014 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2015 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2016 : __ptr_(__p.release()) {}
2017 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2018 {reset(__p.release()); return *this;}
2019 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2020 {reset(__p.release()); return *this;}
2021 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2022 {reset(__p.__ptr_); return *this;}
2023 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2024
2025 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2026 {return *__ptr_;}
2027 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2028 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2029 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2030 {
2031 _Tp* __t = __ptr_;
2032 __ptr_ = 0;
2033 return __t;
2034 }
2035 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2036 {
2037 if (__ptr_ != __p)
2038 delete __ptr_;
2039 __ptr_ = __p;
2040 }
2041
2042 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2043 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2044 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2045 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2046 {return auto_ptr<_Up>(release());}
2047};
2048
2049template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002050class _LIBCPP_TEMPLATE_VIS auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002051{
2052public:
2053 typedef void element_type;
2054};
2055
Howard Hinnantc51e1022010-05-11 19:42:16 +00002056template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
2057 typename remove_cv<_T2>::type>::value,
Howard Hinnantd0aabf82011-12-11 20:31:33 +00002058 bool = is_empty<_T1>::value
Eric Fiselierf7394302015-06-13 07:08:02 +00002059 && !__libcpp_is_final<_T1>::value,
Howard Hinnantd0aabf82011-12-11 20:31:33 +00002060 bool = is_empty<_T2>::value
Eric Fiselierf7394302015-06-13 07:08:02 +00002061 && !__libcpp_is_final<_T2>::value
Howard Hinnantd0aabf82011-12-11 20:31:33 +00002062 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00002063struct __libcpp_compressed_pair_switch;
2064
2065template <class _T1, class _T2, bool IsSame>
2066struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
2067
2068template <class _T1, class _T2, bool IsSame>
2069struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
2070
2071template <class _T1, class _T2, bool IsSame>
2072struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
2073
2074template <class _T1, class _T2>
2075struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
2076
2077template <class _T1, class _T2>
2078struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
2079
2080template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
2081class __libcpp_compressed_pair_imp;
2082
2083template <class _T1, class _T2>
2084class __libcpp_compressed_pair_imp<_T1, _T2, 0>
2085{
2086private:
2087 _T1 __first_;
2088 _T2 __second_;
2089public:
2090 typedef _T1 _T1_param;
2091 typedef _T2 _T2_param;
2092
2093 typedef typename remove_reference<_T1>::type& _T1_reference;
2094 typedef typename remove_reference<_T2>::type& _T2_reference;
2095
2096 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2097 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2098
Marshall Clowac92bfe2015-07-16 03:05:06 +00002099 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_(), __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002100 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002101 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002102 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002103 : __first_(), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002104 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002105 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002106
Howard Hinnant83b1c052011-12-19 17:58:44 +00002107#ifndef _LIBCPP_HAS_NO_VARIADICS
2108
2109 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2110 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00002111 __libcpp_compressed_pair_imp(piecewise_construct_t,
Howard Hinnant83b1c052011-12-19 17:58:44 +00002112 tuple<_Args1...> __first_args,
2113 tuple<_Args2...> __second_args,
2114 __tuple_indices<_I1...>,
2115 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002116 : __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2117 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002118 {}
2119
2120#endif // _LIBCPP_HAS_NO_VARIADICS
2121
Howard Hinnant719bda32011-05-28 14:41:13 +00002122 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2123 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002124
Howard Hinnant719bda32011-05-28 14:41:13 +00002125 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2126 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002127
2128 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002129 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002130 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002132 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133 swap(__first_, __x.__first_);
2134 swap(__second_, __x.__second_);
2135 }
2136};
2137
2138template <class _T1, class _T2>
2139class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2140 : private _T1
2141{
2142private:
2143 _T2 __second_;
2144public:
2145 typedef _T1 _T1_param;
2146 typedef _T2 _T2_param;
2147
2148 typedef _T1& _T1_reference;
2149 typedef typename remove_reference<_T2>::type& _T2_reference;
2150
2151 typedef const _T1& _T1_const_reference;
2152 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2153
Marshall Clowac92bfe2015-07-16 03:05:06 +00002154 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002155 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002156 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002157 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002158 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002159 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002160 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161
Howard Hinnant83b1c052011-12-19 17:58:44 +00002162#ifndef _LIBCPP_HAS_NO_VARIADICS
2163
2164 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2165 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00002166 __libcpp_compressed_pair_imp(piecewise_construct_t,
Howard Hinnant83b1c052011-12-19 17:58:44 +00002167 tuple<_Args1...> __first_args,
2168 tuple<_Args2...> __second_args,
2169 __tuple_indices<_I1...>,
2170 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002171 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2172 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002173 {}
2174
2175#endif // _LIBCPP_HAS_NO_VARIADICS
2176
Howard Hinnant719bda32011-05-28 14:41:13 +00002177 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2178 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179
Howard Hinnant719bda32011-05-28 14:41:13 +00002180 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2181 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002182
2183 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002184 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002185 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002186 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002187 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002188 swap(__second_, __x.__second_);
2189 }
2190};
2191
2192template <class _T1, class _T2>
2193class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2194 : private _T2
2195{
2196private:
2197 _T1 __first_;
2198public:
2199 typedef _T1 _T1_param;
2200 typedef _T2 _T2_param;
2201
2202 typedef typename remove_reference<_T1>::type& _T1_reference;
2203 typedef _T2& _T2_reference;
2204
2205 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2206 typedef const _T2& _T2_const_reference;
2207
Marshall Clowac92bfe2015-07-16 03:05:06 +00002208 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002209 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002210 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002211 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002212 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002213 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant719bda32011-05-28 14:41:13 +00002214 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2215 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002216 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217
Howard Hinnant83b1c052011-12-19 17:58:44 +00002218#ifndef _LIBCPP_HAS_NO_VARIADICS
2219
2220 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2221 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00002222 __libcpp_compressed_pair_imp(piecewise_construct_t,
Howard Hinnant83b1c052011-12-19 17:58:44 +00002223 tuple<_Args1...> __first_args,
2224 tuple<_Args2...> __second_args,
2225 __tuple_indices<_I1...>,
2226 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002227 : _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...),
2228 __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002229
2230 {}
2231
2232#endif // _LIBCPP_HAS_NO_VARIADICS
2233
Howard Hinnant719bda32011-05-28 14:41:13 +00002234 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2235 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002236
Howard Hinnant719bda32011-05-28 14:41:13 +00002237 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2238 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239
2240 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002241 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002242 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002243 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002244 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245 swap(__first_, __x.__first_);
2246 }
2247};
2248
2249template <class _T1, class _T2>
2250class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2251 : private _T1,
2252 private _T2
2253{
2254public:
2255 typedef _T1 _T1_param;
2256 typedef _T2 _T2_param;
2257
2258 typedef _T1& _T1_reference;
2259 typedef _T2& _T2_reference;
2260
2261 typedef const _T1& _T1_const_reference;
2262 typedef const _T2& _T2_const_reference;
2263
2264 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2265 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002266 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002267 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002268 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002270 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002271
Howard Hinnant83b1c052011-12-19 17:58:44 +00002272#ifndef _LIBCPP_HAS_NO_VARIADICS
2273
2274 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2275 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00002276 __libcpp_compressed_pair_imp(piecewise_construct_t,
Howard Hinnant83b1c052011-12-19 17:58:44 +00002277 tuple<_Args1...> __first_args,
2278 tuple<_Args2...> __second_args,
2279 __tuple_indices<_I1...>,
2280 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002281 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2282 _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002283 {}
2284
2285#endif // _LIBCPP_HAS_NO_VARIADICS
2286
Howard Hinnant719bda32011-05-28 14:41:13 +00002287 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2288 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002289
Howard Hinnant719bda32011-05-28 14:41:13 +00002290 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2291 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002292
Howard Hinnant28b24882011-12-01 20:21:04 +00002293 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant719bda32011-05-28 14:41:13 +00002294 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002295 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296 {
2297 }
2298};
2299
2300template <class _T1, class _T2>
2301class __compressed_pair
2302 : private __libcpp_compressed_pair_imp<_T1, _T2>
2303{
2304 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2305public:
2306 typedef typename base::_T1_param _T1_param;
2307 typedef typename base::_T2_param _T2_param;
2308
2309 typedef typename base::_T1_reference _T1_reference;
2310 typedef typename base::_T2_reference _T2_reference;
2311
2312 typedef typename base::_T1_const_reference _T1_const_reference;
2313 typedef typename base::_T2_const_reference _T2_const_reference;
2314
2315 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002316 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002317 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002318 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002319 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002320 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002321 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002322
Howard Hinnant83b1c052011-12-19 17:58:44 +00002323#ifndef _LIBCPP_HAS_NO_VARIADICS
2324
2325 template <class... _Args1, class... _Args2>
2326 _LIBCPP_INLINE_VISIBILITY
2327 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2328 tuple<_Args2...> __second_args)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002329 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant83b1c052011-12-19 17:58:44 +00002330 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2331 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2332 {}
2333
2334#endif // _LIBCPP_HAS_NO_VARIADICS
2335
Howard Hinnant719bda32011-05-28 14:41:13 +00002336 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2337 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002338
Howard Hinnant719bda32011-05-28 14:41:13 +00002339 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2340 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002341
Howard Hinnant719bda32011-05-28 14:41:13 +00002342 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2343 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002344 __is_nothrow_swappable<_T2>::value)
Howard Hinnant719bda32011-05-28 14:41:13 +00002345 {base::swap(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002346};
2347
2348template <class _T1, class _T2>
2349inline _LIBCPP_INLINE_VISIBILITY
2350void
2351swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant719bda32011-05-28 14:41:13 +00002352 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002353 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002354 {__x.swap(__y);}
2355
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002356// __same_or_less_cv_qualified
2357
2358template <class _Ptr1, class _Ptr2,
2359 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2360 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2361 >::value
2362 >
2363struct __same_or_less_cv_qualified_imp
2364 : is_convertible<_Ptr1, _Ptr2> {};
2365
2366template <class _Ptr1, class _Ptr2>
2367struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2368 : false_type {};
2369
Marshall Clowdf296162014-04-26 05:19:48 +00002370template <class _Ptr1, class _Ptr2, bool = is_pointer<_Ptr1>::value ||
2371 is_same<_Ptr1, _Ptr2>::value ||
2372 __has_element_type<_Ptr1>::value>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002373struct __same_or_less_cv_qualified
2374 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2375
2376template <class _Ptr1, class _Ptr2>
Marshall Clowdf296162014-04-26 05:19:48 +00002377struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, false>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002378 : false_type {};
2379
2380// default_delete
2381
Howard Hinnantc51e1022010-05-11 19:42:16 +00002382template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002383struct _LIBCPP_TEMPLATE_VIS default_delete
Howard Hinnantc51e1022010-05-11 19:42:16 +00002384{
Eric Fiselier07b2b552016-11-18 06:42:17 +00002385#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002386 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2387#else
2388 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2389#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002390 template <class _Up>
2391 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant719bda32011-05-28 14:41:13 +00002392 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2393 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002394 {
2395 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant14015fd2013-04-24 19:44:26 +00002396 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002397 delete __ptr;
2398 }
2399};
2400
2401template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002402struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002403{
Howard Hinnant4500ca52011-12-18 21:19:44 +00002404public:
Eric Fiselier07b2b552016-11-18 06:42:17 +00002405#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002406 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2407#else
2408 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2409#endif
Howard Hinnant4500ca52011-12-18 21:19:44 +00002410 template <class _Up>
2411 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002412 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant4500ca52011-12-18 21:19:44 +00002413 template <class _Up>
2414 _LIBCPP_INLINE_VISIBILITY
2415 void operator() (_Up* __ptr,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002416 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002417 {
2418 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Marshall Clowff0e4132016-02-25 16:50:51 +00002419 static_assert(!is_void<_Tp>::value, "default_delete can not delete void type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002420 delete [] __ptr;
2421 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002422};
2423
2424template <class _Tp, class _Dp = default_delete<_Tp> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002425class _LIBCPP_TEMPLATE_VIS unique_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002426{
2427public:
2428 typedef _Tp element_type;
2429 typedef _Dp deleter_type;
2430 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2431private:
2432 __compressed_pair<pointer, deleter_type> __ptr_;
2433
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002434#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002435 unique_ptr(unique_ptr&);
2436 template <class _Up, class _Ep>
2437 unique_ptr(unique_ptr<_Up, _Ep>&);
2438 unique_ptr& operator=(unique_ptr&);
2439 template <class _Up, class _Ep>
2440 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant74279a52010-09-04 23:28:19 +00002441#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002442
2443 struct __nat {int __for_bool_;};
2444
2445 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2446 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2447public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002448 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002449 : __ptr_(pointer())
2450 {
2451 static_assert(!is_pointer<deleter_type>::value,
2452 "unique_ptr constructed with null function pointer deleter");
2453 }
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002454 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002455 : __ptr_(pointer())
2456 {
2457 static_assert(!is_pointer<deleter_type>::value,
2458 "unique_ptr constructed with null function pointer deleter");
2459 }
Howard Hinnant719bda32011-05-28 14:41:13 +00002460 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002461 : __ptr_(_VSTD::move(__p))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002462 {
2463 static_assert(!is_pointer<deleter_type>::value,
2464 "unique_ptr constructed with null function pointer deleter");
2465 }
2466
Howard Hinnant74279a52010-09-04 23:28:19 +00002467#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2469 is_reference<deleter_type>::value,
2470 deleter_type,
2471 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002472 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002473 : __ptr_(__p, __d) {}
2474
2475 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002476 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002477 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002478 {
2479 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2480 }
Howard Hinnant719bda32011-05-28 14:41:13 +00002481 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002482 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002483 template <class _Up, class _Ep>
2484 _LIBCPP_INLINE_VISIBILITY
2485 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2486 typename enable_if
2487 <
2488 !is_array<_Up>::value &&
2489 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2490 is_convertible<_Ep, deleter_type>::value &&
2491 (
2492 !is_reference<deleter_type>::value ||
2493 is_same<deleter_type, _Ep>::value
2494 ),
2495 __nat
Howard Hinnant719bda32011-05-28 14:41:13 +00002496 >::type = __nat()) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002497 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002498
2499 template <class _Up>
2500 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2501 typename enable_if<
2502 is_convertible<_Up*, _Tp*>::value &&
2503 is_same<_Dp, default_delete<_Tp> >::value,
2504 __nat
Howard Hinnant719bda32011-05-28 14:41:13 +00002505 >::type = __nat()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002506 : __ptr_(__p.release())
2507 {
2508 }
2509
Howard Hinnant719bda32011-05-28 14:41:13 +00002510 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002511 {
2512 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002513 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002514 return *this;
2515 }
2516
2517 template <class _Up, class _Ep>
2518 _LIBCPP_INLINE_VISIBILITY
2519 typename enable_if
2520 <
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002521 !is_array<_Up>::value &&
2522 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2523 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002524 unique_ptr&
2525 >::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002526 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002527 {
2528 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002529 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002530 return *this;
2531 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002532#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002533
2534 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2535 {
2536 return __rv<unique_ptr>(*this);
2537 }
2538
2539 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002540 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002541
2542 template <class _Up, class _Ep>
Eric Fiselier6d8c8ed2015-08-28 05:07:06 +00002543 _LIBCPP_INLINE_VISIBILITY
2544 typename enable_if<
2545 !is_array<_Up>::value &&
2546 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2547 is_assignable<deleter_type&, _Ep&>::value,
2548 unique_ptr&
2549 >::type
2550 operator=(unique_ptr<_Up, _Ep> __u)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002551 {
2552 reset(__u.release());
Eric Fiselier6d8c8ed2015-08-28 05:07:06 +00002553 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002554 return *this;
2555 }
2556
2557 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002558 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002559
2560 template <class _Up>
2561 _LIBCPP_INLINE_VISIBILITY
2562 typename enable_if<
2563 is_convertible<_Up*, _Tp*>::value &&
2564 is_same<_Dp, default_delete<_Tp> >::value,
2565 unique_ptr&
2566 >::type
2567 operator=(auto_ptr<_Up> __p)
2568 {reset(__p.release()); return *this;}
2569
Howard Hinnant74279a52010-09-04 23:28:19 +00002570#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002571 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2572
Howard Hinnant719bda32011-05-28 14:41:13 +00002573 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002574 {
2575 reset();
2576 return *this;
2577 }
2578
2579 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2580 {return *__ptr_.first();}
Howard Hinnant719bda32011-05-28 14:41:13 +00002581 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2582 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2583 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2584 {return __ptr_.second();}
2585 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2586 {return __ptr_.second();}
Howard Hinnant86a291f2012-02-21 21:46:43 +00002587 _LIBCPP_INLINE_VISIBILITY
2588 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2589 {return __ptr_.first() != nullptr;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590
Howard Hinnant719bda32011-05-28 14:41:13 +00002591 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002592 {
2593 pointer __t = __ptr_.first();
2594 __ptr_.first() = pointer();
2595 return __t;
2596 }
2597
Howard Hinnant719bda32011-05-28 14:41:13 +00002598 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002599 {
2600 pointer __tmp = __ptr_.first();
2601 __ptr_.first() = __p;
2602 if (__tmp)
2603 __ptr_.second()(__tmp);
2604 }
2605
Howard Hinnant719bda32011-05-28 14:41:13 +00002606 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2607 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002608};
2609
2610template <class _Tp, class _Dp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002611class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002612{
2613public:
2614 typedef _Tp element_type;
2615 typedef _Dp deleter_type;
2616 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2617private:
2618 __compressed_pair<pointer, deleter_type> __ptr_;
2619
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002620#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002621 unique_ptr(unique_ptr&);
2622 template <class _Up>
2623 unique_ptr(unique_ptr<_Up>&);
2624 unique_ptr& operator=(unique_ptr&);
2625 template <class _Up>
2626 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant74279a52010-09-04 23:28:19 +00002627#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628
2629 struct __nat {int __for_bool_;};
2630
2631 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2632 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2633public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002634 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002635 : __ptr_(pointer())
2636 {
2637 static_assert(!is_pointer<deleter_type>::value,
2638 "unique_ptr constructed with null function pointer deleter");
2639 }
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002640 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002641 : __ptr_(pointer())
2642 {
2643 static_assert(!is_pointer<deleter_type>::value,
2644 "unique_ptr constructed with null function pointer deleter");
2645 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002646#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00002647 template <class _Pp>
2648 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p,
2649 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002650 : __ptr_(__p)
2651 {
2652 static_assert(!is_pointer<deleter_type>::value,
2653 "unique_ptr constructed with null function pointer deleter");
2654 }
2655
Logan Chiend435f8b2014-01-31 09:30:46 +00002656 template <class _Pp>
Howard Hinnantc834c512011-11-29 18:15:50 +00002657 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantc51e1022010-05-11 19:42:16 +00002658 is_reference<deleter_type>::value,
2659 deleter_type,
Logan Chiend435f8b2014-01-31 09:30:46 +00002660 typename add_lvalue_reference<const deleter_type>::type>::type __d,
2661 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002662 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002663 : __ptr_(__p, __d) {}
2664
2665 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2666 is_reference<deleter_type>::value,
2667 deleter_type,
2668 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002669 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002670 : __ptr_(pointer(), __d) {}
2671
Logan Chiend435f8b2014-01-31 09:30:46 +00002672 template <class _Pp>
2673 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p,
2674 typename remove_reference<deleter_type>::type&& __d,
2675 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002676 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002677 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678 {
2679 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2680 }
2681
2682 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002683 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002684 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002685 {
2686 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2687 }
2688
Howard Hinnant719bda32011-05-28 14:41:13 +00002689 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002690 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691
Howard Hinnant719bda32011-05-28 14:41:13 +00002692 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002693 {
2694 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002695 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002696 return *this;
2697 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002698
2699 template <class _Up, class _Ep>
2700 _LIBCPP_INLINE_VISIBILITY
2701 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2702 typename enable_if
2703 <
2704 is_array<_Up>::value &&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002705 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant4500ca52011-12-18 21:19:44 +00002706 && is_convertible<_Ep, deleter_type>::value &&
2707 (
2708 !is_reference<deleter_type>::value ||
2709 is_same<deleter_type, _Ep>::value
2710 ),
2711 __nat
2712 >::type = __nat()
2713 ) _NOEXCEPT
2714 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2715
2716
2717 template <class _Up, class _Ep>
2718 _LIBCPP_INLINE_VISIBILITY
2719 typename enable_if
2720 <
2721 is_array<_Up>::value &&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002722 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2723 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant4500ca52011-12-18 21:19:44 +00002724 unique_ptr&
2725 >::type
2726 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2727 {
2728 reset(__u.release());
2729 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2730 return *this;
2731 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002732#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002733
2734 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2735 : __ptr_(__p)
2736 {
2737 static_assert(!is_pointer<deleter_type>::value,
2738 "unique_ptr constructed with null function pointer deleter");
2739 }
2740
2741 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002742 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002743
2744 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002745 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002746
2747 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2748 {
2749 return __rv<unique_ptr>(*this);
2750 }
2751
2752 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002753 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754
2755 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2756 {
2757 reset(__u->release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002758 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002759 return *this;
2760 }
2761
Howard Hinnant74279a52010-09-04 23:28:19 +00002762#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002763 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2764
Howard Hinnant719bda32011-05-28 14:41:13 +00002765 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002766 {
2767 reset();
2768 return *this;
2769 }
2770
2771 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2772 {return __ptr_.first()[__i];}
Howard Hinnant719bda32011-05-28 14:41:13 +00002773 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2774 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2775 {return __ptr_.second();}
2776 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2777 {return __ptr_.second();}
Howard Hinnant86a291f2012-02-21 21:46:43 +00002778 _LIBCPP_INLINE_VISIBILITY
2779 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2780 {return __ptr_.first() != nullptr;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781
Howard Hinnant719bda32011-05-28 14:41:13 +00002782 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002783 {
2784 pointer __t = __ptr_.first();
2785 __ptr_.first() = pointer();
2786 return __t;
2787 }
2788
Logan Chiend435f8b2014-01-31 09:30:46 +00002789 template <class _Pp>
2790 _LIBCPP_INLINE_VISIBILITY
2791 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, void>::type
2792 reset(_Pp __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793 {
2794 pointer __tmp = __ptr_.first();
2795 __ptr_.first() = __p;
2796 if (__tmp)
2797 __ptr_.second()(__tmp);
2798 }
Marshall Clowff0e4132016-02-25 16:50:51 +00002799 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t = nullptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002800 {
2801 pointer __tmp = __ptr_.first();
2802 __ptr_.first() = nullptr;
2803 if (__tmp)
2804 __ptr_.second()(__tmp);
2805 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002806
2807 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2808private:
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002809
Howard Hinnant74279a52010-09-04 23:28:19 +00002810#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002811 template <class _Up>
2812 explicit unique_ptr(_Up);
2813 template <class _Up>
2814 unique_ptr(_Up __u,
2815 typename conditional<
2816 is_reference<deleter_type>::value,
2817 deleter_type,
2818 typename add_lvalue_reference<const deleter_type>::type>::type,
2819 typename enable_if
2820 <
2821 is_convertible<_Up, pointer>::value,
2822 __nat
2823 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00002824#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002825};
2826
2827template <class _Tp, class _Dp>
2828inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002829typename enable_if<
2830 __is_swappable<_Dp>::value,
2831 void
2832>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002833swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002834
2835template <class _T1, class _D1, class _T2, class _D2>
2836inline _LIBCPP_INLINE_VISIBILITY
2837bool
2838operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2839
2840template <class _T1, class _D1, class _T2, class _D2>
2841inline _LIBCPP_INLINE_VISIBILITY
2842bool
2843operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2844
2845template <class _T1, class _D1, class _T2, class _D2>
2846inline _LIBCPP_INLINE_VISIBILITY
2847bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002848operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2849{
2850 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2851 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002852 typedef typename common_type<_P1, _P2>::type _Vp;
2853 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002854}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002855
2856template <class _T1, class _D1, class _T2, class _D2>
2857inline _LIBCPP_INLINE_VISIBILITY
2858bool
2859operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2860
2861template <class _T1, class _D1, class _T2, class _D2>
2862inline _LIBCPP_INLINE_VISIBILITY
2863bool
2864operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2865
2866template <class _T1, class _D1, class _T2, class _D2>
2867inline _LIBCPP_INLINE_VISIBILITY
2868bool
2869operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2870
Howard Hinnantb17caf92012-02-21 21:02:58 +00002871template <class _T1, class _D1>
2872inline _LIBCPP_INLINE_VISIBILITY
2873bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002874operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002875{
2876 return !__x;
2877}
2878
2879template <class _T1, class _D1>
2880inline _LIBCPP_INLINE_VISIBILITY
2881bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002882operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002883{
2884 return !__x;
2885}
2886
2887template <class _T1, class _D1>
2888inline _LIBCPP_INLINE_VISIBILITY
2889bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002890operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002891{
2892 return static_cast<bool>(__x);
2893}
2894
2895template <class _T1, class _D1>
2896inline _LIBCPP_INLINE_VISIBILITY
2897bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002898operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002899{
2900 return static_cast<bool>(__x);
2901}
2902
2903template <class _T1, class _D1>
2904inline _LIBCPP_INLINE_VISIBILITY
2905bool
2906operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2907{
2908 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2909 return less<_P1>()(__x.get(), nullptr);
2910}
2911
2912template <class _T1, class _D1>
2913inline _LIBCPP_INLINE_VISIBILITY
2914bool
2915operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2916{
2917 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2918 return less<_P1>()(nullptr, __x.get());
2919}
2920
2921template <class _T1, class _D1>
2922inline _LIBCPP_INLINE_VISIBILITY
2923bool
2924operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2925{
2926 return nullptr < __x;
2927}
2928
2929template <class _T1, class _D1>
2930inline _LIBCPP_INLINE_VISIBILITY
2931bool
2932operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2933{
2934 return __x < nullptr;
2935}
2936
2937template <class _T1, class _D1>
2938inline _LIBCPP_INLINE_VISIBILITY
2939bool
2940operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2941{
2942 return !(nullptr < __x);
2943}
2944
2945template <class _T1, class _D1>
2946inline _LIBCPP_INLINE_VISIBILITY
2947bool
2948operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2949{
2950 return !(__x < nullptr);
2951}
2952
2953template <class _T1, class _D1>
2954inline _LIBCPP_INLINE_VISIBILITY
2955bool
2956operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2957{
2958 return !(__x < nullptr);
2959}
2960
2961template <class _T1, class _D1>
2962inline _LIBCPP_INLINE_VISIBILITY
2963bool
2964operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2965{
2966 return !(nullptr < __x);
2967}
2968
Howard Hinnant9e028f12012-05-01 15:37:54 +00002969#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2970
2971template <class _Tp, class _Dp>
2972inline _LIBCPP_INLINE_VISIBILITY
2973unique_ptr<_Tp, _Dp>
2974move(unique_ptr<_Tp, _Dp>& __t)
2975{
2976 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
2977}
2978
2979#endif
2980
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002981#if _LIBCPP_STD_VER > 11
2982
2983template<class _Tp>
2984struct __unique_if
2985{
2986 typedef unique_ptr<_Tp> __unique_single;
2987};
2988
2989template<class _Tp>
2990struct __unique_if<_Tp[]>
2991{
2992 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
2993};
2994
2995template<class _Tp, size_t _Np>
2996struct __unique_if<_Tp[_Np]>
2997{
2998 typedef void __unique_array_known_bound;
2999};
3000
3001template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003002inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003003typename __unique_if<_Tp>::__unique_single
3004make_unique(_Args&&... __args)
3005{
3006 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3007}
3008
3009template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003010inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003011typename __unique_if<_Tp>::__unique_array_unknown_bound
3012make_unique(size_t __n)
3013{
3014 typedef typename remove_extent<_Tp>::type _Up;
3015 return unique_ptr<_Tp>(new _Up[__n]());
3016}
3017
3018template<class _Tp, class... _Args>
3019 typename __unique_if<_Tp>::__unique_array_known_bound
3020 make_unique(_Args&&...) = delete;
3021
3022#endif // _LIBCPP_STD_VER > 11
3023
Howard Hinnant6a855442013-07-03 17:39:28 +00003024template <class _Size>
3025inline _LIBCPP_INLINE_VISIBILITY
3026_Size
3027__loadword(const void* __p)
3028{
3029 _Size __r;
3030 std::memcpy(&__r, __p, sizeof(__r));
3031 return __r;
3032}
3033
Howard Hinnantf1973402011-12-10 20:28:56 +00003034// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
3035// is 64 bits. This is because cityhash64 uses 64bit x 64bit
3036// multiplication, which can be very slow on 32-bit systems.
Howard Hinnantad71c232011-12-05 00:08:45 +00003037template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantf1973402011-12-10 20:28:56 +00003038struct __murmur2_or_cityhash;
Howard Hinnantad71c232011-12-05 00:08:45 +00003039
3040template <class _Size>
Howard Hinnantf1973402011-12-10 20:28:56 +00003041struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnantad71c232011-12-05 00:08:45 +00003042{
3043 _Size operator()(const void* __key, _Size __len);
3044};
3045
Howard Hinnantf1973402011-12-10 20:28:56 +00003046// murmur2
Howard Hinnantad71c232011-12-05 00:08:45 +00003047template <class _Size>
3048_Size
Marshall Clow91c7f642016-01-11 19:27:10 +00003049__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantad71c232011-12-05 00:08:45 +00003050{
3051 const _Size __m = 0x5bd1e995;
3052 const _Size __r = 24;
3053 _Size __h = __len;
3054 const unsigned char* __data = static_cast<const unsigned char*>(__key);
3055 for (; __len >= 4; __data += 4, __len -= 4)
3056 {
Howard Hinnant6a855442013-07-03 17:39:28 +00003057 _Size __k = __loadword<_Size>(__data);
Howard Hinnantad71c232011-12-05 00:08:45 +00003058 __k *= __m;
3059 __k ^= __k >> __r;
3060 __k *= __m;
3061 __h *= __m;
3062 __h ^= __k;
3063 }
3064 switch (__len)
3065 {
3066 case 3:
3067 __h ^= __data[2] << 16;
3068 case 2:
3069 __h ^= __data[1] << 8;
3070 case 1:
3071 __h ^= __data[0];
3072 __h *= __m;
3073 }
3074 __h ^= __h >> 13;
3075 __h *= __m;
3076 __h ^= __h >> 15;
3077 return __h;
3078}
3079
3080template <class _Size>
Howard Hinnantf1973402011-12-10 20:28:56 +00003081struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnantad71c232011-12-05 00:08:45 +00003082{
3083 _Size operator()(const void* __key, _Size __len);
Howard Hinnantf1973402011-12-10 20:28:56 +00003084
3085 private:
3086 // Some primes between 2^63 and 2^64.
3087 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
3088 static const _Size __k1 = 0xb492b66fbe98f273ULL;
3089 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
3090 static const _Size __k3 = 0xc949d7c7509e6557ULL;
3091
3092 static _Size __rotate(_Size __val, int __shift) {
3093 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
3094 }
3095
3096 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
3097 return (__val >> __shift) | (__val << (64 - __shift));
3098 }
3099
3100 static _Size __shift_mix(_Size __val) {
3101 return __val ^ (__val >> 47);
3102 }
3103
3104 static _Size __hash_len_16(_Size __u, _Size __v) {
3105 const _Size __mul = 0x9ddfea08eb382d69ULL;
3106 _Size __a = (__u ^ __v) * __mul;
3107 __a ^= (__a >> 47);
3108 _Size __b = (__v ^ __a) * __mul;
3109 __b ^= (__b >> 47);
3110 __b *= __mul;
3111 return __b;
3112 }
3113
3114 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
3115 if (__len > 8) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003116 const _Size __a = __loadword<_Size>(__s);
3117 const _Size __b = __loadword<_Size>(__s + __len - 8);
Howard Hinnantf1973402011-12-10 20:28:56 +00003118 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
3119 }
3120 if (__len >= 4) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003121 const uint32_t __a = __loadword<uint32_t>(__s);
3122 const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
Howard Hinnantf1973402011-12-10 20:28:56 +00003123 return __hash_len_16(__len + (__a << 3), __b);
3124 }
3125 if (__len > 0) {
3126 const unsigned char __a = __s[0];
3127 const unsigned char __b = __s[__len >> 1];
3128 const unsigned char __c = __s[__len - 1];
3129 const uint32_t __y = static_cast<uint32_t>(__a) +
3130 (static_cast<uint32_t>(__b) << 8);
3131 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3132 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3133 }
3134 return __k2;
3135 }
3136
3137 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003138 const _Size __a = __loadword<_Size>(__s) * __k1;
3139 const _Size __b = __loadword<_Size>(__s + 8);
3140 const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;
3141 const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;
Howard Hinnantf1973402011-12-10 20:28:56 +00003142 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3143 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3144 }
3145
3146 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3147 // Callers do best to use "random-looking" values for a and b.
3148 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3149 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3150 __a += __w;
3151 __b = __rotate(__b + __a + __z, 21);
3152 const _Size __c = __a;
3153 __a += __x;
3154 __a += __y;
3155 __b += __rotate(__a, 44);
3156 return pair<_Size, _Size>(__a + __z, __b + __c);
3157 }
3158
3159 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3160 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3161 const char* __s, _Size __a, _Size __b) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003162 return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),
3163 __loadword<_Size>(__s + 8),
3164 __loadword<_Size>(__s + 16),
3165 __loadword<_Size>(__s + 24),
Howard Hinnantf1973402011-12-10 20:28:56 +00003166 __a,
3167 __b);
3168 }
3169
3170 // Return an 8-byte hash for 33 to 64 bytes.
3171 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003172 _Size __z = __loadword<_Size>(__s + 24);
3173 _Size __a = __loadword<_Size>(__s) +
3174 (__len + __loadword<_Size>(__s + __len - 16)) * __k0;
Howard Hinnantf1973402011-12-10 20:28:56 +00003175 _Size __b = __rotate(__a + __z, 52);
3176 _Size __c = __rotate(__a, 37);
Howard Hinnant6a855442013-07-03 17:39:28 +00003177 __a += __loadword<_Size>(__s + 8);
Howard Hinnantf1973402011-12-10 20:28:56 +00003178 __c += __rotate(__a, 7);
Howard Hinnant6a855442013-07-03 17:39:28 +00003179 __a += __loadword<_Size>(__s + 16);
Howard Hinnantf1973402011-12-10 20:28:56 +00003180 _Size __vf = __a + __z;
3181 _Size __vs = __b + __rotate(__a, 31) + __c;
Howard Hinnant6a855442013-07-03 17:39:28 +00003182 __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);
3183 __z += __loadword<_Size>(__s + __len - 8);
Howard Hinnantf1973402011-12-10 20:28:56 +00003184 __b = __rotate(__a + __z, 52);
3185 __c = __rotate(__a, 37);
Howard Hinnant6a855442013-07-03 17:39:28 +00003186 __a += __loadword<_Size>(__s + __len - 24);
Howard Hinnantf1973402011-12-10 20:28:56 +00003187 __c += __rotate(__a, 7);
Howard Hinnant6a855442013-07-03 17:39:28 +00003188 __a += __loadword<_Size>(__s + __len - 16);
Howard Hinnantf1973402011-12-10 20:28:56 +00003189 _Size __wf = __a + __z;
3190 _Size __ws = __b + __rotate(__a, 31) + __c;
3191 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3192 return __shift_mix(__r * __k0 + __vs) * __k2;
3193 }
Howard Hinnantad71c232011-12-05 00:08:45 +00003194};
3195
Howard Hinnantf1973402011-12-10 20:28:56 +00003196// cityhash64
Howard Hinnantad71c232011-12-05 00:08:45 +00003197template <class _Size>
3198_Size
Marshall Clow91c7f642016-01-11 19:27:10 +00003199__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantad71c232011-12-05 00:08:45 +00003200{
Howard Hinnantf1973402011-12-10 20:28:56 +00003201 const char* __s = static_cast<const char*>(__key);
3202 if (__len <= 32) {
3203 if (__len <= 16) {
3204 return __hash_len_0_to_16(__s, __len);
3205 } else {
3206 return __hash_len_17_to_32(__s, __len);
Howard Hinnantad71c232011-12-05 00:08:45 +00003207 }
Howard Hinnantf1973402011-12-10 20:28:56 +00003208 } else if (__len <= 64) {
3209 return __hash_len_33_to_64(__s, __len);
3210 }
3211
3212 // For strings over 64 bytes we hash the end first, and then as we
3213 // loop we keep 56 bytes of state: v, w, x, y, and z.
Howard Hinnant6a855442013-07-03 17:39:28 +00003214 _Size __x = __loadword<_Size>(__s + __len - 40);
3215 _Size __y = __loadword<_Size>(__s + __len - 16) +
3216 __loadword<_Size>(__s + __len - 56);
3217 _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,
3218 __loadword<_Size>(__s + __len - 24));
Howard Hinnantf1973402011-12-10 20:28:56 +00003219 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3220 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
Howard Hinnant6a855442013-07-03 17:39:28 +00003221 __x = __x * __k1 + __loadword<_Size>(__s);
Howard Hinnantf1973402011-12-10 20:28:56 +00003222
3223 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3224 __len = (__len - 1) & ~static_cast<_Size>(63);
3225 do {
Howard Hinnant6a855442013-07-03 17:39:28 +00003226 __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;
3227 __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;
Howard Hinnantf1973402011-12-10 20:28:56 +00003228 __x ^= __w.second;
Howard Hinnant6a855442013-07-03 17:39:28 +00003229 __y += __v.first + __loadword<_Size>(__s + 40);
Howard Hinnantf1973402011-12-10 20:28:56 +00003230 __z = __rotate(__z + __w.first, 33) * __k1;
3231 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3232 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
Howard Hinnant6a855442013-07-03 17:39:28 +00003233 __y + __loadword<_Size>(__s + 16));
Howard Hinnantf1973402011-12-10 20:28:56 +00003234 std::swap(__z, __x);
3235 __s += 64;
3236 __len -= 64;
3237 } while (__len != 0);
3238 return __hash_len_16(
3239 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3240 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnantad71c232011-12-05 00:08:45 +00003241}
3242
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003243template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3244struct __scalar_hash;
3245
3246template <class _Tp>
3247struct __scalar_hash<_Tp, 0>
3248 : public unary_function<_Tp, size_t>
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003249{
Howard Hinnant756c69b2010-09-22 16:48:34 +00003250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003251 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003252 {
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003253 union
3254 {
3255 _Tp __t;
3256 size_t __a;
3257 } __u;
3258 __u.__a = 0;
3259 __u.__t = __v;
3260 return __u.__a;
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003261 }
3262};
3263
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003264template <class _Tp>
3265struct __scalar_hash<_Tp, 1>
3266 : public unary_function<_Tp, size_t>
3267{
3268 _LIBCPP_INLINE_VISIBILITY
3269 size_t operator()(_Tp __v) const _NOEXCEPT
3270 {
3271 union
3272 {
3273 _Tp __t;
3274 size_t __a;
3275 } __u;
3276 __u.__t = __v;
3277 return __u.__a;
3278 }
3279};
3280
3281template <class _Tp>
3282struct __scalar_hash<_Tp, 2>
3283 : public unary_function<_Tp, size_t>
3284{
3285 _LIBCPP_INLINE_VISIBILITY
3286 size_t operator()(_Tp __v) const _NOEXCEPT
3287 {
3288 union
3289 {
3290 _Tp __t;
3291 struct
3292 {
3293 size_t __a;
3294 size_t __b;
Eric Fiseliere012bf22015-07-18 20:40:46 +00003295 } __s;
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003296 } __u;
3297 __u.__t = __v;
Howard Hinnantf1973402011-12-10 20:28:56 +00003298 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003299 }
3300};
3301
3302template <class _Tp>
3303struct __scalar_hash<_Tp, 3>
3304 : public unary_function<_Tp, size_t>
3305{
3306 _LIBCPP_INLINE_VISIBILITY
3307 size_t operator()(_Tp __v) const _NOEXCEPT
3308 {
3309 union
3310 {
3311 _Tp __t;
3312 struct
3313 {
3314 size_t __a;
3315 size_t __b;
3316 size_t __c;
Eric Fiseliere012bf22015-07-18 20:40:46 +00003317 } __s;
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003318 } __u;
3319 __u.__t = __v;
Howard Hinnantf1973402011-12-10 20:28:56 +00003320 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003321 }
3322};
3323
3324template <class _Tp>
3325struct __scalar_hash<_Tp, 4>
3326 : public unary_function<_Tp, size_t>
3327{
3328 _LIBCPP_INLINE_VISIBILITY
3329 size_t operator()(_Tp __v) const _NOEXCEPT
3330 {
3331 union
3332 {
3333 _Tp __t;
3334 struct
3335 {
3336 size_t __a;
3337 size_t __b;
3338 size_t __c;
3339 size_t __d;
Eric Fiseliere012bf22015-07-18 20:40:46 +00003340 } __s;
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003341 } __u;
3342 __u.__t = __v;
Howard Hinnantf1973402011-12-10 20:28:56 +00003343 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003344 }
3345};
3346
Eric Fiselier9a4e3502016-12-02 23:38:31 +00003347_LIBCPP_INLINE_VISIBILITY
3348inline size_t __hash_combine(size_t __lhs, size_t __rhs) _NOEXCEPT {
3349 struct _PairT {
3350 size_t first;
3351 size_t second;
3352 };
3353 typedef __scalar_hash<_PairT> _HashT;
Eric Fiselierfcc2cbe2016-12-02 23:41:18 +00003354 const _PairT __p = {__lhs, __rhs};
Eric Fiselier9a4e3502016-12-02 23:38:31 +00003355 return _HashT()(__p);
3356}
3357
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003358template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003359struct _LIBCPP_TEMPLATE_VIS hash<_Tp*>
Howard Hinnant9fa30202012-07-30 01:40:57 +00003360 : public unary_function<_Tp*, size_t>
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003361{
Howard Hinnant9fa30202012-07-30 01:40:57 +00003362 _LIBCPP_INLINE_VISIBILITY
3363 size_t operator()(_Tp* __v) const _NOEXCEPT
3364 {
3365 union
3366 {
3367 _Tp* __t;
3368 size_t __a;
3369 } __u;
3370 __u.__t = __v;
3371 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
3372 }
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003373};
3374
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003375template <class _Tp, class _Dp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003376struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003377{
3378 typedef unique_ptr<_Tp, _Dp> argument_type;
3379 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003381 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003382 {
3383 typedef typename argument_type::pointer pointer;
3384 return hash<pointer>()(__ptr.get());
3385 }
3386};
3387
Howard Hinnantc51e1022010-05-11 19:42:16 +00003388struct __destruct_n
3389{
3390private:
3391 size_t size;
3392
3393 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003394 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003395 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3396
3397 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003398 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003399 {}
3400
Howard Hinnant719bda32011-05-28 14:41:13 +00003401 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003402 {++size;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003403 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003404 {}
3405
Howard Hinnant719bda32011-05-28 14:41:13 +00003406 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003407 {size = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003408 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003409 {}
3410public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003411 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3412 : size(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003413
3414 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003415 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003416 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003417
3418 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003419 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003420 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003421
3422 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003423 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003424 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003425};
3426
3427template <class _Alloc>
3428class __allocator_destructor
3429{
3430 typedef allocator_traits<_Alloc> __alloc_traits;
3431public:
3432 typedef typename __alloc_traits::pointer pointer;
3433 typedef typename __alloc_traits::size_type size_type;
3434private:
3435 _Alloc& __alloc_;
3436 size_type __s_;
3437public:
3438 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003439 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003440 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003441 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003442 void operator()(pointer __p) _NOEXCEPT
3443 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003444};
3445
3446template <class _InputIterator, class _ForwardIterator>
3447_ForwardIterator
3448uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3449{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003450 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003451#ifndef _LIBCPP_NO_EXCEPTIONS
3452 _ForwardIterator __s = __r;
3453 try
3454 {
3455#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003456 for (; __f != __l; ++__f, (void) ++__r)
3457 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003458#ifndef _LIBCPP_NO_EXCEPTIONS
3459 }
3460 catch (...)
3461 {
3462 for (; __s != __r; ++__s)
3463 __s->~value_type();
3464 throw;
3465 }
3466#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003467 return __r;
3468}
3469
3470template <class _InputIterator, class _Size, class _ForwardIterator>
3471_ForwardIterator
3472uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3473{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003474 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003475#ifndef _LIBCPP_NO_EXCEPTIONS
3476 _ForwardIterator __s = __r;
3477 try
3478 {
3479#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003480 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3481 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003482#ifndef _LIBCPP_NO_EXCEPTIONS
3483 }
3484 catch (...)
3485 {
3486 for (; __s != __r; ++__s)
3487 __s->~value_type();
3488 throw;
3489 }
3490#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003491 return __r;
3492}
3493
3494template <class _ForwardIterator, class _Tp>
3495void
3496uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3497{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003498 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003499#ifndef _LIBCPP_NO_EXCEPTIONS
3500 _ForwardIterator __s = __f;
3501 try
3502 {
3503#endif
3504 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003505 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003506#ifndef _LIBCPP_NO_EXCEPTIONS
3507 }
3508 catch (...)
3509 {
3510 for (; __s != __f; ++__s)
3511 __s->~value_type();
3512 throw;
3513 }
3514#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003515}
3516
3517template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003518_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003519uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3520{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003521 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003522#ifndef _LIBCPP_NO_EXCEPTIONS
3523 _ForwardIterator __s = __f;
3524 try
3525 {
3526#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003527 for (; __n > 0; ++__f, (void) --__n)
3528 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003529#ifndef _LIBCPP_NO_EXCEPTIONS
3530 }
3531 catch (...)
3532 {
3533 for (; __s != __f; ++__s)
3534 __s->~value_type();
3535 throw;
3536 }
3537#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003538 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003539}
3540
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003541#if _LIBCPP_STD_VER > 14
3542
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003543template <class _Tp>
3544inline _LIBCPP_INLINE_VISIBILITY
3545void destroy_at(_Tp* __loc) {
3546 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3547 __loc->~_Tp();
3548}
3549
3550template <class _ForwardIterator>
3551inline _LIBCPP_INLINE_VISIBILITY
3552void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3553 for (; __first != __last; ++__first)
3554 _VSTD::destroy_at(_VSTD::addressof(*__first));
3555}
3556
3557template <class _ForwardIterator, class _Size>
3558inline _LIBCPP_INLINE_VISIBILITY
3559_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3560 for (; __n > 0; (void)++__first, --__n)
3561 _VSTD::destroy_at(_VSTD::addressof(*__first));
3562 return __first;
3563}
3564
Eric Fiselier290c07c2016-10-11 21:13:44 +00003565template <class _ForwardIterator>
3566inline _LIBCPP_INLINE_VISIBILITY
3567void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3568 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3569 auto __idx = __first;
3570#ifndef _LIBCPP_NO_EXCEPTIONS
3571 try {
3572#endif
3573 for (; __idx != __last; ++__idx)
3574 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3575#ifndef _LIBCPP_NO_EXCEPTIONS
3576 } catch (...) {
3577 _VSTD::destroy(__first, __idx);
3578 throw;
3579 }
3580#endif
3581}
3582
3583template <class _ForwardIterator, class _Size>
3584inline _LIBCPP_INLINE_VISIBILITY
3585_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3586 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3587 auto __idx = __first;
3588#ifndef _LIBCPP_NO_EXCEPTIONS
3589 try {
3590#endif
3591 for (; __n > 0; (void)++__idx, --__n)
3592 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3593 return __idx;
3594#ifndef _LIBCPP_NO_EXCEPTIONS
3595 } catch (...) {
3596 _VSTD::destroy(__first, __idx);
3597 throw;
3598 }
3599#endif
3600}
3601
3602
3603template <class _ForwardIterator>
3604inline _LIBCPP_INLINE_VISIBILITY
3605void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3606 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3607 auto __idx = __first;
3608#ifndef _LIBCPP_NO_EXCEPTIONS
3609 try {
3610#endif
3611 for (; __idx != __last; ++__idx)
3612 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3613#ifndef _LIBCPP_NO_EXCEPTIONS
3614 } catch (...) {
3615 _VSTD::destroy(__first, __idx);
3616 throw;
3617 }
3618#endif
3619}
3620
3621template <class _ForwardIterator, class _Size>
3622inline _LIBCPP_INLINE_VISIBILITY
3623_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3624 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3625 auto __idx = __first;
3626#ifndef _LIBCPP_NO_EXCEPTIONS
3627 try {
3628#endif
3629 for (; __n > 0; (void)++__idx, --__n)
3630 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3631 return __idx;
3632#ifndef _LIBCPP_NO_EXCEPTIONS
3633 } catch (...) {
3634 _VSTD::destroy(__first, __idx);
3635 throw;
3636 }
3637#endif
3638}
3639
3640
3641template <class _InputIt, class _ForwardIt>
3642inline _LIBCPP_INLINE_VISIBILITY
3643_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3644 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3645 auto __idx = __first_res;
3646#ifndef _LIBCPP_NO_EXCEPTIONS
3647 try {
3648#endif
3649 for (; __first != __last; (void)++__idx, ++__first)
3650 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3651 return __idx;
3652#ifndef _LIBCPP_NO_EXCEPTIONS
3653 } catch (...) {
3654 _VSTD::destroy(__first_res, __idx);
3655 throw;
3656 }
3657#endif
3658}
3659
3660template <class _InputIt, class _Size, class _ForwardIt>
3661inline _LIBCPP_INLINE_VISIBILITY
3662pair<_InputIt, _ForwardIt>
3663uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3664 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3665 auto __idx = __first_res;
3666#ifndef _LIBCPP_NO_EXCEPTIONS
3667 try {
3668#endif
3669 for (; __n > 0; ++__idx, (void)++__first, --__n)
3670 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3671 return {__first, __idx};
3672#ifndef _LIBCPP_NO_EXCEPTIONS
3673 } catch (...) {
3674 _VSTD::destroy(__first_res, __idx);
3675 throw;
3676 }
3677#endif
3678}
3679
3680
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003681#endif // _LIBCPP_STD_VER > 14
3682
Howard Hinnant756c69b2010-09-22 16:48:34 +00003683class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003684 : public std::exception
3685{
3686public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003687 virtual ~bad_weak_ptr() _NOEXCEPT;
3688 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003689};
3690
Marshall Clow8fea1612016-08-25 15:09:01 +00003691_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
3692void __throw_bad_weak_ptr()
3693{
3694#ifndef _LIBCPP_NO_EXCEPTIONS
3695 throw bad_weak_ptr();
3696#else
3697 _VSTD::abort();
3698#endif
3699}
3700
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003701template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003702
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003703class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003704{
3705 __shared_count(const __shared_count&);
3706 __shared_count& operator=(const __shared_count&);
3707
3708protected:
3709 long __shared_owners_;
3710 virtual ~__shared_count();
3711private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003712 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003713
3714public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003715 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003716 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003717 : __shared_owners_(__refs) {}
3718
Howard Hinnant719bda32011-05-28 14:41:13 +00003719 void __add_shared() _NOEXCEPT;
3720 bool __release_shared() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003721 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003722 long use_count() const _NOEXCEPT {
3723 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3724 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003725};
3726
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003727class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003728 : private __shared_count
3729{
3730 long __shared_weak_owners_;
3731
3732public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003733 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003734 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003735 : __shared_count(__refs),
3736 __shared_weak_owners_(__refs) {}
3737protected:
3738 virtual ~__shared_weak_count();
3739
3740public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003741 void __add_shared() _NOEXCEPT;
3742 void __add_weak() _NOEXCEPT;
3743 void __release_shared() _NOEXCEPT;
3744 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003745 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003746 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3747 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003748
Howard Hinnant807d6332013-02-25 15:50:36 +00003749 // Define the function out only if we build static libc++ without RTTI.
3750 // Otherwise we may break clients who need to compile their projects with
3751 // -fno-rtti and yet link against a libc++.dylib compiled
3752 // without -fno-rtti.
3753#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003754 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003755#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003756private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003757 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003758};
3759
3760template <class _Tp, class _Dp, class _Alloc>
3761class __shared_ptr_pointer
3762 : public __shared_weak_count
3763{
3764 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3765public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003766 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003767 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003768 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003769
Howard Hinnant72f73582010-08-11 17:04:31 +00003770#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003771 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003772#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003773
3774private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003775 virtual void __on_zero_shared() _NOEXCEPT;
3776 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003777};
3778
Howard Hinnant72f73582010-08-11 17:04:31 +00003779#ifndef _LIBCPP_NO_RTTI
3780
Howard Hinnantc51e1022010-05-11 19:42:16 +00003781template <class _Tp, class _Dp, class _Alloc>
3782const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003783__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003784{
Marshall Clowa8dfc942014-11-17 19:05:50 +00003785 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003786}
3787
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003788#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003789
Howard Hinnantc51e1022010-05-11 19:42:16 +00003790template <class _Tp, class _Dp, class _Alloc>
3791void
Howard Hinnant719bda32011-05-28 14:41:13 +00003792__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003793{
3794 __data_.first().second()(__data_.first().first());
3795 __data_.first().second().~_Dp();
3796}
3797
3798template <class _Tp, class _Dp, class _Alloc>
3799void
Howard Hinnant719bda32011-05-28 14:41:13 +00003800__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003801{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003802 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3803 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003804 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3805
Eric Fiselierf8898c82015-02-05 23:01:40 +00003806 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003807 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003808 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003809}
3810
3811template <class _Tp, class _Alloc>
3812class __shared_ptr_emplace
3813 : public __shared_weak_count
3814{
3815 __compressed_pair<_Alloc, _Tp> __data_;
3816public:
3817#ifndef _LIBCPP_HAS_NO_VARIADICS
3818
Howard Hinnant756c69b2010-09-22 16:48:34 +00003819 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003820 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003821 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003822
3823 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003824 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003825 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003826 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3827 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003828
3829#else // _LIBCPP_HAS_NO_VARIADICS
3830
Howard Hinnant756c69b2010-09-22 16:48:34 +00003831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003832 __shared_ptr_emplace(_Alloc __a)
3833 : __data_(__a) {}
3834
3835 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003836 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003837 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3838 : __data_(__a, _Tp(__a0)) {}
3839
3840 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003841 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003842 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3843 : __data_(__a, _Tp(__a0, __a1)) {}
3844
3845 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003847 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3848 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3849
3850#endif // _LIBCPP_HAS_NO_VARIADICS
3851
3852private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003853 virtual void __on_zero_shared() _NOEXCEPT;
3854 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003855public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003857 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003858};
3859
3860template <class _Tp, class _Alloc>
3861void
Howard Hinnant719bda32011-05-28 14:41:13 +00003862__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003863{
3864 __data_.second().~_Tp();
3865}
3866
3867template <class _Tp, class _Alloc>
3868void
Howard Hinnant719bda32011-05-28 14:41:13 +00003869__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003870{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003871 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3872 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003873 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003874 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003875 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003876 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003877}
3878
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003879template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003880
3881template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003882class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003883{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003884public:
3885 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00003886#if _LIBCPP_STD_VER > 14
3887 typedef weak_ptr<_Tp> weak_type;
3888#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003889private:
3890 element_type* __ptr_;
3891 __shared_weak_count* __cntrl_;
3892
3893 struct __nat {int __for_bool_;};
3894public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003895 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003896 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003897 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003898 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003899 template<class _Yp>
3900 explicit shared_ptr(_Yp* __p,
3901 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3902 template<class _Yp, class _Dp>
3903 shared_ptr(_Yp* __p, _Dp __d,
3904 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3905 template<class _Yp, class _Dp, class _Alloc>
3906 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3907 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003908 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3909 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003910 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3911 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003912 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003913 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003914 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003915 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003916 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3917 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003918#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003919 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003920 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003921 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003922 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3923 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003924#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003925 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003926 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003927#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003928 template<class _Yp>
3929 shared_ptr(auto_ptr<_Yp>&& __r,
3930 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003931#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003932 template<class _Yp>
3933 shared_ptr(auto_ptr<_Yp> __r,
3934 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003935#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003936#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003937 template <class _Yp, class _Dp>
3938 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3939 typename enable_if
3940 <
3941 !is_lvalue_reference<_Dp>::value &&
3942 !is_array<_Yp>::value &&
3943 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3944 __nat
3945 >::type = __nat());
3946 template <class _Yp, class _Dp>
3947 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3948 typename enable_if
3949 <
3950 is_lvalue_reference<_Dp>::value &&
3951 !is_array<_Yp>::value &&
3952 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3953 __nat
3954 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003955#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003956 template <class _Yp, class _Dp>
3957 shared_ptr(unique_ptr<_Yp, _Dp>,
3958 typename enable_if
3959 <
3960 !is_lvalue_reference<_Dp>::value &&
3961 !is_array<_Yp>::value &&
3962 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3963 __nat
3964 >::type = __nat());
3965 template <class _Yp, class _Dp>
3966 shared_ptr(unique_ptr<_Yp, _Dp>,
3967 typename enable_if
3968 <
3969 is_lvalue_reference<_Dp>::value &&
3970 !is_array<_Yp>::value &&
3971 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3972 __nat
3973 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003974#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003975
3976 ~shared_ptr();
3977
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003978 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003979 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003980 template<class _Yp>
3981 typename enable_if
3982 <
3983 is_convertible<_Yp*, element_type*>::value,
3984 shared_ptr&
3985 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003986 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003987 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003988#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003990 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003991 template<class _Yp>
3992 typename enable_if
3993 <
3994 is_convertible<_Yp*, element_type*>::value,
3995 shared_ptr<_Tp>&
3996 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003997 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003998 operator=(shared_ptr<_Yp>&& __r);
3999 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00004000 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004001 typename enable_if
4002 <
4003 !is_array<_Yp>::value &&
4004 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004005 shared_ptr
4006 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004007 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00004008#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004009 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00004010 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004011 typename enable_if
4012 <
4013 !is_array<_Yp>::value &&
4014 is_convertible<_Yp*, element_type*>::value,
4015 shared_ptr&
4016 >::type
4017 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004018#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004019 template <class _Yp, class _Dp>
4020 typename enable_if
4021 <
4022 !is_array<_Yp>::value &&
4023 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4024 shared_ptr&
4025 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00004026#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004027 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004028 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00004029#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00004030 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004031 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004032#endif
4033
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004034 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004035 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004036 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004037 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004038 template<class _Yp>
4039 typename enable_if
4040 <
4041 is_convertible<_Yp*, element_type*>::value,
4042 void
4043 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004044 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004045 reset(_Yp* __p);
4046 template<class _Yp, class _Dp>
4047 typename enable_if
4048 <
4049 is_convertible<_Yp*, element_type*>::value,
4050 void
4051 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004052 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004053 reset(_Yp* __p, _Dp __d);
4054 template<class _Yp, class _Dp, class _Alloc>
4055 typename enable_if
4056 <
4057 is_convertible<_Yp*, element_type*>::value,
4058 void
4059 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004060 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004061 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004062
Howard Hinnant756c69b2010-09-22 16:48:34 +00004063 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004064 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004065 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004066 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
4067 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004068 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004069 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004070 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004071 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004072 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004073 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004074 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00004075 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00004076 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004077 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00004078 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00004079 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00004080 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004081 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00004082 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00004083 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00004084 _LIBCPP_INLINE_VISIBILITY
4085 bool
4086 __owner_equivalent(const shared_ptr& __p) const
4087 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004088
Howard Hinnant72f73582010-08-11 17:04:31 +00004089#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004090 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004091 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004092 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004093 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004094#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004095
4096#ifndef _LIBCPP_HAS_NO_VARIADICS
4097
4098 template<class ..._Args>
4099 static
4100 shared_ptr<_Tp>
4101 make_shared(_Args&& ...__args);
4102
4103 template<class _Alloc, class ..._Args>
4104 static
4105 shared_ptr<_Tp>
4106 allocate_shared(const _Alloc& __a, _Args&& ...__args);
4107
4108#else // _LIBCPP_HAS_NO_VARIADICS
4109
4110 static shared_ptr<_Tp> make_shared();
4111
4112 template<class _A0>
4113 static shared_ptr<_Tp> make_shared(_A0&);
4114
4115 template<class _A0, class _A1>
4116 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
4117
4118 template<class _A0, class _A1, class _A2>
4119 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
4120
4121 template<class _Alloc>
4122 static shared_ptr<_Tp>
4123 allocate_shared(const _Alloc& __a);
4124
4125 template<class _Alloc, class _A0>
4126 static shared_ptr<_Tp>
4127 allocate_shared(const _Alloc& __a, _A0& __a0);
4128
4129 template<class _Alloc, class _A0, class _A1>
4130 static shared_ptr<_Tp>
4131 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
4132
4133 template<class _Alloc, class _A0, class _A1, class _A2>
4134 static shared_ptr<_Tp>
4135 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
4136
4137#endif // _LIBCPP_HAS_NO_VARIADICS
4138
4139private:
4140
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004141 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004143 void
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004144 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
4145 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004146 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004147 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00004148 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00004149 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004150 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
4151 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00004152 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004153 }
4154
Howard Hinnant756c69b2010-09-22 16:48:34 +00004155 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004156 void __enable_weak_this(const volatile void*, const volatile void*) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004157
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004158 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
4159 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004160};
4161
4162template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004163inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004164_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004165shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004166 : __ptr_(0),
4167 __cntrl_(0)
4168{
4169}
4170
4171template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004172inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004173_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004174shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004175 : __ptr_(0),
4176 __cntrl_(0)
4177{
4178}
4179
4180template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004181template<class _Yp>
4182shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4183 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004184 : __ptr_(__p)
4185{
4186 unique_ptr<_Yp> __hold(__p);
4187 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4188 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
4189 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004190 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004191}
4192
4193template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004194template<class _Yp, class _Dp>
4195shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4196 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004197 : __ptr_(__p)
4198{
4199#ifndef _LIBCPP_NO_EXCEPTIONS
4200 try
4201 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004202#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004203 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4204 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004205 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004206#ifndef _LIBCPP_NO_EXCEPTIONS
4207 }
4208 catch (...)
4209 {
4210 __d(__p);
4211 throw;
4212 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004213#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004214}
4215
4216template<class _Tp>
4217template<class _Dp>
4218shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4219 : __ptr_(0)
4220{
4221#ifndef _LIBCPP_NO_EXCEPTIONS
4222 try
4223 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004224#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004225 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4226 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4227#ifndef _LIBCPP_NO_EXCEPTIONS
4228 }
4229 catch (...)
4230 {
4231 __d(__p);
4232 throw;
4233 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004234#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004235}
4236
4237template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004238template<class _Yp, class _Dp, class _Alloc>
4239shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4240 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004241 : __ptr_(__p)
4242{
4243#ifndef _LIBCPP_NO_EXCEPTIONS
4244 try
4245 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004246#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004248 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004249 typedef __allocator_destructor<_A2> _D2;
4250 _A2 __a2(__a);
4251 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004252 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4253 _CntrlBlk(__p, __d, __a);
4254 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004255 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004256#ifndef _LIBCPP_NO_EXCEPTIONS
4257 }
4258 catch (...)
4259 {
4260 __d(__p);
4261 throw;
4262 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004263#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004264}
4265
4266template<class _Tp>
4267template<class _Dp, class _Alloc>
4268shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4269 : __ptr_(0)
4270{
4271#ifndef _LIBCPP_NO_EXCEPTIONS
4272 try
4273 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004274#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004275 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004276 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004277 typedef __allocator_destructor<_A2> _D2;
4278 _A2 __a2(__a);
4279 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004280 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4281 _CntrlBlk(__p, __d, __a);
4282 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004283#ifndef _LIBCPP_NO_EXCEPTIONS
4284 }
4285 catch (...)
4286 {
4287 __d(__p);
4288 throw;
4289 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004290#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004291}
4292
4293template<class _Tp>
4294template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004295inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004296shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004297 : __ptr_(__p),
4298 __cntrl_(__r.__cntrl_)
4299{
4300 if (__cntrl_)
4301 __cntrl_->__add_shared();
4302}
4303
4304template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004305inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004306shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004307 : __ptr_(__r.__ptr_),
4308 __cntrl_(__r.__cntrl_)
4309{
4310 if (__cntrl_)
4311 __cntrl_->__add_shared();
4312}
4313
4314template<class _Tp>
4315template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004316inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004317shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4318 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004319 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004320 : __ptr_(__r.__ptr_),
4321 __cntrl_(__r.__cntrl_)
4322{
4323 if (__cntrl_)
4324 __cntrl_->__add_shared();
4325}
4326
Howard Hinnant74279a52010-09-04 23:28:19 +00004327#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004328
4329template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004330inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004331shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004332 : __ptr_(__r.__ptr_),
4333 __cntrl_(__r.__cntrl_)
4334{
4335 __r.__ptr_ = 0;
4336 __r.__cntrl_ = 0;
4337}
4338
4339template<class _Tp>
4340template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004341inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004342shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4343 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004344 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004345 : __ptr_(__r.__ptr_),
4346 __cntrl_(__r.__cntrl_)
4347{
4348 __r.__ptr_ = 0;
4349 __r.__cntrl_ = 0;
4350}
4351
Howard Hinnant74279a52010-09-04 23:28:19 +00004352#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004353
4354template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004355template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004356#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004357shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004358#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004359shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004360#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004361 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004362 : __ptr_(__r.get())
4363{
4364 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4365 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004366 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004367 __r.release();
4368}
4369
4370template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004371template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004372#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004373shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4374#else
4375shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4376#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004377 typename enable_if
4378 <
4379 !is_lvalue_reference<_Dp>::value &&
4380 !is_array<_Yp>::value &&
4381 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4382 __nat
4383 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004384 : __ptr_(__r.get())
4385{
Marshall Clow35cde742015-05-10 13:59:45 +00004386#if _LIBCPP_STD_VER > 11
4387 if (__ptr_ == nullptr)
4388 __cntrl_ = nullptr;
4389 else
4390#endif
4391 {
4392 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4393 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004394 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004395 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004396 __r.release();
4397}
4398
4399template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004400template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004401#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004402shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4403#else
4404shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4405#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004406 typename enable_if
4407 <
4408 is_lvalue_reference<_Dp>::value &&
4409 !is_array<_Yp>::value &&
4410 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4411 __nat
4412 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004413 : __ptr_(__r.get())
4414{
Marshall Clow35cde742015-05-10 13:59:45 +00004415#if _LIBCPP_STD_VER > 11
4416 if (__ptr_ == nullptr)
4417 __cntrl_ = nullptr;
4418 else
4419#endif
4420 {
4421 typedef __shared_ptr_pointer<_Yp*,
4422 reference_wrapper<typename remove_reference<_Dp>::type>,
4423 allocator<_Yp> > _CntrlBlk;
4424 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004425 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004426 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004427 __r.release();
4428}
4429
4430#ifndef _LIBCPP_HAS_NO_VARIADICS
4431
4432template<class _Tp>
4433template<class ..._Args>
4434shared_ptr<_Tp>
4435shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4436{
4437 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4438 typedef allocator<_CntrlBlk> _A2;
4439 typedef __allocator_destructor<_A2> _D2;
4440 _A2 __a2;
4441 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004442 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004443 shared_ptr<_Tp> __r;
4444 __r.__ptr_ = __hold2.get()->get();
4445 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004446 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004447 return __r;
4448}
4449
4450template<class _Tp>
4451template<class _Alloc, class ..._Args>
4452shared_ptr<_Tp>
4453shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4454{
4455 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004456 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004457 typedef __allocator_destructor<_A2> _D2;
4458 _A2 __a2(__a);
4459 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004460 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4461 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004462 shared_ptr<_Tp> __r;
4463 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004464 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004465 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004466 return __r;
4467}
4468
4469#else // _LIBCPP_HAS_NO_VARIADICS
4470
4471template<class _Tp>
4472shared_ptr<_Tp>
4473shared_ptr<_Tp>::make_shared()
4474{
4475 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4476 typedef allocator<_CntrlBlk> _Alloc2;
4477 typedef __allocator_destructor<_Alloc2> _D2;
4478 _Alloc2 __alloc2;
4479 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4480 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4481 shared_ptr<_Tp> __r;
4482 __r.__ptr_ = __hold2.get()->get();
4483 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004484 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004485 return __r;
4486}
4487
4488template<class _Tp>
4489template<class _A0>
4490shared_ptr<_Tp>
4491shared_ptr<_Tp>::make_shared(_A0& __a0)
4492{
4493 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4494 typedef allocator<_CntrlBlk> _Alloc2;
4495 typedef __allocator_destructor<_Alloc2> _D2;
4496 _Alloc2 __alloc2;
4497 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4498 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4499 shared_ptr<_Tp> __r;
4500 __r.__ptr_ = __hold2.get()->get();
4501 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004502 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004503 return __r;
4504}
4505
4506template<class _Tp>
4507template<class _A0, class _A1>
4508shared_ptr<_Tp>
4509shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4510{
4511 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4512 typedef allocator<_CntrlBlk> _Alloc2;
4513 typedef __allocator_destructor<_Alloc2> _D2;
4514 _Alloc2 __alloc2;
4515 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4516 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4517 shared_ptr<_Tp> __r;
4518 __r.__ptr_ = __hold2.get()->get();
4519 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004520 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004521 return __r;
4522}
4523
4524template<class _Tp>
4525template<class _A0, class _A1, class _A2>
4526shared_ptr<_Tp>
4527shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4528{
4529 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4530 typedef allocator<_CntrlBlk> _Alloc2;
4531 typedef __allocator_destructor<_Alloc2> _D2;
4532 _Alloc2 __alloc2;
4533 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4534 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4535 shared_ptr<_Tp> __r;
4536 __r.__ptr_ = __hold2.get()->get();
4537 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004538 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004539 return __r;
4540}
4541
4542template<class _Tp>
4543template<class _Alloc>
4544shared_ptr<_Tp>
4545shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4546{
4547 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004548 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004549 typedef __allocator_destructor<_Alloc2> _D2;
4550 _Alloc2 __alloc2(__a);
4551 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004552 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4553 _CntrlBlk(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004554 shared_ptr<_Tp> __r;
4555 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004556 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004557 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004558 return __r;
4559}
4560
4561template<class _Tp>
4562template<class _Alloc, class _A0>
4563shared_ptr<_Tp>
4564shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4565{
4566 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004567 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004568 typedef __allocator_destructor<_Alloc2> _D2;
4569 _Alloc2 __alloc2(__a);
4570 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004571 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4572 _CntrlBlk(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004573 shared_ptr<_Tp> __r;
4574 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004575 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004576 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004577 return __r;
4578}
4579
4580template<class _Tp>
4581template<class _Alloc, class _A0, class _A1>
4582shared_ptr<_Tp>
4583shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4584{
4585 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004586 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004587 typedef __allocator_destructor<_Alloc2> _D2;
4588 _Alloc2 __alloc2(__a);
4589 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004590 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4591 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004592 shared_ptr<_Tp> __r;
4593 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004594 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004595 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004596 return __r;
4597}
4598
4599template<class _Tp>
4600template<class _Alloc, class _A0, class _A1, class _A2>
4601shared_ptr<_Tp>
4602shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4603{
4604 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004605 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004606 typedef __allocator_destructor<_Alloc2> _D2;
4607 _Alloc2 __alloc2(__a);
4608 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004609 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4610 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004611 shared_ptr<_Tp> __r;
4612 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004613 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004614 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004615 return __r;
4616}
4617
4618#endif // _LIBCPP_HAS_NO_VARIADICS
4619
4620template<class _Tp>
4621shared_ptr<_Tp>::~shared_ptr()
4622{
4623 if (__cntrl_)
4624 __cntrl_->__release_shared();
4625}
4626
4627template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004628inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004629shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004630shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004631{
4632 shared_ptr(__r).swap(*this);
4633 return *this;
4634}
4635
4636template<class _Tp>
4637template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004638inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004639typename enable_if
4640<
4641 is_convertible<_Yp*, _Tp*>::value,
4642 shared_ptr<_Tp>&
4643>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004644shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004645{
4646 shared_ptr(__r).swap(*this);
4647 return *this;
4648}
4649
Howard Hinnant74279a52010-09-04 23:28:19 +00004650#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004651
4652template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004653inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004654shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004655shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004656{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004657 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004658 return *this;
4659}
4660
4661template<class _Tp>
4662template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004663inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004664typename enable_if
4665<
4666 is_convertible<_Yp*, _Tp*>::value,
4667 shared_ptr<_Tp>&
4668>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004669shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4670{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004671 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004672 return *this;
4673}
4674
4675template<class _Tp>
4676template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004677inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004678typename enable_if
4679<
4680 !is_array<_Yp>::value &&
4681 is_convertible<_Yp*, _Tp*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004682 shared_ptr<_Tp>
4683>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004684shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4685{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004686 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004687 return *this;
4688}
4689
4690template<class _Tp>
4691template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004692inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004693typename enable_if
4694<
4695 !is_array<_Yp>::value &&
4696 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4697 shared_ptr<_Tp>&
4698>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004699shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4700{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004701 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004702 return *this;
4703}
4704
Howard Hinnant74279a52010-09-04 23:28:19 +00004705#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004706
4707template<class _Tp>
4708template<class _Yp>
4709inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004710typename enable_if
4711<
4712 !is_array<_Yp>::value &&
4713 is_convertible<_Yp*, _Tp*>::value,
4714 shared_ptr<_Tp>&
4715>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004716shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004717{
4718 shared_ptr(__r).swap(*this);
4719 return *this;
4720}
4721
4722template<class _Tp>
4723template <class _Yp, class _Dp>
4724inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004725typename enable_if
4726<
4727 !is_array<_Yp>::value &&
4728 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4729 shared_ptr<_Tp>&
4730>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004731shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4732{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004733 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004734 return *this;
4735}
4736
Howard Hinnant74279a52010-09-04 23:28:19 +00004737#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004738
4739template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004740inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004741void
Howard Hinnant719bda32011-05-28 14:41:13 +00004742shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004743{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004744 _VSTD::swap(__ptr_, __r.__ptr_);
4745 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004746}
4747
4748template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004749inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004750void
Howard Hinnant719bda32011-05-28 14:41:13 +00004751shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004752{
4753 shared_ptr().swap(*this);
4754}
4755
4756template<class _Tp>
4757template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004758inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004759typename enable_if
4760<
4761 is_convertible<_Yp*, _Tp*>::value,
4762 void
4763>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004764shared_ptr<_Tp>::reset(_Yp* __p)
4765{
4766 shared_ptr(__p).swap(*this);
4767}
4768
4769template<class _Tp>
4770template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004771inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004772typename enable_if
4773<
4774 is_convertible<_Yp*, _Tp*>::value,
4775 void
4776>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004777shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4778{
4779 shared_ptr(__p, __d).swap(*this);
4780}
4781
4782template<class _Tp>
4783template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004784inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004785typename enable_if
4786<
4787 is_convertible<_Yp*, _Tp*>::value,
4788 void
4789>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004790shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4791{
4792 shared_ptr(__p, __d, __a).swap(*this);
4793}
4794
4795#ifndef _LIBCPP_HAS_NO_VARIADICS
4796
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004797template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004798inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004799typename enable_if
4800<
4801 !is_array<_Tp>::value,
4802 shared_ptr<_Tp>
4803>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004804make_shared(_Args&& ...__args)
4805{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004806 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004807}
4808
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004809template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004810inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004811typename enable_if
4812<
4813 !is_array<_Tp>::value,
4814 shared_ptr<_Tp>
4815>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004816allocate_shared(const _Alloc& __a, _Args&& ...__args)
4817{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004818 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004819}
4820
4821#else // _LIBCPP_HAS_NO_VARIADICS
4822
4823template<class _Tp>
4824inline _LIBCPP_INLINE_VISIBILITY
4825shared_ptr<_Tp>
4826make_shared()
4827{
4828 return shared_ptr<_Tp>::make_shared();
4829}
4830
4831template<class _Tp, class _A0>
4832inline _LIBCPP_INLINE_VISIBILITY
4833shared_ptr<_Tp>
4834make_shared(_A0& __a0)
4835{
4836 return shared_ptr<_Tp>::make_shared(__a0);
4837}
4838
4839template<class _Tp, class _A0, class _A1>
4840inline _LIBCPP_INLINE_VISIBILITY
4841shared_ptr<_Tp>
4842make_shared(_A0& __a0, _A1& __a1)
4843{
4844 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4845}
4846
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004847template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004848inline _LIBCPP_INLINE_VISIBILITY
4849shared_ptr<_Tp>
4850make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4851{
4852 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4853}
4854
4855template<class _Tp, class _Alloc>
4856inline _LIBCPP_INLINE_VISIBILITY
4857shared_ptr<_Tp>
4858allocate_shared(const _Alloc& __a)
4859{
4860 return shared_ptr<_Tp>::allocate_shared(__a);
4861}
4862
4863template<class _Tp, class _Alloc, class _A0>
4864inline _LIBCPP_INLINE_VISIBILITY
4865shared_ptr<_Tp>
4866allocate_shared(const _Alloc& __a, _A0& __a0)
4867{
4868 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4869}
4870
4871template<class _Tp, class _Alloc, class _A0, class _A1>
4872inline _LIBCPP_INLINE_VISIBILITY
4873shared_ptr<_Tp>
4874allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4875{
4876 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4877}
4878
4879template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4880inline _LIBCPP_INLINE_VISIBILITY
4881shared_ptr<_Tp>
4882allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4883{
4884 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4885}
4886
4887#endif // _LIBCPP_HAS_NO_VARIADICS
4888
4889template<class _Tp, class _Up>
4890inline _LIBCPP_INLINE_VISIBILITY
4891bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004892operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004893{
4894 return __x.get() == __y.get();
4895}
4896
4897template<class _Tp, class _Up>
4898inline _LIBCPP_INLINE_VISIBILITY
4899bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004900operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004901{
4902 return !(__x == __y);
4903}
4904
4905template<class _Tp, class _Up>
4906inline _LIBCPP_INLINE_VISIBILITY
4907bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004908operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004909{
Eric Fiselierf8898c82015-02-05 23:01:40 +00004910 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4911 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00004912}
4913
4914template<class _Tp, class _Up>
4915inline _LIBCPP_INLINE_VISIBILITY
4916bool
4917operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4918{
4919 return __y < __x;
4920}
4921
4922template<class _Tp, class _Up>
4923inline _LIBCPP_INLINE_VISIBILITY
4924bool
4925operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4926{
4927 return !(__y < __x);
4928}
4929
4930template<class _Tp, class _Up>
4931inline _LIBCPP_INLINE_VISIBILITY
4932bool
4933operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4934{
4935 return !(__x < __y);
4936}
4937
4938template<class _Tp>
4939inline _LIBCPP_INLINE_VISIBILITY
4940bool
4941operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4942{
4943 return !__x;
4944}
4945
4946template<class _Tp>
4947inline _LIBCPP_INLINE_VISIBILITY
4948bool
4949operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4950{
4951 return !__x;
4952}
4953
4954template<class _Tp>
4955inline _LIBCPP_INLINE_VISIBILITY
4956bool
4957operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4958{
4959 return static_cast<bool>(__x);
4960}
4961
4962template<class _Tp>
4963inline _LIBCPP_INLINE_VISIBILITY
4964bool
4965operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4966{
4967 return static_cast<bool>(__x);
4968}
4969
4970template<class _Tp>
4971inline _LIBCPP_INLINE_VISIBILITY
4972bool
4973operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4974{
4975 return less<_Tp*>()(__x.get(), nullptr);
4976}
4977
4978template<class _Tp>
4979inline _LIBCPP_INLINE_VISIBILITY
4980bool
4981operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4982{
4983 return less<_Tp*>()(nullptr, __x.get());
4984}
4985
4986template<class _Tp>
4987inline _LIBCPP_INLINE_VISIBILITY
4988bool
4989operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4990{
4991 return nullptr < __x;
4992}
4993
4994template<class _Tp>
4995inline _LIBCPP_INLINE_VISIBILITY
4996bool
4997operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4998{
4999 return __x < nullptr;
5000}
5001
5002template<class _Tp>
5003inline _LIBCPP_INLINE_VISIBILITY
5004bool
5005operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5006{
5007 return !(nullptr < __x);
5008}
5009
5010template<class _Tp>
5011inline _LIBCPP_INLINE_VISIBILITY
5012bool
5013operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5014{
5015 return !(__x < nullptr);
5016}
5017
5018template<class _Tp>
5019inline _LIBCPP_INLINE_VISIBILITY
5020bool
5021operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5022{
5023 return !(__x < nullptr);
5024}
5025
5026template<class _Tp>
5027inline _LIBCPP_INLINE_VISIBILITY
5028bool
5029operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5030{
5031 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005032}
5033
5034template<class _Tp>
5035inline _LIBCPP_INLINE_VISIBILITY
5036void
Howard Hinnant719bda32011-05-28 14:41:13 +00005037swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005038{
5039 __x.swap(__y);
5040}
5041
5042template<class _Tp, class _Up>
5043inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005044typename enable_if
5045<
5046 !is_array<_Tp>::value && !is_array<_Up>::value,
5047 shared_ptr<_Tp>
5048>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005049static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005050{
5051 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
5052}
5053
5054template<class _Tp, class _Up>
5055inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005056typename enable_if
5057<
5058 !is_array<_Tp>::value && !is_array<_Up>::value,
5059 shared_ptr<_Tp>
5060>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005061dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005062{
5063 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
5064 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
5065}
5066
5067template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005068typename enable_if
5069<
5070 is_array<_Tp>::value == is_array<_Up>::value,
5071 shared_ptr<_Tp>
5072>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005073const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005074{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005075 typedef typename remove_extent<_Tp>::type _RTp;
5076 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00005077}
5078
Howard Hinnant72f73582010-08-11 17:04:31 +00005079#ifndef _LIBCPP_NO_RTTI
5080
Howard Hinnantc51e1022010-05-11 19:42:16 +00005081template<class _Dp, class _Tp>
5082inline _LIBCPP_INLINE_VISIBILITY
5083_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00005084get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005085{
5086 return __p.template __get_deleter<_Dp>();
5087}
5088
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005089#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00005090
Howard Hinnantc51e1022010-05-11 19:42:16 +00005091template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005092class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00005093{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005094public:
5095 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005096private:
5097 element_type* __ptr_;
5098 __shared_weak_count* __cntrl_;
5099
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005100public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005101 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005102 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005103 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00005104 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5105 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005106 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005107 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005108 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00005109 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5110 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005111
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005112#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005113 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005114 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005115 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005116 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5117 _NOEXCEPT;
5118#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005119 ~weak_ptr();
5120
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005122 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005123 template<class _Yp>
5124 typename enable_if
5125 <
5126 is_convertible<_Yp*, element_type*>::value,
5127 weak_ptr&
5128 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005130 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
5131
5132#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5133
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005135 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
5136 template<class _Yp>
5137 typename enable_if
5138 <
5139 is_convertible<_Yp*, element_type*>::value,
5140 weak_ptr&
5141 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005143 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
5144
5145#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5146
5147 template<class _Yp>
5148 typename enable_if
5149 <
5150 is_convertible<_Yp*, element_type*>::value,
5151 weak_ptr&
5152 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005154 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005155
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005157 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005159 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005160
Howard Hinnant756c69b2010-09-22 16:48:34 +00005161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005162 long use_count() const _NOEXCEPT
5163 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005165 bool expired() const _NOEXCEPT
5166 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5167 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005168 template<class _Up>
5169 _LIBCPP_INLINE_VISIBILITY
5170 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00005171 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005172 template<class _Up>
5173 _LIBCPP_INLINE_VISIBILITY
5174 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00005175 {return __cntrl_ < __r.__cntrl_;}
5176
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005177 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
5178 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005179};
5180
5181template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005182inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005183_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005184weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005185 : __ptr_(0),
5186 __cntrl_(0)
5187{
5188}
5189
5190template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005191inline
Howard Hinnant719bda32011-05-28 14:41:13 +00005192weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005193 : __ptr_(__r.__ptr_),
5194 __cntrl_(__r.__cntrl_)
5195{
5196 if (__cntrl_)
5197 __cntrl_->__add_weak();
5198}
5199
5200template<class _Tp>
5201template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005202inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005203weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005204 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005205 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005206 : __ptr_(__r.__ptr_),
5207 __cntrl_(__r.__cntrl_)
5208{
5209 if (__cntrl_)
5210 __cntrl_->__add_weak();
5211}
5212
5213template<class _Tp>
5214template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005215inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005216weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005217 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005218 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005219 : __ptr_(__r.__ptr_),
5220 __cntrl_(__r.__cntrl_)
5221{
5222 if (__cntrl_)
5223 __cntrl_->__add_weak();
5224}
5225
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005226#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5227
5228template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005229inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005230weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5231 : __ptr_(__r.__ptr_),
5232 __cntrl_(__r.__cntrl_)
5233{
5234 __r.__ptr_ = 0;
5235 __r.__cntrl_ = 0;
5236}
5237
5238template<class _Tp>
5239template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005240inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005241weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5242 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5243 _NOEXCEPT
5244 : __ptr_(__r.__ptr_),
5245 __cntrl_(__r.__cntrl_)
5246{
5247 __r.__ptr_ = 0;
5248 __r.__cntrl_ = 0;
5249}
5250
5251#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5252
Howard Hinnantc51e1022010-05-11 19:42:16 +00005253template<class _Tp>
5254weak_ptr<_Tp>::~weak_ptr()
5255{
5256 if (__cntrl_)
5257 __cntrl_->__release_weak();
5258}
5259
5260template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005261inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005262weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005263weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005264{
5265 weak_ptr(__r).swap(*this);
5266 return *this;
5267}
5268
5269template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005270template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005271inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005272typename enable_if
5273<
5274 is_convertible<_Yp*, _Tp*>::value,
5275 weak_ptr<_Tp>&
5276>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005277weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005278{
5279 weak_ptr(__r).swap(*this);
5280 return *this;
5281}
5282
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005283#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5284
5285template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005286inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005287weak_ptr<_Tp>&
5288weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5289{
5290 weak_ptr(_VSTD::move(__r)).swap(*this);
5291 return *this;
5292}
5293
Howard Hinnantc51e1022010-05-11 19:42:16 +00005294template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005295template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005296inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005297typename enable_if
5298<
5299 is_convertible<_Yp*, _Tp*>::value,
5300 weak_ptr<_Tp>&
5301>::type
5302weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5303{
5304 weak_ptr(_VSTD::move(__r)).swap(*this);
5305 return *this;
5306}
5307
5308#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5309
5310template<class _Tp>
5311template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005312inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005313typename enable_if
5314<
5315 is_convertible<_Yp*, _Tp*>::value,
5316 weak_ptr<_Tp>&
5317>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005318weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005319{
5320 weak_ptr(__r).swap(*this);
5321 return *this;
5322}
5323
5324template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005325inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005326void
Howard Hinnant719bda32011-05-28 14:41:13 +00005327weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005328{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005329 _VSTD::swap(__ptr_, __r.__ptr_);
5330 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005331}
5332
5333template<class _Tp>
5334inline _LIBCPP_INLINE_VISIBILITY
5335void
Howard Hinnant719bda32011-05-28 14:41:13 +00005336swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005337{
5338 __x.swap(__y);
5339}
5340
5341template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005342inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005343void
Howard Hinnant719bda32011-05-28 14:41:13 +00005344weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005345{
5346 weak_ptr().swap(*this);
5347}
5348
5349template<class _Tp>
5350template<class _Yp>
5351shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5352 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
5353 : __ptr_(__r.__ptr_),
5354 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5355{
5356 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00005357 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00005358}
5359
5360template<class _Tp>
5361shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005362weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005363{
5364 shared_ptr<_Tp> __r;
5365 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5366 if (__r.__cntrl_)
5367 __r.__ptr_ = __ptr_;
5368 return __r;
5369}
5370
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005371#if _LIBCPP_STD_VER > 14
5372template <class _Tp = void> struct owner_less;
5373#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005374template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005375#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005376
5377template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005378struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005379 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005380{
5381 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005383 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5384 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005386 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5387 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005388 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005389 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5390 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005391};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005392
5393template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005394struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005395 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5396{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005397 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005399 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5400 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005402 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5403 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005405 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5406 {return __x.owner_before(__y);}
5407};
5408
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005409#if _LIBCPP_STD_VER > 14
5410template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005411struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005412{
5413 template <class _Tp, class _Up>
5414 _LIBCPP_INLINE_VISIBILITY
5415 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const
5416 {return __x.owner_before(__y);}
5417 template <class _Tp, class _Up>
5418 _LIBCPP_INLINE_VISIBILITY
5419 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const
5420 {return __x.owner_before(__y);}
5421 template <class _Tp, class _Up>
5422 _LIBCPP_INLINE_VISIBILITY
5423 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const
5424 {return __x.owner_before(__y);}
5425 template <class _Tp, class _Up>
5426 _LIBCPP_INLINE_VISIBILITY
5427 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const
5428 {return __x.owner_before(__y);}
5429 typedef void is_transparent;
5430};
5431#endif
5432
Howard Hinnantc51e1022010-05-11 19:42:16 +00005433template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005434class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005435{
5436 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005437protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005438 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005439 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005440 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005441 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005442 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005443 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5444 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005445 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005446 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005447public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005448 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005449 shared_ptr<_Tp> shared_from_this()
5450 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005451 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005452 shared_ptr<_Tp const> shared_from_this() const
5453 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005454
Eric Fiselier84006862016-06-02 00:15:35 +00005455#if _LIBCPP_STD_VER > 14
5456 _LIBCPP_INLINE_VISIBILITY
5457 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5458 { return __weak_this_; }
5459
5460 _LIBCPP_INLINE_VISIBILITY
5461 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5462 { return __weak_this_; }
5463#endif // _LIBCPP_STD_VER > 14
5464
Howard Hinnantc51e1022010-05-11 19:42:16 +00005465 template <class _Up> friend class shared_ptr;
5466};
5467
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005468template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005469struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005470{
5471 typedef shared_ptr<_Tp> argument_type;
5472 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005473 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005474 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005475 {
5476 return hash<_Tp*>()(__ptr.get());
5477 }
5478};
5479
Howard Hinnantc834c512011-11-29 18:15:50 +00005480template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005481inline _LIBCPP_INLINE_VISIBILITY
5482basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005483operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005484
Eric Fiselier9b492672016-06-18 02:12:53 +00005485
5486#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005487
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005488class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005489{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005490 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005491public:
5492 void lock() _NOEXCEPT;
5493 void unlock() _NOEXCEPT;
5494
5495private:
5496 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5497 __sp_mut(const __sp_mut&);
5498 __sp_mut& operator=(const __sp_mut&);
5499
Howard Hinnant8331b762013-03-06 23:30:19 +00005500 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005501};
5502
Howard Hinnant8331b762013-03-06 23:30:19 +00005503_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005504
5505template <class _Tp>
5506inline _LIBCPP_INLINE_VISIBILITY
5507bool
5508atomic_is_lock_free(const shared_ptr<_Tp>*)
5509{
5510 return false;
5511}
5512
5513template <class _Tp>
5514shared_ptr<_Tp>
5515atomic_load(const shared_ptr<_Tp>* __p)
5516{
5517 __sp_mut& __m = __get_sp_mut(__p);
5518 __m.lock();
5519 shared_ptr<_Tp> __q = *__p;
5520 __m.unlock();
5521 return __q;
5522}
5523
5524template <class _Tp>
5525inline _LIBCPP_INLINE_VISIBILITY
5526shared_ptr<_Tp>
5527atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5528{
5529 return atomic_load(__p);
5530}
5531
5532template <class _Tp>
5533void
5534atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5535{
5536 __sp_mut& __m = __get_sp_mut(__p);
5537 __m.lock();
5538 __p->swap(__r);
5539 __m.unlock();
5540}
5541
5542template <class _Tp>
5543inline _LIBCPP_INLINE_VISIBILITY
5544void
5545atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5546{
5547 atomic_store(__p, __r);
5548}
5549
5550template <class _Tp>
5551shared_ptr<_Tp>
5552atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5553{
5554 __sp_mut& __m = __get_sp_mut(__p);
5555 __m.lock();
5556 __p->swap(__r);
5557 __m.unlock();
5558 return __r;
5559}
5560
5561template <class _Tp>
5562inline _LIBCPP_INLINE_VISIBILITY
5563shared_ptr<_Tp>
5564atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5565{
5566 return atomic_exchange(__p, __r);
5567}
5568
5569template <class _Tp>
5570bool
5571atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5572{
Marshall Clow4201ee82016-05-18 17:50:13 +00005573 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005574 __sp_mut& __m = __get_sp_mut(__p);
5575 __m.lock();
5576 if (__p->__owner_equivalent(*__v))
5577 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005578 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005579 *__p = __w;
5580 __m.unlock();
5581 return true;
5582 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005583 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005584 *__v = *__p;
5585 __m.unlock();
5586 return false;
5587}
5588
5589template <class _Tp>
5590inline _LIBCPP_INLINE_VISIBILITY
5591bool
5592atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5593{
5594 return atomic_compare_exchange_strong(__p, __v, __w);
5595}
5596
5597template <class _Tp>
5598inline _LIBCPP_INLINE_VISIBILITY
5599bool
5600atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5601 shared_ptr<_Tp> __w, memory_order, memory_order)
5602{
5603 return atomic_compare_exchange_strong(__p, __v, __w);
5604}
5605
5606template <class _Tp>
5607inline _LIBCPP_INLINE_VISIBILITY
5608bool
5609atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5610 shared_ptr<_Tp> __w, memory_order, memory_order)
5611{
5612 return atomic_compare_exchange_weak(__p, __v, __w);
5613}
5614
Eric Fiselier9b492672016-06-18 02:12:53 +00005615#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005616
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005617//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005618#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5619# ifndef _LIBCPP_CXX03_LANG
5620enum class pointer_safety : unsigned char {
5621 relaxed,
5622 preferred,
5623 strict
5624};
5625# endif
5626#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005627struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005628{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005629 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005630 {
5631 relaxed,
5632 preferred,
5633 strict
5634 };
5635
Howard Hinnant49e145e2012-10-30 19:06:59 +00005636 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005637
Howard Hinnant756c69b2010-09-22 16:48:34 +00005638 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005639 pointer_safety() : __v_() {}
5640
5641 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005642 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005643 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005644 operator int() const {return __v_;}
5645};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005646#endif
5647
5648#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
5649 defined(_LIBCPP_BUILDING_MEMORY)
5650_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5651#else
5652// This function is only offered in C++03 under ABI v1.
5653# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5654inline _LIBCPP_INLINE_VISIBILITY
5655pointer_safety get_pointer_safety() _NOEXCEPT {
5656 return pointer_safety::relaxed;
5657}
5658# endif
5659#endif
5660
Howard Hinnantc51e1022010-05-11 19:42:16 +00005661
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005662_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5663_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5664_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005665_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005666
5667template <class _Tp>
5668inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005669_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005670undeclare_reachable(_Tp* __p)
5671{
5672 return static_cast<_Tp*>(__undeclare_reachable(__p));
5673}
5674
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005675_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005676
Marshall Clow8982dcd2015-07-13 20:04:56 +00005677// --- Helper for container swap --
5678template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005679inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005680void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5681#if _LIBCPP_STD_VER >= 14
5682 _NOEXCEPT
5683#else
5684 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5685#endif
5686{
5687 __swap_allocator(__a1, __a2,
5688 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5689}
5690
5691template <typename _Alloc>
5692_LIBCPP_INLINE_VISIBILITY
5693void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5694#if _LIBCPP_STD_VER >= 14
5695 _NOEXCEPT
5696#else
5697 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5698#endif
5699{
5700 using _VSTD::swap;
5701 swap(__a1, __a2);
5702}
5703
5704template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005705inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005706void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5707
Marshall Clowff91de82015-08-18 19:51:37 +00005708template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005709struct __noexcept_move_assign_container : public integral_constant<bool,
5710 _Traits::propagate_on_container_move_assignment::value
5711#if _LIBCPP_STD_VER > 14
5712 || _Traits::is_always_equal::value
5713#else
5714 && is_nothrow_move_assignable<_Alloc>::value
5715#endif
5716 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005717
Marshall Clowa591b9a2016-07-11 21:38:08 +00005718
5719#ifndef _LIBCPP_HAS_NO_VARIADICS
5720template <class _Tp, class _Alloc>
5721struct __temp_value {
5722 typedef allocator_traits<_Alloc> _Traits;
5723
5724 typename aligned_storage<sizeof(_Tp), alignof(_Tp)>::type __v;
5725 _Alloc &__a;
5726
5727 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5728 _Tp & get() { return *__addr(); }
5729
5730 template<class... _Args>
5731 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc)
5732 { _Traits::construct(__a, __addr(), _VSTD::forward<_Args>(__args)...); }
5733
5734 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5735 };
5736#endif
5737
Howard Hinnantc51e1022010-05-11 19:42:16 +00005738_LIBCPP_END_NAMESPACE_STD
5739
5740#endif // _LIBCPP_MEMORY