blob: 44a0c34e43e178cab77ba63616e4f83cf23a8bae [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
Marshall Clowb22274f2017-01-24 22:22:33 +0000211template <class Y> struct auto_ptr_ref {}; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212
213template<class X>
Marshall Clowb22274f2017-01-24 22:22:33 +0000214class auto_ptr // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215{
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>
Marshall Clowb22274f2017-01-24 22:22:33 +0000273 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
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);
Marshall Clowb22274f2017-01-24 22:22:33 +0000407 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000408 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);
Marshall Clowb22274f2017-01-24 22:22:33 +0000419 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000420 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;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000436 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
437 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000438};
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 Clow18a7cd52017-04-11 17:08:53 +0000534 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
535 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
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;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000549 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
550 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
551 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000552};
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;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000559 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
560 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
561 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
562};
563
564template <> // Added in C++14
565struct owner_less<void>
566{
567 template <class _Tp, class _Up>
568 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
569 template <class _Tp, class _Up>
570 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
571 template <class _Tp, class _Up>
572 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
573 template <class _Tp, class _Up>
574 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
575
576 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000577};
578
579template<class T>
580class enable_shared_from_this
581{
582protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000583 constexpr enable_shared_from_this() noexcept;
584 enable_shared_from_this(enable_shared_from_this const&) noexcept;
585 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000586 ~enable_shared_from_this();
587public:
588 shared_ptr<T> shared_from_this();
589 shared_ptr<T const> shared_from_this() const;
590};
591
592template<class T>
593 bool atomic_is_lock_free(const shared_ptr<T>* p);
594template<class T>
595 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
596template<class T>
597 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
598template<class T>
599 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
600template<class T>
601 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
602template<class T>
603 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
604template<class T>
605 shared_ptr<T>
606 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
607template<class T>
608 bool
609 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
610template<class T>
611 bool
612 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
613template<class T>
614 bool
615 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
616 shared_ptr<T> w, memory_order success,
617 memory_order failure);
618template<class T>
619 bool
620 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
621 shared_ptr<T> w, memory_order success,
622 memory_order failure);
623// Hash support
624template <class T> struct hash;
625template <class T, class D> struct hash<unique_ptr<T, D> >;
626template <class T> struct hash<shared_ptr<T> >;
627
628// Pointer safety
629enum class pointer_safety { relaxed, preferred, strict };
630void declare_reachable(void *p);
631template <class T> T *undeclare_reachable(T *p);
632void declare_no_pointers(char *p, size_t n);
633void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000634pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000635
Howard Hinnantc51e1022010-05-11 19:42:16 +0000636void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
637
638} // std
639
640*/
641
642#include <__config>
643#include <type_traits>
644#include <typeinfo>
645#include <cstddef>
646#include <cstdint>
647#include <new>
648#include <utility>
649#include <limits>
650#include <iterator>
651#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000652#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000653#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000654#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000655#include <cstring>
Eric Fiselier9d355982017-04-12 23:45:53 +0000656#include <cassert>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000657#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000658# include <atomic>
659#endif
660
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000661#include <__undef_min_max>
662
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000663#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000664#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000665#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000666
667_LIBCPP_BEGIN_NAMESPACE_STD
668
Eric Fiselier89659d12015-07-07 00:27:16 +0000669template <class _ValueType>
670inline _LIBCPP_ALWAYS_INLINE
671_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
672#if !defined(_LIBCPP_HAS_NO_THREADS) && \
673 defined(__ATOMIC_RELAXED) && \
674 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
675 return __atomic_load_n(__value, __ATOMIC_RELAXED);
676#else
677 return *__value;
678#endif
679}
680
Kuba Breckade9d6792016-09-04 09:55:12 +0000681template <class _ValueType>
682inline _LIBCPP_ALWAYS_INLINE
683_ValueType __libcpp_acquire_load(_ValueType const* __value) {
684#if !defined(_LIBCPP_HAS_NO_THREADS) && \
685 defined(__ATOMIC_ACQUIRE) && \
686 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
687 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
688#else
689 return *__value;
690#endif
691}
692
Marshall Clow78dbe462016-11-14 18:22:19 +0000693// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000694
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695template <class _Tp> class allocator;
696
697template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000698class _LIBCPP_TEMPLATE_VIS allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699{
700public:
701 typedef void* pointer;
702 typedef const void* const_pointer;
703 typedef void value_type;
704
705 template <class _Up> struct rebind {typedef allocator<_Up> other;};
706};
707
Howard Hinnant9f771052012-01-19 23:15:22 +0000708template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000709class _LIBCPP_TEMPLATE_VIS allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000710{
711public:
712 typedef const void* pointer;
713 typedef const void* const_pointer;
714 typedef const void value_type;
715
716 template <class _Up> struct rebind {typedef allocator<_Up> other;};
717};
718
Howard Hinnantc51e1022010-05-11 19:42:16 +0000719// pointer_traits
720
721template <class _Tp>
722struct __has_element_type
723{
724private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000725 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726 template <class _Up> static __two __test(...);
727 template <class _Up> static char __test(typename _Up::element_type* = 0);
728public:
729 static const bool value = sizeof(__test<_Tp>(0)) == 1;
730};
731
732template <class _Ptr, bool = __has_element_type<_Ptr>::value>
733struct __pointer_traits_element_type;
734
735template <class _Ptr>
736struct __pointer_traits_element_type<_Ptr, true>
737{
738 typedef typename _Ptr::element_type type;
739};
740
741#ifndef _LIBCPP_HAS_NO_VARIADICS
742
743template <template <class, class...> class _Sp, class _Tp, class ..._Args>
744struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
745{
746 typedef typename _Sp<_Tp, _Args...>::element_type type;
747};
748
749template <template <class, class...> class _Sp, class _Tp, class ..._Args>
750struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
751{
752 typedef _Tp type;
753};
754
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000755#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000756
757template <template <class> class _Sp, class _Tp>
758struct __pointer_traits_element_type<_Sp<_Tp>, true>
759{
760 typedef typename _Sp<_Tp>::element_type type;
761};
762
763template <template <class> class _Sp, class _Tp>
764struct __pointer_traits_element_type<_Sp<_Tp>, false>
765{
766 typedef _Tp type;
767};
768
769template <template <class, class> class _Sp, class _Tp, class _A0>
770struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
771{
772 typedef typename _Sp<_Tp, _A0>::element_type type;
773};
774
775template <template <class, class> class _Sp, class _Tp, class _A0>
776struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
777{
778 typedef _Tp type;
779};
780
781template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
782struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
783{
784 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
785};
786
787template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
788struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
789{
790 typedef _Tp type;
791};
792
793template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
794 class _A1, class _A2>
795struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
796{
797 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
798};
799
800template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
801 class _A1, class _A2>
802struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
803{
804 typedef _Tp type;
805};
806
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000807#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000808
809template <class _Tp>
810struct __has_difference_type
811{
812private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000813 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000814 template <class _Up> static __two __test(...);
815 template <class _Up> static char __test(typename _Up::difference_type* = 0);
816public:
817 static const bool value = sizeof(__test<_Tp>(0)) == 1;
818};
819
820template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
821struct __pointer_traits_difference_type
822{
823 typedef ptrdiff_t type;
824};
825
826template <class _Ptr>
827struct __pointer_traits_difference_type<_Ptr, true>
828{
829 typedef typename _Ptr::difference_type type;
830};
831
832template <class _Tp, class _Up>
833struct __has_rebind
834{
835private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000836 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000837 template <class _Xp> static __two __test(...);
838 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
839public:
840 static const bool value = sizeof(__test<_Tp>(0)) == 1;
841};
842
843template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
844struct __pointer_traits_rebind
845{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000846#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000847 typedef typename _Tp::template rebind<_Up> type;
848#else
849 typedef typename _Tp::template rebind<_Up>::other type;
850#endif
851};
852
853#ifndef _LIBCPP_HAS_NO_VARIADICS
854
855template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
856struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
857{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000858#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000859 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
860#else
861 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
862#endif
863};
864
865template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
866struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
867{
868 typedef _Sp<_Up, _Args...> type;
869};
870
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000871#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872
873template <template <class> class _Sp, class _Tp, class _Up>
874struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
875{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000876#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000877 typedef typename _Sp<_Tp>::template rebind<_Up> type;
878#else
879 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
880#endif
881};
882
883template <template <class> class _Sp, class _Tp, class _Up>
884struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
885{
886 typedef _Sp<_Up> type;
887};
888
889template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
890struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
891{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000892#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000893 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
894#else
895 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
896#endif
897};
898
899template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
900struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
901{
902 typedef _Sp<_Up, _A0> type;
903};
904
905template <template <class, class, class> class _Sp, class _Tp, class _A0,
906 class _A1, class _Up>
907struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
908{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000909#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
911#else
912 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
913#endif
914};
915
916template <template <class, class, class> class _Sp, class _Tp, class _A0,
917 class _A1, class _Up>
918struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
919{
920 typedef _Sp<_Up, _A0, _A1> type;
921};
922
923template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
924 class _A1, class _A2, class _Up>
925struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
926{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000927#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
929#else
930 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
931#endif
932};
933
934template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
935 class _A1, class _A2, class _Up>
936struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
937{
938 typedef _Sp<_Up, _A0, _A1, _A2> type;
939};
940
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000941#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942
943template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000944struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945{
946 typedef _Ptr pointer;
947 typedef typename __pointer_traits_element_type<pointer>::type element_type;
948 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
949
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000950#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000951 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000952#else
953 template <class _Up> struct rebind
954 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000955#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956
957private:
958 struct __nat {};
959public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000960 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961 static pointer pointer_to(typename conditional<is_void<element_type>::value,
962 __nat, element_type>::type& __r)
963 {return pointer::pointer_to(__r);}
964};
965
966template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000967struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968{
969 typedef _Tp* pointer;
970 typedef _Tp element_type;
971 typedef ptrdiff_t difference_type;
972
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000973#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000974 template <class _Up> using rebind = _Up*;
975#else
976 template <class _Up> struct rebind {typedef _Up* other;};
977#endif
978
979private:
980 struct __nat {};
981public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000984 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000985 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986};
987
Eric Fiselierae3ab842015-08-23 02:56:05 +0000988template <class _From, class _To>
989struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000990#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000991 typedef typename pointer_traits<_From>::template rebind<_To> type;
992#else
993 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
994#endif
995};
996
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997// allocator_traits
998
999namespace __has_pointer_type_imp
1000{
Howard Hinnant6e260172013-09-21 01:45:05 +00001001 template <class _Up> static __two __test(...);
1002 template <class _Up> static char __test(typename _Up::pointer* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001003}
1004
1005template <class _Tp>
1006struct __has_pointer_type
Howard Hinnant6e260172013-09-21 01:45:05 +00001007 : public integral_constant<bool, sizeof(__has_pointer_type_imp::__test<_Tp>(0)) == 1>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008{
1009};
1010
1011namespace __pointer_type_imp
1012{
1013
1014template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1015struct __pointer_type
1016{
1017 typedef typename _Dp::pointer type;
1018};
1019
1020template <class _Tp, class _Dp>
1021struct __pointer_type<_Tp, _Dp, false>
1022{
1023 typedef _Tp* type;
1024};
1025
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001026} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027
1028template <class _Tp, class _Dp>
1029struct __pointer_type
1030{
1031 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
1032};
1033
1034template <class _Tp>
1035struct __has_const_pointer
1036{
1037private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001038 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039 template <class _Up> static __two __test(...);
1040 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
1041public:
1042 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1043};
1044
1045template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1046struct __const_pointer
1047{
1048 typedef typename _Alloc::const_pointer type;
1049};
1050
1051template <class _Tp, class _Ptr, class _Alloc>
1052struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1053{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001054#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1056#else
1057 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1058#endif
1059};
1060
1061template <class _Tp>
1062struct __has_void_pointer
1063{
1064private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001065 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066 template <class _Up> static __two __test(...);
1067 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1068public:
1069 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1070};
1071
1072template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1073struct __void_pointer
1074{
1075 typedef typename _Alloc::void_pointer type;
1076};
1077
1078template <class _Ptr, class _Alloc>
1079struct __void_pointer<_Ptr, _Alloc, false>
1080{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001081#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1083#else
1084 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1085#endif
1086};
1087
1088template <class _Tp>
1089struct __has_const_void_pointer
1090{
1091private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001092 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093 template <class _Up> static __two __test(...);
1094 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1095public:
1096 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1097};
1098
1099template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1100struct __const_void_pointer
1101{
1102 typedef typename _Alloc::const_void_pointer type;
1103};
1104
1105template <class _Ptr, class _Alloc>
1106struct __const_void_pointer<_Ptr, _Alloc, false>
1107{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001108#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001109 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1110#else
1111 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1112#endif
1113};
1114
Howard Hinnantc834c512011-11-29 18:15:50 +00001115template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001117_Tp*
1118__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119{
1120 return __p;
1121}
1122
1123template <class _Pointer>
1124inline _LIBCPP_INLINE_VISIBILITY
1125typename pointer_traits<_Pointer>::element_type*
Howard Hinnant719bda32011-05-28 14:41:13 +00001126__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001128 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129}
1130
1131template <class _Tp>
1132struct __has_size_type
1133{
1134private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001135 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136 template <class _Up> static __two __test(...);
1137 template <class _Up> static char __test(typename _Up::size_type* = 0);
1138public:
1139 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1140};
1141
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001142template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143struct __size_type
1144{
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001145 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146};
1147
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001148template <class _Alloc, class _DiffType>
1149struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150{
1151 typedef typename _Alloc::size_type type;
1152};
1153
1154template <class _Tp>
1155struct __has_propagate_on_container_copy_assignment
1156{
1157private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001158 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 template <class _Up> static __two __test(...);
1160 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1161public:
1162 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1163};
1164
1165template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1166struct __propagate_on_container_copy_assignment
1167{
1168 typedef false_type type;
1169};
1170
1171template <class _Alloc>
1172struct __propagate_on_container_copy_assignment<_Alloc, true>
1173{
1174 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1175};
1176
1177template <class _Tp>
1178struct __has_propagate_on_container_move_assignment
1179{
1180private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001181 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182 template <class _Up> static __two __test(...);
1183 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1184public:
1185 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1186};
1187
1188template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1189struct __propagate_on_container_move_assignment
1190{
1191 typedef false_type type;
1192};
1193
1194template <class _Alloc>
1195struct __propagate_on_container_move_assignment<_Alloc, true>
1196{
1197 typedef typename _Alloc::propagate_on_container_move_assignment type;
1198};
1199
1200template <class _Tp>
1201struct __has_propagate_on_container_swap
1202{
1203private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001204 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205 template <class _Up> static __two __test(...);
1206 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1207public:
1208 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1209};
1210
1211template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1212struct __propagate_on_container_swap
1213{
1214 typedef false_type type;
1215};
1216
1217template <class _Alloc>
1218struct __propagate_on_container_swap<_Alloc, true>
1219{
1220 typedef typename _Alloc::propagate_on_container_swap type;
1221};
1222
Marshall Clow0b587562015-06-02 16:34:03 +00001223template <class _Tp>
1224struct __has_is_always_equal
1225{
1226private:
1227 struct __two {char __lx; char __lxx;};
1228 template <class _Up> static __two __test(...);
1229 template <class _Up> static char __test(typename _Up::is_always_equal* = 0);
1230public:
1231 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1232};
1233
1234template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1235struct __is_always_equal
1236{
1237 typedef typename _VSTD::is_empty<_Alloc>::type type;
1238};
1239
1240template <class _Alloc>
1241struct __is_always_equal<_Alloc, true>
1242{
1243 typedef typename _Alloc::is_always_equal type;
1244};
1245
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1247struct __has_rebind_other
1248{
1249private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001250 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251 template <class _Xp> static __two __test(...);
1252 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1253public:
1254 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1255};
1256
1257template <class _Tp, class _Up>
1258struct __has_rebind_other<_Tp, _Up, false>
1259{
1260 static const bool value = false;
1261};
1262
1263template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1264struct __allocator_traits_rebind
1265{
1266 typedef typename _Tp::template rebind<_Up>::other type;
1267};
1268
1269#ifndef _LIBCPP_HAS_NO_VARIADICS
1270
1271template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1272struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1273{
1274 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1275};
1276
1277template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1278struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1279{
1280 typedef _Alloc<_Up, _Args...> type;
1281};
1282
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001283#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284
1285template <template <class> class _Alloc, class _Tp, class _Up>
1286struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1287{
1288 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1289};
1290
1291template <template <class> class _Alloc, class _Tp, class _Up>
1292struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1293{
1294 typedef _Alloc<_Up> type;
1295};
1296
Howard Hinnantc51e1022010-05-11 19:42:16 +00001297template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1298struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1299{
1300 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1301};
1302
1303template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1304struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1305{
1306 typedef _Alloc<_Up, _A0> type;
1307};
1308
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1310 class _A1, class _Up>
1311struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1312{
1313 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1314};
1315
1316template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1317 class _A1, class _Up>
1318struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1319{
1320 typedef _Alloc<_Up, _A0, _A1> type;
1321};
1322
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1324 class _A1, class _A2, class _Up>
1325struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1326{
1327 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1328};
1329
1330template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1331 class _A1, class _A2, class _Up>
1332struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1333{
1334 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1335};
1336
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001337#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001339#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001340
1341template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1342auto
1343__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1344 -> decltype(__a.allocate(__sz, __p), true_type());
1345
1346template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1347auto
1348__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1349 -> false_type;
1350
1351template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1352struct __has_allocate_hint
1353 : integral_constant<bool,
1354 is_same<
1355 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1356 declval<_SizeType>(),
1357 declval<_ConstVoidPtr>())),
1358 true_type>::value>
1359{
1360};
1361
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001362#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001363
1364template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1365struct __has_allocate_hint
1366 : true_type
1367{
1368};
1369
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001370#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001371
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001372#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001373
1374template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001375decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1376 _VSTD::declval<_Args>()...),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377 true_type())
1378__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1379
1380template <class _Alloc, class _Pointer, class ..._Args>
1381false_type
1382__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1383
1384template <class _Alloc, class _Pointer, class ..._Args>
1385struct __has_construct
1386 : integral_constant<bool,
1387 is_same<
1388 decltype(__has_construct_test(declval<_Alloc>(),
1389 declval<_Pointer>(),
1390 declval<_Args>()...)),
1391 true_type>::value>
1392{
1393};
1394
1395template <class _Alloc, class _Pointer>
1396auto
1397__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1398 -> decltype(__a.destroy(__p), true_type());
1399
1400template <class _Alloc, class _Pointer>
1401auto
1402__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1403 -> false_type;
1404
1405template <class _Alloc, class _Pointer>
1406struct __has_destroy
1407 : integral_constant<bool,
1408 is_same<
1409 decltype(__has_destroy_test(declval<_Alloc>(),
1410 declval<_Pointer>())),
1411 true_type>::value>
1412{
1413};
1414
1415template <class _Alloc>
1416auto
1417__has_max_size_test(_Alloc&& __a)
1418 -> decltype(__a.max_size(), true_type());
1419
1420template <class _Alloc>
1421auto
1422__has_max_size_test(const volatile _Alloc& __a)
1423 -> false_type;
1424
1425template <class _Alloc>
1426struct __has_max_size
1427 : integral_constant<bool,
1428 is_same<
1429 decltype(__has_max_size_test(declval<_Alloc&>())),
1430 true_type>::value>
1431{
1432};
1433
1434template <class _Alloc>
1435auto
1436__has_select_on_container_copy_construction_test(_Alloc&& __a)
1437 -> decltype(__a.select_on_container_copy_construction(), true_type());
1438
1439template <class _Alloc>
1440auto
1441__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1442 -> false_type;
1443
1444template <class _Alloc>
1445struct __has_select_on_container_copy_construction
1446 : integral_constant<bool,
1447 is_same<
1448 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1449 true_type>::value>
1450{
1451};
1452
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001453#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454
1455#ifndef _LIBCPP_HAS_NO_VARIADICS
1456
1457template <class _Alloc, class _Pointer, class ..._Args>
1458struct __has_construct
1459 : false_type
1460{
1461};
1462
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001463#else // _LIBCPP_HAS_NO_VARIADICS
1464
1465template <class _Alloc, class _Pointer, class _Args>
1466struct __has_construct
1467 : false_type
1468{
1469};
1470
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001471#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001472
1473template <class _Alloc, class _Pointer>
1474struct __has_destroy
1475 : false_type
1476{
1477};
1478
1479template <class _Alloc>
1480struct __has_max_size
1481 : true_type
1482{
1483};
1484
1485template <class _Alloc>
1486struct __has_select_on_container_copy_construction
1487 : false_type
1488{
1489};
1490
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001491#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001493template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1494struct __alloc_traits_difference_type
1495{
1496 typedef typename pointer_traits<_Ptr>::difference_type type;
1497};
1498
1499template <class _Alloc, class _Ptr>
1500struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1501{
1502 typedef typename _Alloc::difference_type type;
1503};
1504
Howard Hinnantc51e1022010-05-11 19:42:16 +00001505template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001506struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001507{
1508 typedef _Alloc allocator_type;
1509 typedef typename allocator_type::value_type value_type;
1510
1511 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1512 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1513 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1514 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1515
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001516 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1517 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001518
1519 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1520 propagate_on_container_copy_assignment;
1521 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1522 propagate_on_container_move_assignment;
1523 typedef typename __propagate_on_container_swap<allocator_type>::type
1524 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001525 typedef typename __is_always_equal<allocator_type>::type
1526 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001527
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001528#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001530 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001532#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001533 template <class _Tp> struct rebind_alloc
1534 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1535 template <class _Tp> struct rebind_traits
1536 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001537#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538
Howard Hinnant756c69b2010-09-22 16:48:34 +00001539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540 static pointer allocate(allocator_type& __a, size_type __n)
1541 {return __a.allocate(__n);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1544 {return allocate(__a, __n, __hint,
1545 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1546
Howard Hinnant756c69b2010-09-22 16:48:34 +00001547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001548 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549 {__a.deallocate(__p, __n);}
1550
1551#ifndef _LIBCPP_HAS_NO_VARIADICS
1552 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001555 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001556 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001557#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001559 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001560 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561 {
1562 ::new ((void*)__p) _Tp();
1563 }
1564 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001565 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001566 static void construct(allocator_type&, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001567 {
1568 ::new ((void*)__p) _Tp(__a0);
1569 }
1570 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001571 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001572 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 const _A1& __a1)
1574 {
1575 ::new ((void*)__p) _Tp(__a0, __a1);
1576 }
1577 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001578 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001579 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580 const _A1& __a1, const _A2& __a2)
1581 {
1582 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1583 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001584#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585
1586 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 static void destroy(allocator_type& __a, _Tp* __p)
1589 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1590
Howard Hinnant756c69b2010-09-22 16:48:34 +00001591 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001592 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1594
Howard Hinnant756c69b2010-09-22 16:48:34 +00001595 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596 static allocator_type
1597 select_on_container_copy_construction(const allocator_type& __a)
1598 {return select_on_container_copy_construction(
1599 __has_select_on_container_copy_construction<const allocator_type>(),
1600 __a);}
1601
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001602 template <class _Ptr>
1603 _LIBCPP_INLINE_VISIBILITY
1604 static
1605 void
1606 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1607 {
1608 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1609 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1610 }
1611
1612 template <class _Tp>
1613 _LIBCPP_INLINE_VISIBILITY
1614 static
1615 typename enable_if
1616 <
1617 (is_same<allocator_type, allocator<_Tp> >::value
1618 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1619 is_trivially_move_constructible<_Tp>::value,
1620 void
1621 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001622 __construct_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001623 {
1624 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001625 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001626 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001627 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001628 __begin2 += _Np;
1629 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001630 }
1631
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001632 template <class _Iter, class _Ptr>
1633 _LIBCPP_INLINE_VISIBILITY
1634 static
1635 void
1636 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1637 {
1638 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1639 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1640 }
1641
1642 template <class _Tp>
1643 _LIBCPP_INLINE_VISIBILITY
1644 static
1645 typename enable_if
1646 <
1647 (is_same<allocator_type, allocator<_Tp> >::value
1648 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1649 is_trivially_move_constructible<_Tp>::value,
1650 void
1651 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001652 __construct_range_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001653 {
1654 typedef typename remove_const<_Tp>::type _Vp;
1655 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001656 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001657 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001658 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001659 __begin2 += _Np;
1660 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001661 }
1662
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001663 template <class _Ptr>
1664 _LIBCPP_INLINE_VISIBILITY
1665 static
1666 void
1667 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1668 {
1669 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001670 {
1671 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1672 --__end2;
1673 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001674 }
1675
1676 template <class _Tp>
1677 _LIBCPP_INLINE_VISIBILITY
1678 static
1679 typename enable_if
1680 <
1681 (is_same<allocator_type, allocator<_Tp> >::value
1682 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1683 is_trivially_move_constructible<_Tp>::value,
1684 void
1685 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001686 __construct_backward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001687 {
1688 ptrdiff_t _Np = __end1 - __begin1;
1689 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001690 if (_Np > 0)
1691 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001692 }
1693
Howard Hinnantc51e1022010-05-11 19:42:16 +00001694private:
1695
Howard Hinnant756c69b2010-09-22 16:48:34 +00001696 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001697 static pointer allocate(allocator_type& __a, size_type __n,
1698 const_void_pointer __hint, true_type)
1699 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001700 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001701 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001702 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703 {return __a.allocate(__n);}
1704
1705#ifndef _LIBCPP_HAS_NO_VARIADICS
1706 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001709 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001711 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001712 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1713 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001714 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001716#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001717
1718 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1721 {__a.destroy(__p);}
1722 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001723 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001724 static void __destroy(false_type, allocator_type&, _Tp* __p)
1725 {
1726 __p->~_Tp();
1727 }
1728
Howard Hinnant756c69b2010-09-22 16:48:34 +00001729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001730 static size_type __max_size(true_type, const allocator_type& __a)
1731 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001732 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001733 static size_type __max_size(false_type, const allocator_type&)
Marshall Clow33daa162015-10-25 19:34:04 +00001734 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001735
Howard Hinnant756c69b2010-09-22 16:48:34 +00001736 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001737 static allocator_type
1738 select_on_container_copy_construction(true_type, const allocator_type& __a)
1739 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001740 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741 static allocator_type
1742 select_on_container_copy_construction(false_type, const allocator_type& __a)
1743 {return __a;}
1744};
1745
Marshall Clow940e01c2015-04-07 05:21:38 +00001746template <class _Traits, class _Tp>
1747struct __rebind_alloc_helper
1748{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001749#ifndef _LIBCPP_CXX03_LANG
Marshall Clow35cde742015-05-10 13:59:45 +00001750 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001751#else
1752 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1753#endif
1754};
1755
Howard Hinnantc51e1022010-05-11 19:42:16 +00001756// allocator
1757
1758template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001759class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001760{
1761public:
1762 typedef size_t size_type;
1763 typedef ptrdiff_t difference_type;
1764 typedef _Tp* pointer;
1765 typedef const _Tp* const_pointer;
1766 typedef _Tp& reference;
1767 typedef const _Tp& const_reference;
1768 typedef _Tp value_type;
1769
Howard Hinnant4931e092011-06-02 21:38:57 +00001770 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001771 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001772
Howard Hinnantc51e1022010-05-11 19:42:16 +00001773 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1774
Howard Hinnant719bda32011-05-28 14:41:13 +00001775 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1776 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1777 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001778 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001779 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001780 {return _VSTD::addressof(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001782 {
1783 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001784 __throw_length_error("allocator<T>::allocate(size_t n)"
1785 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001786 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
1787 }
Howard Hinnant719bda32011-05-28 14:41:13 +00001788 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Eric Fiselier8122fda2017-01-07 03:01:24 +00001789 {_VSTD::__libcpp_deallocate((void*)__p);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001790 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1791 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001792#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001793 template <class _Up, class... _Args>
1794 _LIBCPP_INLINE_VISIBILITY
1795 void
1796 construct(_Up* __p, _Args&&... __args)
1797 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001798 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001799 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001800#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801 _LIBCPP_INLINE_VISIBILITY
1802 void
1803 construct(pointer __p)
1804 {
1805 ::new((void*)__p) _Tp();
1806 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001807# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001808
Howard Hinnantc51e1022010-05-11 19:42:16 +00001809 template <class _A0>
1810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001811 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812 construct(pointer __p, _A0& __a0)
1813 {
1814 ::new((void*)__p) _Tp(__a0);
1815 }
1816 template <class _A0>
1817 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001818 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001819 construct(pointer __p, const _A0& __a0)
1820 {
1821 ::new((void*)__p) _Tp(__a0);
1822 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001823# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001824 template <class _A0, class _A1>
1825 _LIBCPP_INLINE_VISIBILITY
1826 void
1827 construct(pointer __p, _A0& __a0, _A1& __a1)
1828 {
1829 ::new((void*)__p) _Tp(__a0, __a1);
1830 }
1831 template <class _A0, class _A1>
1832 _LIBCPP_INLINE_VISIBILITY
1833 void
1834 construct(pointer __p, const _A0& __a0, _A1& __a1)
1835 {
1836 ::new((void*)__p) _Tp(__a0, __a1);
1837 }
1838 template <class _A0, class _A1>
1839 _LIBCPP_INLINE_VISIBILITY
1840 void
1841 construct(pointer __p, _A0& __a0, const _A1& __a1)
1842 {
1843 ::new((void*)__p) _Tp(__a0, __a1);
1844 }
1845 template <class _A0, class _A1>
1846 _LIBCPP_INLINE_VISIBILITY
1847 void
1848 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1849 {
1850 ::new((void*)__p) _Tp(__a0, __a1);
1851 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001852#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001853 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1854};
1855
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001856template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001857class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001858{
1859public:
1860 typedef size_t size_type;
1861 typedef ptrdiff_t difference_type;
1862 typedef const _Tp* pointer;
1863 typedef const _Tp* const_pointer;
1864 typedef const _Tp& reference;
1865 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001866 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001867
1868 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001869 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001870
1871 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1872
1873 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1874 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1875 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1876 {return _VSTD::addressof(__x);}
1877 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001878 {
1879 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001880 __throw_length_error("allocator<const T>::allocate(size_t n)"
1881 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001882 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001883 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001884 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Eric Fiselier8122fda2017-01-07 03:01:24 +00001885 {_VSTD::__libcpp_deallocate((void*)__p);}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001886 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1887 {return size_type(~0) / sizeof(_Tp);}
1888#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1889 template <class _Up, class... _Args>
1890 _LIBCPP_INLINE_VISIBILITY
1891 void
1892 construct(_Up* __p, _Args&&... __args)
1893 {
1894 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1895 }
1896#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1897 _LIBCPP_INLINE_VISIBILITY
1898 void
1899 construct(pointer __p)
1900 {
1901 ::new((void*)__p) _Tp();
1902 }
1903# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001904
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001905 template <class _A0>
1906 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001907 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001908 construct(pointer __p, _A0& __a0)
1909 {
1910 ::new((void*)__p) _Tp(__a0);
1911 }
1912 template <class _A0>
1913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001914 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001915 construct(pointer __p, const _A0& __a0)
1916 {
1917 ::new((void*)__p) _Tp(__a0);
1918 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001919# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1920 template <class _A0, class _A1>
1921 _LIBCPP_INLINE_VISIBILITY
1922 void
1923 construct(pointer __p, _A0& __a0, _A1& __a1)
1924 {
1925 ::new((void*)__p) _Tp(__a0, __a1);
1926 }
1927 template <class _A0, class _A1>
1928 _LIBCPP_INLINE_VISIBILITY
1929 void
1930 construct(pointer __p, const _A0& __a0, _A1& __a1)
1931 {
1932 ::new((void*)__p) _Tp(__a0, __a1);
1933 }
1934 template <class _A0, class _A1>
1935 _LIBCPP_INLINE_VISIBILITY
1936 void
1937 construct(pointer __p, _A0& __a0, const _A1& __a1)
1938 {
1939 ::new((void*)__p) _Tp(__a0, __a1);
1940 }
1941 template <class _A0, class _A1>
1942 _LIBCPP_INLINE_VISIBILITY
1943 void
1944 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1945 {
1946 ::new((void*)__p) _Tp(__a0, __a1);
1947 }
1948#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1949 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1950};
1951
Howard Hinnantc51e1022010-05-11 19:42:16 +00001952template <class _Tp, class _Up>
1953inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001954bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955
1956template <class _Tp, class _Up>
1957inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001958bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001959
1960template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001961class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001962 : public iterator<output_iterator_tag,
1963 _Tp, // purposefully not C++03
1964 ptrdiff_t, // purposefully not C++03
1965 _Tp*, // purposefully not C++03
1966 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1967{
1968private:
1969 _OutputIterator __x_;
1970public:
1971 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1972 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1973 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1974 {::new(&*__x_) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001975#if _LIBCPP_STD_VER >= 14
1976 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1977 {::new(&*__x_) _Tp(_VSTD::move(__element)); return *this;}
1978#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001979 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1980 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1981 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001982#if _LIBCPP_STD_VER >= 14
1983 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1984#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001985};
1986
1987template <class _Tp>
1988pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001989get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001990{
1991 pair<_Tp*, ptrdiff_t> __r(0, 0);
1992 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1993 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1994 / sizeof(_Tp);
1995 if (__n > __m)
1996 __n = __m;
1997 while (__n > 0)
1998 {
1999 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
2000 if (__r.first)
2001 {
2002 __r.second = __n;
2003 break;
2004 }
2005 __n /= 2;
2006 }
2007 return __r;
2008}
2009
2010template <class _Tp>
2011inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002012void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002013
Marshall Clowb22274f2017-01-24 22:22:33 +00002014#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015template <class _Tp>
2016struct auto_ptr_ref
2017{
2018 _Tp* __ptr_;
2019};
2020
2021template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002022class _LIBCPP_TEMPLATE_VIS auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023{
2024private:
2025 _Tp* __ptr_;
2026public:
2027 typedef _Tp element_type;
2028
2029 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2030 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2031 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2032 : __ptr_(__p.release()) {}
2033 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2034 {reset(__p.release()); return *this;}
2035 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2036 {reset(__p.release()); return *this;}
2037 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2038 {reset(__p.__ptr_); return *this;}
2039 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2040
2041 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2042 {return *__ptr_;}
2043 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2044 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2045 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2046 {
2047 _Tp* __t = __ptr_;
2048 __ptr_ = 0;
2049 return __t;
2050 }
2051 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2052 {
2053 if (__ptr_ != __p)
2054 delete __ptr_;
2055 __ptr_ = __p;
2056 }
2057
2058 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2059 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2060 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2061 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2062 {return auto_ptr<_Up>(release());}
2063};
2064
2065template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002066class _LIBCPP_TEMPLATE_VIS auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067{
2068public:
2069 typedef void element_type;
2070};
Marshall Clowb22274f2017-01-24 22:22:33 +00002071#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072
Eric Fiselier9d355982017-04-12 23:45:53 +00002073template <class _Tp, int _Idx,
2074 bool _CanBeEmptyBase =
2075 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2076struct __compressed_pair_elem {
2077 typedef _Tp _ParamT;
2078 typedef _Tp& reference;
2079 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002080
Eric Fiselier9d355982017-04-12 23:45:53 +00002081#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4f855492017-04-13 00:50:45 +00002082 template <bool _Dummy = true, class = typename enable_if<
2083 __dependent_type<is_default_constructible<_Tp>, _Dummy>::value
2084 >::type
2085 >
2086 _LIBCPP_CONSTEXPR __compressed_pair_elem()
2087 _NOEXCEPT_(is_nothrow_default_constructible<_Tp>::value)
2088 : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002089
Eric Fiselier9d355982017-04-12 23:45:53 +00002090 template <class _Up, class = typename enable_if<
2091 !is_same<__compressed_pair_elem, _Up>::value>::type>
2092 _LIBCPP_CONSTEXPR explicit
2093 __compressed_pair_elem(_Up&& __u)
2094 : __value_(_VSTD::forward<_Up>(__u)){};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002095
Eric Fiselier9d355982017-04-12 23:45:53 +00002096 template <class... _Args, size_t... _Indexes>
2097 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2098 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2099 __tuple_indices<_Indexes...>)
2100 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2101#else
2102 __compressed_pair_elem() : __value_() {}
2103 __compressed_pair_elem(_ParamT __p) : __value_(std::forward<_ParamT>(__p)) {}
2104#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105
Eric Fiselier9d355982017-04-12 23:45:53 +00002106 reference __get() _NOEXCEPT { return __value_; }
2107 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002108
Howard Hinnantc51e1022010-05-11 19:42:16 +00002109private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002110 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002111};
2112
Eric Fiselier9d355982017-04-12 23:45:53 +00002113template <class _Tp, int _Idx>
2114struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2115 typedef _Tp _ParamT;
2116 typedef _Tp& reference;
2117 typedef const _Tp& const_reference;
2118 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002119
Eric Fiselier9d355982017-04-12 23:45:53 +00002120#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4f855492017-04-13 00:50:45 +00002121 _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002122
Eric Fiselier9d355982017-04-12 23:45:53 +00002123 template <class _Up, class = typename enable_if<
2124 !is_same<__compressed_pair_elem, _Up>::value>::type>
2125 _LIBCPP_CONSTEXPR explicit
2126 __compressed_pair_elem(_Up&& __u)
2127 : __value_type(_VSTD::forward<_Up>(__u)){};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002128
Eric Fiselier9d355982017-04-12 23:45:53 +00002129 template <class... _Args, size_t... _Indexes>
2130 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2131 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2132 __tuple_indices<_Indexes...>)
2133 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2134#else
2135 __compressed_pair_elem() : __value_type() {}
2136 __compressed_pair_elem(_ParamT __p)
2137 : __value_type(std::forward<_ParamT>(__p)) {}
2138#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002139
Eric Fiselier9d355982017-04-12 23:45:53 +00002140 reference __get() _NOEXCEPT { return *this; }
2141 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002142};
2143
Eric Fiselier9d355982017-04-12 23:45:53 +00002144// Tag used to construct the second element of the compressed pair.
2145struct __second_tag {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002146
2147template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002148class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2149 private __compressed_pair_elem<_T2, 1> {
2150 typedef __compressed_pair_elem<_T1, 0> _Base1;
2151 typedef __compressed_pair_elem<_T2, 1> _Base2;
2152
2153 // NOTE: This static assert should never fire because __compressed_pair
2154 // is *almost never* used in a scenario where it's possible for T1 == T2.
2155 // (The exception is std::function where it is possible that the function
2156 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002157 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002158 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2159 "The current implementation is NOT ABI-compatible with the previous "
2160 "implementation for this configuration");
2161
Howard Hinnantc51e1022010-05-11 19:42:16 +00002162public:
Eric Fiselier9d355982017-04-12 23:45:53 +00002163#ifndef _LIBCPP_CXX03_LANG
2164 _LIBCPP_INLINE_VISIBILITY
2165 __compressed_pair() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002166
Eric Fiselier9d355982017-04-12 23:45:53 +00002167 template <class _Tp, typename enable_if<!is_same<typename decay<_Tp>::type,
2168 __compressed_pair>::value,
2169 bool>::type = true>
2170 _LIBCPP_INLINE_VISIBILITY constexpr explicit
2171 __compressed_pair(_Tp&& __t)
2172 : _Base1(std::forward<_Tp>(__t)), _Base2() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002173
Eric Fiselier9d355982017-04-12 23:45:53 +00002174 template <class _Tp>
2175 _LIBCPP_INLINE_VISIBILITY constexpr
2176 __compressed_pair(__second_tag, _Tp&& __t)
2177 : _Base1(), _Base2(std::forward<_Tp>(__t)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002178
Eric Fiselier9d355982017-04-12 23:45:53 +00002179 template <class _U1, class _U2>
2180 _LIBCPP_INLINE_VISIBILITY constexpr
2181 __compressed_pair(_U1&& __t1, _U2&& __t2)
2182 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183
Eric Fiselier9d355982017-04-12 23:45:53 +00002184 template <class... _Args1, class... _Args2>
2185 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2186 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2187 tuple<_Args2...> __second_args)
2188 : _Base1(__pc, _VSTD::move(__first_args),
2189 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2190 _Base2(__pc, _VSTD::move(__second_args),
2191 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002192
Eric Fiselier9d355982017-04-12 23:45:53 +00002193#else
2194 _LIBCPP_INLINE_VISIBILITY
2195 __compressed_pair() {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002196
Eric Fiselier9d355982017-04-12 23:45:53 +00002197 _LIBCPP_INLINE_VISIBILITY explicit
2198 __compressed_pair(_T1 __t1) : _Base1(_VSTD::forward<_T1>(__t1)) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002199
Eric Fiselier9d355982017-04-12 23:45:53 +00002200 _LIBCPP_INLINE_VISIBILITY
2201 __compressed_pair(__second_tag, _T2 __t2)
2202 : _Base1(), _Base2(_VSTD::forward<_T2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002203
Eric Fiselier9d355982017-04-12 23:45:53 +00002204 _LIBCPP_INLINE_VISIBILITY
2205 __compressed_pair(_T1 __t1, _T2 __t2)
2206 : _Base1(_VSTD::forward<_T1>(__t1)), _Base2(_VSTD::forward<_T2>(__t2)) {}
2207#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208
Eric Fiselier9d355982017-04-12 23:45:53 +00002209 _LIBCPP_INLINE_VISIBILITY
2210 typename _Base1::reference first() _NOEXCEPT {
2211 return static_cast<_Base1&>(*this).__get();
2212 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002213
Eric Fiselier9d355982017-04-12 23:45:53 +00002214 _LIBCPP_INLINE_VISIBILITY
2215 typename _Base1::const_reference first() const _NOEXCEPT {
2216 return static_cast<_Base1 const&>(*this).__get();
2217 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002218
Eric Fiselier9d355982017-04-12 23:45:53 +00002219 _LIBCPP_INLINE_VISIBILITY
2220 typename _Base2::reference second() _NOEXCEPT {
2221 return static_cast<_Base2&>(*this).__get();
2222 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002223
Eric Fiselier9d355982017-04-12 23:45:53 +00002224 _LIBCPP_INLINE_VISIBILITY
2225 typename _Base2::const_reference second() const _NOEXCEPT {
2226 return static_cast<_Base2 const&>(*this).__get();
2227 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228
Eric Fiselier9d355982017-04-12 23:45:53 +00002229 _LIBCPP_INLINE_VISIBILITY
2230 void swap(__compressed_pair& __x)
2231 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2232 __is_nothrow_swappable<_T2>::value)
2233 {
2234 using std::swap;
2235 swap(first(), __x.first());
2236 swap(second(), __x.second());
2237 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002238};
2239
2240template <class _T1, class _T2>
2241inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002242void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2243 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2244 __is_nothrow_swappable<_T2>::value) {
2245 __x.swap(__y);
2246}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002247
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002248// default_delete
2249
Howard Hinnantc51e1022010-05-11 19:42:16 +00002250template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002251struct _LIBCPP_TEMPLATE_VIS default_delete {
2252#ifndef _LIBCPP_CXX03_LANG
2253 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002254#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002255 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002256#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002257 template <class _Up>
2258 _LIBCPP_INLINE_VISIBILITY
2259 default_delete(const default_delete<_Up>&,
2260 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2261 0) _NOEXCEPT {}
2262
2263 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2264 static_assert(sizeof(_Tp) > 0,
2265 "default_delete can not delete incomplete type");
2266 static_assert(!is_void<_Tp>::value,
2267 "default_delete can not delete incomplete type");
2268 delete __ptr;
2269 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002270};
2271
2272template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002273struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2274private:
2275 template <class _Up>
2276 struct _EnableIfConvertible
2277 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2278
Howard Hinnant4500ca52011-12-18 21:19:44 +00002279public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002280#ifndef _LIBCPP_CXX03_LANG
2281 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002282#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002283 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002284#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002285
2286 template <class _Up>
2287 _LIBCPP_INLINE_VISIBILITY
2288 default_delete(const default_delete<_Up[]>&,
2289 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2290
2291 template <class _Up>
2292 _LIBCPP_INLINE_VISIBILITY
2293 typename _EnableIfConvertible<_Up>::type
2294 operator()(_Up* __ptr) const _NOEXCEPT {
2295 static_assert(sizeof(_Tp) > 0,
2296 "default_delete can not delete incomplete type");
2297 static_assert(!is_void<_Tp>::value,
2298 "default_delete can not delete void type");
2299 delete[] __ptr;
2300 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002301};
2302
Howard Hinnantc51e1022010-05-11 19:42:16 +00002303
Eric Fiselier6779b062017-04-16 02:06:25 +00002304
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002305#ifndef _LIBCPP_CXX03_LANG
2306template <class _Deleter>
2307struct __unique_ptr_deleter_sfinae {
2308 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2309 typedef const _Deleter& __lval_ref_type;
2310 typedef _Deleter&& __good_rval_ref_type;
2311 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002312};
2313
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002314template <class _Deleter>
2315struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2316 typedef const _Deleter& __lval_ref_type;
2317 typedef const _Deleter&& __bad_rval_ref_type;
2318 typedef false_type __enable_rval_overload;
2319};
2320
2321template <class _Deleter>
2322struct __unique_ptr_deleter_sfinae<_Deleter&> {
2323 typedef _Deleter& __lval_ref_type;
2324 typedef _Deleter&& __bad_rval_ref_type;
2325 typedef false_type __enable_rval_overload;
2326};
2327#endif // !defined(_LIBCPP_CXX03_LANG)
2328
2329template <class _Tp, class _Dp = default_delete<_Tp> >
2330class _LIBCPP_TEMPLATE_VIS unique_ptr {
2331public:
2332 typedef _Tp element_type;
2333 typedef _Dp deleter_type;
2334 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2335
2336 static_assert(!is_rvalue_reference<deleter_type>::value,
2337 "the specified deleter type cannot be an rvalue reference");
2338
2339private:
2340 __compressed_pair<pointer, deleter_type> __ptr_;
2341
2342 struct __nat { int __for_bool_; };
2343
2344#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier31127cd2017-04-16 02:14:31 +00002345 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002346
2347 template <bool _Dummy>
2348 using _LValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002349 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002350
2351 template <bool _Dummy>
2352 using _GoodRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002353 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002354
2355 template <bool _Dummy>
2356 using _BadRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002357 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002358
2359 template <bool _Dummy, class _Deleter = typename __dependent_type<
2360 __identity<deleter_type>, _Dummy>::type>
2361 using _EnableIfDeleterDefaultConstructible =
2362 typename enable_if<is_default_constructible<_Deleter>::value &&
2363 !is_pointer<_Deleter>::value>::type;
2364
2365 template <class _ArgType>
2366 using _EnableIfDeleterConstructible =
2367 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2368
2369 template <class _UPtr, class _Up>
2370 using _EnableIfMoveConvertible = typename enable_if<
2371 is_convertible<typename _UPtr::pointer, pointer>::value &&
2372 !is_array<_Up>::value
2373 >::type;
2374
2375 template <class _UDel>
2376 using _EnableIfDeleterConvertible = typename enable_if<
2377 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2378 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2379 >::type;
2380
2381 template <class _UDel>
2382 using _EnableIfDeleterAssignable = typename enable_if<
2383 is_assignable<_Dp&, _UDel&&>::value
2384 >::type;
2385
2386public:
2387 template <bool _Dummy = true,
2388 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2389 _LIBCPP_INLINE_VISIBILITY
2390 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
2391
2392 template <bool _Dummy = true,
2393 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2394 _LIBCPP_INLINE_VISIBILITY
2395 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
2396
2397 template <bool _Dummy = true,
2398 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2399 _LIBCPP_INLINE_VISIBILITY
2400 explicit unique_ptr(pointer __p) noexcept : __ptr_(__p) {}
2401
2402 template <bool _Dummy = true,
2403 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2404 _LIBCPP_INLINE_VISIBILITY
2405 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) noexcept
2406 : __ptr_(__p, __d) {}
2407
2408 template <bool _Dummy = true,
2409 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2410 _LIBCPP_INLINE_VISIBILITY
2411 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) noexcept
2412 : __ptr_(__p, _VSTD::move(__d)) {
2413 static_assert(!is_reference<deleter_type>::value,
2414 "rvalue deleter bound to reference");
2415 }
2416
2417 template <bool _Dummy = true,
2418 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>>
2419 _LIBCPP_INLINE_VISIBILITY
2420 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2421
2422 _LIBCPP_INLINE_VISIBILITY
2423 unique_ptr(unique_ptr&& __u) noexcept
2424 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2425 }
2426
2427 template <class _Up, class _Ep,
2428 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2429 class = _EnableIfDeleterConvertible<_Ep>
2430 >
2431 _LIBCPP_INLINE_VISIBILITY
2432 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2433 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2434
2435#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2436 template <class _Up>
2437 _LIBCPP_INLINE_VISIBILITY
2438 unique_ptr(auto_ptr<_Up>&& __p,
2439 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2440 is_same<_Dp, default_delete<_Tp>>::value,
2441 __nat>::type = __nat()) _NOEXCEPT
2442 : __ptr_(__p.release()) {}
2443#endif
2444
2445 _LIBCPP_INLINE_VISIBILITY
2446 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2447 reset(__u.release());
2448 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2449 return *this;
2450 }
2451
2452 template <class _Up, class _Ep,
2453 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2454 class = _EnableIfDeleterAssignable<_Ep>
2455 >
2456 _LIBCPP_INLINE_VISIBILITY
2457 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2458 reset(__u.release());
2459 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2460 return *this;
2461 }
2462
2463#else // _LIBCPP_CXX03_LANG
2464private:
2465 unique_ptr(unique_ptr&);
2466 template <class _Up, class _Ep> unique_ptr(unique_ptr<_Up, _Ep>&);
2467
2468 unique_ptr& operator=(unique_ptr&);
2469 template <class _Up, class _Ep> unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
2470
2471public:
2472 _LIBCPP_INLINE_VISIBILITY
2473 unique_ptr() : __ptr_(pointer())
2474 {
2475 static_assert(!is_pointer<deleter_type>::value,
2476 "unique_ptr constructed with null function pointer deleter");
2477 static_assert(is_default_constructible<deleter_type>::value,
2478 "unique_ptr::deleter_type is not default constructible");
2479 }
2480 _LIBCPP_INLINE_VISIBILITY
2481 unique_ptr(nullptr_t) : __ptr_(pointer())
2482 {
2483 static_assert(!is_pointer<deleter_type>::value,
2484 "unique_ptr constructed with null function pointer deleter");
2485 }
2486 _LIBCPP_INLINE_VISIBILITY
2487 explicit unique_ptr(pointer __p)
2488 : __ptr_(_VSTD::move(__p)) {
2489 static_assert(!is_pointer<deleter_type>::value,
2490 "unique_ptr constructed with null function pointer deleter");
2491 }
2492
2493 _LIBCPP_INLINE_VISIBILITY
2494 operator __rv<unique_ptr>() {
2495 return __rv<unique_ptr>(*this);
2496 }
2497
2498 _LIBCPP_INLINE_VISIBILITY
2499 unique_ptr(__rv<unique_ptr> __u)
2500 : __ptr_(__u->release(),
2501 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2502
2503 template <class _Up, class _Ep>
2504 _LIBCPP_INLINE_VISIBILITY
2505 typename enable_if<
2506 !is_array<_Up>::value &&
2507 is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
2508 pointer>::value &&
2509 is_assignable<deleter_type&, _Ep&>::value,
2510 unique_ptr&>::type
2511 operator=(unique_ptr<_Up, _Ep> __u) {
2512 reset(__u.release());
2513 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2514 return *this;
2515 }
2516
2517 _LIBCPP_INLINE_VISIBILITY
2518 unique_ptr(pointer __p, deleter_type __d)
2519 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
2520#endif // _LIBCPP_CXX03_LANG
2521
2522#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2523 template <class _Up>
2524 _LIBCPP_INLINE_VISIBILITY
2525 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2526 is_same<_Dp, default_delete<_Tp> >::value,
2527 unique_ptr&>::type
2528 operator=(auto_ptr<_Up> __p) {
2529 reset(__p.release());
2530 return *this;
2531 }
2532#endif
2533
2534 _LIBCPP_INLINE_VISIBILITY
2535 ~unique_ptr() { reset(); }
2536
2537 _LIBCPP_INLINE_VISIBILITY
2538 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2539 reset();
2540 return *this;
2541 }
2542
2543 _LIBCPP_INLINE_VISIBILITY
2544 typename add_lvalue_reference<_Tp>::type
2545 operator*() const {
2546 return *__ptr_.first();
2547 }
2548 _LIBCPP_INLINE_VISIBILITY
2549 pointer operator->() const _NOEXCEPT {
2550 return __ptr_.first();
2551 }
2552 _LIBCPP_INLINE_VISIBILITY
2553 pointer get() const _NOEXCEPT {
2554 return __ptr_.first();
2555 }
2556 _LIBCPP_INLINE_VISIBILITY
2557 deleter_type& get_deleter() _NOEXCEPT {
2558 return __ptr_.second();
2559 }
2560 _LIBCPP_INLINE_VISIBILITY
2561 const deleter_type& get_deleter() const _NOEXCEPT {
2562 return __ptr_.second();
2563 }
2564 _LIBCPP_INLINE_VISIBILITY
2565 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2566 return __ptr_.first() != nullptr;
2567 }
2568
2569 _LIBCPP_INLINE_VISIBILITY
2570 pointer release() _NOEXCEPT {
2571 pointer __t = __ptr_.first();
2572 __ptr_.first() = pointer();
2573 return __t;
2574 }
2575
2576 _LIBCPP_INLINE_VISIBILITY
2577 void reset(pointer __p = pointer()) _NOEXCEPT {
2578 pointer __tmp = __ptr_.first();
2579 __ptr_.first() = __p;
2580 if (__tmp)
2581 __ptr_.second()(__tmp);
2582 }
2583
2584 _LIBCPP_INLINE_VISIBILITY
2585 void swap(unique_ptr& __u) _NOEXCEPT {
2586 __ptr_.swap(__u.__ptr_);
2587 }
2588};
2589
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002590
Howard Hinnantc51e1022010-05-11 19:42:16 +00002591template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002592class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002593public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002594 typedef _Tp element_type;
2595 typedef _Dp deleter_type;
2596 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2597
Howard Hinnantc51e1022010-05-11 19:42:16 +00002598private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002599 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002600
Eric Fiselier31127cd2017-04-16 02:14:31 +00002601 template <class _From>
2602 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002603
Eric Fiselier31127cd2017-04-16 02:14:31 +00002604 template <class _FromElem>
2605 struct _CheckArrayPointerConversion<_FromElem*>
2606 : integral_constant<bool,
2607 is_same<_FromElem*, pointer>::value ||
2608 (is_same<pointer, element_type*>::value &&
2609 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2610 >
2611 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002612
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002613#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier31127cd2017-04-16 02:14:31 +00002614 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002615
2616 template <bool _Dummy>
2617 using _LValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002618 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002619
2620 template <bool _Dummy>
2621 using _GoodRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002622 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002623
2624 template <bool _Dummy>
2625 using _BadRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002626 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002627
2628 template <bool _Dummy, class _Deleter = typename __dependent_type<
2629 __identity<deleter_type>, _Dummy>::type>
2630 using _EnableIfDeleterDefaultConstructible =
2631 typename enable_if<is_default_constructible<_Deleter>::value &&
2632 !is_pointer<_Deleter>::value>::type;
2633
2634 template <class _ArgType>
2635 using _EnableIfDeleterConstructible =
2636 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2637
2638 template <class _Pp>
2639 using _EnableIfPointerConvertible = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002640 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002641 >::type;
2642
2643 template <class _UPtr, class _Up,
2644 class _ElemT = typename _UPtr::element_type>
2645 using _EnableIfMoveConvertible = typename enable_if<
2646 is_array<_Up>::value &&
2647 is_same<pointer, element_type*>::value &&
2648 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2649 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2650 >::type;
2651
2652 template <class _UDel>
2653 using _EnableIfDeleterConvertible = typename enable_if<
2654 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2655 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2656 >::type;
2657
2658 template <class _UDel>
2659 using _EnableIfDeleterAssignable = typename enable_if<
2660 is_assignable<_Dp&, _UDel&&>::value
2661 >::type;
2662
Howard Hinnantc51e1022010-05-11 19:42:16 +00002663public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002664 template <bool _Dummy = true,
2665 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2666 _LIBCPP_INLINE_VISIBILITY
2667 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002668
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002669 template <bool _Dummy = true,
2670 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2671 _LIBCPP_INLINE_VISIBILITY
2672 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002673
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002674 template <class _Pp, bool _Dummy = true,
2675 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
2676 class = _EnableIfPointerConvertible<_Pp>>
2677 _LIBCPP_INLINE_VISIBILITY
2678 explicit unique_ptr(_Pp __p) noexcept
2679 : __ptr_(__p) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002680
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002681 template <class _Pp, bool _Dummy = true,
2682 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>,
2683 class = _EnableIfPointerConvertible<_Pp>>
2684 _LIBCPP_INLINE_VISIBILITY
2685 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) noexcept
2686 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002687
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002688 template <bool _Dummy = true,
2689 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2690 _LIBCPP_INLINE_VISIBILITY
2691 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) noexcept
2692 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002693
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002694 template <class _Pp, bool _Dummy = true,
2695 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>,
2696 class = _EnableIfPointerConvertible<_Pp>>
2697 _LIBCPP_INLINE_VISIBILITY
2698 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) noexcept
2699 : __ptr_(__p, _VSTD::move(__d)) {
2700 static_assert(!is_reference<deleter_type>::value,
2701 "rvalue deleter bound to reference");
2702 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002703
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002704 template <bool _Dummy = true,
2705 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2706 _LIBCPP_INLINE_VISIBILITY
2707 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) noexcept
2708 : __ptr_(nullptr, _VSTD::move(__d)) {
2709 static_assert(!is_reference<deleter_type>::value,
2710 "rvalue deleter bound to reference");
2711 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002712
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002713 template <class _Pp, bool _Dummy = true,
2714 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>,
2715 class = _EnableIfPointerConvertible<_Pp>>
2716 _LIBCPP_INLINE_VISIBILITY
2717 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002718
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002719 _LIBCPP_INLINE_VISIBILITY
2720 unique_ptr(unique_ptr&& __u) noexcept
2721 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2722 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002723
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002724 _LIBCPP_INLINE_VISIBILITY
2725 unique_ptr& operator=(unique_ptr&& __u) noexcept {
2726 reset(__u.release());
2727 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2728 return *this;
2729 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002730
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002731 template <class _Up, class _Ep,
2732 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2733 class = _EnableIfDeleterConvertible<_Ep>
2734 >
2735 _LIBCPP_INLINE_VISIBILITY
2736 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
2737 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2738 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002739
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002740 template <class _Up, class _Ep,
2741 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2742 class = _EnableIfDeleterAssignable<_Ep>
2743 >
2744 _LIBCPP_INLINE_VISIBILITY
2745 unique_ptr&
2746 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept {
2747 reset(__u.release());
2748 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2749 return *this;
2750 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002751
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002752#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002753private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002754 template <class _Up> explicit unique_ptr(_Up);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002755
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002756 unique_ptr(unique_ptr&);
2757 template <class _Up> unique_ptr(unique_ptr<_Up>&);
2758
2759 unique_ptr& operator=(unique_ptr&);
2760 template <class _Up> unique_ptr& operator=(unique_ptr<_Up>&);
2761
2762 template <class _Up>
2763 unique_ptr(_Up __u,
2764 typename conditional<
2765 is_reference<deleter_type>::value, deleter_type,
2766 typename add_lvalue_reference<const deleter_type>::type>::type,
2767 typename enable_if<is_convertible<_Up, pointer>::value,
2768 __nat>::type = __nat());
2769public:
2770 _LIBCPP_INLINE_VISIBILITY
2771 unique_ptr() : __ptr_(pointer()) {
2772 static_assert(!is_pointer<deleter_type>::value,
2773 "unique_ptr constructed with null function pointer deleter");
2774 }
2775 _LIBCPP_INLINE_VISIBILITY
2776 unique_ptr(nullptr_t) : __ptr_(pointer()) {
2777 static_assert(!is_pointer<deleter_type>::value,
2778 "unique_ptr constructed with null function pointer deleter");
2779 }
2780
2781 _LIBCPP_INLINE_VISIBILITY
2782 explicit unique_ptr(pointer __p) : __ptr_(__p) {
2783 static_assert(!is_pointer<deleter_type>::value,
2784 "unique_ptr constructed with null function pointer deleter");
2785 }
2786
2787 _LIBCPP_INLINE_VISIBILITY
2788 unique_ptr(pointer __p, deleter_type __d)
2789 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
2790
2791 _LIBCPP_INLINE_VISIBILITY
2792 unique_ptr(nullptr_t, deleter_type __d)
2793 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
2794
2795 _LIBCPP_INLINE_VISIBILITY
2796 operator __rv<unique_ptr>() {
2797 return __rv<unique_ptr>(*this);
2798 }
2799
2800 _LIBCPP_INLINE_VISIBILITY
2801 unique_ptr(__rv<unique_ptr> __u)
2802 : __ptr_(__u->release(),
2803 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2804
2805 _LIBCPP_INLINE_VISIBILITY
2806 unique_ptr& operator=(__rv<unique_ptr> __u) {
2807 reset(__u->release());
2808 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
2809 return *this;
2810 }
2811
2812#endif // _LIBCPP_CXX03_LANG
2813
2814public:
2815 _LIBCPP_INLINE_VISIBILITY
2816 ~unique_ptr() { reset(); }
2817
2818 _LIBCPP_INLINE_VISIBILITY
2819 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2820 reset();
2821 return *this;
2822 }
2823
2824 _LIBCPP_INLINE_VISIBILITY
2825 typename add_lvalue_reference<_Tp>::type
2826 operator[](size_t __i) const {
2827 return __ptr_.first()[__i];
2828 }
2829 _LIBCPP_INLINE_VISIBILITY
2830 pointer get() const _NOEXCEPT {
2831 return __ptr_.first();
2832 }
2833
2834 _LIBCPP_INLINE_VISIBILITY
2835 deleter_type& get_deleter() _NOEXCEPT {
2836 return __ptr_.second();
2837 }
2838
2839 _LIBCPP_INLINE_VISIBILITY
2840 const deleter_type& get_deleter() const _NOEXCEPT {
2841 return __ptr_.second();
2842 }
2843 _LIBCPP_INLINE_VISIBILITY
2844 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2845 return __ptr_.first() != nullptr;
2846 }
2847
2848 _LIBCPP_INLINE_VISIBILITY
2849 pointer release() _NOEXCEPT {
2850 pointer __t = __ptr_.first();
2851 __ptr_.first() = pointer();
2852 return __t;
2853 }
2854
2855 template <class _Pp>
2856 _LIBCPP_INLINE_VISIBILITY
2857 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002858 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002859 >::type
2860 reset(_Pp __p) _NOEXCEPT {
2861 pointer __tmp = __ptr_.first();
2862 __ptr_.first() = __p;
2863 if (__tmp)
2864 __ptr_.second()(__tmp);
2865 }
2866
2867 _LIBCPP_INLINE_VISIBILITY
2868 void reset(nullptr_t = nullptr) _NOEXCEPT {
2869 pointer __tmp = __ptr_.first();
2870 __ptr_.first() = nullptr;
2871 if (__tmp)
2872 __ptr_.second()(__tmp);
2873 }
2874
2875 _LIBCPP_INLINE_VISIBILITY
2876 void swap(unique_ptr& __u) _NOEXCEPT {
2877 __ptr_.swap(__u.__ptr_);
2878 }
2879
Howard Hinnantc51e1022010-05-11 19:42:16 +00002880};
2881
2882template <class _Tp, class _Dp>
2883inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002884typename enable_if<
2885 __is_swappable<_Dp>::value,
2886 void
2887>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002888swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002889
2890template <class _T1, class _D1, class _T2, class _D2>
2891inline _LIBCPP_INLINE_VISIBILITY
2892bool
2893operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2894
2895template <class _T1, class _D1, class _T2, class _D2>
2896inline _LIBCPP_INLINE_VISIBILITY
2897bool
2898operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2899
2900template <class _T1, class _D1, class _T2, class _D2>
2901inline _LIBCPP_INLINE_VISIBILITY
2902bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002903operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2904{
2905 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2906 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002907 typedef typename common_type<_P1, _P2>::type _Vp;
2908 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002909}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002910
2911template <class _T1, class _D1, class _T2, class _D2>
2912inline _LIBCPP_INLINE_VISIBILITY
2913bool
2914operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2915
2916template <class _T1, class _D1, class _T2, class _D2>
2917inline _LIBCPP_INLINE_VISIBILITY
2918bool
2919operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2920
2921template <class _T1, class _D1, class _T2, class _D2>
2922inline _LIBCPP_INLINE_VISIBILITY
2923bool
2924operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2925
Howard Hinnantb17caf92012-02-21 21:02:58 +00002926template <class _T1, class _D1>
2927inline _LIBCPP_INLINE_VISIBILITY
2928bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002929operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002930{
2931 return !__x;
2932}
2933
2934template <class _T1, class _D1>
2935inline _LIBCPP_INLINE_VISIBILITY
2936bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002937operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002938{
2939 return !__x;
2940}
2941
2942template <class _T1, class _D1>
2943inline _LIBCPP_INLINE_VISIBILITY
2944bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002945operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002946{
2947 return static_cast<bool>(__x);
2948}
2949
2950template <class _T1, class _D1>
2951inline _LIBCPP_INLINE_VISIBILITY
2952bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002953operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002954{
2955 return static_cast<bool>(__x);
2956}
2957
2958template <class _T1, class _D1>
2959inline _LIBCPP_INLINE_VISIBILITY
2960bool
2961operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2962{
2963 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2964 return less<_P1>()(__x.get(), nullptr);
2965}
2966
2967template <class _T1, class _D1>
2968inline _LIBCPP_INLINE_VISIBILITY
2969bool
2970operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2971{
2972 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2973 return less<_P1>()(nullptr, __x.get());
2974}
2975
2976template <class _T1, class _D1>
2977inline _LIBCPP_INLINE_VISIBILITY
2978bool
2979operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2980{
2981 return nullptr < __x;
2982}
2983
2984template <class _T1, class _D1>
2985inline _LIBCPP_INLINE_VISIBILITY
2986bool
2987operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2988{
2989 return __x < nullptr;
2990}
2991
2992template <class _T1, class _D1>
2993inline _LIBCPP_INLINE_VISIBILITY
2994bool
2995operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2996{
2997 return !(nullptr < __x);
2998}
2999
3000template <class _T1, class _D1>
3001inline _LIBCPP_INLINE_VISIBILITY
3002bool
3003operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3004{
3005 return !(__x < nullptr);
3006}
3007
3008template <class _T1, class _D1>
3009inline _LIBCPP_INLINE_VISIBILITY
3010bool
3011operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3012{
3013 return !(__x < nullptr);
3014}
3015
3016template <class _T1, class _D1>
3017inline _LIBCPP_INLINE_VISIBILITY
3018bool
3019operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3020{
3021 return !(nullptr < __x);
3022}
3023
Howard Hinnant9e028f12012-05-01 15:37:54 +00003024#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3025
3026template <class _Tp, class _Dp>
3027inline _LIBCPP_INLINE_VISIBILITY
3028unique_ptr<_Tp, _Dp>
3029move(unique_ptr<_Tp, _Dp>& __t)
3030{
3031 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3032}
3033
3034#endif
3035
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003036#if _LIBCPP_STD_VER > 11
3037
3038template<class _Tp>
3039struct __unique_if
3040{
3041 typedef unique_ptr<_Tp> __unique_single;
3042};
3043
3044template<class _Tp>
3045struct __unique_if<_Tp[]>
3046{
3047 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3048};
3049
3050template<class _Tp, size_t _Np>
3051struct __unique_if<_Tp[_Np]>
3052{
3053 typedef void __unique_array_known_bound;
3054};
3055
3056template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003057inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003058typename __unique_if<_Tp>::__unique_single
3059make_unique(_Args&&... __args)
3060{
3061 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3062}
3063
3064template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003065inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003066typename __unique_if<_Tp>::__unique_array_unknown_bound
3067make_unique(size_t __n)
3068{
3069 typedef typename remove_extent<_Tp>::type _Up;
3070 return unique_ptr<_Tp>(new _Up[__n]());
3071}
3072
3073template<class _Tp, class... _Args>
3074 typename __unique_if<_Tp>::__unique_array_known_bound
3075 make_unique(_Args&&...) = delete;
3076
3077#endif // _LIBCPP_STD_VER > 11
3078
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003079template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003080#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003081struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003082#else
3083struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
3084 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer>>
3085#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003086{
3087 typedef unique_ptr<_Tp, _Dp> argument_type;
3088 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003089 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003090 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003091 {
3092 typedef typename argument_type::pointer pointer;
3093 return hash<pointer>()(__ptr.get());
3094 }
3095};
3096
Howard Hinnantc51e1022010-05-11 19:42:16 +00003097struct __destruct_n
3098{
3099private:
3100 size_t size;
3101
3102 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003103 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003104 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3105
3106 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003107 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003108 {}
3109
Howard Hinnant719bda32011-05-28 14:41:13 +00003110 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003111 {++size;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003112 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003113 {}
3114
Howard Hinnant719bda32011-05-28 14:41:13 +00003115 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003116 {size = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003117 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003118 {}
3119public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003120 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3121 : size(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003122
3123 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003124 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003125 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003126
3127 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003128 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003129 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003130
3131 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003132 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003133 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003134};
3135
3136template <class _Alloc>
3137class __allocator_destructor
3138{
3139 typedef allocator_traits<_Alloc> __alloc_traits;
3140public:
3141 typedef typename __alloc_traits::pointer pointer;
3142 typedef typename __alloc_traits::size_type size_type;
3143private:
3144 _Alloc& __alloc_;
3145 size_type __s_;
3146public:
3147 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003148 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003149 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003150 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003151 void operator()(pointer __p) _NOEXCEPT
3152 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003153};
3154
3155template <class _InputIterator, class _ForwardIterator>
3156_ForwardIterator
3157uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3158{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003159 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003160#ifndef _LIBCPP_NO_EXCEPTIONS
3161 _ForwardIterator __s = __r;
3162 try
3163 {
3164#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003165 for (; __f != __l; ++__f, (void) ++__r)
3166 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003167#ifndef _LIBCPP_NO_EXCEPTIONS
3168 }
3169 catch (...)
3170 {
3171 for (; __s != __r; ++__s)
3172 __s->~value_type();
3173 throw;
3174 }
3175#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003176 return __r;
3177}
3178
3179template <class _InputIterator, class _Size, class _ForwardIterator>
3180_ForwardIterator
3181uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3182{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003183 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003184#ifndef _LIBCPP_NO_EXCEPTIONS
3185 _ForwardIterator __s = __r;
3186 try
3187 {
3188#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003189 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3190 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003191#ifndef _LIBCPP_NO_EXCEPTIONS
3192 }
3193 catch (...)
3194 {
3195 for (; __s != __r; ++__s)
3196 __s->~value_type();
3197 throw;
3198 }
3199#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003200 return __r;
3201}
3202
3203template <class _ForwardIterator, class _Tp>
3204void
3205uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3206{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003207 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003208#ifndef _LIBCPP_NO_EXCEPTIONS
3209 _ForwardIterator __s = __f;
3210 try
3211 {
3212#endif
3213 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003214 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003215#ifndef _LIBCPP_NO_EXCEPTIONS
3216 }
3217 catch (...)
3218 {
3219 for (; __s != __f; ++__s)
3220 __s->~value_type();
3221 throw;
3222 }
3223#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003224}
3225
3226template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003227_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003228uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3229{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003230 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003231#ifndef _LIBCPP_NO_EXCEPTIONS
3232 _ForwardIterator __s = __f;
3233 try
3234 {
3235#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003236 for (; __n > 0; ++__f, (void) --__n)
3237 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003238#ifndef _LIBCPP_NO_EXCEPTIONS
3239 }
3240 catch (...)
3241 {
3242 for (; __s != __f; ++__s)
3243 __s->~value_type();
3244 throw;
3245 }
3246#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003247 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003248}
3249
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003250#if _LIBCPP_STD_VER > 14
3251
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003252template <class _Tp>
3253inline _LIBCPP_INLINE_VISIBILITY
3254void destroy_at(_Tp* __loc) {
3255 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3256 __loc->~_Tp();
3257}
3258
3259template <class _ForwardIterator>
3260inline _LIBCPP_INLINE_VISIBILITY
3261void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3262 for (; __first != __last; ++__first)
3263 _VSTD::destroy_at(_VSTD::addressof(*__first));
3264}
3265
3266template <class _ForwardIterator, class _Size>
3267inline _LIBCPP_INLINE_VISIBILITY
3268_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3269 for (; __n > 0; (void)++__first, --__n)
3270 _VSTD::destroy_at(_VSTD::addressof(*__first));
3271 return __first;
3272}
3273
Eric Fiselier290c07c2016-10-11 21:13:44 +00003274template <class _ForwardIterator>
3275inline _LIBCPP_INLINE_VISIBILITY
3276void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3277 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3278 auto __idx = __first;
3279#ifndef _LIBCPP_NO_EXCEPTIONS
3280 try {
3281#endif
3282 for (; __idx != __last; ++__idx)
3283 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3284#ifndef _LIBCPP_NO_EXCEPTIONS
3285 } catch (...) {
3286 _VSTD::destroy(__first, __idx);
3287 throw;
3288 }
3289#endif
3290}
3291
3292template <class _ForwardIterator, class _Size>
3293inline _LIBCPP_INLINE_VISIBILITY
3294_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3295 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3296 auto __idx = __first;
3297#ifndef _LIBCPP_NO_EXCEPTIONS
3298 try {
3299#endif
3300 for (; __n > 0; (void)++__idx, --__n)
3301 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3302 return __idx;
3303#ifndef _LIBCPP_NO_EXCEPTIONS
3304 } catch (...) {
3305 _VSTD::destroy(__first, __idx);
3306 throw;
3307 }
3308#endif
3309}
3310
3311
3312template <class _ForwardIterator>
3313inline _LIBCPP_INLINE_VISIBILITY
3314void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3315 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3316 auto __idx = __first;
3317#ifndef _LIBCPP_NO_EXCEPTIONS
3318 try {
3319#endif
3320 for (; __idx != __last; ++__idx)
3321 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3322#ifndef _LIBCPP_NO_EXCEPTIONS
3323 } catch (...) {
3324 _VSTD::destroy(__first, __idx);
3325 throw;
3326 }
3327#endif
3328}
3329
3330template <class _ForwardIterator, class _Size>
3331inline _LIBCPP_INLINE_VISIBILITY
3332_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3333 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3334 auto __idx = __first;
3335#ifndef _LIBCPP_NO_EXCEPTIONS
3336 try {
3337#endif
3338 for (; __n > 0; (void)++__idx, --__n)
3339 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3340 return __idx;
3341#ifndef _LIBCPP_NO_EXCEPTIONS
3342 } catch (...) {
3343 _VSTD::destroy(__first, __idx);
3344 throw;
3345 }
3346#endif
3347}
3348
3349
3350template <class _InputIt, class _ForwardIt>
3351inline _LIBCPP_INLINE_VISIBILITY
3352_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3353 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3354 auto __idx = __first_res;
3355#ifndef _LIBCPP_NO_EXCEPTIONS
3356 try {
3357#endif
3358 for (; __first != __last; (void)++__idx, ++__first)
3359 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3360 return __idx;
3361#ifndef _LIBCPP_NO_EXCEPTIONS
3362 } catch (...) {
3363 _VSTD::destroy(__first_res, __idx);
3364 throw;
3365 }
3366#endif
3367}
3368
3369template <class _InputIt, class _Size, class _ForwardIt>
3370inline _LIBCPP_INLINE_VISIBILITY
3371pair<_InputIt, _ForwardIt>
3372uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3373 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3374 auto __idx = __first_res;
3375#ifndef _LIBCPP_NO_EXCEPTIONS
3376 try {
3377#endif
3378 for (; __n > 0; ++__idx, (void)++__first, --__n)
3379 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3380 return {__first, __idx};
3381#ifndef _LIBCPP_NO_EXCEPTIONS
3382 } catch (...) {
3383 _VSTD::destroy(__first_res, __idx);
3384 throw;
3385 }
3386#endif
3387}
3388
3389
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003390#endif // _LIBCPP_STD_VER > 14
3391
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003392// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3393// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003394// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003395#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3396 && defined(__ATOMIC_RELAXED) \
3397 && defined(__ATOMIC_ACQ_REL)
3398# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3399#elif !defined(__clang__) && defined(_GNUC_VER) && _GNUC_VER >= 407
3400# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3401#endif
3402
3403template <class _Tp>
3404inline _LIBCPP_INLINE_VISIBILITY _Tp
3405__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3406{
3407#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3408 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3409#else
3410 return __t += 1;
3411#endif
3412}
3413
3414template <class _Tp>
3415inline _LIBCPP_INLINE_VISIBILITY _Tp
3416__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3417{
3418#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3419 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3420#else
3421 return __t -= 1;
3422#endif
3423}
3424
Howard Hinnant756c69b2010-09-22 16:48:34 +00003425class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003426 : public std::exception
3427{
3428public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003429 virtual ~bad_weak_ptr() _NOEXCEPT;
3430 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003431};
3432
Marshall Clow8fea1612016-08-25 15:09:01 +00003433_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
3434void __throw_bad_weak_ptr()
3435{
3436#ifndef _LIBCPP_NO_EXCEPTIONS
3437 throw bad_weak_ptr();
3438#else
3439 _VSTD::abort();
3440#endif
3441}
3442
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003443template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003444
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003445class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003446{
3447 __shared_count(const __shared_count&);
3448 __shared_count& operator=(const __shared_count&);
3449
3450protected:
3451 long __shared_owners_;
3452 virtual ~__shared_count();
3453private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003454 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003455
3456public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003458 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003459 : __shared_owners_(__refs) {}
3460
Eric Fiselier98848572017-01-17 03:16:26 +00003461#if defined(_LIBCPP_BUILDING_MEMORY) && \
3462 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003463 void __add_shared() _NOEXCEPT;
3464 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003465#else
3466 _LIBCPP_INLINE_VISIBILITY
3467 void __add_shared() _NOEXCEPT {
3468 __libcpp_atomic_refcount_increment(__shared_owners_);
3469 }
3470 _LIBCPP_INLINE_VISIBILITY
3471 bool __release_shared() _NOEXCEPT {
3472 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3473 __on_zero_shared();
3474 return true;
3475 }
3476 return false;
3477 }
3478#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003479 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003480 long use_count() const _NOEXCEPT {
3481 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3482 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003483};
3484
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003485class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003486 : private __shared_count
3487{
3488 long __shared_weak_owners_;
3489
3490public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003491 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003492 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003493 : __shared_count(__refs),
3494 __shared_weak_owners_(__refs) {}
3495protected:
3496 virtual ~__shared_weak_count();
3497
3498public:
Eric Fiselier98848572017-01-17 03:16:26 +00003499#if defined(_LIBCPP_BUILDING_MEMORY) && \
3500 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003501 void __add_shared() _NOEXCEPT;
3502 void __add_weak() _NOEXCEPT;
3503 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003504#else
3505 _LIBCPP_INLINE_VISIBILITY
3506 void __add_shared() _NOEXCEPT {
3507 __shared_count::__add_shared();
3508 }
3509 _LIBCPP_INLINE_VISIBILITY
3510 void __add_weak() _NOEXCEPT {
3511 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3512 }
3513 _LIBCPP_INLINE_VISIBILITY
3514 void __release_shared() _NOEXCEPT {
3515 if (__shared_count::__release_shared())
3516 __release_weak();
3517 }
3518#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003519 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003520 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003521 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3522 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003523
Howard Hinnant807d6332013-02-25 15:50:36 +00003524 // Define the function out only if we build static libc++ without RTTI.
3525 // Otherwise we may break clients who need to compile their projects with
3526 // -fno-rtti and yet link against a libc++.dylib compiled
3527 // without -fno-rtti.
3528#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003529 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003530#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003531private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003532 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003533};
3534
3535template <class _Tp, class _Dp, class _Alloc>
3536class __shared_ptr_pointer
3537 : public __shared_weak_count
3538{
3539 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3540public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003542 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003543 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003544
Howard Hinnant72f73582010-08-11 17:04:31 +00003545#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003546 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003547#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548
3549private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003550 virtual void __on_zero_shared() _NOEXCEPT;
3551 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003552};
3553
Howard Hinnant72f73582010-08-11 17:04:31 +00003554#ifndef _LIBCPP_NO_RTTI
3555
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556template <class _Tp, class _Dp, class _Alloc>
3557const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003558__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559{
Marshall Clowa8dfc942014-11-17 19:05:50 +00003560 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003561}
3562
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003563#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003564
Howard Hinnantc51e1022010-05-11 19:42:16 +00003565template <class _Tp, class _Dp, class _Alloc>
3566void
Howard Hinnant719bda32011-05-28 14:41:13 +00003567__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003568{
3569 __data_.first().second()(__data_.first().first());
3570 __data_.first().second().~_Dp();
3571}
3572
3573template <class _Tp, class _Dp, class _Alloc>
3574void
Howard Hinnant719bda32011-05-28 14:41:13 +00003575__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003576{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003577 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3578 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003579 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3580
Eric Fiselierf8898c82015-02-05 23:01:40 +00003581 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003582 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003583 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003584}
3585
3586template <class _Tp, class _Alloc>
3587class __shared_ptr_emplace
3588 : public __shared_weak_count
3589{
3590 __compressed_pair<_Alloc, _Tp> __data_;
3591public:
3592#ifndef _LIBCPP_HAS_NO_VARIADICS
3593
Howard Hinnant756c69b2010-09-22 16:48:34 +00003594 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003595 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003596 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003597
3598 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003600 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003601 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3602 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003603
3604#else // _LIBCPP_HAS_NO_VARIADICS
3605
Howard Hinnant756c69b2010-09-22 16:48:34 +00003606 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003607 __shared_ptr_emplace(_Alloc __a)
3608 : __data_(__a) {}
3609
3610 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003611 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003612 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3613 : __data_(__a, _Tp(__a0)) {}
3614
3615 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003616 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3618 : __data_(__a, _Tp(__a0, __a1)) {}
3619
3620 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003621 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003622 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3623 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3624
3625#endif // _LIBCPP_HAS_NO_VARIADICS
3626
3627private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003628 virtual void __on_zero_shared() _NOEXCEPT;
3629 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003630public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003631 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003632 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003633};
3634
3635template <class _Tp, class _Alloc>
3636void
Howard Hinnant719bda32011-05-28 14:41:13 +00003637__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003638{
3639 __data_.second().~_Tp();
3640}
3641
3642template <class _Tp, class _Alloc>
3643void
Howard Hinnant719bda32011-05-28 14:41:13 +00003644__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003645{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003646 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3647 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003648 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003649 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003650 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003651 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003652}
3653
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003654template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003655
3656template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003657class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003658{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003659public:
3660 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003661
Eric Fiselierae5b6672016-06-27 01:02:43 +00003662#if _LIBCPP_STD_VER > 14
3663 typedef weak_ptr<_Tp> weak_type;
3664#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003665private:
3666 element_type* __ptr_;
3667 __shared_weak_count* __cntrl_;
3668
3669 struct __nat {int __for_bool_;};
3670public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003671 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003672 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003673 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003674 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003675 template<class _Yp>
3676 explicit shared_ptr(_Yp* __p,
3677 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3678 template<class _Yp, class _Dp>
3679 shared_ptr(_Yp* __p, _Dp __d,
3680 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3681 template<class _Yp, class _Dp, class _Alloc>
3682 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3683 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003684 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3685 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003686 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3687 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003688 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003689 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003690 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003691 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003692 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003693 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003694#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003695 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003696 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003697 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003698 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003699 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003700#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003701 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003702 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003703#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003704#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003705 template<class _Yp>
3706 shared_ptr(auto_ptr<_Yp>&& __r,
3707 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003708#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003709 template<class _Yp>
3710 shared_ptr(auto_ptr<_Yp> __r,
3711 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003712#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003713#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003714#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003715 template <class _Yp, class _Dp>
3716 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3717 typename enable_if
3718 <
3719 !is_lvalue_reference<_Dp>::value &&
3720 !is_array<_Yp>::value &&
3721 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3722 __nat
3723 >::type = __nat());
3724 template <class _Yp, class _Dp>
3725 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3726 typename enable_if
3727 <
3728 is_lvalue_reference<_Dp>::value &&
3729 !is_array<_Yp>::value &&
3730 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3731 __nat
3732 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003733#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003734 template <class _Yp, class _Dp>
3735 shared_ptr(unique_ptr<_Yp, _Dp>,
3736 typename enable_if
3737 <
3738 !is_lvalue_reference<_Dp>::value &&
3739 !is_array<_Yp>::value &&
3740 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3741 __nat
3742 >::type = __nat());
3743 template <class _Yp, class _Dp>
3744 shared_ptr(unique_ptr<_Yp, _Dp>,
3745 typename enable_if
3746 <
3747 is_lvalue_reference<_Dp>::value &&
3748 !is_array<_Yp>::value &&
3749 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3750 __nat
3751 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003752#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003753
3754 ~shared_ptr();
3755
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003756 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003757 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003758 template<class _Yp>
3759 typename enable_if
3760 <
3761 is_convertible<_Yp*, element_type*>::value,
3762 shared_ptr&
3763 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003764 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003765 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003766#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003767 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003768 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003769 template<class _Yp>
3770 typename enable_if
3771 <
3772 is_convertible<_Yp*, element_type*>::value,
3773 shared_ptr<_Tp>&
3774 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003776 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003777#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003778 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003779 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003780 typename enable_if
3781 <
3782 !is_array<_Yp>::value &&
3783 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003784 shared_ptr
3785 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003786 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003787#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003788#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003789#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003790 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003792 typename enable_if
3793 <
3794 !is_array<_Yp>::value &&
3795 is_convertible<_Yp*, element_type*>::value,
3796 shared_ptr&
3797 >::type
3798 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003799#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003800#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003801 template <class _Yp, class _Dp>
3802 typename enable_if
3803 <
3804 !is_array<_Yp>::value &&
3805 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3806 shared_ptr&
3807 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003808#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003809 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003810 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003811#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003812 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003813 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003814#endif
3815
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003816 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003817 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003818 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003819 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003820 template<class _Yp>
3821 typename enable_if
3822 <
3823 is_convertible<_Yp*, element_type*>::value,
3824 void
3825 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003826 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003827 reset(_Yp* __p);
3828 template<class _Yp, class _Dp>
3829 typename enable_if
3830 <
3831 is_convertible<_Yp*, element_type*>::value,
3832 void
3833 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003834 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003835 reset(_Yp* __p, _Dp __d);
3836 template<class _Yp, class _Dp, class _Alloc>
3837 typename enable_if
3838 <
3839 is_convertible<_Yp*, element_type*>::value,
3840 void
3841 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003842 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003843 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003844
Howard Hinnant756c69b2010-09-22 16:48:34 +00003845 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003846 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003847 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003848 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3849 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003850 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003851 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003852 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003853 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003854 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003855 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003857 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003858 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003859 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003860 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003861 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003862 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003863 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003864 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003865 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003866 _LIBCPP_INLINE_VISIBILITY
3867 bool
3868 __owner_equivalent(const shared_ptr& __p) const
3869 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003870
Howard Hinnant72f73582010-08-11 17:04:31 +00003871#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003872 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003873 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003874 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003875 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003876#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003877
3878#ifndef _LIBCPP_HAS_NO_VARIADICS
3879
3880 template<class ..._Args>
3881 static
3882 shared_ptr<_Tp>
3883 make_shared(_Args&& ...__args);
3884
3885 template<class _Alloc, class ..._Args>
3886 static
3887 shared_ptr<_Tp>
3888 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3889
3890#else // _LIBCPP_HAS_NO_VARIADICS
3891
3892 static shared_ptr<_Tp> make_shared();
3893
3894 template<class _A0>
3895 static shared_ptr<_Tp> make_shared(_A0&);
3896
3897 template<class _A0, class _A1>
3898 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3899
3900 template<class _A0, class _A1, class _A2>
3901 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3902
3903 template<class _Alloc>
3904 static shared_ptr<_Tp>
3905 allocate_shared(const _Alloc& __a);
3906
3907 template<class _Alloc, class _A0>
3908 static shared_ptr<_Tp>
3909 allocate_shared(const _Alloc& __a, _A0& __a0);
3910
3911 template<class _Alloc, class _A0, class _A1>
3912 static shared_ptr<_Tp>
3913 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3914
3915 template<class _Alloc, class _A0, class _A1, class _A2>
3916 static shared_ptr<_Tp>
3917 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3918
3919#endif // _LIBCPP_HAS_NO_VARIADICS
3920
3921private:
3922
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003923 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003924 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003925 void
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003926 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3927 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003928 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003929 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003930 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003931 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003932 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3933 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003934 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003935 }
3936
Howard Hinnant756c69b2010-09-22 16:48:34 +00003937 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003938 void __enable_weak_this(const volatile void*, const volatile void*) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003939
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003940 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3941 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003942};
3943
3944template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003945inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003946_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003947shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003948 : __ptr_(0),
3949 __cntrl_(0)
3950{
3951}
3952
3953template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003954inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003955_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003956shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003957 : __ptr_(0),
3958 __cntrl_(0)
3959{
3960}
3961
3962template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003963template<class _Yp>
3964shared_ptr<_Tp>::shared_ptr(_Yp* __p,
3965 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003966 : __ptr_(__p)
3967{
3968 unique_ptr<_Yp> __hold(__p);
3969 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3970 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
3971 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003972 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003973}
3974
3975template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003976template<class _Yp, class _Dp>
3977shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
3978 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003979 : __ptr_(__p)
3980{
3981#ifndef _LIBCPP_NO_EXCEPTIONS
3982 try
3983 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003984#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003985 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3986 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003987 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003988#ifndef _LIBCPP_NO_EXCEPTIONS
3989 }
3990 catch (...)
3991 {
3992 __d(__p);
3993 throw;
3994 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003995#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003996}
3997
3998template<class _Tp>
3999template<class _Dp>
4000shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4001 : __ptr_(0)
4002{
4003#ifndef _LIBCPP_NO_EXCEPTIONS
4004 try
4005 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004006#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004007 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4008 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4009#ifndef _LIBCPP_NO_EXCEPTIONS
4010 }
4011 catch (...)
4012 {
4013 __d(__p);
4014 throw;
4015 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004016#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004017}
4018
4019template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004020template<class _Yp, class _Dp, class _Alloc>
4021shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4022 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004023 : __ptr_(__p)
4024{
4025#ifndef _LIBCPP_NO_EXCEPTIONS
4026 try
4027 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004028#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004029 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004030 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004031 typedef __allocator_destructor<_A2> _D2;
4032 _A2 __a2(__a);
4033 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004034 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4035 _CntrlBlk(__p, __d, __a);
4036 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004037 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004038#ifndef _LIBCPP_NO_EXCEPTIONS
4039 }
4040 catch (...)
4041 {
4042 __d(__p);
4043 throw;
4044 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004045#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004046}
4047
4048template<class _Tp>
4049template<class _Dp, class _Alloc>
4050shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4051 : __ptr_(0)
4052{
4053#ifndef _LIBCPP_NO_EXCEPTIONS
4054 try
4055 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004056#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004057 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004058 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004059 typedef __allocator_destructor<_A2> _D2;
4060 _A2 __a2(__a);
4061 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004062 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4063 _CntrlBlk(__p, __d, __a);
4064 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004065#ifndef _LIBCPP_NO_EXCEPTIONS
4066 }
4067 catch (...)
4068 {
4069 __d(__p);
4070 throw;
4071 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004072#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004073}
4074
4075template<class _Tp>
4076template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004077inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004078shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004079 : __ptr_(__p),
4080 __cntrl_(__r.__cntrl_)
4081{
4082 if (__cntrl_)
4083 __cntrl_->__add_shared();
4084}
4085
4086template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004087inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004088shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004089 : __ptr_(__r.__ptr_),
4090 __cntrl_(__r.__cntrl_)
4091{
4092 if (__cntrl_)
4093 __cntrl_->__add_shared();
4094}
4095
4096template<class _Tp>
4097template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004098inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004099shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004100 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004101 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004102 : __ptr_(__r.__ptr_),
4103 __cntrl_(__r.__cntrl_)
4104{
4105 if (__cntrl_)
4106 __cntrl_->__add_shared();
4107}
4108
Howard Hinnant74279a52010-09-04 23:28:19 +00004109#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004110
4111template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004112inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004113shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004114 : __ptr_(__r.__ptr_),
4115 __cntrl_(__r.__cntrl_)
4116{
4117 __r.__ptr_ = 0;
4118 __r.__cntrl_ = 0;
4119}
4120
4121template<class _Tp>
4122template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004123inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004124shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004125 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004126 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004127 : __ptr_(__r.__ptr_),
4128 __cntrl_(__r.__cntrl_)
4129{
4130 __r.__ptr_ = 0;
4131 __r.__cntrl_ = 0;
4132}
4133
Howard Hinnant74279a52010-09-04 23:28:19 +00004134#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004135
Marshall Clowb22274f2017-01-24 22:22:33 +00004136#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004137template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004138template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004139#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004140shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004141#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004142shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004143#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004144 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004145 : __ptr_(__r.get())
4146{
4147 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4148 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004149 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004150 __r.release();
4151}
Marshall Clowb22274f2017-01-24 22:22:33 +00004152#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004153
4154template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004155template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004156#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004157shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4158#else
4159shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4160#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004161 typename enable_if
4162 <
4163 !is_lvalue_reference<_Dp>::value &&
4164 !is_array<_Yp>::value &&
4165 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4166 __nat
4167 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004168 : __ptr_(__r.get())
4169{
Marshall Clow35cde742015-05-10 13:59:45 +00004170#if _LIBCPP_STD_VER > 11
4171 if (__ptr_ == nullptr)
4172 __cntrl_ = nullptr;
4173 else
4174#endif
4175 {
4176 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4177 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004178 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004179 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004180 __r.release();
4181}
4182
4183template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004184template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004185#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004186shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4187#else
4188shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4189#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004190 typename enable_if
4191 <
4192 is_lvalue_reference<_Dp>::value &&
4193 !is_array<_Yp>::value &&
4194 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4195 __nat
4196 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004197 : __ptr_(__r.get())
4198{
Marshall Clow35cde742015-05-10 13:59:45 +00004199#if _LIBCPP_STD_VER > 11
4200 if (__ptr_ == nullptr)
4201 __cntrl_ = nullptr;
4202 else
4203#endif
4204 {
4205 typedef __shared_ptr_pointer<_Yp*,
4206 reference_wrapper<typename remove_reference<_Dp>::type>,
4207 allocator<_Yp> > _CntrlBlk;
4208 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004209 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004210 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004211 __r.release();
4212}
4213
4214#ifndef _LIBCPP_HAS_NO_VARIADICS
4215
4216template<class _Tp>
4217template<class ..._Args>
4218shared_ptr<_Tp>
4219shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4220{
4221 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4222 typedef allocator<_CntrlBlk> _A2;
4223 typedef __allocator_destructor<_A2> _D2;
4224 _A2 __a2;
4225 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004226 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004227 shared_ptr<_Tp> __r;
4228 __r.__ptr_ = __hold2.get()->get();
4229 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004230 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004231 return __r;
4232}
4233
4234template<class _Tp>
4235template<class _Alloc, class ..._Args>
4236shared_ptr<_Tp>
4237shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4238{
4239 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004240 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004241 typedef __allocator_destructor<_A2> _D2;
4242 _A2 __a2(__a);
4243 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004244 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4245 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004246 shared_ptr<_Tp> __r;
4247 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004248 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004249 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004250 return __r;
4251}
4252
4253#else // _LIBCPP_HAS_NO_VARIADICS
4254
4255template<class _Tp>
4256shared_ptr<_Tp>
4257shared_ptr<_Tp>::make_shared()
4258{
4259 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4260 typedef allocator<_CntrlBlk> _Alloc2;
4261 typedef __allocator_destructor<_Alloc2> _D2;
4262 _Alloc2 __alloc2;
4263 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4264 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4265 shared_ptr<_Tp> __r;
4266 __r.__ptr_ = __hold2.get()->get();
4267 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004268 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004269 return __r;
4270}
4271
4272template<class _Tp>
4273template<class _A0>
4274shared_ptr<_Tp>
4275shared_ptr<_Tp>::make_shared(_A0& __a0)
4276{
4277 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4278 typedef allocator<_CntrlBlk> _Alloc2;
4279 typedef __allocator_destructor<_Alloc2> _D2;
4280 _Alloc2 __alloc2;
4281 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4282 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4283 shared_ptr<_Tp> __r;
4284 __r.__ptr_ = __hold2.get()->get();
4285 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004286 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004287 return __r;
4288}
4289
4290template<class _Tp>
4291template<class _A0, class _A1>
4292shared_ptr<_Tp>
4293shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4294{
4295 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4296 typedef allocator<_CntrlBlk> _Alloc2;
4297 typedef __allocator_destructor<_Alloc2> _D2;
4298 _Alloc2 __alloc2;
4299 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4300 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4301 shared_ptr<_Tp> __r;
4302 __r.__ptr_ = __hold2.get()->get();
4303 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004304 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004305 return __r;
4306}
4307
4308template<class _Tp>
4309template<class _A0, class _A1, class _A2>
4310shared_ptr<_Tp>
4311shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4312{
4313 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4314 typedef allocator<_CntrlBlk> _Alloc2;
4315 typedef __allocator_destructor<_Alloc2> _D2;
4316 _Alloc2 __alloc2;
4317 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4318 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4319 shared_ptr<_Tp> __r;
4320 __r.__ptr_ = __hold2.get()->get();
4321 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004322 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004323 return __r;
4324}
4325
4326template<class _Tp>
4327template<class _Alloc>
4328shared_ptr<_Tp>
4329shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4330{
4331 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004332 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004333 typedef __allocator_destructor<_Alloc2> _D2;
4334 _Alloc2 __alloc2(__a);
4335 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004336 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4337 _CntrlBlk(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004338 shared_ptr<_Tp> __r;
4339 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004340 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004341 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004342 return __r;
4343}
4344
4345template<class _Tp>
4346template<class _Alloc, class _A0>
4347shared_ptr<_Tp>
4348shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4349{
4350 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004351 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004352 typedef __allocator_destructor<_Alloc2> _D2;
4353 _Alloc2 __alloc2(__a);
4354 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004355 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4356 _CntrlBlk(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004357 shared_ptr<_Tp> __r;
4358 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004359 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004360 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004361 return __r;
4362}
4363
4364template<class _Tp>
4365template<class _Alloc, class _A0, class _A1>
4366shared_ptr<_Tp>
4367shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4368{
4369 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004370 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004371 typedef __allocator_destructor<_Alloc2> _D2;
4372 _Alloc2 __alloc2(__a);
4373 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004374 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4375 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004376 shared_ptr<_Tp> __r;
4377 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004378 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004379 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004380 return __r;
4381}
4382
4383template<class _Tp>
4384template<class _Alloc, class _A0, class _A1, class _A2>
4385shared_ptr<_Tp>
4386shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4387{
4388 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004389 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004390 typedef __allocator_destructor<_Alloc2> _D2;
4391 _Alloc2 __alloc2(__a);
4392 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004393 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4394 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004395 shared_ptr<_Tp> __r;
4396 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004397 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004398 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004399 return __r;
4400}
4401
4402#endif // _LIBCPP_HAS_NO_VARIADICS
4403
4404template<class _Tp>
4405shared_ptr<_Tp>::~shared_ptr()
4406{
4407 if (__cntrl_)
4408 __cntrl_->__release_shared();
4409}
4410
4411template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004412inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004413shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004414shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004415{
4416 shared_ptr(__r).swap(*this);
4417 return *this;
4418}
4419
4420template<class _Tp>
4421template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004422inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004423typename enable_if
4424<
Marshall Clow7e384b72017-01-10 16:59:33 +00004425 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004426 shared_ptr<_Tp>&
4427>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004428shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004429{
4430 shared_ptr(__r).swap(*this);
4431 return *this;
4432}
4433
Howard Hinnant74279a52010-09-04 23:28:19 +00004434#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004435
4436template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004437inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004438shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004439shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004440{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004441 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004442 return *this;
4443}
4444
4445template<class _Tp>
4446template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004447inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004448typename enable_if
4449<
Marshall Clow7e384b72017-01-10 16:59:33 +00004450 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004451 shared_ptr<_Tp>&
4452>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004453shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4454{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004455 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004456 return *this;
4457}
4458
Marshall Clowb22274f2017-01-24 22:22:33 +00004459#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004460template<class _Tp>
4461template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004462inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004463typename enable_if
4464<
4465 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004466 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004467 shared_ptr<_Tp>
4468>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004469shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4470{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004471 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004472 return *this;
4473}
Marshall Clowb22274f2017-01-24 22:22:33 +00004474#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004475
4476template<class _Tp>
4477template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004478inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004479typename enable_if
4480<
4481 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004482 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4483 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004484 shared_ptr<_Tp>&
4485>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004486shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4487{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004488 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004489 return *this;
4490}
4491
Howard Hinnant74279a52010-09-04 23:28:19 +00004492#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004493
Marshall Clowb22274f2017-01-24 22:22:33 +00004494#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004495template<class _Tp>
4496template<class _Yp>
4497inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004498typename enable_if
4499<
4500 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004501 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004502 shared_ptr<_Tp>&
4503>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004504shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004505{
4506 shared_ptr(__r).swap(*this);
4507 return *this;
4508}
Marshall Clowb22274f2017-01-24 22:22:33 +00004509#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004510
4511template<class _Tp>
4512template <class _Yp, class _Dp>
4513inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004514typename enable_if
4515<
4516 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004517 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4518 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004519 shared_ptr<_Tp>&
4520>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004521shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4522{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004523 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004524 return *this;
4525}
4526
Howard Hinnant74279a52010-09-04 23:28:19 +00004527#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004528
4529template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004530inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004531void
Howard Hinnant719bda32011-05-28 14:41:13 +00004532shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004533{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004534 _VSTD::swap(__ptr_, __r.__ptr_);
4535 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004536}
4537
4538template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004539inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004540void
Howard Hinnant719bda32011-05-28 14:41:13 +00004541shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004542{
4543 shared_ptr().swap(*this);
4544}
4545
4546template<class _Tp>
4547template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004548inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004549typename enable_if
4550<
Marshall Clow7e384b72017-01-10 16:59:33 +00004551 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004552 void
4553>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004554shared_ptr<_Tp>::reset(_Yp* __p)
4555{
4556 shared_ptr(__p).swap(*this);
4557}
4558
4559template<class _Tp>
4560template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004561inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004562typename enable_if
4563<
Marshall Clow7e384b72017-01-10 16:59:33 +00004564 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004565 void
4566>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004567shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4568{
4569 shared_ptr(__p, __d).swap(*this);
4570}
4571
4572template<class _Tp>
4573template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004574inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004575typename enable_if
4576<
Marshall Clow7e384b72017-01-10 16:59:33 +00004577 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004578 void
4579>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004580shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4581{
4582 shared_ptr(__p, __d, __a).swap(*this);
4583}
4584
4585#ifndef _LIBCPP_HAS_NO_VARIADICS
4586
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004587template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004588inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004589typename enable_if
4590<
4591 !is_array<_Tp>::value,
4592 shared_ptr<_Tp>
4593>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004594make_shared(_Args&& ...__args)
4595{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004596 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004597}
4598
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004599template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004600inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004601typename enable_if
4602<
4603 !is_array<_Tp>::value,
4604 shared_ptr<_Tp>
4605>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004606allocate_shared(const _Alloc& __a, _Args&& ...__args)
4607{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004608 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004609}
4610
4611#else // _LIBCPP_HAS_NO_VARIADICS
4612
4613template<class _Tp>
4614inline _LIBCPP_INLINE_VISIBILITY
4615shared_ptr<_Tp>
4616make_shared()
4617{
4618 return shared_ptr<_Tp>::make_shared();
4619}
4620
4621template<class _Tp, class _A0>
4622inline _LIBCPP_INLINE_VISIBILITY
4623shared_ptr<_Tp>
4624make_shared(_A0& __a0)
4625{
4626 return shared_ptr<_Tp>::make_shared(__a0);
4627}
4628
4629template<class _Tp, class _A0, class _A1>
4630inline _LIBCPP_INLINE_VISIBILITY
4631shared_ptr<_Tp>
4632make_shared(_A0& __a0, _A1& __a1)
4633{
4634 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4635}
4636
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004637template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004638inline _LIBCPP_INLINE_VISIBILITY
4639shared_ptr<_Tp>
4640make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4641{
4642 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4643}
4644
4645template<class _Tp, class _Alloc>
4646inline _LIBCPP_INLINE_VISIBILITY
4647shared_ptr<_Tp>
4648allocate_shared(const _Alloc& __a)
4649{
4650 return shared_ptr<_Tp>::allocate_shared(__a);
4651}
4652
4653template<class _Tp, class _Alloc, class _A0>
4654inline _LIBCPP_INLINE_VISIBILITY
4655shared_ptr<_Tp>
4656allocate_shared(const _Alloc& __a, _A0& __a0)
4657{
4658 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4659}
4660
4661template<class _Tp, class _Alloc, class _A0, class _A1>
4662inline _LIBCPP_INLINE_VISIBILITY
4663shared_ptr<_Tp>
4664allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4665{
4666 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4667}
4668
4669template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4670inline _LIBCPP_INLINE_VISIBILITY
4671shared_ptr<_Tp>
4672allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4673{
4674 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4675}
4676
4677#endif // _LIBCPP_HAS_NO_VARIADICS
4678
4679template<class _Tp, class _Up>
4680inline _LIBCPP_INLINE_VISIBILITY
4681bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004682operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004683{
4684 return __x.get() == __y.get();
4685}
4686
4687template<class _Tp, class _Up>
4688inline _LIBCPP_INLINE_VISIBILITY
4689bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004690operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004691{
4692 return !(__x == __y);
4693}
4694
4695template<class _Tp, class _Up>
4696inline _LIBCPP_INLINE_VISIBILITY
4697bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004698operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004699{
Eric Fiselierf8898c82015-02-05 23:01:40 +00004700 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4701 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00004702}
4703
4704template<class _Tp, class _Up>
4705inline _LIBCPP_INLINE_VISIBILITY
4706bool
4707operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4708{
4709 return __y < __x;
4710}
4711
4712template<class _Tp, class _Up>
4713inline _LIBCPP_INLINE_VISIBILITY
4714bool
4715operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4716{
4717 return !(__y < __x);
4718}
4719
4720template<class _Tp, class _Up>
4721inline _LIBCPP_INLINE_VISIBILITY
4722bool
4723operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4724{
4725 return !(__x < __y);
4726}
4727
4728template<class _Tp>
4729inline _LIBCPP_INLINE_VISIBILITY
4730bool
4731operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4732{
4733 return !__x;
4734}
4735
4736template<class _Tp>
4737inline _LIBCPP_INLINE_VISIBILITY
4738bool
4739operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4740{
4741 return !__x;
4742}
4743
4744template<class _Tp>
4745inline _LIBCPP_INLINE_VISIBILITY
4746bool
4747operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4748{
4749 return static_cast<bool>(__x);
4750}
4751
4752template<class _Tp>
4753inline _LIBCPP_INLINE_VISIBILITY
4754bool
4755operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4756{
4757 return static_cast<bool>(__x);
4758}
4759
4760template<class _Tp>
4761inline _LIBCPP_INLINE_VISIBILITY
4762bool
4763operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4764{
4765 return less<_Tp*>()(__x.get(), nullptr);
4766}
4767
4768template<class _Tp>
4769inline _LIBCPP_INLINE_VISIBILITY
4770bool
4771operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4772{
4773 return less<_Tp*>()(nullptr, __x.get());
4774}
4775
4776template<class _Tp>
4777inline _LIBCPP_INLINE_VISIBILITY
4778bool
4779operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4780{
4781 return nullptr < __x;
4782}
4783
4784template<class _Tp>
4785inline _LIBCPP_INLINE_VISIBILITY
4786bool
4787operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4788{
4789 return __x < nullptr;
4790}
4791
4792template<class _Tp>
4793inline _LIBCPP_INLINE_VISIBILITY
4794bool
4795operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4796{
4797 return !(nullptr < __x);
4798}
4799
4800template<class _Tp>
4801inline _LIBCPP_INLINE_VISIBILITY
4802bool
4803operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4804{
4805 return !(__x < nullptr);
4806}
4807
4808template<class _Tp>
4809inline _LIBCPP_INLINE_VISIBILITY
4810bool
4811operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4812{
4813 return !(__x < nullptr);
4814}
4815
4816template<class _Tp>
4817inline _LIBCPP_INLINE_VISIBILITY
4818bool
4819operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4820{
4821 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004822}
4823
4824template<class _Tp>
4825inline _LIBCPP_INLINE_VISIBILITY
4826void
Howard Hinnant719bda32011-05-28 14:41:13 +00004827swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004828{
4829 __x.swap(__y);
4830}
4831
4832template<class _Tp, class _Up>
4833inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004834typename enable_if
4835<
4836 !is_array<_Tp>::value && !is_array<_Up>::value,
4837 shared_ptr<_Tp>
4838>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004839static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004840{
4841 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4842}
4843
4844template<class _Tp, class _Up>
4845inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004846typename enable_if
4847<
4848 !is_array<_Tp>::value && !is_array<_Up>::value,
4849 shared_ptr<_Tp>
4850>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004851dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004852{
4853 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4854 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4855}
4856
4857template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004858typename enable_if
4859<
4860 is_array<_Tp>::value == is_array<_Up>::value,
4861 shared_ptr<_Tp>
4862>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004863const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004864{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004865 typedef typename remove_extent<_Tp>::type _RTp;
4866 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004867}
4868
Howard Hinnant72f73582010-08-11 17:04:31 +00004869#ifndef _LIBCPP_NO_RTTI
4870
Howard Hinnantc51e1022010-05-11 19:42:16 +00004871template<class _Dp, class _Tp>
4872inline _LIBCPP_INLINE_VISIBILITY
4873_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004874get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004875{
4876 return __p.template __get_deleter<_Dp>();
4877}
4878
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004879#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004880
Howard Hinnantc51e1022010-05-11 19:42:16 +00004881template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004882class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004883{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004884public:
4885 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004886private:
4887 element_type* __ptr_;
4888 __shared_weak_count* __cntrl_;
4889
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004890public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004891 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004892 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004893 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004894 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4895 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004896 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004897 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004898 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004899 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4900 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004901
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004902#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004903 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004904 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004905 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004906 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4907 _NOEXCEPT;
4908#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004909 ~weak_ptr();
4910
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004911 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004912 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004913 template<class _Yp>
4914 typename enable_if
4915 <
4916 is_convertible<_Yp*, element_type*>::value,
4917 weak_ptr&
4918 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004919 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004920 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4921
4922#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4923
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004924 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004925 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4926 template<class _Yp>
4927 typename enable_if
4928 <
4929 is_convertible<_Yp*, element_type*>::value,
4930 weak_ptr&
4931 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004932 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004933 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4934
4935#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4936
4937 template<class _Yp>
4938 typename enable_if
4939 <
4940 is_convertible<_Yp*, element_type*>::value,
4941 weak_ptr&
4942 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004943 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004944 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004945
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004946 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004947 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004949 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004950
Howard Hinnant756c69b2010-09-22 16:48:34 +00004951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004952 long use_count() const _NOEXCEPT
4953 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004955 bool expired() const _NOEXCEPT
4956 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4957 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004958 template<class _Up>
4959 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004960 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004961 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004962 template<class _Up>
4963 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004964 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004965 {return __cntrl_ < __r.__cntrl_;}
4966
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004967 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4968 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004969};
4970
4971template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004972inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004973_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004974weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004975 : __ptr_(0),
4976 __cntrl_(0)
4977{
4978}
4979
4980template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004981inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004982weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004983 : __ptr_(__r.__ptr_),
4984 __cntrl_(__r.__cntrl_)
4985{
4986 if (__cntrl_)
4987 __cntrl_->__add_weak();
4988}
4989
4990template<class _Tp>
4991template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004992inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004993weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004994 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004995 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004996 : __ptr_(__r.__ptr_),
4997 __cntrl_(__r.__cntrl_)
4998{
4999 if (__cntrl_)
5000 __cntrl_->__add_weak();
5001}
5002
5003template<class _Tp>
5004template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005005inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005006weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005007 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005008 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005009 : __ptr_(__r.__ptr_),
5010 __cntrl_(__r.__cntrl_)
5011{
5012 if (__cntrl_)
5013 __cntrl_->__add_weak();
5014}
5015
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005016#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5017
5018template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005019inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005020weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5021 : __ptr_(__r.__ptr_),
5022 __cntrl_(__r.__cntrl_)
5023{
5024 __r.__ptr_ = 0;
5025 __r.__cntrl_ = 0;
5026}
5027
5028template<class _Tp>
5029template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005030inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005031weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5032 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5033 _NOEXCEPT
5034 : __ptr_(__r.__ptr_),
5035 __cntrl_(__r.__cntrl_)
5036{
5037 __r.__ptr_ = 0;
5038 __r.__cntrl_ = 0;
5039}
5040
5041#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5042
Howard Hinnantc51e1022010-05-11 19:42:16 +00005043template<class _Tp>
5044weak_ptr<_Tp>::~weak_ptr()
5045{
5046 if (__cntrl_)
5047 __cntrl_->__release_weak();
5048}
5049
5050template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005051inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005052weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005053weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005054{
5055 weak_ptr(__r).swap(*this);
5056 return *this;
5057}
5058
5059template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005060template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005061inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005062typename enable_if
5063<
5064 is_convertible<_Yp*, _Tp*>::value,
5065 weak_ptr<_Tp>&
5066>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005067weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005068{
5069 weak_ptr(__r).swap(*this);
5070 return *this;
5071}
5072
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005073#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5074
5075template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005076inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005077weak_ptr<_Tp>&
5078weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5079{
5080 weak_ptr(_VSTD::move(__r)).swap(*this);
5081 return *this;
5082}
5083
Howard Hinnantc51e1022010-05-11 19:42:16 +00005084template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005085template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005086inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005087typename enable_if
5088<
5089 is_convertible<_Yp*, _Tp*>::value,
5090 weak_ptr<_Tp>&
5091>::type
5092weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5093{
5094 weak_ptr(_VSTD::move(__r)).swap(*this);
5095 return *this;
5096}
5097
5098#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5099
5100template<class _Tp>
5101template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005102inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005103typename enable_if
5104<
5105 is_convertible<_Yp*, _Tp*>::value,
5106 weak_ptr<_Tp>&
5107>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005108weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005109{
5110 weak_ptr(__r).swap(*this);
5111 return *this;
5112}
5113
5114template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005115inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005116void
Howard Hinnant719bda32011-05-28 14:41:13 +00005117weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005118{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005119 _VSTD::swap(__ptr_, __r.__ptr_);
5120 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005121}
5122
5123template<class _Tp>
5124inline _LIBCPP_INLINE_VISIBILITY
5125void
Howard Hinnant719bda32011-05-28 14:41:13 +00005126swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005127{
5128 __x.swap(__y);
5129}
5130
5131template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005132inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005133void
Howard Hinnant719bda32011-05-28 14:41:13 +00005134weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005135{
5136 weak_ptr().swap(*this);
5137}
5138
5139template<class _Tp>
5140template<class _Yp>
5141shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00005142 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005143 : __ptr_(__r.__ptr_),
5144 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5145{
5146 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00005147 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00005148}
5149
5150template<class _Tp>
5151shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005152weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005153{
5154 shared_ptr<_Tp> __r;
5155 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5156 if (__r.__cntrl_)
5157 __r.__ptr_ = __ptr_;
5158 return __r;
5159}
5160
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005161#if _LIBCPP_STD_VER > 14
5162template <class _Tp = void> struct owner_less;
5163#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005164template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005165#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005166
5167template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005168struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005169 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005170{
5171 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005172 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005173 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005174 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005175 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005176 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005177 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005178 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005179 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005180 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005181};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005182
5183template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005184struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005185 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5186{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005187 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005188 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005189 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005190 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005191 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005192 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005193 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005194 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005195 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005196 {return __x.owner_before(__y);}
5197};
5198
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005199#if _LIBCPP_STD_VER > 14
5200template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005201struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005202{
5203 template <class _Tp, class _Up>
5204 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005205 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005206 {return __x.owner_before(__y);}
5207 template <class _Tp, class _Up>
5208 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005209 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005210 {return __x.owner_before(__y);}
5211 template <class _Tp, class _Up>
5212 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005213 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005214 {return __x.owner_before(__y);}
5215 template <class _Tp, class _Up>
5216 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005217 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005218 {return __x.owner_before(__y);}
5219 typedef void is_transparent;
5220};
5221#endif
5222
Howard Hinnantc51e1022010-05-11 19:42:16 +00005223template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005224class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005225{
5226 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005227protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005228 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005229 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005231 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005232 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005233 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5234 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005235 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005236 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005237public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005238 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005239 shared_ptr<_Tp> shared_from_this()
5240 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005242 shared_ptr<_Tp const> shared_from_this() const
5243 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005244
Eric Fiselier84006862016-06-02 00:15:35 +00005245#if _LIBCPP_STD_VER > 14
5246 _LIBCPP_INLINE_VISIBILITY
5247 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5248 { return __weak_this_; }
5249
5250 _LIBCPP_INLINE_VISIBILITY
5251 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5252 { return __weak_this_; }
5253#endif // _LIBCPP_STD_VER > 14
5254
Howard Hinnantc51e1022010-05-11 19:42:16 +00005255 template <class _Up> friend class shared_ptr;
5256};
5257
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005258template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005259struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005260{
5261 typedef shared_ptr<_Tp> argument_type;
5262 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005263
Howard Hinnant756c69b2010-09-22 16:48:34 +00005264 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005265 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005266 {
5267 return hash<_Tp*>()(__ptr.get());
5268 }
5269};
5270
Howard Hinnantc834c512011-11-29 18:15:50 +00005271template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005272inline _LIBCPP_INLINE_VISIBILITY
5273basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005274operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005275
Eric Fiselier9b492672016-06-18 02:12:53 +00005276
5277#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005278
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005279class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005280{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005281 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005282public:
5283 void lock() _NOEXCEPT;
5284 void unlock() _NOEXCEPT;
5285
5286private:
5287 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5288 __sp_mut(const __sp_mut&);
5289 __sp_mut& operator=(const __sp_mut&);
5290
Howard Hinnant8331b762013-03-06 23:30:19 +00005291 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005292};
5293
Howard Hinnant8331b762013-03-06 23:30:19 +00005294_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005295
5296template <class _Tp>
5297inline _LIBCPP_INLINE_VISIBILITY
5298bool
5299atomic_is_lock_free(const shared_ptr<_Tp>*)
5300{
5301 return false;
5302}
5303
5304template <class _Tp>
5305shared_ptr<_Tp>
5306atomic_load(const shared_ptr<_Tp>* __p)
5307{
5308 __sp_mut& __m = __get_sp_mut(__p);
5309 __m.lock();
5310 shared_ptr<_Tp> __q = *__p;
5311 __m.unlock();
5312 return __q;
5313}
5314
5315template <class _Tp>
5316inline _LIBCPP_INLINE_VISIBILITY
5317shared_ptr<_Tp>
5318atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5319{
5320 return atomic_load(__p);
5321}
5322
5323template <class _Tp>
5324void
5325atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5326{
5327 __sp_mut& __m = __get_sp_mut(__p);
5328 __m.lock();
5329 __p->swap(__r);
5330 __m.unlock();
5331}
5332
5333template <class _Tp>
5334inline _LIBCPP_INLINE_VISIBILITY
5335void
5336atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5337{
5338 atomic_store(__p, __r);
5339}
5340
5341template <class _Tp>
5342shared_ptr<_Tp>
5343atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5344{
5345 __sp_mut& __m = __get_sp_mut(__p);
5346 __m.lock();
5347 __p->swap(__r);
5348 __m.unlock();
5349 return __r;
5350}
5351
5352template <class _Tp>
5353inline _LIBCPP_INLINE_VISIBILITY
5354shared_ptr<_Tp>
5355atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5356{
5357 return atomic_exchange(__p, __r);
5358}
5359
5360template <class _Tp>
5361bool
5362atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5363{
Marshall Clow4201ee82016-05-18 17:50:13 +00005364 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005365 __sp_mut& __m = __get_sp_mut(__p);
5366 __m.lock();
5367 if (__p->__owner_equivalent(*__v))
5368 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005369 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005370 *__p = __w;
5371 __m.unlock();
5372 return true;
5373 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005374 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005375 *__v = *__p;
5376 __m.unlock();
5377 return false;
5378}
5379
5380template <class _Tp>
5381inline _LIBCPP_INLINE_VISIBILITY
5382bool
5383atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5384{
5385 return atomic_compare_exchange_strong(__p, __v, __w);
5386}
5387
5388template <class _Tp>
5389inline _LIBCPP_INLINE_VISIBILITY
5390bool
5391atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5392 shared_ptr<_Tp> __w, memory_order, memory_order)
5393{
5394 return atomic_compare_exchange_strong(__p, __v, __w);
5395}
5396
5397template <class _Tp>
5398inline _LIBCPP_INLINE_VISIBILITY
5399bool
5400atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5401 shared_ptr<_Tp> __w, memory_order, memory_order)
5402{
5403 return atomic_compare_exchange_weak(__p, __v, __w);
5404}
5405
Eric Fiselier9b492672016-06-18 02:12:53 +00005406#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005407
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005408//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005409#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5410# ifndef _LIBCPP_CXX03_LANG
5411enum class pointer_safety : unsigned char {
5412 relaxed,
5413 preferred,
5414 strict
5415};
5416# endif
5417#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005418struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005419{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005420 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005421 {
5422 relaxed,
5423 preferred,
5424 strict
5425 };
5426
Howard Hinnant49e145e2012-10-30 19:06:59 +00005427 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005428
Howard Hinnant756c69b2010-09-22 16:48:34 +00005429 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005430 pointer_safety() : __v_() {}
5431
5432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005433 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005435 operator int() const {return __v_;}
5436};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005437#endif
5438
5439#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
5440 defined(_LIBCPP_BUILDING_MEMORY)
5441_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5442#else
5443// This function is only offered in C++03 under ABI v1.
5444# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5445inline _LIBCPP_INLINE_VISIBILITY
5446pointer_safety get_pointer_safety() _NOEXCEPT {
5447 return pointer_safety::relaxed;
5448}
5449# endif
5450#endif
5451
Howard Hinnantc51e1022010-05-11 19:42:16 +00005452
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005453_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5454_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5455_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005456_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005457
5458template <class _Tp>
5459inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005460_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005461undeclare_reachable(_Tp* __p)
5462{
5463 return static_cast<_Tp*>(__undeclare_reachable(__p));
5464}
5465
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005466_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005467
Marshall Clow8982dcd2015-07-13 20:04:56 +00005468// --- Helper for container swap --
5469template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005470inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005471void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5472#if _LIBCPP_STD_VER >= 14
5473 _NOEXCEPT
5474#else
5475 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5476#endif
5477{
5478 __swap_allocator(__a1, __a2,
5479 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5480}
5481
5482template <typename _Alloc>
5483_LIBCPP_INLINE_VISIBILITY
5484void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5485#if _LIBCPP_STD_VER >= 14
5486 _NOEXCEPT
5487#else
5488 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5489#endif
5490{
5491 using _VSTD::swap;
5492 swap(__a1, __a2);
5493}
5494
5495template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005496inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005497void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5498
Marshall Clowff91de82015-08-18 19:51:37 +00005499template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005500struct __noexcept_move_assign_container : public integral_constant<bool,
5501 _Traits::propagate_on_container_move_assignment::value
5502#if _LIBCPP_STD_VER > 14
5503 || _Traits::is_always_equal::value
5504#else
5505 && is_nothrow_move_assignable<_Alloc>::value
5506#endif
5507 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005508
Marshall Clowa591b9a2016-07-11 21:38:08 +00005509
5510#ifndef _LIBCPP_HAS_NO_VARIADICS
5511template <class _Tp, class _Alloc>
5512struct __temp_value {
5513 typedef allocator_traits<_Alloc> _Traits;
5514
5515 typename aligned_storage<sizeof(_Tp), alignof(_Tp)>::type __v;
5516 _Alloc &__a;
5517
5518 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5519 _Tp & get() { return *__addr(); }
5520
5521 template<class... _Args>
5522 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc)
5523 { _Traits::construct(__a, __addr(), _VSTD::forward<_Args>(__args)...); }
5524
5525 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5526 };
5527#endif
5528
Howard Hinnantc51e1022010-05-11 19:42:16 +00005529_LIBCPP_END_NAMESPACE_STD
5530
5531#endif // _LIBCPP_MEMORY