blob: 3fc0e5a1bf12d2ee5281de65832e68e72ca34ead [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 Fiselier209ecde2017-04-17 22:32:02 +00002082 constexpr __compressed_pair_elem() : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002083
Eric Fiselier9d355982017-04-12 23:45:53 +00002084 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002085 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2086 >::type>
2087 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002088 __compressed_pair_elem(_Up&& __u)
2089 : __value_(_VSTD::forward<_Up>(__u)){};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002090
Eric Fiselier9d355982017-04-12 23:45:53 +00002091 template <class... _Args, size_t... _Indexes>
2092 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2093 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2094 __tuple_indices<_Indexes...>)
2095 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2096#else
2097 __compressed_pair_elem() : __value_() {}
2098 __compressed_pair_elem(_ParamT __p) : __value_(std::forward<_ParamT>(__p)) {}
2099#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002100
Eric Fiselier9d355982017-04-12 23:45:53 +00002101 reference __get() _NOEXCEPT { return __value_; }
2102 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002103
Howard Hinnantc51e1022010-05-11 19:42:16 +00002104private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002105 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002106};
2107
Eric Fiselier9d355982017-04-12 23:45:53 +00002108template <class _Tp, int _Idx>
2109struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2110 typedef _Tp _ParamT;
2111 typedef _Tp& reference;
2112 typedef const _Tp& const_reference;
2113 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002114
Eric Fiselier9d355982017-04-12 23:45:53 +00002115#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier209ecde2017-04-17 22:32:02 +00002116 constexpr __compressed_pair_elem() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002117
Eric Fiselier9d355982017-04-12 23:45:53 +00002118 template <class _Up, class = typename enable_if<
Eric Fiselier209ecde2017-04-17 22:32:02 +00002119 !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
2120 >::type>
2121 constexpr explicit
Eric Fiselier9d355982017-04-12 23:45:53 +00002122 __compressed_pair_elem(_Up&& __u)
2123 : __value_type(_VSTD::forward<_Up>(__u)){};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002124
Eric Fiselier9d355982017-04-12 23:45:53 +00002125 template <class... _Args, size_t... _Indexes>
2126 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2127 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2128 __tuple_indices<_Indexes...>)
2129 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2130#else
2131 __compressed_pair_elem() : __value_type() {}
2132 __compressed_pair_elem(_ParamT __p)
2133 : __value_type(std::forward<_ParamT>(__p)) {}
2134#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002135
Eric Fiselier9d355982017-04-12 23:45:53 +00002136 reference __get() _NOEXCEPT { return *this; }
2137 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002138};
2139
Eric Fiselier9d355982017-04-12 23:45:53 +00002140// Tag used to construct the second element of the compressed pair.
2141struct __second_tag {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002142
2143template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002144class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2145 private __compressed_pair_elem<_T2, 1> {
2146 typedef __compressed_pair_elem<_T1, 0> _Base1;
2147 typedef __compressed_pair_elem<_T2, 1> _Base2;
2148
2149 // NOTE: This static assert should never fire because __compressed_pair
2150 // is *almost never* used in a scenario where it's possible for T1 == T2.
2151 // (The exception is std::function where it is possible that the function
2152 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002153 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002154 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2155 "The current implementation is NOT ABI-compatible with the previous "
2156 "implementation for this configuration");
2157
Howard Hinnantc51e1022010-05-11 19:42:16 +00002158public:
Eric Fiselier9d355982017-04-12 23:45:53 +00002159#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier209ecde2017-04-17 22:32:02 +00002160 template <bool _Dummy = true,
2161 class = typename enable_if<
2162 __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
2163 __dependent_type<is_default_constructible<_T2>, _Dummy>::value
2164 >::type
2165 >
Eric Fiselier9d355982017-04-12 23:45:53 +00002166 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier209ecde2017-04-17 22:32:02 +00002167 constexpr __compressed_pair() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002168
Eric Fiselier9d355982017-04-12 23:45:53 +00002169 template <class _Tp, typename enable_if<!is_same<typename decay<_Tp>::type,
2170 __compressed_pair>::value,
2171 bool>::type = true>
2172 _LIBCPP_INLINE_VISIBILITY constexpr explicit
2173 __compressed_pair(_Tp&& __t)
2174 : _Base1(std::forward<_Tp>(__t)), _Base2() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002175
Eric Fiselier9d355982017-04-12 23:45:53 +00002176 template <class _Tp>
2177 _LIBCPP_INLINE_VISIBILITY constexpr
2178 __compressed_pair(__second_tag, _Tp&& __t)
2179 : _Base1(), _Base2(std::forward<_Tp>(__t)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002180
Eric Fiselier9d355982017-04-12 23:45:53 +00002181 template <class _U1, class _U2>
2182 _LIBCPP_INLINE_VISIBILITY constexpr
2183 __compressed_pair(_U1&& __t1, _U2&& __t2)
2184 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002185
Eric Fiselier9d355982017-04-12 23:45:53 +00002186 template <class... _Args1, class... _Args2>
2187 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2188 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2189 tuple<_Args2...> __second_args)
2190 : _Base1(__pc, _VSTD::move(__first_args),
2191 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2192 _Base2(__pc, _VSTD::move(__second_args),
2193 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002194
Eric Fiselier9d355982017-04-12 23:45:53 +00002195#else
2196 _LIBCPP_INLINE_VISIBILITY
2197 __compressed_pair() {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002198
Eric Fiselier9d355982017-04-12 23:45:53 +00002199 _LIBCPP_INLINE_VISIBILITY explicit
2200 __compressed_pair(_T1 __t1) : _Base1(_VSTD::forward<_T1>(__t1)) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002201
Eric Fiselier9d355982017-04-12 23:45:53 +00002202 _LIBCPP_INLINE_VISIBILITY
2203 __compressed_pair(__second_tag, _T2 __t2)
2204 : _Base1(), _Base2(_VSTD::forward<_T2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205
Eric Fiselier9d355982017-04-12 23:45:53 +00002206 _LIBCPP_INLINE_VISIBILITY
2207 __compressed_pair(_T1 __t1, _T2 __t2)
2208 : _Base1(_VSTD::forward<_T1>(__t1)), _Base2(_VSTD::forward<_T2>(__t2)) {}
2209#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002210
Eric Fiselier9d355982017-04-12 23:45:53 +00002211 _LIBCPP_INLINE_VISIBILITY
2212 typename _Base1::reference first() _NOEXCEPT {
2213 return static_cast<_Base1&>(*this).__get();
2214 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002215
Eric Fiselier9d355982017-04-12 23:45:53 +00002216 _LIBCPP_INLINE_VISIBILITY
2217 typename _Base1::const_reference first() const _NOEXCEPT {
2218 return static_cast<_Base1 const&>(*this).__get();
2219 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220
Eric Fiselier9d355982017-04-12 23:45:53 +00002221 _LIBCPP_INLINE_VISIBILITY
2222 typename _Base2::reference second() _NOEXCEPT {
2223 return static_cast<_Base2&>(*this).__get();
2224 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225
Eric Fiselier9d355982017-04-12 23:45:53 +00002226 _LIBCPP_INLINE_VISIBILITY
2227 typename _Base2::const_reference second() const _NOEXCEPT {
2228 return static_cast<_Base2 const&>(*this).__get();
2229 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230
Eric Fiselier9d355982017-04-12 23:45:53 +00002231 _LIBCPP_INLINE_VISIBILITY
2232 void swap(__compressed_pair& __x)
2233 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2234 __is_nothrow_swappable<_T2>::value)
2235 {
2236 using std::swap;
2237 swap(first(), __x.first());
2238 swap(second(), __x.second());
2239 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240};
2241
2242template <class _T1, class _T2>
2243inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002244void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2245 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2246 __is_nothrow_swappable<_T2>::value) {
2247 __x.swap(__y);
2248}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002249
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002250// default_delete
2251
Howard Hinnantc51e1022010-05-11 19:42:16 +00002252template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002253struct _LIBCPP_TEMPLATE_VIS default_delete {
2254#ifndef _LIBCPP_CXX03_LANG
2255 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002256#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002257 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002258#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002259 template <class _Up>
2260 _LIBCPP_INLINE_VISIBILITY
2261 default_delete(const default_delete<_Up>&,
2262 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2263 0) _NOEXCEPT {}
2264
2265 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2266 static_assert(sizeof(_Tp) > 0,
2267 "default_delete can not delete incomplete type");
2268 static_assert(!is_void<_Tp>::value,
2269 "default_delete can not delete incomplete type");
2270 delete __ptr;
2271 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002272};
2273
2274template <class _Tp>
Eric Fiselier6779b062017-04-16 02:06:25 +00002275struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2276private:
2277 template <class _Up>
2278 struct _EnableIfConvertible
2279 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2280
Howard Hinnant4500ca52011-12-18 21:19:44 +00002281public:
Eric Fiselier6779b062017-04-16 02:06:25 +00002282#ifndef _LIBCPP_CXX03_LANG
2283 _LIBCPP_INLINE_VISIBILITY constexpr default_delete() noexcept = default;
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002284#else
Eric Fiselier6779b062017-04-16 02:06:25 +00002285 _LIBCPP_INLINE_VISIBILITY default_delete() {}
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002286#endif
Eric Fiselier6779b062017-04-16 02:06:25 +00002287
2288 template <class _Up>
2289 _LIBCPP_INLINE_VISIBILITY
2290 default_delete(const default_delete<_Up[]>&,
2291 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2292
2293 template <class _Up>
2294 _LIBCPP_INLINE_VISIBILITY
2295 typename _EnableIfConvertible<_Up>::type
2296 operator()(_Up* __ptr) const _NOEXCEPT {
2297 static_assert(sizeof(_Tp) > 0,
2298 "default_delete can not delete incomplete type");
2299 static_assert(!is_void<_Tp>::value,
2300 "default_delete can not delete void type");
2301 delete[] __ptr;
2302 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002303};
2304
Howard Hinnantc51e1022010-05-11 19:42:16 +00002305
Eric Fiselier6779b062017-04-16 02:06:25 +00002306
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002307#ifndef _LIBCPP_CXX03_LANG
2308template <class _Deleter>
2309struct __unique_ptr_deleter_sfinae {
2310 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2311 typedef const _Deleter& __lval_ref_type;
2312 typedef _Deleter&& __good_rval_ref_type;
2313 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002314};
2315
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002316template <class _Deleter>
2317struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2318 typedef const _Deleter& __lval_ref_type;
2319 typedef const _Deleter&& __bad_rval_ref_type;
2320 typedef false_type __enable_rval_overload;
2321};
2322
2323template <class _Deleter>
2324struct __unique_ptr_deleter_sfinae<_Deleter&> {
2325 typedef _Deleter& __lval_ref_type;
2326 typedef _Deleter&& __bad_rval_ref_type;
2327 typedef false_type __enable_rval_overload;
2328};
2329#endif // !defined(_LIBCPP_CXX03_LANG)
2330
2331template <class _Tp, class _Dp = default_delete<_Tp> >
2332class _LIBCPP_TEMPLATE_VIS unique_ptr {
2333public:
2334 typedef _Tp element_type;
2335 typedef _Dp deleter_type;
2336 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2337
2338 static_assert(!is_rvalue_reference<deleter_type>::value,
2339 "the specified deleter type cannot be an rvalue reference");
2340
2341private:
2342 __compressed_pair<pointer, deleter_type> __ptr_;
2343
2344 struct __nat { int __for_bool_; };
2345
2346#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier31127cd2017-04-16 02:14:31 +00002347 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002348
2349 template <bool _Dummy>
2350 using _LValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002351 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002352
2353 template <bool _Dummy>
2354 using _GoodRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002355 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002356
2357 template <bool _Dummy>
2358 using _BadRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002359 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002360
2361 template <bool _Dummy, class _Deleter = typename __dependent_type<
2362 __identity<deleter_type>, _Dummy>::type>
2363 using _EnableIfDeleterDefaultConstructible =
2364 typename enable_if<is_default_constructible<_Deleter>::value &&
2365 !is_pointer<_Deleter>::value>::type;
2366
2367 template <class _ArgType>
2368 using _EnableIfDeleterConstructible =
2369 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2370
2371 template <class _UPtr, class _Up>
2372 using _EnableIfMoveConvertible = typename enable_if<
2373 is_convertible<typename _UPtr::pointer, pointer>::value &&
2374 !is_array<_Up>::value
2375 >::type;
2376
2377 template <class _UDel>
2378 using _EnableIfDeleterConvertible = typename enable_if<
2379 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2380 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2381 >::type;
2382
2383 template <class _UDel>
2384 using _EnableIfDeleterAssignable = typename enable_if<
2385 is_assignable<_Dp&, _UDel&&>::value
2386 >::type;
2387
2388public:
2389 template <bool _Dummy = true,
2390 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2391 _LIBCPP_INLINE_VISIBILITY
2392 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
2393
2394 template <bool _Dummy = true,
2395 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2396 _LIBCPP_INLINE_VISIBILITY
2397 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
2398
2399 template <bool _Dummy = true,
2400 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2401 _LIBCPP_INLINE_VISIBILITY
2402 explicit unique_ptr(pointer __p) noexcept : __ptr_(__p) {}
2403
2404 template <bool _Dummy = true,
2405 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2406 _LIBCPP_INLINE_VISIBILITY
2407 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) noexcept
2408 : __ptr_(__p, __d) {}
2409
2410 template <bool _Dummy = true,
2411 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2412 _LIBCPP_INLINE_VISIBILITY
2413 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) noexcept
2414 : __ptr_(__p, _VSTD::move(__d)) {
2415 static_assert(!is_reference<deleter_type>::value,
2416 "rvalue deleter bound to reference");
2417 }
2418
2419 template <bool _Dummy = true,
2420 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>>
2421 _LIBCPP_INLINE_VISIBILITY
2422 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2423
2424 _LIBCPP_INLINE_VISIBILITY
2425 unique_ptr(unique_ptr&& __u) noexcept
2426 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2427 }
2428
2429 template <class _Up, class _Ep,
2430 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2431 class = _EnableIfDeleterConvertible<_Ep>
2432 >
2433 _LIBCPP_INLINE_VISIBILITY
2434 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2435 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2436
2437#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2438 template <class _Up>
2439 _LIBCPP_INLINE_VISIBILITY
2440 unique_ptr(auto_ptr<_Up>&& __p,
2441 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2442 is_same<_Dp, default_delete<_Tp>>::value,
2443 __nat>::type = __nat()) _NOEXCEPT
2444 : __ptr_(__p.release()) {}
2445#endif
2446
2447 _LIBCPP_INLINE_VISIBILITY
2448 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2449 reset(__u.release());
2450 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2451 return *this;
2452 }
2453
2454 template <class _Up, class _Ep,
2455 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2456 class = _EnableIfDeleterAssignable<_Ep>
2457 >
2458 _LIBCPP_INLINE_VISIBILITY
2459 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2460 reset(__u.release());
2461 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2462 return *this;
2463 }
2464
2465#else // _LIBCPP_CXX03_LANG
2466private:
2467 unique_ptr(unique_ptr&);
2468 template <class _Up, class _Ep> unique_ptr(unique_ptr<_Up, _Ep>&);
2469
2470 unique_ptr& operator=(unique_ptr&);
2471 template <class _Up, class _Ep> unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
2472
2473public:
2474 _LIBCPP_INLINE_VISIBILITY
2475 unique_ptr() : __ptr_(pointer())
2476 {
2477 static_assert(!is_pointer<deleter_type>::value,
2478 "unique_ptr constructed with null function pointer deleter");
2479 static_assert(is_default_constructible<deleter_type>::value,
2480 "unique_ptr::deleter_type is not default constructible");
2481 }
2482 _LIBCPP_INLINE_VISIBILITY
2483 unique_ptr(nullptr_t) : __ptr_(pointer())
2484 {
2485 static_assert(!is_pointer<deleter_type>::value,
2486 "unique_ptr constructed with null function pointer deleter");
2487 }
2488 _LIBCPP_INLINE_VISIBILITY
2489 explicit unique_ptr(pointer __p)
2490 : __ptr_(_VSTD::move(__p)) {
2491 static_assert(!is_pointer<deleter_type>::value,
2492 "unique_ptr constructed with null function pointer deleter");
2493 }
2494
2495 _LIBCPP_INLINE_VISIBILITY
2496 operator __rv<unique_ptr>() {
2497 return __rv<unique_ptr>(*this);
2498 }
2499
2500 _LIBCPP_INLINE_VISIBILITY
2501 unique_ptr(__rv<unique_ptr> __u)
2502 : __ptr_(__u->release(),
2503 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2504
2505 template <class _Up, class _Ep>
2506 _LIBCPP_INLINE_VISIBILITY
2507 typename enable_if<
2508 !is_array<_Up>::value &&
2509 is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
2510 pointer>::value &&
2511 is_assignable<deleter_type&, _Ep&>::value,
2512 unique_ptr&>::type
2513 operator=(unique_ptr<_Up, _Ep> __u) {
2514 reset(__u.release());
2515 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2516 return *this;
2517 }
2518
2519 _LIBCPP_INLINE_VISIBILITY
2520 unique_ptr(pointer __p, deleter_type __d)
2521 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
2522#endif // _LIBCPP_CXX03_LANG
2523
2524#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2525 template <class _Up>
2526 _LIBCPP_INLINE_VISIBILITY
2527 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2528 is_same<_Dp, default_delete<_Tp> >::value,
2529 unique_ptr&>::type
2530 operator=(auto_ptr<_Up> __p) {
2531 reset(__p.release());
2532 return *this;
2533 }
2534#endif
2535
2536 _LIBCPP_INLINE_VISIBILITY
2537 ~unique_ptr() { reset(); }
2538
2539 _LIBCPP_INLINE_VISIBILITY
2540 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2541 reset();
2542 return *this;
2543 }
2544
2545 _LIBCPP_INLINE_VISIBILITY
2546 typename add_lvalue_reference<_Tp>::type
2547 operator*() const {
2548 return *__ptr_.first();
2549 }
2550 _LIBCPP_INLINE_VISIBILITY
2551 pointer operator->() const _NOEXCEPT {
2552 return __ptr_.first();
2553 }
2554 _LIBCPP_INLINE_VISIBILITY
2555 pointer get() const _NOEXCEPT {
2556 return __ptr_.first();
2557 }
2558 _LIBCPP_INLINE_VISIBILITY
2559 deleter_type& get_deleter() _NOEXCEPT {
2560 return __ptr_.second();
2561 }
2562 _LIBCPP_INLINE_VISIBILITY
2563 const deleter_type& get_deleter() const _NOEXCEPT {
2564 return __ptr_.second();
2565 }
2566 _LIBCPP_INLINE_VISIBILITY
2567 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2568 return __ptr_.first() != nullptr;
2569 }
2570
2571 _LIBCPP_INLINE_VISIBILITY
2572 pointer release() _NOEXCEPT {
2573 pointer __t = __ptr_.first();
2574 __ptr_.first() = pointer();
2575 return __t;
2576 }
2577
2578 _LIBCPP_INLINE_VISIBILITY
2579 void reset(pointer __p = pointer()) _NOEXCEPT {
2580 pointer __tmp = __ptr_.first();
2581 __ptr_.first() = __p;
2582 if (__tmp)
2583 __ptr_.second()(__tmp);
2584 }
2585
2586 _LIBCPP_INLINE_VISIBILITY
2587 void swap(unique_ptr& __u) _NOEXCEPT {
2588 __ptr_.swap(__u.__ptr_);
2589 }
2590};
2591
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002592
Howard Hinnantc51e1022010-05-11 19:42:16 +00002593template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002594class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002595public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002596 typedef _Tp element_type;
2597 typedef _Dp deleter_type;
2598 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2599
Howard Hinnantc51e1022010-05-11 19:42:16 +00002600private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002601 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002602
Eric Fiselier31127cd2017-04-16 02:14:31 +00002603 template <class _From>
2604 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002605
Eric Fiselier31127cd2017-04-16 02:14:31 +00002606 template <class _FromElem>
2607 struct _CheckArrayPointerConversion<_FromElem*>
2608 : integral_constant<bool,
2609 is_same<_FromElem*, pointer>::value ||
2610 (is_same<pointer, element_type*>::value &&
2611 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
2612 >
2613 {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002614
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002615#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier31127cd2017-04-16 02:14:31 +00002616 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002617
2618 template <bool _Dummy>
2619 using _LValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002620 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002621
2622 template <bool _Dummy>
2623 using _GoodRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002624 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002625
2626 template <bool _Dummy>
2627 using _BadRValRefType =
Eric Fiselier31127cd2017-04-16 02:14:31 +00002628 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002629
2630 template <bool _Dummy, class _Deleter = typename __dependent_type<
2631 __identity<deleter_type>, _Dummy>::type>
2632 using _EnableIfDeleterDefaultConstructible =
2633 typename enable_if<is_default_constructible<_Deleter>::value &&
2634 !is_pointer<_Deleter>::value>::type;
2635
2636 template <class _ArgType>
2637 using _EnableIfDeleterConstructible =
2638 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2639
2640 template <class _Pp>
2641 using _EnableIfPointerConvertible = typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002642 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002643 >::type;
2644
2645 template <class _UPtr, class _Up,
2646 class _ElemT = typename _UPtr::element_type>
2647 using _EnableIfMoveConvertible = typename enable_if<
2648 is_array<_Up>::value &&
2649 is_same<pointer, element_type*>::value &&
2650 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2651 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2652 >::type;
2653
2654 template <class _UDel>
2655 using _EnableIfDeleterConvertible = typename enable_if<
2656 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2657 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2658 >::type;
2659
2660 template <class _UDel>
2661 using _EnableIfDeleterAssignable = typename enable_if<
2662 is_assignable<_Dp&, _UDel&&>::value
2663 >::type;
2664
Howard Hinnantc51e1022010-05-11 19:42:16 +00002665public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002666 template <bool _Dummy = true,
2667 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2668 _LIBCPP_INLINE_VISIBILITY
2669 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002670
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002671 template <bool _Dummy = true,
2672 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2673 _LIBCPP_INLINE_VISIBILITY
2674 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002675
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002676 template <class _Pp, bool _Dummy = true,
2677 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
2678 class = _EnableIfPointerConvertible<_Pp>>
2679 _LIBCPP_INLINE_VISIBILITY
2680 explicit unique_ptr(_Pp __p) noexcept
2681 : __ptr_(__p) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002683 template <class _Pp, bool _Dummy = true,
2684 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>,
2685 class = _EnableIfPointerConvertible<_Pp>>
2686 _LIBCPP_INLINE_VISIBILITY
2687 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) noexcept
2688 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002689
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002690 template <bool _Dummy = true,
2691 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2692 _LIBCPP_INLINE_VISIBILITY
2693 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) noexcept
2694 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002696 template <class _Pp, bool _Dummy = true,
2697 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>,
2698 class = _EnableIfPointerConvertible<_Pp>>
2699 _LIBCPP_INLINE_VISIBILITY
2700 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) noexcept
2701 : __ptr_(__p, _VSTD::move(__d)) {
2702 static_assert(!is_reference<deleter_type>::value,
2703 "rvalue deleter bound to reference");
2704 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002705
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002706 template <bool _Dummy = true,
2707 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2708 _LIBCPP_INLINE_VISIBILITY
2709 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) noexcept
2710 : __ptr_(nullptr, _VSTD::move(__d)) {
2711 static_assert(!is_reference<deleter_type>::value,
2712 "rvalue deleter bound to reference");
2713 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002714
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002715 template <class _Pp, bool _Dummy = true,
2716 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>,
2717 class = _EnableIfPointerConvertible<_Pp>>
2718 _LIBCPP_INLINE_VISIBILITY
2719 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002720
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002721 _LIBCPP_INLINE_VISIBILITY
2722 unique_ptr(unique_ptr&& __u) noexcept
2723 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2724 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002725
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002726 _LIBCPP_INLINE_VISIBILITY
2727 unique_ptr& operator=(unique_ptr&& __u) noexcept {
2728 reset(__u.release());
2729 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2730 return *this;
2731 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002732
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002733 template <class _Up, class _Ep,
2734 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2735 class = _EnableIfDeleterConvertible<_Ep>
2736 >
2737 _LIBCPP_INLINE_VISIBILITY
2738 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
Eric Fiselier3827e6a2017-04-17 20:20:27 +00002739 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002740 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002741
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002742 template <class _Up, class _Ep,
2743 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2744 class = _EnableIfDeleterAssignable<_Ep>
2745 >
2746 _LIBCPP_INLINE_VISIBILITY
2747 unique_ptr&
2748 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept {
2749 reset(__u.release());
2750 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2751 return *this;
2752 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002753
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002754#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002755private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002756 template <class _Up> explicit unique_ptr(_Up);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002757
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002758 unique_ptr(unique_ptr&);
2759 template <class _Up> unique_ptr(unique_ptr<_Up>&);
2760
2761 unique_ptr& operator=(unique_ptr&);
2762 template <class _Up> unique_ptr& operator=(unique_ptr<_Up>&);
2763
2764 template <class _Up>
2765 unique_ptr(_Up __u,
2766 typename conditional<
2767 is_reference<deleter_type>::value, deleter_type,
2768 typename add_lvalue_reference<const deleter_type>::type>::type,
2769 typename enable_if<is_convertible<_Up, pointer>::value,
2770 __nat>::type = __nat());
2771public:
2772 _LIBCPP_INLINE_VISIBILITY
2773 unique_ptr() : __ptr_(pointer()) {
2774 static_assert(!is_pointer<deleter_type>::value,
2775 "unique_ptr constructed with null function pointer deleter");
2776 }
2777 _LIBCPP_INLINE_VISIBILITY
2778 unique_ptr(nullptr_t) : __ptr_(pointer()) {
2779 static_assert(!is_pointer<deleter_type>::value,
2780 "unique_ptr constructed with null function pointer deleter");
2781 }
2782
2783 _LIBCPP_INLINE_VISIBILITY
2784 explicit unique_ptr(pointer __p) : __ptr_(__p) {
2785 static_assert(!is_pointer<deleter_type>::value,
2786 "unique_ptr constructed with null function pointer deleter");
2787 }
2788
2789 _LIBCPP_INLINE_VISIBILITY
2790 unique_ptr(pointer __p, deleter_type __d)
2791 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
2792
2793 _LIBCPP_INLINE_VISIBILITY
2794 unique_ptr(nullptr_t, deleter_type __d)
2795 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
2796
2797 _LIBCPP_INLINE_VISIBILITY
2798 operator __rv<unique_ptr>() {
2799 return __rv<unique_ptr>(*this);
2800 }
2801
2802 _LIBCPP_INLINE_VISIBILITY
2803 unique_ptr(__rv<unique_ptr> __u)
2804 : __ptr_(__u->release(),
2805 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2806
2807 _LIBCPP_INLINE_VISIBILITY
2808 unique_ptr& operator=(__rv<unique_ptr> __u) {
2809 reset(__u->release());
2810 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
2811 return *this;
2812 }
2813
2814#endif // _LIBCPP_CXX03_LANG
2815
2816public:
2817 _LIBCPP_INLINE_VISIBILITY
2818 ~unique_ptr() { reset(); }
2819
2820 _LIBCPP_INLINE_VISIBILITY
2821 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2822 reset();
2823 return *this;
2824 }
2825
2826 _LIBCPP_INLINE_VISIBILITY
2827 typename add_lvalue_reference<_Tp>::type
2828 operator[](size_t __i) const {
2829 return __ptr_.first()[__i];
2830 }
2831 _LIBCPP_INLINE_VISIBILITY
2832 pointer get() const _NOEXCEPT {
2833 return __ptr_.first();
2834 }
2835
2836 _LIBCPP_INLINE_VISIBILITY
2837 deleter_type& get_deleter() _NOEXCEPT {
2838 return __ptr_.second();
2839 }
2840
2841 _LIBCPP_INLINE_VISIBILITY
2842 const deleter_type& get_deleter() const _NOEXCEPT {
2843 return __ptr_.second();
2844 }
2845 _LIBCPP_INLINE_VISIBILITY
2846 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2847 return __ptr_.first() != nullptr;
2848 }
2849
2850 _LIBCPP_INLINE_VISIBILITY
2851 pointer release() _NOEXCEPT {
2852 pointer __t = __ptr_.first();
2853 __ptr_.first() = pointer();
2854 return __t;
2855 }
2856
2857 template <class _Pp>
2858 _LIBCPP_INLINE_VISIBILITY
2859 typename enable_if<
Eric Fiselier31127cd2017-04-16 02:14:31 +00002860 _CheckArrayPointerConversion<_Pp>::value
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002861 >::type
2862 reset(_Pp __p) _NOEXCEPT {
2863 pointer __tmp = __ptr_.first();
2864 __ptr_.first() = __p;
2865 if (__tmp)
2866 __ptr_.second()(__tmp);
2867 }
2868
2869 _LIBCPP_INLINE_VISIBILITY
2870 void reset(nullptr_t = nullptr) _NOEXCEPT {
2871 pointer __tmp = __ptr_.first();
2872 __ptr_.first() = nullptr;
2873 if (__tmp)
2874 __ptr_.second()(__tmp);
2875 }
2876
2877 _LIBCPP_INLINE_VISIBILITY
2878 void swap(unique_ptr& __u) _NOEXCEPT {
2879 __ptr_.swap(__u.__ptr_);
2880 }
2881
Howard Hinnantc51e1022010-05-11 19:42:16 +00002882};
2883
2884template <class _Tp, class _Dp>
2885inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002886typename enable_if<
2887 __is_swappable<_Dp>::value,
2888 void
2889>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002890swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002891
2892template <class _T1, class _D1, class _T2, class _D2>
2893inline _LIBCPP_INLINE_VISIBILITY
2894bool
2895operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2896
2897template <class _T1, class _D1, class _T2, class _D2>
2898inline _LIBCPP_INLINE_VISIBILITY
2899bool
2900operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2901
2902template <class _T1, class _D1, class _T2, class _D2>
2903inline _LIBCPP_INLINE_VISIBILITY
2904bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002905operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2906{
2907 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2908 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002909 typedef typename common_type<_P1, _P2>::type _Vp;
2910 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002911}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002912
2913template <class _T1, class _D1, class _T2, class _D2>
2914inline _LIBCPP_INLINE_VISIBILITY
2915bool
2916operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2917
2918template <class _T1, class _D1, class _T2, class _D2>
2919inline _LIBCPP_INLINE_VISIBILITY
2920bool
2921operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2922
2923template <class _T1, class _D1, class _T2, class _D2>
2924inline _LIBCPP_INLINE_VISIBILITY
2925bool
2926operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2927
Howard Hinnantb17caf92012-02-21 21:02:58 +00002928template <class _T1, class _D1>
2929inline _LIBCPP_INLINE_VISIBILITY
2930bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002931operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002932{
2933 return !__x;
2934}
2935
2936template <class _T1, class _D1>
2937inline _LIBCPP_INLINE_VISIBILITY
2938bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002939operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002940{
2941 return !__x;
2942}
2943
2944template <class _T1, class _D1>
2945inline _LIBCPP_INLINE_VISIBILITY
2946bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002947operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002948{
2949 return static_cast<bool>(__x);
2950}
2951
2952template <class _T1, class _D1>
2953inline _LIBCPP_INLINE_VISIBILITY
2954bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002955operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002956{
2957 return static_cast<bool>(__x);
2958}
2959
2960template <class _T1, class _D1>
2961inline _LIBCPP_INLINE_VISIBILITY
2962bool
2963operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2964{
2965 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2966 return less<_P1>()(__x.get(), nullptr);
2967}
2968
2969template <class _T1, class _D1>
2970inline _LIBCPP_INLINE_VISIBILITY
2971bool
2972operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2973{
2974 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2975 return less<_P1>()(nullptr, __x.get());
2976}
2977
2978template <class _T1, class _D1>
2979inline _LIBCPP_INLINE_VISIBILITY
2980bool
2981operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2982{
2983 return nullptr < __x;
2984}
2985
2986template <class _T1, class _D1>
2987inline _LIBCPP_INLINE_VISIBILITY
2988bool
2989operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2990{
2991 return __x < nullptr;
2992}
2993
2994template <class _T1, class _D1>
2995inline _LIBCPP_INLINE_VISIBILITY
2996bool
2997operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2998{
2999 return !(nullptr < __x);
3000}
3001
3002template <class _T1, class _D1>
3003inline _LIBCPP_INLINE_VISIBILITY
3004bool
3005operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3006{
3007 return !(__x < nullptr);
3008}
3009
3010template <class _T1, class _D1>
3011inline _LIBCPP_INLINE_VISIBILITY
3012bool
3013operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3014{
3015 return !(__x < nullptr);
3016}
3017
3018template <class _T1, class _D1>
3019inline _LIBCPP_INLINE_VISIBILITY
3020bool
3021operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3022{
3023 return !(nullptr < __x);
3024}
3025
Howard Hinnant9e028f12012-05-01 15:37:54 +00003026#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3027
3028template <class _Tp, class _Dp>
3029inline _LIBCPP_INLINE_VISIBILITY
3030unique_ptr<_Tp, _Dp>
3031move(unique_ptr<_Tp, _Dp>& __t)
3032{
3033 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3034}
3035
3036#endif
3037
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003038#if _LIBCPP_STD_VER > 11
3039
3040template<class _Tp>
3041struct __unique_if
3042{
3043 typedef unique_ptr<_Tp> __unique_single;
3044};
3045
3046template<class _Tp>
3047struct __unique_if<_Tp[]>
3048{
3049 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3050};
3051
3052template<class _Tp, size_t _Np>
3053struct __unique_if<_Tp[_Np]>
3054{
3055 typedef void __unique_array_known_bound;
3056};
3057
3058template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003059inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003060typename __unique_if<_Tp>::__unique_single
3061make_unique(_Args&&... __args)
3062{
3063 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3064}
3065
3066template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003067inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003068typename __unique_if<_Tp>::__unique_array_unknown_bound
3069make_unique(size_t __n)
3070{
3071 typedef typename remove_extent<_Tp>::type _Up;
3072 return unique_ptr<_Tp>(new _Up[__n]());
3073}
3074
3075template<class _Tp, class... _Args>
3076 typename __unique_if<_Tp>::__unique_array_known_bound
3077 make_unique(_Args&&...) = delete;
3078
3079#endif // _LIBCPP_STD_VER > 11
3080
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003081template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003082#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003083struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003084#else
3085struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
3086 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer>>
3087#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003088{
3089 typedef unique_ptr<_Tp, _Dp> argument_type;
3090 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003091 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003092 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003093 {
3094 typedef typename argument_type::pointer pointer;
3095 return hash<pointer>()(__ptr.get());
3096 }
3097};
3098
Howard Hinnantc51e1022010-05-11 19:42:16 +00003099struct __destruct_n
3100{
3101private:
3102 size_t size;
3103
3104 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003105 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003106 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3107
3108 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003109 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003110 {}
3111
Howard Hinnant719bda32011-05-28 14:41:13 +00003112 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003113 {++size;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003114 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003115 {}
3116
Howard Hinnant719bda32011-05-28 14:41:13 +00003117 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003118 {size = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003119 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003120 {}
3121public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003122 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3123 : size(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003124
3125 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003126 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003127 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128
3129 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003130 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003131 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003132
3133 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003134 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003135 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003136};
3137
3138template <class _Alloc>
3139class __allocator_destructor
3140{
3141 typedef allocator_traits<_Alloc> __alloc_traits;
3142public:
3143 typedef typename __alloc_traits::pointer pointer;
3144 typedef typename __alloc_traits::size_type size_type;
3145private:
3146 _Alloc& __alloc_;
3147 size_type __s_;
3148public:
3149 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003150 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003153 void operator()(pointer __p) _NOEXCEPT
3154 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003155};
3156
3157template <class _InputIterator, class _ForwardIterator>
3158_ForwardIterator
3159uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3160{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003161 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003162#ifndef _LIBCPP_NO_EXCEPTIONS
3163 _ForwardIterator __s = __r;
3164 try
3165 {
3166#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003167 for (; __f != __l; ++__f, (void) ++__r)
3168 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003169#ifndef _LIBCPP_NO_EXCEPTIONS
3170 }
3171 catch (...)
3172 {
3173 for (; __s != __r; ++__s)
3174 __s->~value_type();
3175 throw;
3176 }
3177#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003178 return __r;
3179}
3180
3181template <class _InputIterator, class _Size, class _ForwardIterator>
3182_ForwardIterator
3183uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3184{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003185 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003186#ifndef _LIBCPP_NO_EXCEPTIONS
3187 _ForwardIterator __s = __r;
3188 try
3189 {
3190#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003191 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3192 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003193#ifndef _LIBCPP_NO_EXCEPTIONS
3194 }
3195 catch (...)
3196 {
3197 for (; __s != __r; ++__s)
3198 __s->~value_type();
3199 throw;
3200 }
3201#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003202 return __r;
3203}
3204
3205template <class _ForwardIterator, class _Tp>
3206void
3207uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3208{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003209 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003210#ifndef _LIBCPP_NO_EXCEPTIONS
3211 _ForwardIterator __s = __f;
3212 try
3213 {
3214#endif
3215 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003216 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003217#ifndef _LIBCPP_NO_EXCEPTIONS
3218 }
3219 catch (...)
3220 {
3221 for (; __s != __f; ++__s)
3222 __s->~value_type();
3223 throw;
3224 }
3225#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003226}
3227
3228template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003229_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003230uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3231{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003232 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003233#ifndef _LIBCPP_NO_EXCEPTIONS
3234 _ForwardIterator __s = __f;
3235 try
3236 {
3237#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003238 for (; __n > 0; ++__f, (void) --__n)
3239 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003240#ifndef _LIBCPP_NO_EXCEPTIONS
3241 }
3242 catch (...)
3243 {
3244 for (; __s != __f; ++__s)
3245 __s->~value_type();
3246 throw;
3247 }
3248#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003249 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003250}
3251
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003252#if _LIBCPP_STD_VER > 14
3253
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003254template <class _Tp>
3255inline _LIBCPP_INLINE_VISIBILITY
3256void destroy_at(_Tp* __loc) {
3257 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3258 __loc->~_Tp();
3259}
3260
3261template <class _ForwardIterator>
3262inline _LIBCPP_INLINE_VISIBILITY
3263void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3264 for (; __first != __last; ++__first)
3265 _VSTD::destroy_at(_VSTD::addressof(*__first));
3266}
3267
3268template <class _ForwardIterator, class _Size>
3269inline _LIBCPP_INLINE_VISIBILITY
3270_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3271 for (; __n > 0; (void)++__first, --__n)
3272 _VSTD::destroy_at(_VSTD::addressof(*__first));
3273 return __first;
3274}
3275
Eric Fiselier290c07c2016-10-11 21:13:44 +00003276template <class _ForwardIterator>
3277inline _LIBCPP_INLINE_VISIBILITY
3278void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3279 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3280 auto __idx = __first;
3281#ifndef _LIBCPP_NO_EXCEPTIONS
3282 try {
3283#endif
3284 for (; __idx != __last; ++__idx)
3285 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3286#ifndef _LIBCPP_NO_EXCEPTIONS
3287 } catch (...) {
3288 _VSTD::destroy(__first, __idx);
3289 throw;
3290 }
3291#endif
3292}
3293
3294template <class _ForwardIterator, class _Size>
3295inline _LIBCPP_INLINE_VISIBILITY
3296_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3297 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3298 auto __idx = __first;
3299#ifndef _LIBCPP_NO_EXCEPTIONS
3300 try {
3301#endif
3302 for (; __n > 0; (void)++__idx, --__n)
3303 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3304 return __idx;
3305#ifndef _LIBCPP_NO_EXCEPTIONS
3306 } catch (...) {
3307 _VSTD::destroy(__first, __idx);
3308 throw;
3309 }
3310#endif
3311}
3312
3313
3314template <class _ForwardIterator>
3315inline _LIBCPP_INLINE_VISIBILITY
3316void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3317 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3318 auto __idx = __first;
3319#ifndef _LIBCPP_NO_EXCEPTIONS
3320 try {
3321#endif
3322 for (; __idx != __last; ++__idx)
3323 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3324#ifndef _LIBCPP_NO_EXCEPTIONS
3325 } catch (...) {
3326 _VSTD::destroy(__first, __idx);
3327 throw;
3328 }
3329#endif
3330}
3331
3332template <class _ForwardIterator, class _Size>
3333inline _LIBCPP_INLINE_VISIBILITY
3334_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3335 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3336 auto __idx = __first;
3337#ifndef _LIBCPP_NO_EXCEPTIONS
3338 try {
3339#endif
3340 for (; __n > 0; (void)++__idx, --__n)
3341 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3342 return __idx;
3343#ifndef _LIBCPP_NO_EXCEPTIONS
3344 } catch (...) {
3345 _VSTD::destroy(__first, __idx);
3346 throw;
3347 }
3348#endif
3349}
3350
3351
3352template <class _InputIt, class _ForwardIt>
3353inline _LIBCPP_INLINE_VISIBILITY
3354_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3355 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3356 auto __idx = __first_res;
3357#ifndef _LIBCPP_NO_EXCEPTIONS
3358 try {
3359#endif
3360 for (; __first != __last; (void)++__idx, ++__first)
3361 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3362 return __idx;
3363#ifndef _LIBCPP_NO_EXCEPTIONS
3364 } catch (...) {
3365 _VSTD::destroy(__first_res, __idx);
3366 throw;
3367 }
3368#endif
3369}
3370
3371template <class _InputIt, class _Size, class _ForwardIt>
3372inline _LIBCPP_INLINE_VISIBILITY
3373pair<_InputIt, _ForwardIt>
3374uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3375 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3376 auto __idx = __first_res;
3377#ifndef _LIBCPP_NO_EXCEPTIONS
3378 try {
3379#endif
3380 for (; __n > 0; ++__idx, (void)++__first, --__n)
3381 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3382 return {__first, __idx};
3383#ifndef _LIBCPP_NO_EXCEPTIONS
3384 } catch (...) {
3385 _VSTD::destroy(__first_res, __idx);
3386 throw;
3387 }
3388#endif
3389}
3390
3391
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003392#endif // _LIBCPP_STD_VER > 14
3393
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003394// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3395// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003396// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003397#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3398 && defined(__ATOMIC_RELAXED) \
3399 && defined(__ATOMIC_ACQ_REL)
3400# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3401#elif !defined(__clang__) && defined(_GNUC_VER) && _GNUC_VER >= 407
3402# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3403#endif
3404
3405template <class _Tp>
3406inline _LIBCPP_INLINE_VISIBILITY _Tp
3407__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3408{
3409#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3410 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3411#else
3412 return __t += 1;
3413#endif
3414}
3415
3416template <class _Tp>
3417inline _LIBCPP_INLINE_VISIBILITY _Tp
3418__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3419{
3420#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3421 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3422#else
3423 return __t -= 1;
3424#endif
3425}
3426
Howard Hinnant756c69b2010-09-22 16:48:34 +00003427class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003428 : public std::exception
3429{
3430public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003431 virtual ~bad_weak_ptr() _NOEXCEPT;
3432 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003433};
3434
Marshall Clow8fea1612016-08-25 15:09:01 +00003435_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
3436void __throw_bad_weak_ptr()
3437{
3438#ifndef _LIBCPP_NO_EXCEPTIONS
3439 throw bad_weak_ptr();
3440#else
3441 _VSTD::abort();
3442#endif
3443}
3444
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003445template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003446
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003447class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003448{
3449 __shared_count(const __shared_count&);
3450 __shared_count& operator=(const __shared_count&);
3451
3452protected:
3453 long __shared_owners_;
3454 virtual ~__shared_count();
3455private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003456 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003457
3458public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003459 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003460 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003461 : __shared_owners_(__refs) {}
3462
Eric Fiselier98848572017-01-17 03:16:26 +00003463#if defined(_LIBCPP_BUILDING_MEMORY) && \
3464 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003465 void __add_shared() _NOEXCEPT;
3466 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003467#else
3468 _LIBCPP_INLINE_VISIBILITY
3469 void __add_shared() _NOEXCEPT {
3470 __libcpp_atomic_refcount_increment(__shared_owners_);
3471 }
3472 _LIBCPP_INLINE_VISIBILITY
3473 bool __release_shared() _NOEXCEPT {
3474 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3475 __on_zero_shared();
3476 return true;
3477 }
3478 return false;
3479 }
3480#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003481 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003482 long use_count() const _NOEXCEPT {
3483 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3484 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003485};
3486
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003487class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003488 : private __shared_count
3489{
3490 long __shared_weak_owners_;
3491
3492public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003493 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003494 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003495 : __shared_count(__refs),
3496 __shared_weak_owners_(__refs) {}
3497protected:
3498 virtual ~__shared_weak_count();
3499
3500public:
Eric Fiselier98848572017-01-17 03:16:26 +00003501#if defined(_LIBCPP_BUILDING_MEMORY) && \
3502 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003503 void __add_shared() _NOEXCEPT;
3504 void __add_weak() _NOEXCEPT;
3505 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003506#else
3507 _LIBCPP_INLINE_VISIBILITY
3508 void __add_shared() _NOEXCEPT {
3509 __shared_count::__add_shared();
3510 }
3511 _LIBCPP_INLINE_VISIBILITY
3512 void __add_weak() _NOEXCEPT {
3513 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3514 }
3515 _LIBCPP_INLINE_VISIBILITY
3516 void __release_shared() _NOEXCEPT {
3517 if (__shared_count::__release_shared())
3518 __release_weak();
3519 }
3520#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003521 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003522 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003523 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3524 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003525
Howard Hinnant807d6332013-02-25 15:50:36 +00003526 // Define the function out only if we build static libc++ without RTTI.
3527 // Otherwise we may break clients who need to compile their projects with
3528 // -fno-rtti and yet link against a libc++.dylib compiled
3529 // without -fno-rtti.
3530#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003531 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003532#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003533private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003534 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003535};
3536
3537template <class _Tp, class _Dp, class _Alloc>
3538class __shared_ptr_pointer
3539 : public __shared_weak_count
3540{
3541 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3542public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003543 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003544 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003545 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003546
Howard Hinnant72f73582010-08-11 17:04:31 +00003547#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003548 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003549#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003550
3551private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003552 virtual void __on_zero_shared() _NOEXCEPT;
3553 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003554};
3555
Howard Hinnant72f73582010-08-11 17:04:31 +00003556#ifndef _LIBCPP_NO_RTTI
3557
Howard Hinnantc51e1022010-05-11 19:42:16 +00003558template <class _Tp, class _Dp, class _Alloc>
3559const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003560__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003561{
Marshall Clowa8dfc942014-11-17 19:05:50 +00003562 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003563}
3564
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003565#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003566
Howard Hinnantc51e1022010-05-11 19:42:16 +00003567template <class _Tp, class _Dp, class _Alloc>
3568void
Howard Hinnant719bda32011-05-28 14:41:13 +00003569__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003570{
3571 __data_.first().second()(__data_.first().first());
3572 __data_.first().second().~_Dp();
3573}
3574
3575template <class _Tp, class _Dp, class _Alloc>
3576void
Howard Hinnant719bda32011-05-28 14:41:13 +00003577__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003578{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003579 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3580 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003581 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3582
Eric Fiselierf8898c82015-02-05 23:01:40 +00003583 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003584 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003585 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003586}
3587
3588template <class _Tp, class _Alloc>
3589class __shared_ptr_emplace
3590 : public __shared_weak_count
3591{
3592 __compressed_pair<_Alloc, _Tp> __data_;
3593public:
3594#ifndef _LIBCPP_HAS_NO_VARIADICS
3595
Howard Hinnant756c69b2010-09-22 16:48:34 +00003596 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003597 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003598 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003599
3600 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003602 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003603 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3604 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003605
3606#else // _LIBCPP_HAS_NO_VARIADICS
3607
Howard Hinnant756c69b2010-09-22 16:48:34 +00003608 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003609 __shared_ptr_emplace(_Alloc __a)
3610 : __data_(__a) {}
3611
3612 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003613 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003614 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3615 : __data_(__a, _Tp(__a0)) {}
3616
3617 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003618 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003619 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3620 : __data_(__a, _Tp(__a0, __a1)) {}
3621
3622 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003623 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003624 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3625 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3626
3627#endif // _LIBCPP_HAS_NO_VARIADICS
3628
3629private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003630 virtual void __on_zero_shared() _NOEXCEPT;
3631 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003632public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003633 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003634 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635};
3636
3637template <class _Tp, class _Alloc>
3638void
Howard Hinnant719bda32011-05-28 14:41:13 +00003639__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003640{
3641 __data_.second().~_Tp();
3642}
3643
3644template <class _Tp, class _Alloc>
3645void
Howard Hinnant719bda32011-05-28 14:41:13 +00003646__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003647{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003648 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3649 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003650 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003651 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003652 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003653 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003654}
3655
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003656template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003657
3658template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003659class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003660{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003661public:
3662 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003663
Eric Fiselierae5b6672016-06-27 01:02:43 +00003664#if _LIBCPP_STD_VER > 14
3665 typedef weak_ptr<_Tp> weak_type;
3666#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003667private:
3668 element_type* __ptr_;
3669 __shared_weak_count* __cntrl_;
3670
3671 struct __nat {int __for_bool_;};
3672public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003673 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003674 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003675 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003676 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003677 template<class _Yp>
3678 explicit shared_ptr(_Yp* __p,
3679 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3680 template<class _Yp, class _Dp>
3681 shared_ptr(_Yp* __p, _Dp __d,
3682 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3683 template<class _Yp, class _Dp, class _Alloc>
3684 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3685 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003686 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3687 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003688 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003690 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003691 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003693 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003694 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003695 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003696#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003697 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003698 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003699 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003700 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003701 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003702#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003703 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003704 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003705#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003706#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003707 template<class _Yp>
3708 shared_ptr(auto_ptr<_Yp>&& __r,
3709 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003710#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003711 template<class _Yp>
3712 shared_ptr(auto_ptr<_Yp> __r,
3713 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003714#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003715#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003716#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003717 template <class _Yp, class _Dp>
3718 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3719 typename enable_if
3720 <
3721 !is_lvalue_reference<_Dp>::value &&
3722 !is_array<_Yp>::value &&
3723 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3724 __nat
3725 >::type = __nat());
3726 template <class _Yp, class _Dp>
3727 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3728 typename enable_if
3729 <
3730 is_lvalue_reference<_Dp>::value &&
3731 !is_array<_Yp>::value &&
3732 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3733 __nat
3734 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003735#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003736 template <class _Yp, class _Dp>
3737 shared_ptr(unique_ptr<_Yp, _Dp>,
3738 typename enable_if
3739 <
3740 !is_lvalue_reference<_Dp>::value &&
3741 !is_array<_Yp>::value &&
3742 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3743 __nat
3744 >::type = __nat());
3745 template <class _Yp, class _Dp>
3746 shared_ptr(unique_ptr<_Yp, _Dp>,
3747 typename enable_if
3748 <
3749 is_lvalue_reference<_Dp>::value &&
3750 !is_array<_Yp>::value &&
3751 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3752 __nat
3753 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003754#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003755
3756 ~shared_ptr();
3757
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003758 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003759 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003760 template<class _Yp>
3761 typename enable_if
3762 <
3763 is_convertible<_Yp*, element_type*>::value,
3764 shared_ptr&
3765 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003766 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003767 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003768#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003769 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003770 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003771 template<class _Yp>
3772 typename enable_if
3773 <
3774 is_convertible<_Yp*, element_type*>::value,
3775 shared_ptr<_Tp>&
3776 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003777 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003778 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003779#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003780 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003782 typename enable_if
3783 <
3784 !is_array<_Yp>::value &&
3785 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003786 shared_ptr
3787 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003788 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003789#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003790#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003791#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003792 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003794 typename enable_if
3795 <
3796 !is_array<_Yp>::value &&
3797 is_convertible<_Yp*, element_type*>::value,
3798 shared_ptr&
3799 >::type
3800 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003801#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003802#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003803 template <class _Yp, class _Dp>
3804 typename enable_if
3805 <
3806 !is_array<_Yp>::value &&
3807 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3808 shared_ptr&
3809 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003810#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003812 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003813#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003814 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003815 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003816#endif
3817
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003818 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003819 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003820 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003821 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003822 template<class _Yp>
3823 typename enable_if
3824 <
3825 is_convertible<_Yp*, element_type*>::value,
3826 void
3827 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003829 reset(_Yp* __p);
3830 template<class _Yp, class _Dp>
3831 typename enable_if
3832 <
3833 is_convertible<_Yp*, element_type*>::value,
3834 void
3835 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003836 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003837 reset(_Yp* __p, _Dp __d);
3838 template<class _Yp, class _Dp, class _Alloc>
3839 typename enable_if
3840 <
3841 is_convertible<_Yp*, element_type*>::value,
3842 void
3843 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003845 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003846
Howard Hinnant756c69b2010-09-22 16:48:34 +00003847 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003848 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003849 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003850 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3851 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003852 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003853 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003854 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003855 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003857 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003858 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003859 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003860 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003861 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003862 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003863 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003864 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003865 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003866 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003867 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003868 _LIBCPP_INLINE_VISIBILITY
3869 bool
3870 __owner_equivalent(const shared_ptr& __p) const
3871 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003872
Howard Hinnant72f73582010-08-11 17:04:31 +00003873#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003874 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003875 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003876 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003877 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003878#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003879
3880#ifndef _LIBCPP_HAS_NO_VARIADICS
3881
3882 template<class ..._Args>
3883 static
3884 shared_ptr<_Tp>
3885 make_shared(_Args&& ...__args);
3886
3887 template<class _Alloc, class ..._Args>
3888 static
3889 shared_ptr<_Tp>
3890 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3891
3892#else // _LIBCPP_HAS_NO_VARIADICS
3893
3894 static shared_ptr<_Tp> make_shared();
3895
3896 template<class _A0>
3897 static shared_ptr<_Tp> make_shared(_A0&);
3898
3899 template<class _A0, class _A1>
3900 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3901
3902 template<class _A0, class _A1, class _A2>
3903 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3904
3905 template<class _Alloc>
3906 static shared_ptr<_Tp>
3907 allocate_shared(const _Alloc& __a);
3908
3909 template<class _Alloc, class _A0>
3910 static shared_ptr<_Tp>
3911 allocate_shared(const _Alloc& __a, _A0& __a0);
3912
3913 template<class _Alloc, class _A0, class _A1>
3914 static shared_ptr<_Tp>
3915 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3916
3917 template<class _Alloc, class _A0, class _A1, class _A2>
3918 static shared_ptr<_Tp>
3919 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3920
3921#endif // _LIBCPP_HAS_NO_VARIADICS
3922
3923private:
3924
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003925 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003926 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003927 void
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003928 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3929 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003930 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003931 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003932 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003933 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003934 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3935 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003936 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003937 }
3938
Howard Hinnant756c69b2010-09-22 16:48:34 +00003939 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003940 void __enable_weak_this(const volatile void*, const volatile void*) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003941
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003942 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3943 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003944};
3945
3946template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003947inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003948_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003949shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003950 : __ptr_(0),
3951 __cntrl_(0)
3952{
3953}
3954
3955template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003956inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003957_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003958shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003959 : __ptr_(0),
3960 __cntrl_(0)
3961{
3962}
3963
3964template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003965template<class _Yp>
3966shared_ptr<_Tp>::shared_ptr(_Yp* __p,
3967 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003968 : __ptr_(__p)
3969{
3970 unique_ptr<_Yp> __hold(__p);
3971 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3972 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
3973 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003974 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003975}
3976
3977template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003978template<class _Yp, class _Dp>
3979shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
3980 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003981 : __ptr_(__p)
3982{
3983#ifndef _LIBCPP_NO_EXCEPTIONS
3984 try
3985 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003986#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003987 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
3988 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003989 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003990#ifndef _LIBCPP_NO_EXCEPTIONS
3991 }
3992 catch (...)
3993 {
3994 __d(__p);
3995 throw;
3996 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003997#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003998}
3999
4000template<class _Tp>
4001template<class _Dp>
4002shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4003 : __ptr_(0)
4004{
4005#ifndef _LIBCPP_NO_EXCEPTIONS
4006 try
4007 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004008#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004009 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4010 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4011#ifndef _LIBCPP_NO_EXCEPTIONS
4012 }
4013 catch (...)
4014 {
4015 __d(__p);
4016 throw;
4017 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004018#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004019}
4020
4021template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004022template<class _Yp, class _Dp, class _Alloc>
4023shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4024 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004025 : __ptr_(__p)
4026{
4027#ifndef _LIBCPP_NO_EXCEPTIONS
4028 try
4029 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004030#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004031 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004032 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004033 typedef __allocator_destructor<_A2> _D2;
4034 _A2 __a2(__a);
4035 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004036 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4037 _CntrlBlk(__p, __d, __a);
4038 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004039 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004040#ifndef _LIBCPP_NO_EXCEPTIONS
4041 }
4042 catch (...)
4043 {
4044 __d(__p);
4045 throw;
4046 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004047#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004048}
4049
4050template<class _Tp>
4051template<class _Dp, class _Alloc>
4052shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4053 : __ptr_(0)
4054{
4055#ifndef _LIBCPP_NO_EXCEPTIONS
4056 try
4057 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004058#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004059 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004060 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004061 typedef __allocator_destructor<_A2> _D2;
4062 _A2 __a2(__a);
4063 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004064 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4065 _CntrlBlk(__p, __d, __a);
4066 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004067#ifndef _LIBCPP_NO_EXCEPTIONS
4068 }
4069 catch (...)
4070 {
4071 __d(__p);
4072 throw;
4073 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004074#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004075}
4076
4077template<class _Tp>
4078template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004079inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004080shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004081 : __ptr_(__p),
4082 __cntrl_(__r.__cntrl_)
4083{
4084 if (__cntrl_)
4085 __cntrl_->__add_shared();
4086}
4087
4088template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004089inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004090shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004091 : __ptr_(__r.__ptr_),
4092 __cntrl_(__r.__cntrl_)
4093{
4094 if (__cntrl_)
4095 __cntrl_->__add_shared();
4096}
4097
4098template<class _Tp>
4099template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004100inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004101shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004102 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004103 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004104 : __ptr_(__r.__ptr_),
4105 __cntrl_(__r.__cntrl_)
4106{
4107 if (__cntrl_)
4108 __cntrl_->__add_shared();
4109}
4110
Howard Hinnant74279a52010-09-04 23:28:19 +00004111#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004112
4113template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004114inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004115shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004116 : __ptr_(__r.__ptr_),
4117 __cntrl_(__r.__cntrl_)
4118{
4119 __r.__ptr_ = 0;
4120 __r.__cntrl_ = 0;
4121}
4122
4123template<class _Tp>
4124template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004125inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004126shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004127 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004128 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004129 : __ptr_(__r.__ptr_),
4130 __cntrl_(__r.__cntrl_)
4131{
4132 __r.__ptr_ = 0;
4133 __r.__cntrl_ = 0;
4134}
4135
Howard Hinnant74279a52010-09-04 23:28:19 +00004136#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004137
Marshall Clowb22274f2017-01-24 22:22:33 +00004138#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004139template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004140template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004141#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004142shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004143#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004144shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004145#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004146 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004147 : __ptr_(__r.get())
4148{
4149 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4150 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004151 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004152 __r.release();
4153}
Marshall Clowb22274f2017-01-24 22:22:33 +00004154#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004155
4156template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004157template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004158#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004159shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4160#else
4161shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4162#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004163 typename enable_if
4164 <
4165 !is_lvalue_reference<_Dp>::value &&
4166 !is_array<_Yp>::value &&
4167 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4168 __nat
4169 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004170 : __ptr_(__r.get())
4171{
Marshall Clow35cde742015-05-10 13:59:45 +00004172#if _LIBCPP_STD_VER > 11
4173 if (__ptr_ == nullptr)
4174 __cntrl_ = nullptr;
4175 else
4176#endif
4177 {
4178 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4179 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004180 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004181 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004182 __r.release();
4183}
4184
4185template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004186template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004187#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004188shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4189#else
4190shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4191#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004192 typename enable_if
4193 <
4194 is_lvalue_reference<_Dp>::value &&
4195 !is_array<_Yp>::value &&
4196 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4197 __nat
4198 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004199 : __ptr_(__r.get())
4200{
Marshall Clow35cde742015-05-10 13:59:45 +00004201#if _LIBCPP_STD_VER > 11
4202 if (__ptr_ == nullptr)
4203 __cntrl_ = nullptr;
4204 else
4205#endif
4206 {
4207 typedef __shared_ptr_pointer<_Yp*,
4208 reference_wrapper<typename remove_reference<_Dp>::type>,
4209 allocator<_Yp> > _CntrlBlk;
4210 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004211 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004212 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004213 __r.release();
4214}
4215
4216#ifndef _LIBCPP_HAS_NO_VARIADICS
4217
4218template<class _Tp>
4219template<class ..._Args>
4220shared_ptr<_Tp>
4221shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4222{
4223 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4224 typedef allocator<_CntrlBlk> _A2;
4225 typedef __allocator_destructor<_A2> _D2;
4226 _A2 __a2;
4227 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004228 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004229 shared_ptr<_Tp> __r;
4230 __r.__ptr_ = __hold2.get()->get();
4231 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004232 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004233 return __r;
4234}
4235
4236template<class _Tp>
4237template<class _Alloc, class ..._Args>
4238shared_ptr<_Tp>
4239shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4240{
4241 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004242 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004243 typedef __allocator_destructor<_A2> _D2;
4244 _A2 __a2(__a);
4245 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004246 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4247 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004248 shared_ptr<_Tp> __r;
4249 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004250 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004251 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004252 return __r;
4253}
4254
4255#else // _LIBCPP_HAS_NO_VARIADICS
4256
4257template<class _Tp>
4258shared_ptr<_Tp>
4259shared_ptr<_Tp>::make_shared()
4260{
4261 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4262 typedef allocator<_CntrlBlk> _Alloc2;
4263 typedef __allocator_destructor<_Alloc2> _D2;
4264 _Alloc2 __alloc2;
4265 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4266 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4267 shared_ptr<_Tp> __r;
4268 __r.__ptr_ = __hold2.get()->get();
4269 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004270 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004271 return __r;
4272}
4273
4274template<class _Tp>
4275template<class _A0>
4276shared_ptr<_Tp>
4277shared_ptr<_Tp>::make_shared(_A0& __a0)
4278{
4279 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4280 typedef allocator<_CntrlBlk> _Alloc2;
4281 typedef __allocator_destructor<_Alloc2> _D2;
4282 _Alloc2 __alloc2;
4283 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4284 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4285 shared_ptr<_Tp> __r;
4286 __r.__ptr_ = __hold2.get()->get();
4287 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004288 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004289 return __r;
4290}
4291
4292template<class _Tp>
4293template<class _A0, class _A1>
4294shared_ptr<_Tp>
4295shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4296{
4297 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4298 typedef allocator<_CntrlBlk> _Alloc2;
4299 typedef __allocator_destructor<_Alloc2> _D2;
4300 _Alloc2 __alloc2;
4301 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4302 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4303 shared_ptr<_Tp> __r;
4304 __r.__ptr_ = __hold2.get()->get();
4305 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004306 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004307 return __r;
4308}
4309
4310template<class _Tp>
4311template<class _A0, class _A1, class _A2>
4312shared_ptr<_Tp>
4313shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4314{
4315 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4316 typedef allocator<_CntrlBlk> _Alloc2;
4317 typedef __allocator_destructor<_Alloc2> _D2;
4318 _Alloc2 __alloc2;
4319 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4320 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4321 shared_ptr<_Tp> __r;
4322 __r.__ptr_ = __hold2.get()->get();
4323 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004324 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004325 return __r;
4326}
4327
4328template<class _Tp>
4329template<class _Alloc>
4330shared_ptr<_Tp>
4331shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4332{
4333 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004334 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004335 typedef __allocator_destructor<_Alloc2> _D2;
4336 _Alloc2 __alloc2(__a);
4337 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004338 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4339 _CntrlBlk(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004340 shared_ptr<_Tp> __r;
4341 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004342 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004343 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004344 return __r;
4345}
4346
4347template<class _Tp>
4348template<class _Alloc, class _A0>
4349shared_ptr<_Tp>
4350shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4351{
4352 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004353 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004354 typedef __allocator_destructor<_Alloc2> _D2;
4355 _Alloc2 __alloc2(__a);
4356 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004357 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4358 _CntrlBlk(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004359 shared_ptr<_Tp> __r;
4360 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004361 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004362 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004363 return __r;
4364}
4365
4366template<class _Tp>
4367template<class _Alloc, class _A0, class _A1>
4368shared_ptr<_Tp>
4369shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4370{
4371 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004372 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004373 typedef __allocator_destructor<_Alloc2> _D2;
4374 _Alloc2 __alloc2(__a);
4375 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004376 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4377 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004378 shared_ptr<_Tp> __r;
4379 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004380 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004381 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004382 return __r;
4383}
4384
4385template<class _Tp>
4386template<class _Alloc, class _A0, class _A1, class _A2>
4387shared_ptr<_Tp>
4388shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4389{
4390 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004391 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004392 typedef __allocator_destructor<_Alloc2> _D2;
4393 _Alloc2 __alloc2(__a);
4394 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004395 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4396 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004397 shared_ptr<_Tp> __r;
4398 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004399 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004400 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004401 return __r;
4402}
4403
4404#endif // _LIBCPP_HAS_NO_VARIADICS
4405
4406template<class _Tp>
4407shared_ptr<_Tp>::~shared_ptr()
4408{
4409 if (__cntrl_)
4410 __cntrl_->__release_shared();
4411}
4412
4413template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004414inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004415shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004416shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004417{
4418 shared_ptr(__r).swap(*this);
4419 return *this;
4420}
4421
4422template<class _Tp>
4423template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004424inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004425typename enable_if
4426<
Marshall Clow7e384b72017-01-10 16:59:33 +00004427 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004428 shared_ptr<_Tp>&
4429>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004430shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004431{
4432 shared_ptr(__r).swap(*this);
4433 return *this;
4434}
4435
Howard Hinnant74279a52010-09-04 23:28:19 +00004436#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004437
4438template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004439inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004440shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004441shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004442{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004443 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004444 return *this;
4445}
4446
4447template<class _Tp>
4448template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004449inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004450typename enable_if
4451<
Marshall Clow7e384b72017-01-10 16:59:33 +00004452 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004453 shared_ptr<_Tp>&
4454>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004455shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4456{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004457 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004458 return *this;
4459}
4460
Marshall Clowb22274f2017-01-24 22:22:33 +00004461#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004462template<class _Tp>
4463template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004464inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004465typename enable_if
4466<
4467 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004468 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004469 shared_ptr<_Tp>
4470>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004471shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4472{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004473 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004474 return *this;
4475}
Marshall Clowb22274f2017-01-24 22:22:33 +00004476#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004477
4478template<class _Tp>
4479template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004480inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004481typename enable_if
4482<
4483 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004484 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4485 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004486 shared_ptr<_Tp>&
4487>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004488shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4489{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004490 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004491 return *this;
4492}
4493
Howard Hinnant74279a52010-09-04 23:28:19 +00004494#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004495
Marshall Clowb22274f2017-01-24 22:22:33 +00004496#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004497template<class _Tp>
4498template<class _Yp>
4499inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004500typename enable_if
4501<
4502 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004503 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004504 shared_ptr<_Tp>&
4505>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004506shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004507{
4508 shared_ptr(__r).swap(*this);
4509 return *this;
4510}
Marshall Clowb22274f2017-01-24 22:22:33 +00004511#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004512
4513template<class _Tp>
4514template <class _Yp, class _Dp>
4515inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004516typename enable_if
4517<
4518 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004519 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4520 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004521 shared_ptr<_Tp>&
4522>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004523shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4524{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004525 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004526 return *this;
4527}
4528
Howard Hinnant74279a52010-09-04 23:28:19 +00004529#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004530
4531template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004532inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004533void
Howard Hinnant719bda32011-05-28 14:41:13 +00004534shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004535{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004536 _VSTD::swap(__ptr_, __r.__ptr_);
4537 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004538}
4539
4540template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004541inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004542void
Howard Hinnant719bda32011-05-28 14:41:13 +00004543shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004544{
4545 shared_ptr().swap(*this);
4546}
4547
4548template<class _Tp>
4549template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004550inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004551typename enable_if
4552<
Marshall Clow7e384b72017-01-10 16:59:33 +00004553 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004554 void
4555>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004556shared_ptr<_Tp>::reset(_Yp* __p)
4557{
4558 shared_ptr(__p).swap(*this);
4559}
4560
4561template<class _Tp>
4562template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004563inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004564typename enable_if
4565<
Marshall Clow7e384b72017-01-10 16:59:33 +00004566 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004567 void
4568>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004569shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4570{
4571 shared_ptr(__p, __d).swap(*this);
4572}
4573
4574template<class _Tp>
4575template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004576inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004577typename enable_if
4578<
Marshall Clow7e384b72017-01-10 16:59:33 +00004579 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004580 void
4581>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004582shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4583{
4584 shared_ptr(__p, __d, __a).swap(*this);
4585}
4586
4587#ifndef _LIBCPP_HAS_NO_VARIADICS
4588
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004589template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004590inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004591typename enable_if
4592<
4593 !is_array<_Tp>::value,
4594 shared_ptr<_Tp>
4595>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004596make_shared(_Args&& ...__args)
4597{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004598 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004599}
4600
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004601template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004602inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004603typename enable_if
4604<
4605 !is_array<_Tp>::value,
4606 shared_ptr<_Tp>
4607>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004608allocate_shared(const _Alloc& __a, _Args&& ...__args)
4609{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004610 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004611}
4612
4613#else // _LIBCPP_HAS_NO_VARIADICS
4614
4615template<class _Tp>
4616inline _LIBCPP_INLINE_VISIBILITY
4617shared_ptr<_Tp>
4618make_shared()
4619{
4620 return shared_ptr<_Tp>::make_shared();
4621}
4622
4623template<class _Tp, class _A0>
4624inline _LIBCPP_INLINE_VISIBILITY
4625shared_ptr<_Tp>
4626make_shared(_A0& __a0)
4627{
4628 return shared_ptr<_Tp>::make_shared(__a0);
4629}
4630
4631template<class _Tp, class _A0, class _A1>
4632inline _LIBCPP_INLINE_VISIBILITY
4633shared_ptr<_Tp>
4634make_shared(_A0& __a0, _A1& __a1)
4635{
4636 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4637}
4638
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004639template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004640inline _LIBCPP_INLINE_VISIBILITY
4641shared_ptr<_Tp>
4642make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4643{
4644 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4645}
4646
4647template<class _Tp, class _Alloc>
4648inline _LIBCPP_INLINE_VISIBILITY
4649shared_ptr<_Tp>
4650allocate_shared(const _Alloc& __a)
4651{
4652 return shared_ptr<_Tp>::allocate_shared(__a);
4653}
4654
4655template<class _Tp, class _Alloc, class _A0>
4656inline _LIBCPP_INLINE_VISIBILITY
4657shared_ptr<_Tp>
4658allocate_shared(const _Alloc& __a, _A0& __a0)
4659{
4660 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4661}
4662
4663template<class _Tp, class _Alloc, class _A0, class _A1>
4664inline _LIBCPP_INLINE_VISIBILITY
4665shared_ptr<_Tp>
4666allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4667{
4668 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4669}
4670
4671template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4672inline _LIBCPP_INLINE_VISIBILITY
4673shared_ptr<_Tp>
4674allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4675{
4676 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4677}
4678
4679#endif // _LIBCPP_HAS_NO_VARIADICS
4680
4681template<class _Tp, class _Up>
4682inline _LIBCPP_INLINE_VISIBILITY
4683bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004684operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004685{
4686 return __x.get() == __y.get();
4687}
4688
4689template<class _Tp, class _Up>
4690inline _LIBCPP_INLINE_VISIBILITY
4691bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004692operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004693{
4694 return !(__x == __y);
4695}
4696
4697template<class _Tp, class _Up>
4698inline _LIBCPP_INLINE_VISIBILITY
4699bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004700operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004701{
Eric Fiselierf8898c82015-02-05 23:01:40 +00004702 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4703 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00004704}
4705
4706template<class _Tp, class _Up>
4707inline _LIBCPP_INLINE_VISIBILITY
4708bool
4709operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4710{
4711 return __y < __x;
4712}
4713
4714template<class _Tp, class _Up>
4715inline _LIBCPP_INLINE_VISIBILITY
4716bool
4717operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4718{
4719 return !(__y < __x);
4720}
4721
4722template<class _Tp, class _Up>
4723inline _LIBCPP_INLINE_VISIBILITY
4724bool
4725operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4726{
4727 return !(__x < __y);
4728}
4729
4730template<class _Tp>
4731inline _LIBCPP_INLINE_VISIBILITY
4732bool
4733operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4734{
4735 return !__x;
4736}
4737
4738template<class _Tp>
4739inline _LIBCPP_INLINE_VISIBILITY
4740bool
4741operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4742{
4743 return !__x;
4744}
4745
4746template<class _Tp>
4747inline _LIBCPP_INLINE_VISIBILITY
4748bool
4749operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4750{
4751 return static_cast<bool>(__x);
4752}
4753
4754template<class _Tp>
4755inline _LIBCPP_INLINE_VISIBILITY
4756bool
4757operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4758{
4759 return static_cast<bool>(__x);
4760}
4761
4762template<class _Tp>
4763inline _LIBCPP_INLINE_VISIBILITY
4764bool
4765operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4766{
4767 return less<_Tp*>()(__x.get(), nullptr);
4768}
4769
4770template<class _Tp>
4771inline _LIBCPP_INLINE_VISIBILITY
4772bool
4773operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4774{
4775 return less<_Tp*>()(nullptr, __x.get());
4776}
4777
4778template<class _Tp>
4779inline _LIBCPP_INLINE_VISIBILITY
4780bool
4781operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4782{
4783 return nullptr < __x;
4784}
4785
4786template<class _Tp>
4787inline _LIBCPP_INLINE_VISIBILITY
4788bool
4789operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4790{
4791 return __x < nullptr;
4792}
4793
4794template<class _Tp>
4795inline _LIBCPP_INLINE_VISIBILITY
4796bool
4797operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4798{
4799 return !(nullptr < __x);
4800}
4801
4802template<class _Tp>
4803inline _LIBCPP_INLINE_VISIBILITY
4804bool
4805operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4806{
4807 return !(__x < nullptr);
4808}
4809
4810template<class _Tp>
4811inline _LIBCPP_INLINE_VISIBILITY
4812bool
4813operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4814{
4815 return !(__x < nullptr);
4816}
4817
4818template<class _Tp>
4819inline _LIBCPP_INLINE_VISIBILITY
4820bool
4821operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4822{
4823 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004824}
4825
4826template<class _Tp>
4827inline _LIBCPP_INLINE_VISIBILITY
4828void
Howard Hinnant719bda32011-05-28 14:41:13 +00004829swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004830{
4831 __x.swap(__y);
4832}
4833
4834template<class _Tp, class _Up>
4835inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004836typename enable_if
4837<
4838 !is_array<_Tp>::value && !is_array<_Up>::value,
4839 shared_ptr<_Tp>
4840>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004841static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004842{
4843 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4844}
4845
4846template<class _Tp, class _Up>
4847inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004848typename enable_if
4849<
4850 !is_array<_Tp>::value && !is_array<_Up>::value,
4851 shared_ptr<_Tp>
4852>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004853dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004854{
4855 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4856 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4857}
4858
4859template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004860typename enable_if
4861<
4862 is_array<_Tp>::value == is_array<_Up>::value,
4863 shared_ptr<_Tp>
4864>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004865const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004866{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004867 typedef typename remove_extent<_Tp>::type _RTp;
4868 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004869}
4870
Howard Hinnant72f73582010-08-11 17:04:31 +00004871#ifndef _LIBCPP_NO_RTTI
4872
Howard Hinnantc51e1022010-05-11 19:42:16 +00004873template<class _Dp, class _Tp>
4874inline _LIBCPP_INLINE_VISIBILITY
4875_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004876get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004877{
4878 return __p.template __get_deleter<_Dp>();
4879}
4880
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004881#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004882
Howard Hinnantc51e1022010-05-11 19:42:16 +00004883template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004884class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004885{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004886public:
4887 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004888private:
4889 element_type* __ptr_;
4890 __shared_weak_count* __cntrl_;
4891
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004892public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004894 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004895 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004896 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4897 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004899 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004900 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004901 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4902 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004903
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004904#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004905 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004906 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004907 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004908 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4909 _NOEXCEPT;
4910#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004911 ~weak_ptr();
4912
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004914 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004915 template<class _Yp>
4916 typename enable_if
4917 <
4918 is_convertible<_Yp*, element_type*>::value,
4919 weak_ptr&
4920 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004921 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004922 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4923
4924#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4925
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004926 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004927 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4928 template<class _Yp>
4929 typename enable_if
4930 <
4931 is_convertible<_Yp*, element_type*>::value,
4932 weak_ptr&
4933 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004934 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004935 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4936
4937#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4938
4939 template<class _Yp>
4940 typename enable_if
4941 <
4942 is_convertible<_Yp*, element_type*>::value,
4943 weak_ptr&
4944 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004945 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004946 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004947
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004949 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004950 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004951 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004952
Howard Hinnant756c69b2010-09-22 16:48:34 +00004953 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004954 long use_count() const _NOEXCEPT
4955 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004956 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004957 bool expired() const _NOEXCEPT
4958 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4959 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004960 template<class _Up>
4961 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004962 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004963 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004964 template<class _Up>
4965 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004966 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004967 {return __cntrl_ < __r.__cntrl_;}
4968
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004969 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4970 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004971};
4972
4973template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004974inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004975_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004976weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004977 : __ptr_(0),
4978 __cntrl_(0)
4979{
4980}
4981
4982template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004983inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004984weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004985 : __ptr_(__r.__ptr_),
4986 __cntrl_(__r.__cntrl_)
4987{
4988 if (__cntrl_)
4989 __cntrl_->__add_weak();
4990}
4991
4992template<class _Tp>
4993template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004994inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004995weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00004996 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004997 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004998 : __ptr_(__r.__ptr_),
4999 __cntrl_(__r.__cntrl_)
5000{
5001 if (__cntrl_)
5002 __cntrl_->__add_weak();
5003}
5004
5005template<class _Tp>
5006template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005007inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005008weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005009 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005010 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005011 : __ptr_(__r.__ptr_),
5012 __cntrl_(__r.__cntrl_)
5013{
5014 if (__cntrl_)
5015 __cntrl_->__add_weak();
5016}
5017
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005018#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5019
5020template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005021inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005022weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5023 : __ptr_(__r.__ptr_),
5024 __cntrl_(__r.__cntrl_)
5025{
5026 __r.__ptr_ = 0;
5027 __r.__cntrl_ = 0;
5028}
5029
5030template<class _Tp>
5031template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005032inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005033weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5034 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5035 _NOEXCEPT
5036 : __ptr_(__r.__ptr_),
5037 __cntrl_(__r.__cntrl_)
5038{
5039 __r.__ptr_ = 0;
5040 __r.__cntrl_ = 0;
5041}
5042
5043#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5044
Howard Hinnantc51e1022010-05-11 19:42:16 +00005045template<class _Tp>
5046weak_ptr<_Tp>::~weak_ptr()
5047{
5048 if (__cntrl_)
5049 __cntrl_->__release_weak();
5050}
5051
5052template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005053inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005054weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005055weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005056{
5057 weak_ptr(__r).swap(*this);
5058 return *this;
5059}
5060
5061template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005062template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005063inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005064typename enable_if
5065<
5066 is_convertible<_Yp*, _Tp*>::value,
5067 weak_ptr<_Tp>&
5068>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005069weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005070{
5071 weak_ptr(__r).swap(*this);
5072 return *this;
5073}
5074
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005075#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5076
5077template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005078inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005079weak_ptr<_Tp>&
5080weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5081{
5082 weak_ptr(_VSTD::move(__r)).swap(*this);
5083 return *this;
5084}
5085
Howard Hinnantc51e1022010-05-11 19:42:16 +00005086template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005087template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005088inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005089typename enable_if
5090<
5091 is_convertible<_Yp*, _Tp*>::value,
5092 weak_ptr<_Tp>&
5093>::type
5094weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5095{
5096 weak_ptr(_VSTD::move(__r)).swap(*this);
5097 return *this;
5098}
5099
5100#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5101
5102template<class _Tp>
5103template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005104inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005105typename enable_if
5106<
5107 is_convertible<_Yp*, _Tp*>::value,
5108 weak_ptr<_Tp>&
5109>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005110weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005111{
5112 weak_ptr(__r).swap(*this);
5113 return *this;
5114}
5115
5116template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005117inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005118void
Howard Hinnant719bda32011-05-28 14:41:13 +00005119weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005120{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005121 _VSTD::swap(__ptr_, __r.__ptr_);
5122 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005123}
5124
5125template<class _Tp>
5126inline _LIBCPP_INLINE_VISIBILITY
5127void
Howard Hinnant719bda32011-05-28 14:41:13 +00005128swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005129{
5130 __x.swap(__y);
5131}
5132
5133template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005134inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005135void
Howard Hinnant719bda32011-05-28 14:41:13 +00005136weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005137{
5138 weak_ptr().swap(*this);
5139}
5140
5141template<class _Tp>
5142template<class _Yp>
5143shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00005144 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005145 : __ptr_(__r.__ptr_),
5146 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5147{
5148 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00005149 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00005150}
5151
5152template<class _Tp>
5153shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005154weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005155{
5156 shared_ptr<_Tp> __r;
5157 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5158 if (__r.__cntrl_)
5159 __r.__ptr_ = __ptr_;
5160 return __r;
5161}
5162
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005163#if _LIBCPP_STD_VER > 14
5164template <class _Tp = void> struct owner_less;
5165#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005166template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005167#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005168
5169template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005170struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005171 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005172{
5173 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005174 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005175 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005176 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005177 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005178 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005179 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005180 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005181 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005182 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005183};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005184
5185template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005186struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005187 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5188{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005189 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005190 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005191 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005192 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005193 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005194 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005195 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005196 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005197 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005198 {return __x.owner_before(__y);}
5199};
5200
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005201#if _LIBCPP_STD_VER > 14
5202template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005203struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005204{
5205 template <class _Tp, class _Up>
5206 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005207 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005208 {return __x.owner_before(__y);}
5209 template <class _Tp, class _Up>
5210 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005211 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005212 {return __x.owner_before(__y);}
5213 template <class _Tp, class _Up>
5214 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005215 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005216 {return __x.owner_before(__y);}
5217 template <class _Tp, class _Up>
5218 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005219 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005220 {return __x.owner_before(__y);}
5221 typedef void is_transparent;
5222};
5223#endif
5224
Howard Hinnantc51e1022010-05-11 19:42:16 +00005225template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005226class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005227{
5228 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005229protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005230 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005231 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005232 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005233 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005235 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5236 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005238 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005239public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005241 shared_ptr<_Tp> shared_from_this()
5242 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005244 shared_ptr<_Tp const> shared_from_this() const
5245 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005246
Eric Fiselier84006862016-06-02 00:15:35 +00005247#if _LIBCPP_STD_VER > 14
5248 _LIBCPP_INLINE_VISIBILITY
5249 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5250 { return __weak_this_; }
5251
5252 _LIBCPP_INLINE_VISIBILITY
5253 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5254 { return __weak_this_; }
5255#endif // _LIBCPP_STD_VER > 14
5256
Howard Hinnantc51e1022010-05-11 19:42:16 +00005257 template <class _Up> friend class shared_ptr;
5258};
5259
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005260template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005261struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005262{
5263 typedef shared_ptr<_Tp> argument_type;
5264 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005265
Howard Hinnant756c69b2010-09-22 16:48:34 +00005266 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005267 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005268 {
5269 return hash<_Tp*>()(__ptr.get());
5270 }
5271};
5272
Howard Hinnantc834c512011-11-29 18:15:50 +00005273template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005274inline _LIBCPP_INLINE_VISIBILITY
5275basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005276operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005277
Eric Fiselier9b492672016-06-18 02:12:53 +00005278
5279#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005280
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005281class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005282{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005283 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005284public:
5285 void lock() _NOEXCEPT;
5286 void unlock() _NOEXCEPT;
5287
5288private:
5289 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5290 __sp_mut(const __sp_mut&);
5291 __sp_mut& operator=(const __sp_mut&);
5292
Howard Hinnant8331b762013-03-06 23:30:19 +00005293 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005294};
5295
Howard Hinnant8331b762013-03-06 23:30:19 +00005296_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005297
5298template <class _Tp>
5299inline _LIBCPP_INLINE_VISIBILITY
5300bool
5301atomic_is_lock_free(const shared_ptr<_Tp>*)
5302{
5303 return false;
5304}
5305
5306template <class _Tp>
5307shared_ptr<_Tp>
5308atomic_load(const shared_ptr<_Tp>* __p)
5309{
5310 __sp_mut& __m = __get_sp_mut(__p);
5311 __m.lock();
5312 shared_ptr<_Tp> __q = *__p;
5313 __m.unlock();
5314 return __q;
5315}
5316
5317template <class _Tp>
5318inline _LIBCPP_INLINE_VISIBILITY
5319shared_ptr<_Tp>
5320atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5321{
5322 return atomic_load(__p);
5323}
5324
5325template <class _Tp>
5326void
5327atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5328{
5329 __sp_mut& __m = __get_sp_mut(__p);
5330 __m.lock();
5331 __p->swap(__r);
5332 __m.unlock();
5333}
5334
5335template <class _Tp>
5336inline _LIBCPP_INLINE_VISIBILITY
5337void
5338atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5339{
5340 atomic_store(__p, __r);
5341}
5342
5343template <class _Tp>
5344shared_ptr<_Tp>
5345atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5346{
5347 __sp_mut& __m = __get_sp_mut(__p);
5348 __m.lock();
5349 __p->swap(__r);
5350 __m.unlock();
5351 return __r;
5352}
5353
5354template <class _Tp>
5355inline _LIBCPP_INLINE_VISIBILITY
5356shared_ptr<_Tp>
5357atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5358{
5359 return atomic_exchange(__p, __r);
5360}
5361
5362template <class _Tp>
5363bool
5364atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5365{
Marshall Clow4201ee82016-05-18 17:50:13 +00005366 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005367 __sp_mut& __m = __get_sp_mut(__p);
5368 __m.lock();
5369 if (__p->__owner_equivalent(*__v))
5370 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005371 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005372 *__p = __w;
5373 __m.unlock();
5374 return true;
5375 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005376 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005377 *__v = *__p;
5378 __m.unlock();
5379 return false;
5380}
5381
5382template <class _Tp>
5383inline _LIBCPP_INLINE_VISIBILITY
5384bool
5385atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5386{
5387 return atomic_compare_exchange_strong(__p, __v, __w);
5388}
5389
5390template <class _Tp>
5391inline _LIBCPP_INLINE_VISIBILITY
5392bool
5393atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5394 shared_ptr<_Tp> __w, memory_order, memory_order)
5395{
5396 return atomic_compare_exchange_strong(__p, __v, __w);
5397}
5398
5399template <class _Tp>
5400inline _LIBCPP_INLINE_VISIBILITY
5401bool
5402atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5403 shared_ptr<_Tp> __w, memory_order, memory_order)
5404{
5405 return atomic_compare_exchange_weak(__p, __v, __w);
5406}
5407
Eric Fiselier9b492672016-06-18 02:12:53 +00005408#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005409
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005410//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005411#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5412# ifndef _LIBCPP_CXX03_LANG
5413enum class pointer_safety : unsigned char {
5414 relaxed,
5415 preferred,
5416 strict
5417};
5418# endif
5419#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005420struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005421{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005422 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005423 {
5424 relaxed,
5425 preferred,
5426 strict
5427 };
5428
Howard Hinnant49e145e2012-10-30 19:06:59 +00005429 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005430
Howard Hinnant756c69b2010-09-22 16:48:34 +00005431 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005432 pointer_safety() : __v_() {}
5433
5434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005435 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005436 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005437 operator int() const {return __v_;}
5438};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005439#endif
5440
5441#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
5442 defined(_LIBCPP_BUILDING_MEMORY)
5443_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5444#else
5445// This function is only offered in C++03 under ABI v1.
5446# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5447inline _LIBCPP_INLINE_VISIBILITY
5448pointer_safety get_pointer_safety() _NOEXCEPT {
5449 return pointer_safety::relaxed;
5450}
5451# endif
5452#endif
5453
Howard Hinnantc51e1022010-05-11 19:42:16 +00005454
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005455_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5456_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5457_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005458_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005459
5460template <class _Tp>
5461inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005462_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005463undeclare_reachable(_Tp* __p)
5464{
5465 return static_cast<_Tp*>(__undeclare_reachable(__p));
5466}
5467
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005468_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005469
Marshall Clow8982dcd2015-07-13 20:04:56 +00005470// --- Helper for container swap --
5471template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005472inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005473void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5474#if _LIBCPP_STD_VER >= 14
5475 _NOEXCEPT
5476#else
5477 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5478#endif
5479{
5480 __swap_allocator(__a1, __a2,
5481 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5482}
5483
5484template <typename _Alloc>
5485_LIBCPP_INLINE_VISIBILITY
5486void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5487#if _LIBCPP_STD_VER >= 14
5488 _NOEXCEPT
5489#else
5490 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5491#endif
5492{
5493 using _VSTD::swap;
5494 swap(__a1, __a2);
5495}
5496
5497template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005498inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005499void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5500
Marshall Clowff91de82015-08-18 19:51:37 +00005501template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005502struct __noexcept_move_assign_container : public integral_constant<bool,
5503 _Traits::propagate_on_container_move_assignment::value
5504#if _LIBCPP_STD_VER > 14
5505 || _Traits::is_always_equal::value
5506#else
5507 && is_nothrow_move_assignable<_Alloc>::value
5508#endif
5509 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005510
Marshall Clowa591b9a2016-07-11 21:38:08 +00005511
5512#ifndef _LIBCPP_HAS_NO_VARIADICS
5513template <class _Tp, class _Alloc>
5514struct __temp_value {
5515 typedef allocator_traits<_Alloc> _Traits;
5516
5517 typename aligned_storage<sizeof(_Tp), alignof(_Tp)>::type __v;
5518 _Alloc &__a;
5519
5520 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5521 _Tp & get() { return *__addr(); }
5522
5523 template<class... _Args>
5524 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc)
5525 { _Traits::construct(__a, __addr(), _VSTD::forward<_Args>(__args)...); }
5526
5527 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5528 };
5529#endif
5530
Howard Hinnantc51e1022010-05-11 19:42:16 +00005531_LIBCPP_END_NAMESPACE_STD
5532
5533#endif // _LIBCPP_MEMORY