blob: faf89aa0ccbfb4176373ab07fb7cd8c9c75a1324 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_MEMORY
12#define _LIBCPP_MEMORY
13
14/*
15 memory synopsis
16
17namespace std
18{
19
20struct allocator_arg_t { };
21constexpr allocator_arg_t allocator_arg = allocator_arg_t();
22
23template <class T, class Alloc> struct uses_allocator;
24
25template <class Ptr>
26struct pointer_traits
27{
28 typedef Ptr pointer;
29 typedef <details> element_type;
30 typedef <details> difference_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000031
Howard Hinnantc51e1022010-05-11 19:42:16 +000032 template <class U> using rebind = <details>;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000033
Howard Hinnantc51e1022010-05-11 19:42:16 +000034 static pointer pointer_to(<details>);
35};
36
Howard Hinnant719bda32011-05-28 14:41:13 +000037template <class T>
38struct pointer_traits<T*>
39{
40 typedef T* pointer;
41 typedef T element_type;
42 typedef ptrdiff_t difference_type;
43
44 template <class U> using rebind = U*;
45
46 static pointer pointer_to(<details>) noexcept;
47};
48
Howard Hinnantc51e1022010-05-11 19:42:16 +000049template <class Alloc>
50struct allocator_traits
51{
52 typedef Alloc allocator_type;
53 typedef typename allocator_type::value_type
54 value_type;
55
56 typedef Alloc::pointer | value_type* pointer;
57 typedef Alloc::const_pointer
58 | pointer_traits<pointer>::rebind<const value_type>
59 const_pointer;
60 typedef Alloc::void_pointer
61 | pointer_traits<pointer>::rebind<void>
62 void_pointer;
63 typedef Alloc::const_void_pointer
64 | pointer_traits<pointer>::rebind<const void>
65 const_void_pointer;
66 typedef Alloc::difference_type
Howard Hinnant8e4e6002010-11-18 01:40:00 +000067 | pointer_traits<pointer>::difference_type
68 difference_type;
69 typedef Alloc::size_type
70 | make_unsigned<difference_type>::type
71 size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000072 typedef Alloc::propagate_on_container_copy_assignment
73 | false_type propagate_on_container_copy_assignment;
74 typedef Alloc::propagate_on_container_move_assignment
75 | false_type propagate_on_container_move_assignment;
76 typedef Alloc::propagate_on_container_swap
77 | false_type propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +000078 typedef Alloc::is_always_equal
79 | is_empty is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +000080
81 template <class T> using rebind_alloc = Alloc::rebind<U>::other | Alloc<T, Args...>;
82 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
83
84 static pointer allocate(allocator_type& a, size_type n);
85 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint);
86
Howard Hinnant719bda32011-05-28 14:41:13 +000087 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
89 template <class T, class... Args>
90 static void construct(allocator_type& a, T* p, Args&&... args);
91
92 template <class T>
93 static void destroy(allocator_type& a, T* p);
94
Marshall Clow4f834b52013-08-27 20:22:15 +000095 static size_type max_size(const allocator_type& a); // noexcept in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000096
97 static allocator_type
98 select_on_container_copy_construction(const allocator_type& a);
99};
100
101template <>
102class allocator<void>
103{
104public:
105 typedef void* pointer;
106 typedef const void* const_pointer;
107 typedef void value_type;
108
109 template <class _Up> struct rebind {typedef allocator<_Up> other;};
110};
111
112template <class T>
113class allocator
114{
115public:
116 typedef size_t size_type;
117 typedef ptrdiff_t difference_type;
118 typedef T* pointer;
119 typedef const T* const_pointer;
120 typedef typename add_lvalue_reference<T>::type reference;
121 typedef typename add_lvalue_reference<const T>::type const_reference;
122 typedef T value_type;
123
124 template <class U> struct rebind {typedef allocator<U> other;};
125
Howard Hinnant719bda32011-05-28 14:41:13 +0000126 allocator() noexcept;
127 allocator(const allocator&) noexcept;
128 template <class U> allocator(const allocator<U>&) noexcept;
129 ~allocator();
130 pointer address(reference x) const noexcept;
131 const_pointer address(const_reference x) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
Howard Hinnant719bda32011-05-28 14:41:13 +0000133 void deallocate(pointer p, size_type n) noexcept;
134 size_type max_size() const noexcept;
135 template<class U, class... Args>
136 void construct(U* p, Args&&... args);
137 template <class U>
138 void destroy(U* p);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000139};
140
141template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000142bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000143
144template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000145bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000146
147template <class OutputIterator, class T>
148class raw_storage_iterator
149 : public iterator<output_iterator_tag,
150 T, // purposefully not C++03
151 ptrdiff_t, // purposefully not C++03
152 T*, // purposefully not C++03
153 raw_storage_iterator&> // purposefully not C++03
154{
155public:
156 explicit raw_storage_iterator(OutputIterator x);
157 raw_storage_iterator& operator*();
158 raw_storage_iterator& operator=(const T& element);
159 raw_storage_iterator& operator++();
160 raw_storage_iterator operator++(int);
161};
162
Howard Hinnant719bda32011-05-28 14:41:13 +0000163template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
164template <class T> void return_temporary_buffer(T* p) noexcept;
165
166template <class T> T* addressof(T& r) noexcept;
Marshall Clow78dbe462016-11-14 18:22:19 +0000167template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000168
169template <class InputIterator, class ForwardIterator>
170ForwardIterator
171uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
172
Howard Hinnant719bda32011-05-28 14:41:13 +0000173template <class InputIterator, class Size, class ForwardIterator>
174ForwardIterator
175uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
176
Howard Hinnantc51e1022010-05-11 19:42:16 +0000177template <class ForwardIterator, class T>
178void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
179
180template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000181ForwardIterator
182uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000183
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000184template <class T>
185void destroy_at(T* location);
186
187template <class ForwardIterator>
188 void destroy(ForwardIterator first, ForwardIterator last);
189
190template <class ForwardIterator, class Size>
191 ForwardIterator destroy_n(ForwardIterator first, Size n);
192
193template <class InputIterator, class ForwardIterator>
194 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
195
196template <class InputIterator, class Size, class ForwardIterator>
197 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
198
199template <class ForwardIterator>
200 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
201
202template <class ForwardIterator, class Size>
203 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
204
205template <class ForwardIterator>
206 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
207
208template <class ForwardIterator, class Size>
209 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
210
Marshall Clowb22274f2017-01-24 22:22:33 +0000211template <class Y> struct auto_ptr_ref {}; // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212
213template<class X>
Marshall Clowb22274f2017-01-24 22:22:33 +0000214class auto_ptr // removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215{
216public:
217 typedef X element_type;
218
219 explicit auto_ptr(X* p =0) throw();
220 auto_ptr(auto_ptr&) throw();
221 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
222 auto_ptr& operator=(auto_ptr&) throw();
223 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
224 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
225 ~auto_ptr() throw();
226
227 typename add_lvalue_reference<X>::type operator*() const throw();
228 X* operator->() const throw();
229 X* get() const throw();
230 X* release() throw();
231 void reset(X* p =0) throw();
232
233 auto_ptr(auto_ptr_ref<X>) throw();
234 template<class Y> operator auto_ptr_ref<Y>() throw();
235 template<class Y> operator auto_ptr<Y>() throw();
236};
237
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000238template <class T>
239struct default_delete
240{
Howard Hinnant719bda32011-05-28 14:41:13 +0000241 constexpr default_delete() noexcept = default;
242 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000243
Howard Hinnant719bda32011-05-28 14:41:13 +0000244 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000245};
246
247template <class T>
248struct default_delete<T[]>
249{
Howard Hinnant719bda32011-05-28 14:41:13 +0000250 constexpr default_delete() noexcept = default;
251 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000252 template <class U> void operator()(U*) const = delete;
253};
254
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000255template <class T, class D = default_delete<T>>
256class unique_ptr
257{
258public:
259 typedef see below pointer;
260 typedef T element_type;
261 typedef D deleter_type;
262
263 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000264 constexpr unique_ptr() noexcept;
265 explicit unique_ptr(pointer p) noexcept;
266 unique_ptr(pointer p, see below d1) noexcept;
267 unique_ptr(pointer p, see below d2) noexcept;
268 unique_ptr(unique_ptr&& u) noexcept;
269 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000270 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000271 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000272 template <class U>
Marshall Clowb22274f2017-01-24 22:22:33 +0000273 unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000274
275 // destructor
276 ~unique_ptr();
277
278 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000279 unique_ptr& operator=(unique_ptr&& u) noexcept;
280 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
281 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000282
283 // observers
284 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000285 pointer operator->() const noexcept;
286 pointer get() const noexcept;
287 deleter_type& get_deleter() noexcept;
288 const deleter_type& get_deleter() const noexcept;
289 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000290
291 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000292 pointer release() noexcept;
293 void reset(pointer p = pointer()) noexcept;
294 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000295};
296
297template <class T, class D>
298class unique_ptr<T[], D>
299{
300public:
301 typedef implementation-defined pointer;
302 typedef T element_type;
303 typedef D deleter_type;
304
305 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000306 constexpr unique_ptr() noexcept;
307 explicit unique_ptr(pointer p) noexcept;
308 unique_ptr(pointer p, see below d) noexcept;
309 unique_ptr(pointer p, see below d) noexcept;
310 unique_ptr(unique_ptr&& u) noexcept;
311 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000312
313 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000314 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000315
316 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000317 unique_ptr& operator=(unique_ptr&& u) noexcept;
318 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000319
320 // observers
321 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000322 pointer get() const noexcept;
323 deleter_type& get_deleter() noexcept;
324 const deleter_type& get_deleter() const noexcept;
325 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000326
327 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000328 pointer release() noexcept;
329 void reset(pointer p = pointer()) noexcept;
330 void reset(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000331 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000332 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000333};
334
335template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000336 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000337
338template <class T1, class D1, class T2, class D2>
339 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
340template <class T1, class D1, class T2, class D2>
341 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
342template <class T1, class D1, class T2, class D2>
343 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
344template <class T1, class D1, class T2, class D2>
345 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
346template <class T1, class D1, class T2, class D2>
347 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
348template <class T1, class D1, class T2, class D2>
349 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
350
Howard Hinnant719bda32011-05-28 14:41:13 +0000351template <class T, class D>
352 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
353template <class T, class D>
354 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
355template <class T, class D>
356 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
357template <class T, class D>
358 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
359
360template <class T, class D>
361 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
362template <class T, class D>
363 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
364template <class T, class D>
365 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
366template <class T, class D>
367 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
368template <class T, class D>
369 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
370template <class T, class D>
371 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
372template <class T, class D>
373 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
374template <class T, class D>
375 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
376
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000377class bad_weak_ptr
378 : public std::exception
379{
Howard Hinnant719bda32011-05-28 14:41:13 +0000380 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000381};
382
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000383template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
384template<class T> unique_ptr<T> make_unique(size_t n); // C++14
385template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
386
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000387template<class T>
388class shared_ptr
389{
390public:
391 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000392 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000393
394 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000395 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000396 template<class Y> explicit shared_ptr(Y* p);
397 template<class Y, class D> shared_ptr(Y* p, D d);
398 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
399 template <class D> shared_ptr(nullptr_t p, D d);
400 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000401 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
402 shared_ptr(const shared_ptr& r) noexcept;
403 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
404 shared_ptr(shared_ptr&& r) noexcept;
405 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000406 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000407 template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000408 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
409 shared_ptr(nullptr_t) : shared_ptr() { }
410
411 // destructor:
412 ~shared_ptr();
413
414 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000415 shared_ptr& operator=(const shared_ptr& r) noexcept;
416 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
417 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000418 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
Marshall Clowb22274f2017-01-24 22:22:33 +0000419 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000420 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
421
422 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000423 void swap(shared_ptr& r) noexcept;
424 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000425 template<class Y> void reset(Y* p);
426 template<class Y, class D> void reset(Y* p, D d);
427 template<class Y, class D, class A> void reset(Y* p, D d, A a);
428
Howard Hinnant719bda32011-05-28 14:41:13 +0000429 // observers:
430 T* get() const noexcept;
431 T& operator*() const noexcept;
432 T* operator->() const noexcept;
433 long use_count() const noexcept;
434 bool unique() const noexcept;
435 explicit operator bool() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000436 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
437 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000438};
439
440// shared_ptr comparisons:
441template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000442 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000443template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000444 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000445template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000446 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000447template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000448 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000449template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000450 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000451template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000452 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
453
454template <class T>
455 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
456template <class T>
457 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
458template <class T>
459 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
460template <class T>
461 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
462template <class T>
463 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
464template <class T>
465bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
466template <class T>
467 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
468template <class T>
469 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
470template <class T>
471 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
472template <class T>
473 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
474template <class T>
475 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
476template <class T>
477 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000478
479// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000480template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000481
482// shared_ptr casts:
483template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000484 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000485template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000486 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000487template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000488 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000489
490// shared_ptr I/O:
491template<class E, class T, class Y>
492 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
493
494// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000495template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000496
497template<class T, class... Args>
498 shared_ptr<T> make_shared(Args&&... args);
499template<class T, class A, class... Args>
500 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
501
502template<class T>
503class weak_ptr
504{
505public:
506 typedef T element_type;
507
508 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000509 constexpr weak_ptr() noexcept;
510 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
511 weak_ptr(weak_ptr const& r) noexcept;
512 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000513 weak_ptr(weak_ptr&& r) noexcept; // C++14
514 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000515
516 // destructor
517 ~weak_ptr();
518
519 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000520 weak_ptr& operator=(weak_ptr const& r) noexcept;
521 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
522 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000523 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
524 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000525
526 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000527 void swap(weak_ptr& r) noexcept;
528 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000529
530 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000531 long use_count() const noexcept;
532 bool expired() const noexcept;
533 shared_ptr<T> lock() const noexcept;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000534 template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
535 template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000536};
537
538// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000539template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000540
541// class owner_less:
542template<class T> struct owner_less;
543
544template<class T>
545struct owner_less<shared_ptr<T>>
546 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
547{
548 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000549 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
550 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
551 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000552};
553
554template<class T>
555struct owner_less<weak_ptr<T>>
556 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
557{
558 typedef bool result_type;
Marshall Clow18a7cd52017-04-11 17:08:53 +0000559 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
560 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
561 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
562};
563
564template <> // Added in C++14
565struct owner_less<void>
566{
567 template <class _Tp, class _Up>
568 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
569 template <class _Tp, class _Up>
570 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
571 template <class _Tp, class _Up>
572 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
573 template <class _Tp, class _Up>
574 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
575
576 typedef void is_transparent;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000577};
578
579template<class T>
580class enable_shared_from_this
581{
582protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000583 constexpr enable_shared_from_this() noexcept;
584 enable_shared_from_this(enable_shared_from_this const&) noexcept;
585 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000586 ~enable_shared_from_this();
587public:
588 shared_ptr<T> shared_from_this();
589 shared_ptr<T const> shared_from_this() const;
590};
591
592template<class T>
593 bool atomic_is_lock_free(const shared_ptr<T>* p);
594template<class T>
595 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
596template<class T>
597 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
598template<class T>
599 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
600template<class T>
601 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
602template<class T>
603 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
604template<class T>
605 shared_ptr<T>
606 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
607template<class T>
608 bool
609 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
610template<class T>
611 bool
612 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
613template<class T>
614 bool
615 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
616 shared_ptr<T> w, memory_order success,
617 memory_order failure);
618template<class T>
619 bool
620 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
621 shared_ptr<T> w, memory_order success,
622 memory_order failure);
623// Hash support
624template <class T> struct hash;
625template <class T, class D> struct hash<unique_ptr<T, D> >;
626template <class T> struct hash<shared_ptr<T> >;
627
628// Pointer safety
629enum class pointer_safety { relaxed, preferred, strict };
630void declare_reachable(void *p);
631template <class T> T *undeclare_reachable(T *p);
632void declare_no_pointers(char *p, size_t n);
633void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000634pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000635
Howard Hinnantc51e1022010-05-11 19:42:16 +0000636void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
637
638} // std
639
640*/
641
642#include <__config>
643#include <type_traits>
644#include <typeinfo>
645#include <cstddef>
646#include <cstdint>
647#include <new>
648#include <utility>
649#include <limits>
650#include <iterator>
651#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000652#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000653#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000654#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000655#include <cstring>
Eric Fiselier9d355982017-04-12 23:45:53 +0000656#include <cassert>
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000657#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000658# include <atomic>
659#endif
660
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000661#include <__undef_min_max>
662
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000663#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000664#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000665#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000666
667_LIBCPP_BEGIN_NAMESPACE_STD
668
Eric Fiselier89659d12015-07-07 00:27:16 +0000669template <class _ValueType>
670inline _LIBCPP_ALWAYS_INLINE
671_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
672#if !defined(_LIBCPP_HAS_NO_THREADS) && \
673 defined(__ATOMIC_RELAXED) && \
674 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
675 return __atomic_load_n(__value, __ATOMIC_RELAXED);
676#else
677 return *__value;
678#endif
679}
680
Kuba Breckade9d6792016-09-04 09:55:12 +0000681template <class _ValueType>
682inline _LIBCPP_ALWAYS_INLINE
683_ValueType __libcpp_acquire_load(_ValueType const* __value) {
684#if !defined(_LIBCPP_HAS_NO_THREADS) && \
685 defined(__ATOMIC_ACQUIRE) && \
686 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
687 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
688#else
689 return *__value;
690#endif
691}
692
Marshall Clow78dbe462016-11-14 18:22:19 +0000693// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000694
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695template <class _Tp> class allocator;
696
697template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000698class _LIBCPP_TEMPLATE_VIS allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699{
700public:
701 typedef void* pointer;
702 typedef const void* const_pointer;
703 typedef void value_type;
704
705 template <class _Up> struct rebind {typedef allocator<_Up> other;};
706};
707
Howard Hinnant9f771052012-01-19 23:15:22 +0000708template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000709class _LIBCPP_TEMPLATE_VIS allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000710{
711public:
712 typedef const void* pointer;
713 typedef const void* const_pointer;
714 typedef const void value_type;
715
716 template <class _Up> struct rebind {typedef allocator<_Up> other;};
717};
718
Howard Hinnantc51e1022010-05-11 19:42:16 +0000719// pointer_traits
720
721template <class _Tp>
722struct __has_element_type
723{
724private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000725 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726 template <class _Up> static __two __test(...);
727 template <class _Up> static char __test(typename _Up::element_type* = 0);
728public:
729 static const bool value = sizeof(__test<_Tp>(0)) == 1;
730};
731
732template <class _Ptr, bool = __has_element_type<_Ptr>::value>
733struct __pointer_traits_element_type;
734
735template <class _Ptr>
736struct __pointer_traits_element_type<_Ptr, true>
737{
738 typedef typename _Ptr::element_type type;
739};
740
741#ifndef _LIBCPP_HAS_NO_VARIADICS
742
743template <template <class, class...> class _Sp, class _Tp, class ..._Args>
744struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
745{
746 typedef typename _Sp<_Tp, _Args...>::element_type type;
747};
748
749template <template <class, class...> class _Sp, class _Tp, class ..._Args>
750struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
751{
752 typedef _Tp type;
753};
754
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000755#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000756
757template <template <class> class _Sp, class _Tp>
758struct __pointer_traits_element_type<_Sp<_Tp>, true>
759{
760 typedef typename _Sp<_Tp>::element_type type;
761};
762
763template <template <class> class _Sp, class _Tp>
764struct __pointer_traits_element_type<_Sp<_Tp>, false>
765{
766 typedef _Tp type;
767};
768
769template <template <class, class> class _Sp, class _Tp, class _A0>
770struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
771{
772 typedef typename _Sp<_Tp, _A0>::element_type type;
773};
774
775template <template <class, class> class _Sp, class _Tp, class _A0>
776struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
777{
778 typedef _Tp type;
779};
780
781template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
782struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
783{
784 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
785};
786
787template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
788struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
789{
790 typedef _Tp type;
791};
792
793template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
794 class _A1, class _A2>
795struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
796{
797 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
798};
799
800template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
801 class _A1, class _A2>
802struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
803{
804 typedef _Tp type;
805};
806
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000807#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000808
809template <class _Tp>
810struct __has_difference_type
811{
812private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000813 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000814 template <class _Up> static __two __test(...);
815 template <class _Up> static char __test(typename _Up::difference_type* = 0);
816public:
817 static const bool value = sizeof(__test<_Tp>(0)) == 1;
818};
819
820template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
821struct __pointer_traits_difference_type
822{
823 typedef ptrdiff_t type;
824};
825
826template <class _Ptr>
827struct __pointer_traits_difference_type<_Ptr, true>
828{
829 typedef typename _Ptr::difference_type type;
830};
831
832template <class _Tp, class _Up>
833struct __has_rebind
834{
835private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000836 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000837 template <class _Xp> static __two __test(...);
838 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
839public:
840 static const bool value = sizeof(__test<_Tp>(0)) == 1;
841};
842
843template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
844struct __pointer_traits_rebind
845{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000846#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000847 typedef typename _Tp::template rebind<_Up> type;
848#else
849 typedef typename _Tp::template rebind<_Up>::other type;
850#endif
851};
852
853#ifndef _LIBCPP_HAS_NO_VARIADICS
854
855template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
856struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
857{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000858#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000859 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
860#else
861 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
862#endif
863};
864
865template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
866struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
867{
868 typedef _Sp<_Up, _Args...> type;
869};
870
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000871#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872
873template <template <class> class _Sp, class _Tp, class _Up>
874struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
875{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000876#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000877 typedef typename _Sp<_Tp>::template rebind<_Up> type;
878#else
879 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
880#endif
881};
882
883template <template <class> class _Sp, class _Tp, class _Up>
884struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
885{
886 typedef _Sp<_Up> type;
887};
888
889template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
890struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
891{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000892#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000893 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
894#else
895 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
896#endif
897};
898
899template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
900struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
901{
902 typedef _Sp<_Up, _A0> type;
903};
904
905template <template <class, class, class> class _Sp, class _Tp, class _A0,
906 class _A1, class _Up>
907struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
908{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000909#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
911#else
912 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
913#endif
914};
915
916template <template <class, class, class> class _Sp, class _Tp, class _A0,
917 class _A1, class _Up>
918struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
919{
920 typedef _Sp<_Up, _A0, _A1> type;
921};
922
923template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
924 class _A1, class _A2, class _Up>
925struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
926{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000927#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
929#else
930 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
931#endif
932};
933
934template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
935 class _A1, class _A2, class _Up>
936struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
937{
938 typedef _Sp<_Up, _A0, _A1, _A2> type;
939};
940
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000941#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942
943template <class _Ptr>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000944struct _LIBCPP_TEMPLATE_VIS pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945{
946 typedef _Ptr pointer;
947 typedef typename __pointer_traits_element_type<pointer>::type element_type;
948 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
949
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000950#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000951 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000952#else
953 template <class _Up> struct rebind
954 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000955#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956
957private:
958 struct __nat {};
959public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000960 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961 static pointer pointer_to(typename conditional<is_void<element_type>::value,
962 __nat, element_type>::type& __r)
963 {return pointer::pointer_to(__r);}
964};
965
966template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000967struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968{
969 typedef _Tp* pointer;
970 typedef _Tp element_type;
971 typedef ptrdiff_t difference_type;
972
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000973#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000974 template <class _Up> using rebind = _Up*;
975#else
976 template <class _Up> struct rebind {typedef _Up* other;};
977#endif
978
979private:
980 struct __nat {};
981public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000984 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000985 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986};
987
Eric Fiselierae3ab842015-08-23 02:56:05 +0000988template <class _From, class _To>
989struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000990#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000991 typedef typename pointer_traits<_From>::template rebind<_To> type;
992#else
993 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
994#endif
995};
996
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997// allocator_traits
998
999namespace __has_pointer_type_imp
1000{
Howard Hinnant6e260172013-09-21 01:45:05 +00001001 template <class _Up> static __two __test(...);
1002 template <class _Up> static char __test(typename _Up::pointer* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001003}
1004
1005template <class _Tp>
1006struct __has_pointer_type
Howard Hinnant6e260172013-09-21 01:45:05 +00001007 : public integral_constant<bool, sizeof(__has_pointer_type_imp::__test<_Tp>(0)) == 1>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008{
1009};
1010
1011namespace __pointer_type_imp
1012{
1013
1014template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1015struct __pointer_type
1016{
1017 typedef typename _Dp::pointer type;
1018};
1019
1020template <class _Tp, class _Dp>
1021struct __pointer_type<_Tp, _Dp, false>
1022{
1023 typedef _Tp* type;
1024};
1025
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001026} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027
1028template <class _Tp, class _Dp>
1029struct __pointer_type
1030{
1031 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
1032};
1033
1034template <class _Tp>
1035struct __has_const_pointer
1036{
1037private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001038 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039 template <class _Up> static __two __test(...);
1040 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
1041public:
1042 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1043};
1044
1045template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1046struct __const_pointer
1047{
1048 typedef typename _Alloc::const_pointer type;
1049};
1050
1051template <class _Tp, class _Ptr, class _Alloc>
1052struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1053{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001054#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1056#else
1057 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1058#endif
1059};
1060
1061template <class _Tp>
1062struct __has_void_pointer
1063{
1064private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001065 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066 template <class _Up> static __two __test(...);
1067 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1068public:
1069 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1070};
1071
1072template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1073struct __void_pointer
1074{
1075 typedef typename _Alloc::void_pointer type;
1076};
1077
1078template <class _Ptr, class _Alloc>
1079struct __void_pointer<_Ptr, _Alloc, false>
1080{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001081#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1083#else
1084 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1085#endif
1086};
1087
1088template <class _Tp>
1089struct __has_const_void_pointer
1090{
1091private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001092 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093 template <class _Up> static __two __test(...);
1094 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1095public:
1096 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1097};
1098
1099template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1100struct __const_void_pointer
1101{
1102 typedef typename _Alloc::const_void_pointer type;
1103};
1104
1105template <class _Ptr, class _Alloc>
1106struct __const_void_pointer<_Ptr, _Alloc, false>
1107{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001108#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001109 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1110#else
1111 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1112#endif
1113};
1114
Howard Hinnantc834c512011-11-29 18:15:50 +00001115template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001117_Tp*
1118__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119{
1120 return __p;
1121}
1122
1123template <class _Pointer>
1124inline _LIBCPP_INLINE_VISIBILITY
1125typename pointer_traits<_Pointer>::element_type*
Howard Hinnant719bda32011-05-28 14:41:13 +00001126__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001128 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129}
1130
1131template <class _Tp>
1132struct __has_size_type
1133{
1134private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001135 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136 template <class _Up> static __two __test(...);
1137 template <class _Up> static char __test(typename _Up::size_type* = 0);
1138public:
1139 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1140};
1141
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001142template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143struct __size_type
1144{
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001145 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146};
1147
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001148template <class _Alloc, class _DiffType>
1149struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150{
1151 typedef typename _Alloc::size_type type;
1152};
1153
1154template <class _Tp>
1155struct __has_propagate_on_container_copy_assignment
1156{
1157private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001158 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 template <class _Up> static __two __test(...);
1160 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1161public:
1162 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1163};
1164
1165template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1166struct __propagate_on_container_copy_assignment
1167{
1168 typedef false_type type;
1169};
1170
1171template <class _Alloc>
1172struct __propagate_on_container_copy_assignment<_Alloc, true>
1173{
1174 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1175};
1176
1177template <class _Tp>
1178struct __has_propagate_on_container_move_assignment
1179{
1180private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001181 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182 template <class _Up> static __two __test(...);
1183 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1184public:
1185 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1186};
1187
1188template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1189struct __propagate_on_container_move_assignment
1190{
1191 typedef false_type type;
1192};
1193
1194template <class _Alloc>
1195struct __propagate_on_container_move_assignment<_Alloc, true>
1196{
1197 typedef typename _Alloc::propagate_on_container_move_assignment type;
1198};
1199
1200template <class _Tp>
1201struct __has_propagate_on_container_swap
1202{
1203private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001204 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205 template <class _Up> static __two __test(...);
1206 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1207public:
1208 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1209};
1210
1211template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1212struct __propagate_on_container_swap
1213{
1214 typedef false_type type;
1215};
1216
1217template <class _Alloc>
1218struct __propagate_on_container_swap<_Alloc, true>
1219{
1220 typedef typename _Alloc::propagate_on_container_swap type;
1221};
1222
Marshall Clow0b587562015-06-02 16:34:03 +00001223template <class _Tp>
1224struct __has_is_always_equal
1225{
1226private:
1227 struct __two {char __lx; char __lxx;};
1228 template <class _Up> static __two __test(...);
1229 template <class _Up> static char __test(typename _Up::is_always_equal* = 0);
1230public:
1231 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1232};
1233
1234template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1235struct __is_always_equal
1236{
1237 typedef typename _VSTD::is_empty<_Alloc>::type type;
1238};
1239
1240template <class _Alloc>
1241struct __is_always_equal<_Alloc, true>
1242{
1243 typedef typename _Alloc::is_always_equal type;
1244};
1245
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1247struct __has_rebind_other
1248{
1249private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001250 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251 template <class _Xp> static __two __test(...);
1252 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1253public:
1254 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1255};
1256
1257template <class _Tp, class _Up>
1258struct __has_rebind_other<_Tp, _Up, false>
1259{
1260 static const bool value = false;
1261};
1262
1263template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1264struct __allocator_traits_rebind
1265{
1266 typedef typename _Tp::template rebind<_Up>::other type;
1267};
1268
1269#ifndef _LIBCPP_HAS_NO_VARIADICS
1270
1271template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1272struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1273{
1274 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1275};
1276
1277template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1278struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1279{
1280 typedef _Alloc<_Up, _Args...> type;
1281};
1282
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001283#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284
1285template <template <class> class _Alloc, class _Tp, class _Up>
1286struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1287{
1288 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1289};
1290
1291template <template <class> class _Alloc, class _Tp, class _Up>
1292struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1293{
1294 typedef _Alloc<_Up> type;
1295};
1296
Howard Hinnantc51e1022010-05-11 19:42:16 +00001297template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1298struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1299{
1300 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1301};
1302
1303template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1304struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1305{
1306 typedef _Alloc<_Up, _A0> type;
1307};
1308
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1310 class _A1, class _Up>
1311struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1312{
1313 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1314};
1315
1316template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1317 class _A1, class _Up>
1318struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1319{
1320 typedef _Alloc<_Up, _A0, _A1> type;
1321};
1322
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1324 class _A1, class _A2, class _Up>
1325struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1326{
1327 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1328};
1329
1330template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1331 class _A1, class _A2, class _Up>
1332struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1333{
1334 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1335};
1336
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001337#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001339#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001340
1341template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1342auto
1343__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1344 -> decltype(__a.allocate(__sz, __p), true_type());
1345
1346template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1347auto
1348__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1349 -> false_type;
1350
1351template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1352struct __has_allocate_hint
1353 : integral_constant<bool,
1354 is_same<
1355 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1356 declval<_SizeType>(),
1357 declval<_ConstVoidPtr>())),
1358 true_type>::value>
1359{
1360};
1361
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001362#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001363
1364template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1365struct __has_allocate_hint
1366 : true_type
1367{
1368};
1369
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001370#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001371
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001372#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001373
1374template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001375decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1376 _VSTD::declval<_Args>()...),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377 true_type())
1378__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1379
1380template <class _Alloc, class _Pointer, class ..._Args>
1381false_type
1382__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1383
1384template <class _Alloc, class _Pointer, class ..._Args>
1385struct __has_construct
1386 : integral_constant<bool,
1387 is_same<
1388 decltype(__has_construct_test(declval<_Alloc>(),
1389 declval<_Pointer>(),
1390 declval<_Args>()...)),
1391 true_type>::value>
1392{
1393};
1394
1395template <class _Alloc, class _Pointer>
1396auto
1397__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1398 -> decltype(__a.destroy(__p), true_type());
1399
1400template <class _Alloc, class _Pointer>
1401auto
1402__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1403 -> false_type;
1404
1405template <class _Alloc, class _Pointer>
1406struct __has_destroy
1407 : integral_constant<bool,
1408 is_same<
1409 decltype(__has_destroy_test(declval<_Alloc>(),
1410 declval<_Pointer>())),
1411 true_type>::value>
1412{
1413};
1414
1415template <class _Alloc>
1416auto
1417__has_max_size_test(_Alloc&& __a)
1418 -> decltype(__a.max_size(), true_type());
1419
1420template <class _Alloc>
1421auto
1422__has_max_size_test(const volatile _Alloc& __a)
1423 -> false_type;
1424
1425template <class _Alloc>
1426struct __has_max_size
1427 : integral_constant<bool,
1428 is_same<
1429 decltype(__has_max_size_test(declval<_Alloc&>())),
1430 true_type>::value>
1431{
1432};
1433
1434template <class _Alloc>
1435auto
1436__has_select_on_container_copy_construction_test(_Alloc&& __a)
1437 -> decltype(__a.select_on_container_copy_construction(), true_type());
1438
1439template <class _Alloc>
1440auto
1441__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1442 -> false_type;
1443
1444template <class _Alloc>
1445struct __has_select_on_container_copy_construction
1446 : integral_constant<bool,
1447 is_same<
1448 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1449 true_type>::value>
1450{
1451};
1452
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001453#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454
1455#ifndef _LIBCPP_HAS_NO_VARIADICS
1456
1457template <class _Alloc, class _Pointer, class ..._Args>
1458struct __has_construct
1459 : false_type
1460{
1461};
1462
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001463#else // _LIBCPP_HAS_NO_VARIADICS
1464
1465template <class _Alloc, class _Pointer, class _Args>
1466struct __has_construct
1467 : false_type
1468{
1469};
1470
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001471#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001472
1473template <class _Alloc, class _Pointer>
1474struct __has_destroy
1475 : false_type
1476{
1477};
1478
1479template <class _Alloc>
1480struct __has_max_size
1481 : true_type
1482{
1483};
1484
1485template <class _Alloc>
1486struct __has_select_on_container_copy_construction
1487 : false_type
1488{
1489};
1490
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001491#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001493template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1494struct __alloc_traits_difference_type
1495{
1496 typedef typename pointer_traits<_Ptr>::difference_type type;
1497};
1498
1499template <class _Alloc, class _Ptr>
1500struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1501{
1502 typedef typename _Alloc::difference_type type;
1503};
1504
Howard Hinnantc51e1022010-05-11 19:42:16 +00001505template <class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001506struct _LIBCPP_TEMPLATE_VIS allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001507{
1508 typedef _Alloc allocator_type;
1509 typedef typename allocator_type::value_type value_type;
1510
1511 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1512 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1513 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1514 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1515
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001516 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1517 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001518
1519 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1520 propagate_on_container_copy_assignment;
1521 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1522 propagate_on_container_move_assignment;
1523 typedef typename __propagate_on_container_swap<allocator_type>::type
1524 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001525 typedef typename __is_always_equal<allocator_type>::type
1526 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001527
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001528#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001530 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001532#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001533 template <class _Tp> struct rebind_alloc
1534 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1535 template <class _Tp> struct rebind_traits
1536 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001537#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538
Howard Hinnant756c69b2010-09-22 16:48:34 +00001539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540 static pointer allocate(allocator_type& __a, size_type __n)
1541 {return __a.allocate(__n);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1544 {return allocate(__a, __n, __hint,
1545 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1546
Howard Hinnant756c69b2010-09-22 16:48:34 +00001547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001548 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549 {__a.deallocate(__p, __n);}
1550
1551#ifndef _LIBCPP_HAS_NO_VARIADICS
1552 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001555 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001556 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001557#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001559 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001560 static void construct(allocator_type&, _Tp* __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561 {
1562 ::new ((void*)__p) _Tp();
1563 }
1564 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001565 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001566 static void construct(allocator_type&, _Tp* __p, const _A0& __a0)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001567 {
1568 ::new ((void*)__p) _Tp(__a0);
1569 }
1570 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001571 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001572 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 const _A1& __a1)
1574 {
1575 ::new ((void*)__p) _Tp(__a0, __a1);
1576 }
1577 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001578 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2b2ec3e2017-01-14 01:33:53 +00001579 static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580 const _A1& __a1, const _A2& __a2)
1581 {
1582 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1583 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001584#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585
1586 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 static void destroy(allocator_type& __a, _Tp* __p)
1589 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1590
Howard Hinnant756c69b2010-09-22 16:48:34 +00001591 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001592 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1594
Howard Hinnant756c69b2010-09-22 16:48:34 +00001595 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596 static allocator_type
1597 select_on_container_copy_construction(const allocator_type& __a)
1598 {return select_on_container_copy_construction(
1599 __has_select_on_container_copy_construction<const allocator_type>(),
1600 __a);}
1601
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001602 template <class _Ptr>
1603 _LIBCPP_INLINE_VISIBILITY
1604 static
1605 void
1606 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1607 {
1608 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1609 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1610 }
1611
1612 template <class _Tp>
1613 _LIBCPP_INLINE_VISIBILITY
1614 static
1615 typename enable_if
1616 <
1617 (is_same<allocator_type, allocator<_Tp> >::value
1618 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1619 is_trivially_move_constructible<_Tp>::value,
1620 void
1621 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001622 __construct_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001623 {
1624 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001625 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001626 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001627 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001628 __begin2 += _Np;
1629 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001630 }
1631
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001632 template <class _Iter, class _Ptr>
1633 _LIBCPP_INLINE_VISIBILITY
1634 static
1635 void
1636 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1637 {
1638 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1639 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1640 }
1641
1642 template <class _Tp>
1643 _LIBCPP_INLINE_VISIBILITY
1644 static
1645 typename enable_if
1646 <
1647 (is_same<allocator_type, allocator<_Tp> >::value
1648 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1649 is_trivially_move_constructible<_Tp>::value,
1650 void
1651 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001652 __construct_range_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001653 {
1654 typedef typename remove_const<_Tp>::type _Vp;
1655 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001656 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001657 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001658 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001659 __begin2 += _Np;
1660 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001661 }
1662
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001663 template <class _Ptr>
1664 _LIBCPP_INLINE_VISIBILITY
1665 static
1666 void
1667 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1668 {
1669 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001670 {
1671 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1672 --__end2;
1673 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001674 }
1675
1676 template <class _Tp>
1677 _LIBCPP_INLINE_VISIBILITY
1678 static
1679 typename enable_if
1680 <
1681 (is_same<allocator_type, allocator<_Tp> >::value
1682 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1683 is_trivially_move_constructible<_Tp>::value,
1684 void
1685 >::type
Eric Fiselier6003c772016-12-23 23:37:52 +00001686 __construct_backward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001687 {
1688 ptrdiff_t _Np = __end1 - __begin1;
1689 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001690 if (_Np > 0)
1691 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001692 }
1693
Howard Hinnantc51e1022010-05-11 19:42:16 +00001694private:
1695
Howard Hinnant756c69b2010-09-22 16:48:34 +00001696 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001697 static pointer allocate(allocator_type& __a, size_type __n,
1698 const_void_pointer __hint, true_type)
1699 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001700 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001701 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001702 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703 {return __a.allocate(__n);}
1704
1705#ifndef _LIBCPP_HAS_NO_VARIADICS
1706 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001709 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001711 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001712 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1713 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001714 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001716#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001717
1718 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1721 {__a.destroy(__p);}
1722 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001723 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001724 static void __destroy(false_type, allocator_type&, _Tp* __p)
1725 {
1726 __p->~_Tp();
1727 }
1728
Howard Hinnant756c69b2010-09-22 16:48:34 +00001729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001730 static size_type __max_size(true_type, const allocator_type& __a)
1731 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001732 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001733 static size_type __max_size(false_type, const allocator_type&)
Marshall Clow33daa162015-10-25 19:34:04 +00001734 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001735
Howard Hinnant756c69b2010-09-22 16:48:34 +00001736 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001737 static allocator_type
1738 select_on_container_copy_construction(true_type, const allocator_type& __a)
1739 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001740 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741 static allocator_type
1742 select_on_container_copy_construction(false_type, const allocator_type& __a)
1743 {return __a;}
1744};
1745
Marshall Clow940e01c2015-04-07 05:21:38 +00001746template <class _Traits, class _Tp>
1747struct __rebind_alloc_helper
1748{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001749#ifndef _LIBCPP_CXX03_LANG
Marshall Clow35cde742015-05-10 13:59:45 +00001750 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001751#else
1752 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1753#endif
1754};
1755
Howard Hinnantc51e1022010-05-11 19:42:16 +00001756// allocator
1757
1758template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001759class _LIBCPP_TEMPLATE_VIS allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001760{
1761public:
1762 typedef size_t size_type;
1763 typedef ptrdiff_t difference_type;
1764 typedef _Tp* pointer;
1765 typedef const _Tp* const_pointer;
1766 typedef _Tp& reference;
1767 typedef const _Tp& const_reference;
1768 typedef _Tp value_type;
1769
Howard Hinnant4931e092011-06-02 21:38:57 +00001770 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001771 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001772
Howard Hinnantc51e1022010-05-11 19:42:16 +00001773 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1774
Howard Hinnant719bda32011-05-28 14:41:13 +00001775 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1776 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1777 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001778 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001779 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001780 {return _VSTD::addressof(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001782 {
1783 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001784 __throw_length_error("allocator<T>::allocate(size_t n)"
1785 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001786 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
1787 }
Howard Hinnant719bda32011-05-28 14:41:13 +00001788 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Eric Fiselier8122fda2017-01-07 03:01:24 +00001789 {_VSTD::__libcpp_deallocate((void*)__p);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001790 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1791 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001792#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001793 template <class _Up, class... _Args>
1794 _LIBCPP_INLINE_VISIBILITY
1795 void
1796 construct(_Up* __p, _Args&&... __args)
1797 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001798 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001799 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001800#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801 _LIBCPP_INLINE_VISIBILITY
1802 void
1803 construct(pointer __p)
1804 {
1805 ::new((void*)__p) _Tp();
1806 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001807# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001808
Howard Hinnantc51e1022010-05-11 19:42:16 +00001809 template <class _A0>
1810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001811 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812 construct(pointer __p, _A0& __a0)
1813 {
1814 ::new((void*)__p) _Tp(__a0);
1815 }
1816 template <class _A0>
1817 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001818 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001819 construct(pointer __p, const _A0& __a0)
1820 {
1821 ::new((void*)__p) _Tp(__a0);
1822 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001823# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001824 template <class _A0, class _A1>
1825 _LIBCPP_INLINE_VISIBILITY
1826 void
1827 construct(pointer __p, _A0& __a0, _A1& __a1)
1828 {
1829 ::new((void*)__p) _Tp(__a0, __a1);
1830 }
1831 template <class _A0, class _A1>
1832 _LIBCPP_INLINE_VISIBILITY
1833 void
1834 construct(pointer __p, const _A0& __a0, _A1& __a1)
1835 {
1836 ::new((void*)__p) _Tp(__a0, __a1);
1837 }
1838 template <class _A0, class _A1>
1839 _LIBCPP_INLINE_VISIBILITY
1840 void
1841 construct(pointer __p, _A0& __a0, const _A1& __a1)
1842 {
1843 ::new((void*)__p) _Tp(__a0, __a1);
1844 }
1845 template <class _A0, class _A1>
1846 _LIBCPP_INLINE_VISIBILITY
1847 void
1848 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1849 {
1850 ::new((void*)__p) _Tp(__a0, __a1);
1851 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001852#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001853 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1854};
1855
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001856template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001857class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001858{
1859public:
1860 typedef size_t size_type;
1861 typedef ptrdiff_t difference_type;
1862 typedef const _Tp* pointer;
1863 typedef const _Tp* const_pointer;
1864 typedef const _Tp& reference;
1865 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001866 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001867
1868 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001869 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001870
1871 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1872
1873 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1874 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1875 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1876 {return _VSTD::addressof(__x);}
1877 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001878 {
1879 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001880 __throw_length_error("allocator<const T>::allocate(size_t n)"
1881 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001882 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001883 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001884 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Eric Fiselier8122fda2017-01-07 03:01:24 +00001885 {_VSTD::__libcpp_deallocate((void*)__p);}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001886 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1887 {return size_type(~0) / sizeof(_Tp);}
1888#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1889 template <class _Up, class... _Args>
1890 _LIBCPP_INLINE_VISIBILITY
1891 void
1892 construct(_Up* __p, _Args&&... __args)
1893 {
1894 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1895 }
1896#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1897 _LIBCPP_INLINE_VISIBILITY
1898 void
1899 construct(pointer __p)
1900 {
1901 ::new((void*)__p) _Tp();
1902 }
1903# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001904
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001905 template <class _A0>
1906 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001907 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001908 construct(pointer __p, _A0& __a0)
1909 {
1910 ::new((void*)__p) _Tp(__a0);
1911 }
1912 template <class _A0>
1913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001914 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001915 construct(pointer __p, const _A0& __a0)
1916 {
1917 ::new((void*)__p) _Tp(__a0);
1918 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001919# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1920 template <class _A0, class _A1>
1921 _LIBCPP_INLINE_VISIBILITY
1922 void
1923 construct(pointer __p, _A0& __a0, _A1& __a1)
1924 {
1925 ::new((void*)__p) _Tp(__a0, __a1);
1926 }
1927 template <class _A0, class _A1>
1928 _LIBCPP_INLINE_VISIBILITY
1929 void
1930 construct(pointer __p, const _A0& __a0, _A1& __a1)
1931 {
1932 ::new((void*)__p) _Tp(__a0, __a1);
1933 }
1934 template <class _A0, class _A1>
1935 _LIBCPP_INLINE_VISIBILITY
1936 void
1937 construct(pointer __p, _A0& __a0, const _A1& __a1)
1938 {
1939 ::new((void*)__p) _Tp(__a0, __a1);
1940 }
1941 template <class _A0, class _A1>
1942 _LIBCPP_INLINE_VISIBILITY
1943 void
1944 construct(pointer __p, const _A0& __a0, const _A1& __a1)
1945 {
1946 ::new((void*)__p) _Tp(__a0, __a1);
1947 }
1948#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1949 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1950};
1951
Howard Hinnantc51e1022010-05-11 19:42:16 +00001952template <class _Tp, class _Up>
1953inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001954bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955
1956template <class _Tp, class _Up>
1957inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001958bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001959
1960template <class _OutputIterator, class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001961class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001962 : public iterator<output_iterator_tag,
1963 _Tp, // purposefully not C++03
1964 ptrdiff_t, // purposefully not C++03
1965 _Tp*, // purposefully not C++03
1966 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1967{
1968private:
1969 _OutputIterator __x_;
1970public:
1971 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1972 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1973 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1974 {::new(&*__x_) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001975#if _LIBCPP_STD_VER >= 14
1976 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1977 {::new(&*__x_) _Tp(_VSTD::move(__element)); return *this;}
1978#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001979 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1980 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1981 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001982#if _LIBCPP_STD_VER >= 14
1983 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1984#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001985};
1986
1987template <class _Tp>
1988pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001989get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001990{
1991 pair<_Tp*, ptrdiff_t> __r(0, 0);
1992 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1993 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1994 / sizeof(_Tp);
1995 if (__n > __m)
1996 __n = __m;
1997 while (__n > 0)
1998 {
1999 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
2000 if (__r.first)
2001 {
2002 __r.second = __n;
2003 break;
2004 }
2005 __n /= 2;
2006 }
2007 return __r;
2008}
2009
2010template <class _Tp>
2011inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00002012void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002013
Marshall Clowb22274f2017-01-24 22:22:33 +00002014#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015template <class _Tp>
2016struct auto_ptr_ref
2017{
2018 _Tp* __ptr_;
2019};
2020
2021template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002022class _LIBCPP_TEMPLATE_VIS auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023{
2024private:
2025 _Tp* __ptr_;
2026public:
2027 typedef _Tp element_type;
2028
2029 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2030 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2031 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2032 : __ptr_(__p.release()) {}
2033 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2034 {reset(__p.release()); return *this;}
2035 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2036 {reset(__p.release()); return *this;}
2037 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2038 {reset(__p.__ptr_); return *this;}
2039 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2040
2041 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2042 {return *__ptr_;}
2043 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2044 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2045 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2046 {
2047 _Tp* __t = __ptr_;
2048 __ptr_ = 0;
2049 return __t;
2050 }
2051 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2052 {
2053 if (__ptr_ != __p)
2054 delete __ptr_;
2055 __ptr_ = __p;
2056 }
2057
2058 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2059 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2060 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2061 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2062 {return auto_ptr<_Up>(release());}
2063};
2064
2065template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002066class _LIBCPP_TEMPLATE_VIS auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067{
2068public:
2069 typedef void element_type;
2070};
Marshall Clowb22274f2017-01-24 22:22:33 +00002071#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072
Eric Fiselier9d355982017-04-12 23:45:53 +00002073template <class _Tp, int _Idx,
2074 bool _CanBeEmptyBase =
2075 is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2076struct __compressed_pair_elem {
2077 typedef _Tp _ParamT;
2078 typedef _Tp& reference;
2079 typedef const _Tp& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002080
Eric Fiselier9d355982017-04-12 23:45:53 +00002081#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4f855492017-04-13 00:50:45 +00002082 template <bool _Dummy = true, class = typename enable_if<
2083 __dependent_type<is_default_constructible<_Tp>, _Dummy>::value
2084 >::type
2085 >
2086 _LIBCPP_CONSTEXPR __compressed_pair_elem()
2087 _NOEXCEPT_(is_nothrow_default_constructible<_Tp>::value)
2088 : __value_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002089
Eric Fiselier9d355982017-04-12 23:45:53 +00002090 template <class _Up, class = typename enable_if<
2091 !is_same<__compressed_pair_elem, _Up>::value>::type>
2092 _LIBCPP_CONSTEXPR explicit
2093 __compressed_pair_elem(_Up&& __u)
2094 : __value_(_VSTD::forward<_Up>(__u)){};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002095
Eric Fiselier9d355982017-04-12 23:45:53 +00002096 template <class... _Args, size_t... _Indexes>
2097 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2098 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2099 __tuple_indices<_Indexes...>)
2100 : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2101#else
2102 __compressed_pair_elem() : __value_() {}
2103 __compressed_pair_elem(_ParamT __p) : __value_(std::forward<_ParamT>(__p)) {}
2104#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105
Eric Fiselier9d355982017-04-12 23:45:53 +00002106 reference __get() _NOEXCEPT { return __value_; }
2107 const_reference __get() const _NOEXCEPT { return __value_; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002108
Howard Hinnantc51e1022010-05-11 19:42:16 +00002109private:
Eric Fiselier9d355982017-04-12 23:45:53 +00002110 _Tp __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002111};
2112
Eric Fiselier9d355982017-04-12 23:45:53 +00002113template <class _Tp, int _Idx>
2114struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2115 typedef _Tp _ParamT;
2116 typedef _Tp& reference;
2117 typedef const _Tp& const_reference;
2118 typedef _Tp __value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002119
Eric Fiselier9d355982017-04-12 23:45:53 +00002120#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier4f855492017-04-13 00:50:45 +00002121 _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002122
Eric Fiselier9d355982017-04-12 23:45:53 +00002123 template <class _Up, class = typename enable_if<
2124 !is_same<__compressed_pair_elem, _Up>::value>::type>
2125 _LIBCPP_CONSTEXPR explicit
2126 __compressed_pair_elem(_Up&& __u)
2127 : __value_type(_VSTD::forward<_Up>(__u)){};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002128
Eric Fiselier9d355982017-04-12 23:45:53 +00002129 template <class... _Args, size_t... _Indexes>
2130 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2131 __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2132 __tuple_indices<_Indexes...>)
2133 : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2134#else
2135 __compressed_pair_elem() : __value_type() {}
2136 __compressed_pair_elem(_ParamT __p)
2137 : __value_type(std::forward<_ParamT>(__p)) {}
2138#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002139
Eric Fiselier9d355982017-04-12 23:45:53 +00002140 reference __get() _NOEXCEPT { return *this; }
2141 const_reference __get() const _NOEXCEPT { return *this; }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002142};
2143
Eric Fiselier9d355982017-04-12 23:45:53 +00002144// Tag used to construct the second element of the compressed pair.
2145struct __second_tag {};
Howard Hinnantc51e1022010-05-11 19:42:16 +00002146
2147template <class _T1, class _T2>
Eric Fiselier9d355982017-04-12 23:45:53 +00002148class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2149 private __compressed_pair_elem<_T2, 1> {
2150 typedef __compressed_pair_elem<_T1, 0> _Base1;
2151 typedef __compressed_pair_elem<_T2, 1> _Base2;
2152
2153 // NOTE: This static assert should never fire because __compressed_pair
2154 // is *almost never* used in a scenario where it's possible for T1 == T2.
2155 // (The exception is std::function where it is possible that the function
2156 // object and the allocator have the same type).
Eric Fiselierc5adf472017-04-13 01:13:58 +00002157 static_assert((!is_same<_T1, _T2>::value),
Eric Fiselier9d355982017-04-12 23:45:53 +00002158 "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2159 "The current implementation is NOT ABI-compatible with the previous "
2160 "implementation for this configuration");
2161
Howard Hinnantc51e1022010-05-11 19:42:16 +00002162public:
Eric Fiselier9d355982017-04-12 23:45:53 +00002163#ifndef _LIBCPP_CXX03_LANG
2164 _LIBCPP_INLINE_VISIBILITY
2165 __compressed_pair() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002166
Eric Fiselier9d355982017-04-12 23:45:53 +00002167 template <class _Tp, typename enable_if<!is_same<typename decay<_Tp>::type,
2168 __compressed_pair>::value,
2169 bool>::type = true>
2170 _LIBCPP_INLINE_VISIBILITY constexpr explicit
2171 __compressed_pair(_Tp&& __t)
2172 : _Base1(std::forward<_Tp>(__t)), _Base2() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002173
Eric Fiselier9d355982017-04-12 23:45:53 +00002174 template <class _Tp>
2175 _LIBCPP_INLINE_VISIBILITY constexpr
2176 __compressed_pair(__second_tag, _Tp&& __t)
2177 : _Base1(), _Base2(std::forward<_Tp>(__t)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002178
Eric Fiselier9d355982017-04-12 23:45:53 +00002179 template <class _U1, class _U2>
2180 _LIBCPP_INLINE_VISIBILITY constexpr
2181 __compressed_pair(_U1&& __t1, _U2&& __t2)
2182 : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183
Eric Fiselier9d355982017-04-12 23:45:53 +00002184 template <class... _Args1, class... _Args2>
2185 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2186 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2187 tuple<_Args2...> __second_args)
2188 : _Base1(__pc, _VSTD::move(__first_args),
2189 typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2190 _Base2(__pc, _VSTD::move(__second_args),
2191 typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002192
Eric Fiselier9d355982017-04-12 23:45:53 +00002193#else
2194 _LIBCPP_INLINE_VISIBILITY
2195 __compressed_pair() {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002196
Eric Fiselier9d355982017-04-12 23:45:53 +00002197 _LIBCPP_INLINE_VISIBILITY explicit
2198 __compressed_pair(_T1 __t1) : _Base1(_VSTD::forward<_T1>(__t1)) {}
Howard Hinnant83b1c052011-12-19 17:58:44 +00002199
Eric Fiselier9d355982017-04-12 23:45:53 +00002200 _LIBCPP_INLINE_VISIBILITY
2201 __compressed_pair(__second_tag, _T2 __t2)
2202 : _Base1(), _Base2(_VSTD::forward<_T2>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002203
Eric Fiselier9d355982017-04-12 23:45:53 +00002204 _LIBCPP_INLINE_VISIBILITY
2205 __compressed_pair(_T1 __t1, _T2 __t2)
2206 : _Base1(_VSTD::forward<_T1>(__t1)), _Base2(_VSTD::forward<_T2>(__t2)) {}
2207#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208
Eric Fiselier9d355982017-04-12 23:45:53 +00002209 _LIBCPP_INLINE_VISIBILITY
2210 typename _Base1::reference first() _NOEXCEPT {
2211 return static_cast<_Base1&>(*this).__get();
2212 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002213
Eric Fiselier9d355982017-04-12 23:45:53 +00002214 _LIBCPP_INLINE_VISIBILITY
2215 typename _Base1::const_reference first() const _NOEXCEPT {
2216 return static_cast<_Base1 const&>(*this).__get();
2217 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002218
Eric Fiselier9d355982017-04-12 23:45:53 +00002219 _LIBCPP_INLINE_VISIBILITY
2220 typename _Base2::reference second() _NOEXCEPT {
2221 return static_cast<_Base2&>(*this).__get();
2222 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002223
Eric Fiselier9d355982017-04-12 23:45:53 +00002224 _LIBCPP_INLINE_VISIBILITY
2225 typename _Base2::const_reference second() const _NOEXCEPT {
2226 return static_cast<_Base2 const&>(*this).__get();
2227 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228
Eric Fiselier9d355982017-04-12 23:45:53 +00002229 _LIBCPP_INLINE_VISIBILITY
2230 void swap(__compressed_pair& __x)
2231 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2232 __is_nothrow_swappable<_T2>::value)
2233 {
2234 using std::swap;
2235 swap(first(), __x.first());
2236 swap(second(), __x.second());
2237 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002238};
2239
2240template <class _T1, class _T2>
2241inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9d355982017-04-12 23:45:53 +00002242void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
2243 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2244 __is_nothrow_swappable<_T2>::value) {
2245 __x.swap(__y);
2246}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002247
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002248// __same_or_less_cv_qualified
2249
2250template <class _Ptr1, class _Ptr2,
2251 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2252 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2253 >::value
2254 >
2255struct __same_or_less_cv_qualified_imp
2256 : is_convertible<_Ptr1, _Ptr2> {};
2257
2258template <class _Ptr1, class _Ptr2>
2259struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2260 : false_type {};
2261
Marshall Clowdf296162014-04-26 05:19:48 +00002262template <class _Ptr1, class _Ptr2, bool = is_pointer<_Ptr1>::value ||
2263 is_same<_Ptr1, _Ptr2>::value ||
2264 __has_element_type<_Ptr1>::value>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002265struct __same_or_less_cv_qualified
2266 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2267
2268template <class _Ptr1, class _Ptr2>
Marshall Clowdf296162014-04-26 05:19:48 +00002269struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, false>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002270 : false_type {};
2271
2272// default_delete
2273
Howard Hinnantc51e1022010-05-11 19:42:16 +00002274template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002275struct _LIBCPP_TEMPLATE_VIS default_delete
Howard Hinnantc51e1022010-05-11 19:42:16 +00002276{
Eric Fiselier9d355982017-04-12 23:45:53 +00002277#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002278 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2279#else
2280 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2281#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002282 template <class _Up>
2283 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant719bda32011-05-28 14:41:13 +00002284 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2285 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002286 {
2287 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant14015fd2013-04-24 19:44:26 +00002288 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002289 delete __ptr;
2290 }
2291};
2292
2293template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00002294struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002295{
Howard Hinnant4500ca52011-12-18 21:19:44 +00002296public:
Eric Fiselier9d355982017-04-12 23:45:53 +00002297#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002298 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2299#else
2300 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2301#endif
Howard Hinnant4500ca52011-12-18 21:19:44 +00002302 template <class _Up>
2303 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002304 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant4500ca52011-12-18 21:19:44 +00002305 template <class _Up>
2306 _LIBCPP_INLINE_VISIBILITY
2307 void operator() (_Up* __ptr,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002308 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002309 {
2310 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Marshall Clowff0e4132016-02-25 16:50:51 +00002311 static_assert(!is_void<_Tp>::value, "default_delete can not delete void type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002312 delete [] __ptr;
2313 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002314};
2315
Howard Hinnantc51e1022010-05-11 19:42:16 +00002316
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002317#ifndef _LIBCPP_CXX03_LANG
2318template <class _Deleter>
2319struct __unique_ptr_deleter_sfinae {
2320 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
2321 typedef const _Deleter& __lval_ref_type;
2322 typedef _Deleter&& __good_rval_ref_type;
2323 typedef true_type __enable_rval_overload;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002324};
2325
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002326template <class _Deleter>
2327struct __unique_ptr_deleter_sfinae<_Deleter const&> {
2328 typedef const _Deleter& __lval_ref_type;
2329 typedef const _Deleter&& __bad_rval_ref_type;
2330 typedef false_type __enable_rval_overload;
2331};
2332
2333template <class _Deleter>
2334struct __unique_ptr_deleter_sfinae<_Deleter&> {
2335 typedef _Deleter& __lval_ref_type;
2336 typedef _Deleter&& __bad_rval_ref_type;
2337 typedef false_type __enable_rval_overload;
2338};
2339#endif // !defined(_LIBCPP_CXX03_LANG)
2340
2341template <class _Tp, class _Dp = default_delete<_Tp> >
2342class _LIBCPP_TEMPLATE_VIS unique_ptr {
2343public:
2344 typedef _Tp element_type;
2345 typedef _Dp deleter_type;
2346 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2347
2348 static_assert(!is_rvalue_reference<deleter_type>::value,
2349 "the specified deleter type cannot be an rvalue reference");
2350
2351private:
2352 __compressed_pair<pointer, deleter_type> __ptr_;
2353
2354 struct __nat { int __for_bool_; };
2355
2356#ifndef _LIBCPP_CXX03_LANG
2357 typedef __unique_ptr_deleter_sfinae<_Dp> _SFINAE;
2358
2359 template <bool _Dummy>
2360 using _LValRefType =
2361 typename __dependent_type<_SFINAE, _Dummy>::__lval_ref_type;
2362
2363 template <bool _Dummy>
2364 using _GoodRValRefType =
2365 typename __dependent_type<_SFINAE, _Dummy>::__good_rval_ref_type;
2366
2367 template <bool _Dummy>
2368 using _BadRValRefType =
2369 typename __dependent_type<_SFINAE, _Dummy>::__bad_rval_ref_type;
2370
2371 template <bool _Dummy, class _Deleter = typename __dependent_type<
2372 __identity<deleter_type>, _Dummy>::type>
2373 using _EnableIfDeleterDefaultConstructible =
2374 typename enable_if<is_default_constructible<_Deleter>::value &&
2375 !is_pointer<_Deleter>::value>::type;
2376
2377 template <class _ArgType>
2378 using _EnableIfDeleterConstructible =
2379 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2380
2381 template <class _UPtr, class _Up>
2382 using _EnableIfMoveConvertible = typename enable_if<
2383 is_convertible<typename _UPtr::pointer, pointer>::value &&
2384 !is_array<_Up>::value
2385 >::type;
2386
2387 template <class _UDel>
2388 using _EnableIfDeleterConvertible = typename enable_if<
2389 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2390 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2391 >::type;
2392
2393 template <class _UDel>
2394 using _EnableIfDeleterAssignable = typename enable_if<
2395 is_assignable<_Dp&, _UDel&&>::value
2396 >::type;
2397
2398public:
2399 template <bool _Dummy = true,
2400 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2401 _LIBCPP_INLINE_VISIBILITY
2402 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
2403
2404 template <bool _Dummy = true,
2405 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2406 _LIBCPP_INLINE_VISIBILITY
2407 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
2408
2409 template <bool _Dummy = true,
2410 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2411 _LIBCPP_INLINE_VISIBILITY
2412 explicit unique_ptr(pointer __p) noexcept : __ptr_(__p) {}
2413
2414 template <bool _Dummy = true,
2415 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2416 _LIBCPP_INLINE_VISIBILITY
2417 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) noexcept
2418 : __ptr_(__p, __d) {}
2419
2420 template <bool _Dummy = true,
2421 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2422 _LIBCPP_INLINE_VISIBILITY
2423 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) noexcept
2424 : __ptr_(__p, _VSTD::move(__d)) {
2425 static_assert(!is_reference<deleter_type>::value,
2426 "rvalue deleter bound to reference");
2427 }
2428
2429 template <bool _Dummy = true,
2430 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>>
2431 _LIBCPP_INLINE_VISIBILITY
2432 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
2433
2434 _LIBCPP_INLINE_VISIBILITY
2435 unique_ptr(unique_ptr&& __u) noexcept
2436 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2437 }
2438
2439 template <class _Up, class _Ep,
2440 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2441 class = _EnableIfDeleterConvertible<_Ep>
2442 >
2443 _LIBCPP_INLINE_VISIBILITY
2444 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2445 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
2446
2447#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2448 template <class _Up>
2449 _LIBCPP_INLINE_VISIBILITY
2450 unique_ptr(auto_ptr<_Up>&& __p,
2451 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2452 is_same<_Dp, default_delete<_Tp>>::value,
2453 __nat>::type = __nat()) _NOEXCEPT
2454 : __ptr_(__p.release()) {}
2455#endif
2456
2457 _LIBCPP_INLINE_VISIBILITY
2458 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
2459 reset(__u.release());
2460 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2461 return *this;
2462 }
2463
2464 template <class _Up, class _Ep,
2465 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2466 class = _EnableIfDeleterAssignable<_Ep>
2467 >
2468 _LIBCPP_INLINE_VISIBILITY
2469 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2470 reset(__u.release());
2471 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2472 return *this;
2473 }
2474
2475#else // _LIBCPP_CXX03_LANG
2476private:
2477 unique_ptr(unique_ptr&);
2478 template <class _Up, class _Ep> unique_ptr(unique_ptr<_Up, _Ep>&);
2479
2480 unique_ptr& operator=(unique_ptr&);
2481 template <class _Up, class _Ep> unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
2482
2483public:
2484 _LIBCPP_INLINE_VISIBILITY
2485 unique_ptr() : __ptr_(pointer())
2486 {
2487 static_assert(!is_pointer<deleter_type>::value,
2488 "unique_ptr constructed with null function pointer deleter");
2489 static_assert(is_default_constructible<deleter_type>::value,
2490 "unique_ptr::deleter_type is not default constructible");
2491 }
2492 _LIBCPP_INLINE_VISIBILITY
2493 unique_ptr(nullptr_t) : __ptr_(pointer())
2494 {
2495 static_assert(!is_pointer<deleter_type>::value,
2496 "unique_ptr constructed with null function pointer deleter");
2497 }
2498 _LIBCPP_INLINE_VISIBILITY
2499 explicit unique_ptr(pointer __p)
2500 : __ptr_(_VSTD::move(__p)) {
2501 static_assert(!is_pointer<deleter_type>::value,
2502 "unique_ptr constructed with null function pointer deleter");
2503 }
2504
2505 _LIBCPP_INLINE_VISIBILITY
2506 operator __rv<unique_ptr>() {
2507 return __rv<unique_ptr>(*this);
2508 }
2509
2510 _LIBCPP_INLINE_VISIBILITY
2511 unique_ptr(__rv<unique_ptr> __u)
2512 : __ptr_(__u->release(),
2513 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2514
2515 template <class _Up, class _Ep>
2516 _LIBCPP_INLINE_VISIBILITY
2517 typename enable_if<
2518 !is_array<_Up>::value &&
2519 is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
2520 pointer>::value &&
2521 is_assignable<deleter_type&, _Ep&>::value,
2522 unique_ptr&>::type
2523 operator=(unique_ptr<_Up, _Ep> __u) {
2524 reset(__u.release());
2525 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2526 return *this;
2527 }
2528
2529 _LIBCPP_INLINE_VISIBILITY
2530 unique_ptr(pointer __p, deleter_type __d)
2531 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
2532#endif // _LIBCPP_CXX03_LANG
2533
2534#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
2535 template <class _Up>
2536 _LIBCPP_INLINE_VISIBILITY
2537 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
2538 is_same<_Dp, default_delete<_Tp> >::value,
2539 unique_ptr&>::type
2540 operator=(auto_ptr<_Up> __p) {
2541 reset(__p.release());
2542 return *this;
2543 }
2544#endif
2545
2546 _LIBCPP_INLINE_VISIBILITY
2547 ~unique_ptr() { reset(); }
2548
2549 _LIBCPP_INLINE_VISIBILITY
2550 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2551 reset();
2552 return *this;
2553 }
2554
2555 _LIBCPP_INLINE_VISIBILITY
2556 typename add_lvalue_reference<_Tp>::type
2557 operator*() const {
2558 return *__ptr_.first();
2559 }
2560 _LIBCPP_INLINE_VISIBILITY
2561 pointer operator->() const _NOEXCEPT {
2562 return __ptr_.first();
2563 }
2564 _LIBCPP_INLINE_VISIBILITY
2565 pointer get() const _NOEXCEPT {
2566 return __ptr_.first();
2567 }
2568 _LIBCPP_INLINE_VISIBILITY
2569 deleter_type& get_deleter() _NOEXCEPT {
2570 return __ptr_.second();
2571 }
2572 _LIBCPP_INLINE_VISIBILITY
2573 const deleter_type& get_deleter() const _NOEXCEPT {
2574 return __ptr_.second();
2575 }
2576 _LIBCPP_INLINE_VISIBILITY
2577 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2578 return __ptr_.first() != nullptr;
2579 }
2580
2581 _LIBCPP_INLINE_VISIBILITY
2582 pointer release() _NOEXCEPT {
2583 pointer __t = __ptr_.first();
2584 __ptr_.first() = pointer();
2585 return __t;
2586 }
2587
2588 _LIBCPP_INLINE_VISIBILITY
2589 void reset(pointer __p = pointer()) _NOEXCEPT {
2590 pointer __tmp = __ptr_.first();
2591 __ptr_.first() = __p;
2592 if (__tmp)
2593 __ptr_.second()(__tmp);
2594 }
2595
2596 _LIBCPP_INLINE_VISIBILITY
2597 void swap(unique_ptr& __u) _NOEXCEPT {
2598 __ptr_.swap(__u.__ptr_);
2599 }
2600};
2601
2602template <class _From, class _ToUnique>
2603struct __check_array_pointer_conversion : is_same<_From, typename _ToUnique::pointer> {};
2604
2605template <class _FromElem, class _ToUnique>
2606struct __check_array_pointer_conversion<_FromElem*, _ToUnique>
2607 : integral_constant<bool,
2608 is_same<_FromElem*, typename _ToUnique::pointer>::value ||
2609 (is_same<typename _ToUnique::pointer, typename _ToUnique::element_type*>::value &&
2610 is_convertible<_FromElem(*)[], typename _ToUnique::element_type(*)[]>::value)
2611 >
2612{};
2613
Howard Hinnantc51e1022010-05-11 19:42:16 +00002614template <class _Tp, class _Dp>
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002615class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002616public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002617 typedef _Tp element_type;
2618 typedef _Dp deleter_type;
2619 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2620
Howard Hinnantc51e1022010-05-11 19:42:16 +00002621private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002622 __compressed_pair<pointer, deleter_type> __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002623
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002624 struct __nat { int __for_bool_; };
Howard Hinnantc51e1022010-05-11 19:42:16 +00002625
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002626 typedef deleter_type& _Dp_reference;
2627 typedef typename remove_reference<typename add_const<deleter_type>::type>::type&
2628 _Dp_const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002629
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002630#ifndef _LIBCPP_CXX03_LANG
2631 typedef __unique_ptr_deleter_sfinae<_Dp> _SFINAE;
2632
2633 template <bool _Dummy>
2634 using _LValRefType =
2635 typename __dependent_type<_SFINAE, _Dummy>::__lval_ref_type;
2636
2637 template <bool _Dummy>
2638 using _GoodRValRefType =
2639 typename __dependent_type<_SFINAE, _Dummy>::__good_rval_ref_type;
2640
2641 template <bool _Dummy>
2642 using _BadRValRefType =
2643 typename __dependent_type<_SFINAE, _Dummy>::__bad_rval_ref_type;
2644
2645 template <bool _Dummy, class _Deleter = typename __dependent_type<
2646 __identity<deleter_type>, _Dummy>::type>
2647 using _EnableIfDeleterDefaultConstructible =
2648 typename enable_if<is_default_constructible<_Deleter>::value &&
2649 !is_pointer<_Deleter>::value>::type;
2650
2651 template <class _ArgType>
2652 using _EnableIfDeleterConstructible =
2653 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
2654
2655 template <class _Pp>
2656 using _EnableIfPointerConvertible = typename enable_if<
2657 __check_array_pointer_conversion<_Pp, unique_ptr>::value
2658 >::type;
2659
2660 template <class _UPtr, class _Up,
2661 class _ElemT = typename _UPtr::element_type>
2662 using _EnableIfMoveConvertible = typename enable_if<
2663 is_array<_Up>::value &&
2664 is_same<pointer, element_type*>::value &&
2665 is_same<typename _UPtr::pointer, _ElemT*>::value &&
2666 is_convertible<_ElemT(*)[], element_type(*)[]>::value
2667 >::type;
2668
2669 template <class _UDel>
2670 using _EnableIfDeleterConvertible = typename enable_if<
2671 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
2672 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
2673 >::type;
2674
2675 template <class _UDel>
2676 using _EnableIfDeleterAssignable = typename enable_if<
2677 is_assignable<_Dp&, _UDel&&>::value
2678 >::type;
2679
Howard Hinnantc51e1022010-05-11 19:42:16 +00002680public:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002681 template <bool _Dummy = true,
2682 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2683 _LIBCPP_INLINE_VISIBILITY
2684 constexpr unique_ptr() noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002685
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002686 template <bool _Dummy = true,
2687 class = _EnableIfDeleterDefaultConstructible<_Dummy>>
2688 _LIBCPP_INLINE_VISIBILITY
2689 constexpr unique_ptr(nullptr_t) noexcept : __ptr_(pointer()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002690
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002691 template <class _Pp, bool _Dummy = true,
2692 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
2693 class = _EnableIfPointerConvertible<_Pp>>
2694 _LIBCPP_INLINE_VISIBILITY
2695 explicit unique_ptr(_Pp __p) noexcept
2696 : __ptr_(__p) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002697
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002698 template <class _Pp, bool _Dummy = true,
2699 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>,
2700 class = _EnableIfPointerConvertible<_Pp>>
2701 _LIBCPP_INLINE_VISIBILITY
2702 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) noexcept
2703 : __ptr_(__p, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002705 template <bool _Dummy = true,
2706 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy>>>
2707 _LIBCPP_INLINE_VISIBILITY
2708 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) noexcept
2709 : __ptr_(nullptr, __d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002710
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002711 template <class _Pp, bool _Dummy = true,
2712 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>,
2713 class = _EnableIfPointerConvertible<_Pp>>
2714 _LIBCPP_INLINE_VISIBILITY
2715 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) noexcept
2716 : __ptr_(__p, _VSTD::move(__d)) {
2717 static_assert(!is_reference<deleter_type>::value,
2718 "rvalue deleter bound to reference");
2719 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002720
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002721 template <bool _Dummy = true,
2722 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy>>>
2723 _LIBCPP_INLINE_VISIBILITY
2724 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) noexcept
2725 : __ptr_(nullptr, _VSTD::move(__d)) {
2726 static_assert(!is_reference<deleter_type>::value,
2727 "rvalue deleter bound to reference");
2728 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002729
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002730 template <class _Pp, bool _Dummy = true,
2731 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy>>,
2732 class = _EnableIfPointerConvertible<_Pp>>
2733 _LIBCPP_INLINE_VISIBILITY
2734 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
Howard Hinnant4500ca52011-12-18 21:19:44 +00002735
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002736 _LIBCPP_INLINE_VISIBILITY
2737 unique_ptr(unique_ptr&& __u) noexcept
2738 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2739 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002740
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002741 _LIBCPP_INLINE_VISIBILITY
2742 unique_ptr& operator=(unique_ptr&& __u) noexcept {
2743 reset(__u.release());
2744 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
2745 return *this;
2746 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002747
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002748 template <class _Up, class _Ep,
2749 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2750 class = _EnableIfDeleterConvertible<_Ep>
2751 >
2752 _LIBCPP_INLINE_VISIBILITY
2753 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
2754 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
2755 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002756
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002757 template <class _Up, class _Ep,
2758 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
2759 class = _EnableIfDeleterAssignable<_Ep>
2760 >
2761 _LIBCPP_INLINE_VISIBILITY
2762 unique_ptr&
2763 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept {
2764 reset(__u.release());
2765 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2766 return *this;
2767 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002768
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002769#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002770private:
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002771 template <class _Up> explicit unique_ptr(_Up);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002772
Eric Fiselierfecf9ea2017-04-16 01:51:04 +00002773 unique_ptr(unique_ptr&);
2774 template <class _Up> unique_ptr(unique_ptr<_Up>&);
2775
2776 unique_ptr& operator=(unique_ptr&);
2777 template <class _Up> unique_ptr& operator=(unique_ptr<_Up>&);
2778
2779 template <class _Up>
2780 unique_ptr(_Up __u,
2781 typename conditional<
2782 is_reference<deleter_type>::value, deleter_type,
2783 typename add_lvalue_reference<const deleter_type>::type>::type,
2784 typename enable_if<is_convertible<_Up, pointer>::value,
2785 __nat>::type = __nat());
2786public:
2787 _LIBCPP_INLINE_VISIBILITY
2788 unique_ptr() : __ptr_(pointer()) {
2789 static_assert(!is_pointer<deleter_type>::value,
2790 "unique_ptr constructed with null function pointer deleter");
2791 }
2792 _LIBCPP_INLINE_VISIBILITY
2793 unique_ptr(nullptr_t) : __ptr_(pointer()) {
2794 static_assert(!is_pointer<deleter_type>::value,
2795 "unique_ptr constructed with null function pointer deleter");
2796 }
2797
2798 _LIBCPP_INLINE_VISIBILITY
2799 explicit unique_ptr(pointer __p) : __ptr_(__p) {
2800 static_assert(!is_pointer<deleter_type>::value,
2801 "unique_ptr constructed with null function pointer deleter");
2802 }
2803
2804 _LIBCPP_INLINE_VISIBILITY
2805 unique_ptr(pointer __p, deleter_type __d)
2806 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
2807
2808 _LIBCPP_INLINE_VISIBILITY
2809 unique_ptr(nullptr_t, deleter_type __d)
2810 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
2811
2812 _LIBCPP_INLINE_VISIBILITY
2813 operator __rv<unique_ptr>() {
2814 return __rv<unique_ptr>(*this);
2815 }
2816
2817 _LIBCPP_INLINE_VISIBILITY
2818 unique_ptr(__rv<unique_ptr> __u)
2819 : __ptr_(__u->release(),
2820 _VSTD::forward<deleter_type>(__u->get_deleter())) {}
2821
2822 _LIBCPP_INLINE_VISIBILITY
2823 unique_ptr& operator=(__rv<unique_ptr> __u) {
2824 reset(__u->release());
2825 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
2826 return *this;
2827 }
2828
2829#endif // _LIBCPP_CXX03_LANG
2830
2831public:
2832 _LIBCPP_INLINE_VISIBILITY
2833 ~unique_ptr() { reset(); }
2834
2835 _LIBCPP_INLINE_VISIBILITY
2836 unique_ptr& operator=(nullptr_t) _NOEXCEPT {
2837 reset();
2838 return *this;
2839 }
2840
2841 _LIBCPP_INLINE_VISIBILITY
2842 typename add_lvalue_reference<_Tp>::type
2843 operator[](size_t __i) const {
2844 return __ptr_.first()[__i];
2845 }
2846 _LIBCPP_INLINE_VISIBILITY
2847 pointer get() const _NOEXCEPT {
2848 return __ptr_.first();
2849 }
2850
2851 _LIBCPP_INLINE_VISIBILITY
2852 deleter_type& get_deleter() _NOEXCEPT {
2853 return __ptr_.second();
2854 }
2855
2856 _LIBCPP_INLINE_VISIBILITY
2857 const deleter_type& get_deleter() const _NOEXCEPT {
2858 return __ptr_.second();
2859 }
2860 _LIBCPP_INLINE_VISIBILITY
2861 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
2862 return __ptr_.first() != nullptr;
2863 }
2864
2865 _LIBCPP_INLINE_VISIBILITY
2866 pointer release() _NOEXCEPT {
2867 pointer __t = __ptr_.first();
2868 __ptr_.first() = pointer();
2869 return __t;
2870 }
2871
2872 template <class _Pp>
2873 _LIBCPP_INLINE_VISIBILITY
2874 typename enable_if<
2875 __check_array_pointer_conversion<_Pp, unique_ptr>::value
2876 >::type
2877 reset(_Pp __p) _NOEXCEPT {
2878 pointer __tmp = __ptr_.first();
2879 __ptr_.first() = __p;
2880 if (__tmp)
2881 __ptr_.second()(__tmp);
2882 }
2883
2884 _LIBCPP_INLINE_VISIBILITY
2885 void reset(nullptr_t = nullptr) _NOEXCEPT {
2886 pointer __tmp = __ptr_.first();
2887 __ptr_.first() = nullptr;
2888 if (__tmp)
2889 __ptr_.second()(__tmp);
2890 }
2891
2892 _LIBCPP_INLINE_VISIBILITY
2893 void swap(unique_ptr& __u) _NOEXCEPT {
2894 __ptr_.swap(__u.__ptr_);
2895 }
2896
Howard Hinnantc51e1022010-05-11 19:42:16 +00002897};
2898
2899template <class _Tp, class _Dp>
2900inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002901typename enable_if<
2902 __is_swappable<_Dp>::value,
2903 void
2904>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002905swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906
2907template <class _T1, class _D1, class _T2, class _D2>
2908inline _LIBCPP_INLINE_VISIBILITY
2909bool
2910operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2911
2912template <class _T1, class _D1, class _T2, class _D2>
2913inline _LIBCPP_INLINE_VISIBILITY
2914bool
2915operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2916
2917template <class _T1, class _D1, class _T2, class _D2>
2918inline _LIBCPP_INLINE_VISIBILITY
2919bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002920operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2921{
2922 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2923 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002924 typedef typename common_type<_P1, _P2>::type _Vp;
2925 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002926}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927
2928template <class _T1, class _D1, class _T2, class _D2>
2929inline _LIBCPP_INLINE_VISIBILITY
2930bool
2931operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2932
2933template <class _T1, class _D1, class _T2, class _D2>
2934inline _LIBCPP_INLINE_VISIBILITY
2935bool
2936operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2937
2938template <class _T1, class _D1, class _T2, class _D2>
2939inline _LIBCPP_INLINE_VISIBILITY
2940bool
2941operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2942
Howard Hinnantb17caf92012-02-21 21:02:58 +00002943template <class _T1, class _D1>
2944inline _LIBCPP_INLINE_VISIBILITY
2945bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002946operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002947{
2948 return !__x;
2949}
2950
2951template <class _T1, class _D1>
2952inline _LIBCPP_INLINE_VISIBILITY
2953bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002954operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002955{
2956 return !__x;
2957}
2958
2959template <class _T1, class _D1>
2960inline _LIBCPP_INLINE_VISIBILITY
2961bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002962operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002963{
2964 return static_cast<bool>(__x);
2965}
2966
2967template <class _T1, class _D1>
2968inline _LIBCPP_INLINE_VISIBILITY
2969bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002970operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002971{
2972 return static_cast<bool>(__x);
2973}
2974
2975template <class _T1, class _D1>
2976inline _LIBCPP_INLINE_VISIBILITY
2977bool
2978operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2979{
2980 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2981 return less<_P1>()(__x.get(), nullptr);
2982}
2983
2984template <class _T1, class _D1>
2985inline _LIBCPP_INLINE_VISIBILITY
2986bool
2987operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2988{
2989 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2990 return less<_P1>()(nullptr, __x.get());
2991}
2992
2993template <class _T1, class _D1>
2994inline _LIBCPP_INLINE_VISIBILITY
2995bool
2996operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2997{
2998 return nullptr < __x;
2999}
3000
3001template <class _T1, class _D1>
3002inline _LIBCPP_INLINE_VISIBILITY
3003bool
3004operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3005{
3006 return __x < nullptr;
3007}
3008
3009template <class _T1, class _D1>
3010inline _LIBCPP_INLINE_VISIBILITY
3011bool
3012operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3013{
3014 return !(nullptr < __x);
3015}
3016
3017template <class _T1, class _D1>
3018inline _LIBCPP_INLINE_VISIBILITY
3019bool
3020operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3021{
3022 return !(__x < nullptr);
3023}
3024
3025template <class _T1, class _D1>
3026inline _LIBCPP_INLINE_VISIBILITY
3027bool
3028operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3029{
3030 return !(__x < nullptr);
3031}
3032
3033template <class _T1, class _D1>
3034inline _LIBCPP_INLINE_VISIBILITY
3035bool
3036operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3037{
3038 return !(nullptr < __x);
3039}
3040
Howard Hinnant9e028f12012-05-01 15:37:54 +00003041#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3042
3043template <class _Tp, class _Dp>
3044inline _LIBCPP_INLINE_VISIBILITY
3045unique_ptr<_Tp, _Dp>
3046move(unique_ptr<_Tp, _Dp>& __t)
3047{
3048 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3049}
3050
3051#endif
3052
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003053#if _LIBCPP_STD_VER > 11
3054
3055template<class _Tp>
3056struct __unique_if
3057{
3058 typedef unique_ptr<_Tp> __unique_single;
3059};
3060
3061template<class _Tp>
3062struct __unique_if<_Tp[]>
3063{
3064 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3065};
3066
3067template<class _Tp, size_t _Np>
3068struct __unique_if<_Tp[_Np]>
3069{
3070 typedef void __unique_array_known_bound;
3071};
3072
3073template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003074inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003075typename __unique_if<_Tp>::__unique_single
3076make_unique(_Args&&... __args)
3077{
3078 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3079}
3080
3081template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003082inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003083typename __unique_if<_Tp>::__unique_array_unknown_bound
3084make_unique(size_t __n)
3085{
3086 typedef typename remove_extent<_Tp>::type _Up;
3087 return unique_ptr<_Tp>(new _Up[__n]());
3088}
3089
3090template<class _Tp, class... _Args>
3091 typename __unique_if<_Tp>::__unique_array_known_bound
3092 make_unique(_Args&&...) = delete;
3093
3094#endif // _LIBCPP_STD_VER > 11
3095
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003096template <class _Tp, class _Dp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00003097#ifdef _LIBCPP_CXX03_LANG
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003098struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
Eric Fiselier698a97b2017-01-21 00:02:12 +00003099#else
3100struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
3101 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer>>
3102#endif
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003103{
3104 typedef unique_ptr<_Tp, _Dp> argument_type;
3105 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003106 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00003107 result_type operator()(const argument_type& __ptr) const
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003108 {
3109 typedef typename argument_type::pointer pointer;
3110 return hash<pointer>()(__ptr.get());
3111 }
3112};
3113
Howard Hinnantc51e1022010-05-11 19:42:16 +00003114struct __destruct_n
3115{
3116private:
3117 size_t size;
3118
3119 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003120 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003121 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3122
3123 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003124 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003125 {}
3126
Howard Hinnant719bda32011-05-28 14:41:13 +00003127 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128 {++size;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003129 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003130 {}
3131
Howard Hinnant719bda32011-05-28 14:41:13 +00003132 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003133 {size = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003134 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003135 {}
3136public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003137 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3138 : size(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003139
3140 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003141 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003142 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003143
3144 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003145 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003146 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003147
3148 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003149 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003150 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151};
3152
3153template <class _Alloc>
3154class __allocator_destructor
3155{
3156 typedef allocator_traits<_Alloc> __alloc_traits;
3157public:
3158 typedef typename __alloc_traits::pointer pointer;
3159 typedef typename __alloc_traits::size_type size_type;
3160private:
3161 _Alloc& __alloc_;
3162 size_type __s_;
3163public:
3164 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003165 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003166 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003168 void operator()(pointer __p) _NOEXCEPT
3169 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003170};
3171
3172template <class _InputIterator, class _ForwardIterator>
3173_ForwardIterator
3174uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3175{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003176 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003177#ifndef _LIBCPP_NO_EXCEPTIONS
3178 _ForwardIterator __s = __r;
3179 try
3180 {
3181#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003182 for (; __f != __l; ++__f, (void) ++__r)
3183 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003184#ifndef _LIBCPP_NO_EXCEPTIONS
3185 }
3186 catch (...)
3187 {
3188 for (; __s != __r; ++__s)
3189 __s->~value_type();
3190 throw;
3191 }
3192#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003193 return __r;
3194}
3195
3196template <class _InputIterator, class _Size, class _ForwardIterator>
3197_ForwardIterator
3198uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3199{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003200 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003201#ifndef _LIBCPP_NO_EXCEPTIONS
3202 _ForwardIterator __s = __r;
3203 try
3204 {
3205#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003206 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3207 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003208#ifndef _LIBCPP_NO_EXCEPTIONS
3209 }
3210 catch (...)
3211 {
3212 for (; __s != __r; ++__s)
3213 __s->~value_type();
3214 throw;
3215 }
3216#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003217 return __r;
3218}
3219
3220template <class _ForwardIterator, class _Tp>
3221void
3222uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3223{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003224 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003225#ifndef _LIBCPP_NO_EXCEPTIONS
3226 _ForwardIterator __s = __f;
3227 try
3228 {
3229#endif
3230 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003231 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003232#ifndef _LIBCPP_NO_EXCEPTIONS
3233 }
3234 catch (...)
3235 {
3236 for (; __s != __f; ++__s)
3237 __s->~value_type();
3238 throw;
3239 }
3240#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003241}
3242
3243template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003244_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003245uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3246{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003247 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003248#ifndef _LIBCPP_NO_EXCEPTIONS
3249 _ForwardIterator __s = __f;
3250 try
3251 {
3252#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003253 for (; __n > 0; ++__f, (void) --__n)
3254 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003255#ifndef _LIBCPP_NO_EXCEPTIONS
3256 }
3257 catch (...)
3258 {
3259 for (; __s != __f; ++__s)
3260 __s->~value_type();
3261 throw;
3262 }
3263#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003264 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003265}
3266
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003267#if _LIBCPP_STD_VER > 14
3268
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003269template <class _Tp>
3270inline _LIBCPP_INLINE_VISIBILITY
3271void destroy_at(_Tp* __loc) {
3272 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3273 __loc->~_Tp();
3274}
3275
3276template <class _ForwardIterator>
3277inline _LIBCPP_INLINE_VISIBILITY
3278void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3279 for (; __first != __last; ++__first)
3280 _VSTD::destroy_at(_VSTD::addressof(*__first));
3281}
3282
3283template <class _ForwardIterator, class _Size>
3284inline _LIBCPP_INLINE_VISIBILITY
3285_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3286 for (; __n > 0; (void)++__first, --__n)
3287 _VSTD::destroy_at(_VSTD::addressof(*__first));
3288 return __first;
3289}
3290
Eric Fiselier290c07c2016-10-11 21:13:44 +00003291template <class _ForwardIterator>
3292inline _LIBCPP_INLINE_VISIBILITY
3293void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3294 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3295 auto __idx = __first;
3296#ifndef _LIBCPP_NO_EXCEPTIONS
3297 try {
3298#endif
3299 for (; __idx != __last; ++__idx)
3300 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3301#ifndef _LIBCPP_NO_EXCEPTIONS
3302 } catch (...) {
3303 _VSTD::destroy(__first, __idx);
3304 throw;
3305 }
3306#endif
3307}
3308
3309template <class _ForwardIterator, class _Size>
3310inline _LIBCPP_INLINE_VISIBILITY
3311_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3312 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3313 auto __idx = __first;
3314#ifndef _LIBCPP_NO_EXCEPTIONS
3315 try {
3316#endif
3317 for (; __n > 0; (void)++__idx, --__n)
3318 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3319 return __idx;
3320#ifndef _LIBCPP_NO_EXCEPTIONS
3321 } catch (...) {
3322 _VSTD::destroy(__first, __idx);
3323 throw;
3324 }
3325#endif
3326}
3327
3328
3329template <class _ForwardIterator>
3330inline _LIBCPP_INLINE_VISIBILITY
3331void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3332 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3333 auto __idx = __first;
3334#ifndef _LIBCPP_NO_EXCEPTIONS
3335 try {
3336#endif
3337 for (; __idx != __last; ++__idx)
3338 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3339#ifndef _LIBCPP_NO_EXCEPTIONS
3340 } catch (...) {
3341 _VSTD::destroy(__first, __idx);
3342 throw;
3343 }
3344#endif
3345}
3346
3347template <class _ForwardIterator, class _Size>
3348inline _LIBCPP_INLINE_VISIBILITY
3349_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3350 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3351 auto __idx = __first;
3352#ifndef _LIBCPP_NO_EXCEPTIONS
3353 try {
3354#endif
3355 for (; __n > 0; (void)++__idx, --__n)
3356 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3357 return __idx;
3358#ifndef _LIBCPP_NO_EXCEPTIONS
3359 } catch (...) {
3360 _VSTD::destroy(__first, __idx);
3361 throw;
3362 }
3363#endif
3364}
3365
3366
3367template <class _InputIt, class _ForwardIt>
3368inline _LIBCPP_INLINE_VISIBILITY
3369_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3370 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3371 auto __idx = __first_res;
3372#ifndef _LIBCPP_NO_EXCEPTIONS
3373 try {
3374#endif
3375 for (; __first != __last; (void)++__idx, ++__first)
3376 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3377 return __idx;
3378#ifndef _LIBCPP_NO_EXCEPTIONS
3379 } catch (...) {
3380 _VSTD::destroy(__first_res, __idx);
3381 throw;
3382 }
3383#endif
3384}
3385
3386template <class _InputIt, class _Size, class _ForwardIt>
3387inline _LIBCPP_INLINE_VISIBILITY
3388pair<_InputIt, _ForwardIt>
3389uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3390 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3391 auto __idx = __first_res;
3392#ifndef _LIBCPP_NO_EXCEPTIONS
3393 try {
3394#endif
3395 for (; __n > 0; ++__idx, (void)++__first, --__n)
3396 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3397 return {__first, __idx};
3398#ifndef _LIBCPP_NO_EXCEPTIONS
3399 } catch (...) {
3400 _VSTD::destroy(__first_res, __idx);
3401 throw;
3402 }
3403#endif
3404}
3405
3406
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003407#endif // _LIBCPP_STD_VER > 14
3408
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003409// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3410// should be sufficient for thread safety.
Eric Fiselier5d604012017-02-17 08:37:03 +00003411// See https://bugs.llvm.org/show_bug.cgi?id=22803
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003412#if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
3413 && defined(__ATOMIC_RELAXED) \
3414 && defined(__ATOMIC_ACQ_REL)
3415# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3416#elif !defined(__clang__) && defined(_GNUC_VER) && _GNUC_VER >= 407
3417# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3418#endif
3419
3420template <class _Tp>
3421inline _LIBCPP_INLINE_VISIBILITY _Tp
3422__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3423{
3424#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3425 return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3426#else
3427 return __t += 1;
3428#endif
3429}
3430
3431template <class _Tp>
3432inline _LIBCPP_INLINE_VISIBILITY _Tp
3433__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3434{
3435#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3436 return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3437#else
3438 return __t -= 1;
3439#endif
3440}
3441
Howard Hinnant756c69b2010-09-22 16:48:34 +00003442class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003443 : public std::exception
3444{
3445public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003446 virtual ~bad_weak_ptr() _NOEXCEPT;
3447 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003448};
3449
Marshall Clow8fea1612016-08-25 15:09:01 +00003450_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
3451void __throw_bad_weak_ptr()
3452{
3453#ifndef _LIBCPP_NO_EXCEPTIONS
3454 throw bad_weak_ptr();
3455#else
3456 _VSTD::abort();
3457#endif
3458}
3459
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003460template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003461
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003462class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003463{
3464 __shared_count(const __shared_count&);
3465 __shared_count& operator=(const __shared_count&);
3466
3467protected:
3468 long __shared_owners_;
3469 virtual ~__shared_count();
3470private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003471 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003472
3473public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003474 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003475 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003476 : __shared_owners_(__refs) {}
3477
Eric Fiselier98848572017-01-17 03:16:26 +00003478#if defined(_LIBCPP_BUILDING_MEMORY) && \
3479 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003480 void __add_shared() _NOEXCEPT;
3481 bool __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003482#else
3483 _LIBCPP_INLINE_VISIBILITY
3484 void __add_shared() _NOEXCEPT {
3485 __libcpp_atomic_refcount_increment(__shared_owners_);
3486 }
3487 _LIBCPP_INLINE_VISIBILITY
3488 bool __release_shared() _NOEXCEPT {
3489 if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3490 __on_zero_shared();
3491 return true;
3492 }
3493 return false;
3494 }
3495#endif
Howard Hinnant756c69b2010-09-22 16:48:34 +00003496 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003497 long use_count() const _NOEXCEPT {
3498 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3499 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003500};
3501
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003502class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003503 : private __shared_count
3504{
3505 long __shared_weak_owners_;
3506
3507public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003508 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003509 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003510 : __shared_count(__refs),
3511 __shared_weak_owners_(__refs) {}
3512protected:
3513 virtual ~__shared_weak_count();
3514
3515public:
Eric Fiselier98848572017-01-17 03:16:26 +00003516#if defined(_LIBCPP_BUILDING_MEMORY) && \
3517 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
Eric Fiseliere0700ff2017-01-17 03:05:31 +00003518 void __add_shared() _NOEXCEPT;
3519 void __add_weak() _NOEXCEPT;
3520 void __release_shared() _NOEXCEPT;
Kevin Hu4bdc8a02017-01-17 02:46:33 +00003521#else
3522 _LIBCPP_INLINE_VISIBILITY
3523 void __add_shared() _NOEXCEPT {
3524 __shared_count::__add_shared();
3525 }
3526 _LIBCPP_INLINE_VISIBILITY
3527 void __add_weak() _NOEXCEPT {
3528 __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3529 }
3530 _LIBCPP_INLINE_VISIBILITY
3531 void __release_shared() _NOEXCEPT {
3532 if (__shared_count::__release_shared())
3533 __release_weak();
3534 }
3535#endif
Howard Hinnant719bda32011-05-28 14:41:13 +00003536 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003537 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003538 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3539 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003540
Howard Hinnant807d6332013-02-25 15:50:36 +00003541 // Define the function out only if we build static libc++ without RTTI.
3542 // Otherwise we may break clients who need to compile their projects with
3543 // -fno-rtti and yet link against a libc++.dylib compiled
3544 // without -fno-rtti.
3545#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003546 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003547#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003549 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003550};
3551
3552template <class _Tp, class _Dp, class _Alloc>
3553class __shared_ptr_pointer
3554 : public __shared_weak_count
3555{
3556 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3557public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003560 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003561
Howard Hinnant72f73582010-08-11 17:04:31 +00003562#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003563 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003564#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003565
3566private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003567 virtual void __on_zero_shared() _NOEXCEPT;
3568 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003569};
3570
Howard Hinnant72f73582010-08-11 17:04:31 +00003571#ifndef _LIBCPP_NO_RTTI
3572
Howard Hinnantc51e1022010-05-11 19:42:16 +00003573template <class _Tp, class _Dp, class _Alloc>
3574const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003575__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003576{
Marshall Clowa8dfc942014-11-17 19:05:50 +00003577 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003578}
3579
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003580#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003581
Howard Hinnantc51e1022010-05-11 19:42:16 +00003582template <class _Tp, class _Dp, class _Alloc>
3583void
Howard Hinnant719bda32011-05-28 14:41:13 +00003584__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003585{
3586 __data_.first().second()(__data_.first().first());
3587 __data_.first().second().~_Dp();
3588}
3589
3590template <class _Tp, class _Dp, class _Alloc>
3591void
Howard Hinnant719bda32011-05-28 14:41:13 +00003592__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003593{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003594 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3595 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003596 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3597
Eric Fiselierf8898c82015-02-05 23:01:40 +00003598 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003599 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003600 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003601}
3602
3603template <class _Tp, class _Alloc>
3604class __shared_ptr_emplace
3605 : public __shared_weak_count
3606{
3607 __compressed_pair<_Alloc, _Tp> __data_;
3608public:
3609#ifndef _LIBCPP_HAS_NO_VARIADICS
3610
Howard Hinnant756c69b2010-09-22 16:48:34 +00003611 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003612 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003613 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003614
3615 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003616 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003618 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3619 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003620
3621#else // _LIBCPP_HAS_NO_VARIADICS
3622
Howard Hinnant756c69b2010-09-22 16:48:34 +00003623 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003624 __shared_ptr_emplace(_Alloc __a)
3625 : __data_(__a) {}
3626
3627 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003628 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003629 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3630 : __data_(__a, _Tp(__a0)) {}
3631
3632 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003633 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003634 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3635 : __data_(__a, _Tp(__a0, __a1)) {}
3636
3637 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003638 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003639 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3640 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3641
3642#endif // _LIBCPP_HAS_NO_VARIADICS
3643
3644private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003645 virtual void __on_zero_shared() _NOEXCEPT;
3646 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003647public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003648 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003649 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003650};
3651
3652template <class _Tp, class _Alloc>
3653void
Howard Hinnant719bda32011-05-28 14:41:13 +00003654__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003655{
3656 __data_.second().~_Tp();
3657}
3658
3659template <class _Tp, class _Alloc>
3660void
Howard Hinnant719bda32011-05-28 14:41:13 +00003661__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003662{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003663 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3664 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003665 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003666 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003667 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003668 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003669}
3670
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003671template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003672
3673template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003674class _LIBCPP_TEMPLATE_VIS shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003675{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003676public:
3677 typedef _Tp element_type;
Marshall Clow7e384b72017-01-10 16:59:33 +00003678
Eric Fiselierae5b6672016-06-27 01:02:43 +00003679#if _LIBCPP_STD_VER > 14
3680 typedef weak_ptr<_Tp> weak_type;
3681#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003682private:
3683 element_type* __ptr_;
3684 __shared_weak_count* __cntrl_;
3685
3686 struct __nat {int __for_bool_;};
3687public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003688 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003689 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003690 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003691 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003692 template<class _Yp>
3693 explicit shared_ptr(_Yp* __p,
3694 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3695 template<class _Yp, class _Dp>
3696 shared_ptr(_Yp* __p, _Dp __d,
3697 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3698 template<class _Yp, class _Dp, class _Alloc>
3699 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3700 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003701 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3702 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003703 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3704 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003705 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003706 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003708 shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003709 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003710 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003711#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003712 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003713 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003714 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003715 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00003716 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003717#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003718 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00003719 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
Marshall Clowb22274f2017-01-24 22:22:33 +00003720#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant74279a52010-09-04 23:28:19 +00003721#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003722 template<class _Yp>
3723 shared_ptr(auto_ptr<_Yp>&& __r,
3724 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003725#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003726 template<class _Yp>
3727 shared_ptr(auto_ptr<_Yp> __r,
3728 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003729#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003730#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003731#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003732 template <class _Yp, class _Dp>
3733 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3734 typename enable_if
3735 <
3736 !is_lvalue_reference<_Dp>::value &&
3737 !is_array<_Yp>::value &&
3738 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3739 __nat
3740 >::type = __nat());
3741 template <class _Yp, class _Dp>
3742 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3743 typename enable_if
3744 <
3745 is_lvalue_reference<_Dp>::value &&
3746 !is_array<_Yp>::value &&
3747 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3748 __nat
3749 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003750#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003751 template <class _Yp, class _Dp>
3752 shared_ptr(unique_ptr<_Yp, _Dp>,
3753 typename enable_if
3754 <
3755 !is_lvalue_reference<_Dp>::value &&
3756 !is_array<_Yp>::value &&
3757 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3758 __nat
3759 >::type = __nat());
3760 template <class _Yp, class _Dp>
3761 shared_ptr(unique_ptr<_Yp, _Dp>,
3762 typename enable_if
3763 <
3764 is_lvalue_reference<_Dp>::value &&
3765 !is_array<_Yp>::value &&
3766 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3767 __nat
3768 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003769#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003770
3771 ~shared_ptr();
3772
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003774 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003775 template<class _Yp>
3776 typename enable_if
3777 <
3778 is_convertible<_Yp*, element_type*>::value,
3779 shared_ptr&
3780 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003782 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003783#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003785 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003786 template<class _Yp>
3787 typename enable_if
3788 <
3789 is_convertible<_Yp*, element_type*>::value,
3790 shared_ptr<_Tp>&
3791 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003792 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003793 operator=(shared_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003794#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003795 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003796 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003797 typename enable_if
3798 <
3799 !is_array<_Yp>::value &&
3800 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003801 shared_ptr
3802 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003803 operator=(auto_ptr<_Yp>&& __r);
Marshall Clowb22274f2017-01-24 22:22:33 +00003804#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003805#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Marshall Clowb22274f2017-01-24 22:22:33 +00003806#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003807 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003809 typename enable_if
3810 <
3811 !is_array<_Yp>::value &&
3812 is_convertible<_Yp*, element_type*>::value,
3813 shared_ptr&
3814 >::type
3815 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003816#endif
Marshall Clowb22274f2017-01-24 22:22:33 +00003817#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003818 template <class _Yp, class _Dp>
3819 typename enable_if
3820 <
3821 !is_array<_Yp>::value &&
3822 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3823 shared_ptr&
3824 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00003825#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003826 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003827 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003828#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00003829 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003830 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003831#endif
3832
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003833 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003834 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003835 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003836 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003837 template<class _Yp>
3838 typename enable_if
3839 <
3840 is_convertible<_Yp*, element_type*>::value,
3841 void
3842 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003843 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003844 reset(_Yp* __p);
3845 template<class _Yp, class _Dp>
3846 typename enable_if
3847 <
3848 is_convertible<_Yp*, element_type*>::value,
3849 void
3850 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003851 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003852 reset(_Yp* __p, _Dp __d);
3853 template<class _Yp, class _Dp, class _Alloc>
3854 typename enable_if
3855 <
3856 is_convertible<_Yp*, element_type*>::value,
3857 void
3858 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003859 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003860 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003861
Howard Hinnant756c69b2010-09-22 16:48:34 +00003862 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003863 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003865 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
3866 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003867 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003868 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003869 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003870 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003872 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003873 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00003874 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003875 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003876 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003877 bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003878 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00003879 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003880 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00003881 bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003882 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00003883 _LIBCPP_INLINE_VISIBILITY
3884 bool
3885 __owner_equivalent(const shared_ptr& __p) const
3886 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003887
Howard Hinnant72f73582010-08-11 17:04:31 +00003888#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003889 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003890 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003891 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003892 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003893#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00003894
3895#ifndef _LIBCPP_HAS_NO_VARIADICS
3896
3897 template<class ..._Args>
3898 static
3899 shared_ptr<_Tp>
3900 make_shared(_Args&& ...__args);
3901
3902 template<class _Alloc, class ..._Args>
3903 static
3904 shared_ptr<_Tp>
3905 allocate_shared(const _Alloc& __a, _Args&& ...__args);
3906
3907#else // _LIBCPP_HAS_NO_VARIADICS
3908
3909 static shared_ptr<_Tp> make_shared();
3910
3911 template<class _A0>
3912 static shared_ptr<_Tp> make_shared(_A0&);
3913
3914 template<class _A0, class _A1>
3915 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
3916
3917 template<class _A0, class _A1, class _A2>
3918 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
3919
3920 template<class _Alloc>
3921 static shared_ptr<_Tp>
3922 allocate_shared(const _Alloc& __a);
3923
3924 template<class _Alloc, class _A0>
3925 static shared_ptr<_Tp>
3926 allocate_shared(const _Alloc& __a, _A0& __a0);
3927
3928 template<class _Alloc, class _A0, class _A1>
3929 static shared_ptr<_Tp>
3930 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
3931
3932 template<class _Alloc, class _A0, class _A1, class _A2>
3933 static shared_ptr<_Tp>
3934 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
3935
3936#endif // _LIBCPP_HAS_NO_VARIADICS
3937
3938private:
3939
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003940 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003941 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003942 void
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003943 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
3944 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003945 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003946 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00003947 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00003948 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003949 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
3950 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00003951 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003952 }
3953
Howard Hinnant756c69b2010-09-22 16:48:34 +00003954 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003955 void __enable_weak_this(const volatile void*, const volatile void*) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003956
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00003957 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3958 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003959};
3960
3961template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003962inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003963_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003964shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003965 : __ptr_(0),
3966 __cntrl_(0)
3967{
3968}
3969
3970template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003971inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003972_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00003973shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003974 : __ptr_(0),
3975 __cntrl_(0)
3976{
3977}
3978
3979template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003980template<class _Yp>
3981shared_ptr<_Tp>::shared_ptr(_Yp* __p,
3982 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003983 : __ptr_(__p)
3984{
3985 unique_ptr<_Yp> __hold(__p);
3986 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
3987 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
3988 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00003989 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003990}
3991
3992template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00003993template<class _Yp, class _Dp>
3994shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
3995 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003996 : __ptr_(__p)
3997{
3998#ifndef _LIBCPP_NO_EXCEPTIONS
3999 try
4000 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004001#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004002 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4003 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004004 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004005#ifndef _LIBCPP_NO_EXCEPTIONS
4006 }
4007 catch (...)
4008 {
4009 __d(__p);
4010 throw;
4011 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004012#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004013}
4014
4015template<class _Tp>
4016template<class _Dp>
4017shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4018 : __ptr_(0)
4019{
4020#ifndef _LIBCPP_NO_EXCEPTIONS
4021 try
4022 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004023#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004024 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4025 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4026#ifndef _LIBCPP_NO_EXCEPTIONS
4027 }
4028 catch (...)
4029 {
4030 __d(__p);
4031 throw;
4032 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004033#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004034}
4035
4036template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004037template<class _Yp, class _Dp, class _Alloc>
4038shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4039 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004040 : __ptr_(__p)
4041{
4042#ifndef _LIBCPP_NO_EXCEPTIONS
4043 try
4044 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004045#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004046 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004047 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004048 typedef __allocator_destructor<_A2> _D2;
4049 _A2 __a2(__a);
4050 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004051 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4052 _CntrlBlk(__p, __d, __a);
4053 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004054 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004055#ifndef _LIBCPP_NO_EXCEPTIONS
4056 }
4057 catch (...)
4058 {
4059 __d(__p);
4060 throw;
4061 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004062#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004063}
4064
4065template<class _Tp>
4066template<class _Dp, class _Alloc>
4067shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4068 : __ptr_(0)
4069{
4070#ifndef _LIBCPP_NO_EXCEPTIONS
4071 try
4072 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004073#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004074 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004075 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004076 typedef __allocator_destructor<_A2> _D2;
4077 _A2 __a2(__a);
4078 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004079 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4080 _CntrlBlk(__p, __d, __a);
4081 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004082#ifndef _LIBCPP_NO_EXCEPTIONS
4083 }
4084 catch (...)
4085 {
4086 __d(__p);
4087 throw;
4088 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004089#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004090}
4091
4092template<class _Tp>
4093template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004094inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004095shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004096 : __ptr_(__p),
4097 __cntrl_(__r.__cntrl_)
4098{
4099 if (__cntrl_)
4100 __cntrl_->__add_shared();
4101}
4102
4103template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004104inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004105shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004106 : __ptr_(__r.__ptr_),
4107 __cntrl_(__r.__cntrl_)
4108{
4109 if (__cntrl_)
4110 __cntrl_->__add_shared();
4111}
4112
4113template<class _Tp>
4114template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004115inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004116shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004117 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004118 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004119 : __ptr_(__r.__ptr_),
4120 __cntrl_(__r.__cntrl_)
4121{
4122 if (__cntrl_)
4123 __cntrl_->__add_shared();
4124}
4125
Howard Hinnant74279a52010-09-04 23:28:19 +00004126#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004127
4128template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004129inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004130shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004131 : __ptr_(__r.__ptr_),
4132 __cntrl_(__r.__cntrl_)
4133{
4134 __r.__ptr_ = 0;
4135 __r.__cntrl_ = 0;
4136}
4137
4138template<class _Tp>
4139template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004140inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004141shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00004142 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004143 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004144 : __ptr_(__r.__ptr_),
4145 __cntrl_(__r.__cntrl_)
4146{
4147 __r.__ptr_ = 0;
4148 __r.__cntrl_ = 0;
4149}
4150
Howard Hinnant74279a52010-09-04 23:28:19 +00004151#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004152
Marshall Clowb22274f2017-01-24 22:22:33 +00004153#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004154template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004155template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004156#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004157shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004158#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004159shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004160#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004161 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004162 : __ptr_(__r.get())
4163{
4164 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4165 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004166 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004167 __r.release();
4168}
Marshall Clowb22274f2017-01-24 22:22:33 +00004169#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004170
4171template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004172template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004173#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004174shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4175#else
4176shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4177#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004178 typename enable_if
4179 <
4180 !is_lvalue_reference<_Dp>::value &&
4181 !is_array<_Yp>::value &&
4182 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4183 __nat
4184 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004185 : __ptr_(__r.get())
4186{
Marshall Clow35cde742015-05-10 13:59:45 +00004187#if _LIBCPP_STD_VER > 11
4188 if (__ptr_ == nullptr)
4189 __cntrl_ = nullptr;
4190 else
4191#endif
4192 {
4193 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4194 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004195 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004196 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004197 __r.release();
4198}
4199
4200template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004201template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004202#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004203shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4204#else
4205shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4206#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004207 typename enable_if
4208 <
4209 is_lvalue_reference<_Dp>::value &&
4210 !is_array<_Yp>::value &&
4211 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4212 __nat
4213 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004214 : __ptr_(__r.get())
4215{
Marshall Clow35cde742015-05-10 13:59:45 +00004216#if _LIBCPP_STD_VER > 11
4217 if (__ptr_ == nullptr)
4218 __cntrl_ = nullptr;
4219 else
4220#endif
4221 {
4222 typedef __shared_ptr_pointer<_Yp*,
4223 reference_wrapper<typename remove_reference<_Dp>::type>,
4224 allocator<_Yp> > _CntrlBlk;
4225 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004226 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004227 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004228 __r.release();
4229}
4230
4231#ifndef _LIBCPP_HAS_NO_VARIADICS
4232
4233template<class _Tp>
4234template<class ..._Args>
4235shared_ptr<_Tp>
4236shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4237{
4238 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4239 typedef allocator<_CntrlBlk> _A2;
4240 typedef __allocator_destructor<_A2> _D2;
4241 _A2 __a2;
4242 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004243 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004244 shared_ptr<_Tp> __r;
4245 __r.__ptr_ = __hold2.get()->get();
4246 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004247 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004248 return __r;
4249}
4250
4251template<class _Tp>
4252template<class _Alloc, class ..._Args>
4253shared_ptr<_Tp>
4254shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4255{
4256 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004257 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004258 typedef __allocator_destructor<_A2> _D2;
4259 _A2 __a2(__a);
4260 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004261 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4262 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004263 shared_ptr<_Tp> __r;
4264 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004265 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004266 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004267 return __r;
4268}
4269
4270#else // _LIBCPP_HAS_NO_VARIADICS
4271
4272template<class _Tp>
4273shared_ptr<_Tp>
4274shared_ptr<_Tp>::make_shared()
4275{
4276 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4277 typedef allocator<_CntrlBlk> _Alloc2;
4278 typedef __allocator_destructor<_Alloc2> _D2;
4279 _Alloc2 __alloc2;
4280 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4281 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4282 shared_ptr<_Tp> __r;
4283 __r.__ptr_ = __hold2.get()->get();
4284 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004285 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004286 return __r;
4287}
4288
4289template<class _Tp>
4290template<class _A0>
4291shared_ptr<_Tp>
4292shared_ptr<_Tp>::make_shared(_A0& __a0)
4293{
4294 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4295 typedef allocator<_CntrlBlk> _Alloc2;
4296 typedef __allocator_destructor<_Alloc2> _D2;
4297 _Alloc2 __alloc2;
4298 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4299 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4300 shared_ptr<_Tp> __r;
4301 __r.__ptr_ = __hold2.get()->get();
4302 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004303 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004304 return __r;
4305}
4306
4307template<class _Tp>
4308template<class _A0, class _A1>
4309shared_ptr<_Tp>
4310shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4311{
4312 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4313 typedef allocator<_CntrlBlk> _Alloc2;
4314 typedef __allocator_destructor<_Alloc2> _D2;
4315 _Alloc2 __alloc2;
4316 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4317 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4318 shared_ptr<_Tp> __r;
4319 __r.__ptr_ = __hold2.get()->get();
4320 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004321 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004322 return __r;
4323}
4324
4325template<class _Tp>
4326template<class _A0, class _A1, class _A2>
4327shared_ptr<_Tp>
4328shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4329{
4330 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4331 typedef allocator<_CntrlBlk> _Alloc2;
4332 typedef __allocator_destructor<_Alloc2> _D2;
4333 _Alloc2 __alloc2;
4334 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4335 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4336 shared_ptr<_Tp> __r;
4337 __r.__ptr_ = __hold2.get()->get();
4338 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004339 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004340 return __r;
4341}
4342
4343template<class _Tp>
4344template<class _Alloc>
4345shared_ptr<_Tp>
4346shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4347{
4348 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004349 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004350 typedef __allocator_destructor<_Alloc2> _D2;
4351 _Alloc2 __alloc2(__a);
4352 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004353 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4354 _CntrlBlk(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004355 shared_ptr<_Tp> __r;
4356 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004357 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004358 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004359 return __r;
4360}
4361
4362template<class _Tp>
4363template<class _Alloc, class _A0>
4364shared_ptr<_Tp>
4365shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4366{
4367 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004368 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004369 typedef __allocator_destructor<_Alloc2> _D2;
4370 _Alloc2 __alloc2(__a);
4371 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004372 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4373 _CntrlBlk(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004374 shared_ptr<_Tp> __r;
4375 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004376 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004377 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004378 return __r;
4379}
4380
4381template<class _Tp>
4382template<class _Alloc, class _A0, class _A1>
4383shared_ptr<_Tp>
4384shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4385{
4386 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004387 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004388 typedef __allocator_destructor<_Alloc2> _D2;
4389 _Alloc2 __alloc2(__a);
4390 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004391 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4392 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004393 shared_ptr<_Tp> __r;
4394 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004395 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004396 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004397 return __r;
4398}
4399
4400template<class _Tp>
4401template<class _Alloc, class _A0, class _A1, class _A2>
4402shared_ptr<_Tp>
4403shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4404{
4405 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004406 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004407 typedef __allocator_destructor<_Alloc2> _D2;
4408 _Alloc2 __alloc2(__a);
4409 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004410 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4411 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004412 shared_ptr<_Tp> __r;
4413 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004414 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004415 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004416 return __r;
4417}
4418
4419#endif // _LIBCPP_HAS_NO_VARIADICS
4420
4421template<class _Tp>
4422shared_ptr<_Tp>::~shared_ptr()
4423{
4424 if (__cntrl_)
4425 __cntrl_->__release_shared();
4426}
4427
4428template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004429inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004430shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004431shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004432{
4433 shared_ptr(__r).swap(*this);
4434 return *this;
4435}
4436
4437template<class _Tp>
4438template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004439inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004440typename enable_if
4441<
Marshall Clow7e384b72017-01-10 16:59:33 +00004442 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004443 shared_ptr<_Tp>&
4444>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004445shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004446{
4447 shared_ptr(__r).swap(*this);
4448 return *this;
4449}
4450
Howard Hinnant74279a52010-09-04 23:28:19 +00004451#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004452
4453template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004454inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004455shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004456shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004457{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004458 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004459 return *this;
4460}
4461
4462template<class _Tp>
4463template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004464inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004465typename enable_if
4466<
Marshall Clow7e384b72017-01-10 16:59:33 +00004467 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004468 shared_ptr<_Tp>&
4469>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004470shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4471{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004472 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004473 return *this;
4474}
4475
Marshall Clowb22274f2017-01-24 22:22:33 +00004476#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004477template<class _Tp>
4478template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004479inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004480typename enable_if
4481<
4482 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004483 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004484 shared_ptr<_Tp>
4485>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004486shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4487{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004488 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004489 return *this;
4490}
Marshall Clowb22274f2017-01-24 22:22:33 +00004491#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004492
4493template<class _Tp>
4494template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004495inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004496typename enable_if
4497<
4498 !is_array<_Yp>::value &&
Marshall Clow7e384b72017-01-10 16:59:33 +00004499 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4500 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004501 shared_ptr<_Tp>&
4502>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004503shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4504{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004505 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004506 return *this;
4507}
4508
Howard Hinnant74279a52010-09-04 23:28:19 +00004509#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004510
Marshall Clowb22274f2017-01-24 22:22:33 +00004511#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004512template<class _Tp>
4513template<class _Yp>
4514inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004515typename enable_if
4516<
4517 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004518 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004519 shared_ptr<_Tp>&
4520>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004521shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004522{
4523 shared_ptr(__r).swap(*this);
4524 return *this;
4525}
Marshall Clowb22274f2017-01-24 22:22:33 +00004526#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004527
4528template<class _Tp>
4529template <class _Yp, class _Dp>
4530inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004531typename enable_if
4532<
4533 !is_array<_Yp>::value &&
Marshall Clowb89a86e2017-01-10 18:40:01 +00004534 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4535 typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004536 shared_ptr<_Tp>&
4537>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004538shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4539{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004540 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004541 return *this;
4542}
4543
Howard Hinnant74279a52010-09-04 23:28:19 +00004544#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004545
4546template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004547inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004548void
Howard Hinnant719bda32011-05-28 14:41:13 +00004549shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004550{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004551 _VSTD::swap(__ptr_, __r.__ptr_);
4552 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004553}
4554
4555template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004556inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004557void
Howard Hinnant719bda32011-05-28 14:41:13 +00004558shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004559{
4560 shared_ptr().swap(*this);
4561}
4562
4563template<class _Tp>
4564template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004565inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004566typename enable_if
4567<
Marshall Clow7e384b72017-01-10 16:59:33 +00004568 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004569 void
4570>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004571shared_ptr<_Tp>::reset(_Yp* __p)
4572{
4573 shared_ptr(__p).swap(*this);
4574}
4575
4576template<class _Tp>
4577template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004578inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004579typename enable_if
4580<
Marshall Clow7e384b72017-01-10 16:59:33 +00004581 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004582 void
4583>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004584shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4585{
4586 shared_ptr(__p, __d).swap(*this);
4587}
4588
4589template<class _Tp>
4590template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004591inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004592typename enable_if
4593<
Marshall Clow7e384b72017-01-10 16:59:33 +00004594 is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004595 void
4596>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004597shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4598{
4599 shared_ptr(__p, __d, __a).swap(*this);
4600}
4601
4602#ifndef _LIBCPP_HAS_NO_VARIADICS
4603
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004604template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004605inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004606typename enable_if
4607<
4608 !is_array<_Tp>::value,
4609 shared_ptr<_Tp>
4610>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004611make_shared(_Args&& ...__args)
4612{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004613 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004614}
4615
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004616template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004617inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004618typename enable_if
4619<
4620 !is_array<_Tp>::value,
4621 shared_ptr<_Tp>
4622>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004623allocate_shared(const _Alloc& __a, _Args&& ...__args)
4624{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004625 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004626}
4627
4628#else // _LIBCPP_HAS_NO_VARIADICS
4629
4630template<class _Tp>
4631inline _LIBCPP_INLINE_VISIBILITY
4632shared_ptr<_Tp>
4633make_shared()
4634{
4635 return shared_ptr<_Tp>::make_shared();
4636}
4637
4638template<class _Tp, class _A0>
4639inline _LIBCPP_INLINE_VISIBILITY
4640shared_ptr<_Tp>
4641make_shared(_A0& __a0)
4642{
4643 return shared_ptr<_Tp>::make_shared(__a0);
4644}
4645
4646template<class _Tp, class _A0, class _A1>
4647inline _LIBCPP_INLINE_VISIBILITY
4648shared_ptr<_Tp>
4649make_shared(_A0& __a0, _A1& __a1)
4650{
4651 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4652}
4653
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004654template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004655inline _LIBCPP_INLINE_VISIBILITY
4656shared_ptr<_Tp>
4657make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4658{
4659 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4660}
4661
4662template<class _Tp, class _Alloc>
4663inline _LIBCPP_INLINE_VISIBILITY
4664shared_ptr<_Tp>
4665allocate_shared(const _Alloc& __a)
4666{
4667 return shared_ptr<_Tp>::allocate_shared(__a);
4668}
4669
4670template<class _Tp, class _Alloc, class _A0>
4671inline _LIBCPP_INLINE_VISIBILITY
4672shared_ptr<_Tp>
4673allocate_shared(const _Alloc& __a, _A0& __a0)
4674{
4675 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4676}
4677
4678template<class _Tp, class _Alloc, class _A0, class _A1>
4679inline _LIBCPP_INLINE_VISIBILITY
4680shared_ptr<_Tp>
4681allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4682{
4683 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4684}
4685
4686template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4687inline _LIBCPP_INLINE_VISIBILITY
4688shared_ptr<_Tp>
4689allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4690{
4691 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4692}
4693
4694#endif // _LIBCPP_HAS_NO_VARIADICS
4695
4696template<class _Tp, class _Up>
4697inline _LIBCPP_INLINE_VISIBILITY
4698bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004699operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004700{
4701 return __x.get() == __y.get();
4702}
4703
4704template<class _Tp, class _Up>
4705inline _LIBCPP_INLINE_VISIBILITY
4706bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004707operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004708{
4709 return !(__x == __y);
4710}
4711
4712template<class _Tp, class _Up>
4713inline _LIBCPP_INLINE_VISIBILITY
4714bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004715operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004716{
Eric Fiselierf8898c82015-02-05 23:01:40 +00004717 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4718 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00004719}
4720
4721template<class _Tp, class _Up>
4722inline _LIBCPP_INLINE_VISIBILITY
4723bool
4724operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4725{
4726 return __y < __x;
4727}
4728
4729template<class _Tp, class _Up>
4730inline _LIBCPP_INLINE_VISIBILITY
4731bool
4732operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4733{
4734 return !(__y < __x);
4735}
4736
4737template<class _Tp, class _Up>
4738inline _LIBCPP_INLINE_VISIBILITY
4739bool
4740operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4741{
4742 return !(__x < __y);
4743}
4744
4745template<class _Tp>
4746inline _LIBCPP_INLINE_VISIBILITY
4747bool
4748operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4749{
4750 return !__x;
4751}
4752
4753template<class _Tp>
4754inline _LIBCPP_INLINE_VISIBILITY
4755bool
4756operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4757{
4758 return !__x;
4759}
4760
4761template<class _Tp>
4762inline _LIBCPP_INLINE_VISIBILITY
4763bool
4764operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4765{
4766 return static_cast<bool>(__x);
4767}
4768
4769template<class _Tp>
4770inline _LIBCPP_INLINE_VISIBILITY
4771bool
4772operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4773{
4774 return static_cast<bool>(__x);
4775}
4776
4777template<class _Tp>
4778inline _LIBCPP_INLINE_VISIBILITY
4779bool
4780operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4781{
4782 return less<_Tp*>()(__x.get(), nullptr);
4783}
4784
4785template<class _Tp>
4786inline _LIBCPP_INLINE_VISIBILITY
4787bool
4788operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4789{
4790 return less<_Tp*>()(nullptr, __x.get());
4791}
4792
4793template<class _Tp>
4794inline _LIBCPP_INLINE_VISIBILITY
4795bool
4796operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4797{
4798 return nullptr < __x;
4799}
4800
4801template<class _Tp>
4802inline _LIBCPP_INLINE_VISIBILITY
4803bool
4804operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4805{
4806 return __x < nullptr;
4807}
4808
4809template<class _Tp>
4810inline _LIBCPP_INLINE_VISIBILITY
4811bool
4812operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4813{
4814 return !(nullptr < __x);
4815}
4816
4817template<class _Tp>
4818inline _LIBCPP_INLINE_VISIBILITY
4819bool
4820operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4821{
4822 return !(__x < nullptr);
4823}
4824
4825template<class _Tp>
4826inline _LIBCPP_INLINE_VISIBILITY
4827bool
4828operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4829{
4830 return !(__x < nullptr);
4831}
4832
4833template<class _Tp>
4834inline _LIBCPP_INLINE_VISIBILITY
4835bool
4836operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4837{
4838 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004839}
4840
4841template<class _Tp>
4842inline _LIBCPP_INLINE_VISIBILITY
4843void
Howard Hinnant719bda32011-05-28 14:41:13 +00004844swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004845{
4846 __x.swap(__y);
4847}
4848
4849template<class _Tp, class _Up>
4850inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004851typename enable_if
4852<
4853 !is_array<_Tp>::value && !is_array<_Up>::value,
4854 shared_ptr<_Tp>
4855>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004856static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004857{
4858 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
4859}
4860
4861template<class _Tp, class _Up>
4862inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004863typename enable_if
4864<
4865 !is_array<_Tp>::value && !is_array<_Up>::value,
4866 shared_ptr<_Tp>
4867>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004868dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004869{
4870 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
4871 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
4872}
4873
4874template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004875typename enable_if
4876<
4877 is_array<_Tp>::value == is_array<_Up>::value,
4878 shared_ptr<_Tp>
4879>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004880const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004881{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004882 typedef typename remove_extent<_Tp>::type _RTp;
4883 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004884}
4885
Howard Hinnant72f73582010-08-11 17:04:31 +00004886#ifndef _LIBCPP_NO_RTTI
4887
Howard Hinnantc51e1022010-05-11 19:42:16 +00004888template<class _Dp, class _Tp>
4889inline _LIBCPP_INLINE_VISIBILITY
4890_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00004891get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004892{
4893 return __p.template __get_deleter<_Dp>();
4894}
4895
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004896#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00004897
Howard Hinnantc51e1022010-05-11 19:42:16 +00004898template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004899class _LIBCPP_TEMPLATE_VIS weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004900{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004901public:
4902 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004903private:
4904 element_type* __ptr_;
4905 __shared_weak_count* __cntrl_;
4906
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004907public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004908 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004909 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004910 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004911 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4912 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004913 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004914 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004915 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004916 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4917 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004918
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004919#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004920 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004921 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004922 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004923 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
4924 _NOEXCEPT;
4925#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004926 ~weak_ptr();
4927
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004929 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004930 template<class _Yp>
4931 typename enable_if
4932 <
4933 is_convertible<_Yp*, element_type*>::value,
4934 weak_ptr&
4935 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004936 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004937 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
4938
4939#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4940
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004941 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004942 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
4943 template<class _Yp>
4944 typename enable_if
4945 <
4946 is_convertible<_Yp*, element_type*>::value,
4947 weak_ptr&
4948 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004949 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004950 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
4951
4952#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4953
4954 template<class _Yp>
4955 typename enable_if
4956 <
4957 is_convertible<_Yp*, element_type*>::value,
4958 weak_ptr&
4959 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004960 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004961 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004962
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004963 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004964 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004965 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004966 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004967
Howard Hinnant756c69b2010-09-22 16:48:34 +00004968 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004969 long use_count() const _NOEXCEPT
4970 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004971 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004972 bool expired() const _NOEXCEPT
4973 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
4974 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00004975 template<class _Up>
4976 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004977 bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004978 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004979 template<class _Up>
4980 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00004981 bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004982 {return __cntrl_ < __r.__cntrl_;}
4983
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00004984 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4985 template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004986};
4987
4988template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004989inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004990_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004991weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004992 : __ptr_(0),
4993 __cntrl_(0)
4994{
4995}
4996
4997template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004998inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004999weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005000 : __ptr_(__r.__ptr_),
5001 __cntrl_(__r.__cntrl_)
5002{
5003 if (__cntrl_)
5004 __cntrl_->__add_weak();
5005}
5006
5007template<class _Tp>
5008template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005009inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005010weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005011 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005012 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005013 : __ptr_(__r.__ptr_),
5014 __cntrl_(__r.__cntrl_)
5015{
5016 if (__cntrl_)
5017 __cntrl_->__add_weak();
5018}
5019
5020template<class _Tp>
5021template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005022inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005023weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005024 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005025 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005026 : __ptr_(__r.__ptr_),
5027 __cntrl_(__r.__cntrl_)
5028{
5029 if (__cntrl_)
5030 __cntrl_->__add_weak();
5031}
5032
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005033#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5034
5035template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005036inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005037weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5038 : __ptr_(__r.__ptr_),
5039 __cntrl_(__r.__cntrl_)
5040{
5041 __r.__ptr_ = 0;
5042 __r.__cntrl_ = 0;
5043}
5044
5045template<class _Tp>
5046template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005047inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005048weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5049 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5050 _NOEXCEPT
5051 : __ptr_(__r.__ptr_),
5052 __cntrl_(__r.__cntrl_)
5053{
5054 __r.__ptr_ = 0;
5055 __r.__cntrl_ = 0;
5056}
5057
5058#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5059
Howard Hinnantc51e1022010-05-11 19:42:16 +00005060template<class _Tp>
5061weak_ptr<_Tp>::~weak_ptr()
5062{
5063 if (__cntrl_)
5064 __cntrl_->__release_weak();
5065}
5066
5067template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005068inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005069weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005070weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005071{
5072 weak_ptr(__r).swap(*this);
5073 return *this;
5074}
5075
5076template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005077template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005078inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005079typename enable_if
5080<
5081 is_convertible<_Yp*, _Tp*>::value,
5082 weak_ptr<_Tp>&
5083>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005084weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005085{
5086 weak_ptr(__r).swap(*this);
5087 return *this;
5088}
5089
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005090#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5091
5092template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005093inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005094weak_ptr<_Tp>&
5095weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5096{
5097 weak_ptr(_VSTD::move(__r)).swap(*this);
5098 return *this;
5099}
5100
Howard Hinnantc51e1022010-05-11 19:42:16 +00005101template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005102template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005103inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005104typename enable_if
5105<
5106 is_convertible<_Yp*, _Tp*>::value,
5107 weak_ptr<_Tp>&
5108>::type
5109weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5110{
5111 weak_ptr(_VSTD::move(__r)).swap(*this);
5112 return *this;
5113}
5114
5115#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5116
5117template<class _Tp>
5118template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005119inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005120typename enable_if
5121<
5122 is_convertible<_Yp*, _Tp*>::value,
5123 weak_ptr<_Tp>&
5124>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005125weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005126{
5127 weak_ptr(__r).swap(*this);
5128 return *this;
5129}
5130
5131template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005132inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005133void
Howard Hinnant719bda32011-05-28 14:41:13 +00005134weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005135{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005136 _VSTD::swap(__ptr_, __r.__ptr_);
5137 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005138}
5139
5140template<class _Tp>
5141inline _LIBCPP_INLINE_VISIBILITY
5142void
Howard Hinnant719bda32011-05-28 14:41:13 +00005143swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005144{
5145 __x.swap(__y);
5146}
5147
5148template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005149inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005150void
Howard Hinnant719bda32011-05-28 14:41:13 +00005151weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005152{
5153 weak_ptr().swap(*this);
5154}
5155
5156template<class _Tp>
5157template<class _Yp>
5158shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
Marshall Clow7e384b72017-01-10 16:59:33 +00005159 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005160 : __ptr_(__r.__ptr_),
5161 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5162{
5163 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00005164 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00005165}
5166
5167template<class _Tp>
5168shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005169weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005170{
5171 shared_ptr<_Tp> __r;
5172 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5173 if (__r.__cntrl_)
5174 __r.__ptr_ = __ptr_;
5175 return __r;
5176}
5177
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005178#if _LIBCPP_STD_VER > 14
5179template <class _Tp = void> struct owner_less;
5180#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005181template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005182#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005183
5184template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005185struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005186 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005187{
5188 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005189 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005190 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005191 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005192 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005193 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005194 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005195 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005196 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005197 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005198};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005199
5200template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005201struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005202 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5203{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005204 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005205 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005206 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005207 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005208 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005209 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005210 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005211 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005212 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005213 {return __x.owner_before(__y);}
5214};
5215
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005216#if _LIBCPP_STD_VER > 14
5217template <>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005218struct _LIBCPP_TEMPLATE_VIS owner_less<void>
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005219{
5220 template <class _Tp, class _Up>
5221 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005222 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005223 {return __x.owner_before(__y);}
5224 template <class _Tp, class _Up>
5225 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005226 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005227 {return __x.owner_before(__y);}
5228 template <class _Tp, class _Up>
5229 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005230 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005231 {return __x.owner_before(__y);}
5232 template <class _Tp, class _Up>
5233 _LIBCPP_INLINE_VISIBILITY
Marshall Clow18a7cd52017-04-11 17:08:53 +00005234 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005235 {return __x.owner_before(__y);}
5236 typedef void is_transparent;
5237};
5238#endif
5239
Howard Hinnantc51e1022010-05-11 19:42:16 +00005240template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005241class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005242{
5243 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005244protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005245 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005246 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005248 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005250 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5251 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005252 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005253 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005254public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005255 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005256 shared_ptr<_Tp> shared_from_this()
5257 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005258 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005259 shared_ptr<_Tp const> shared_from_this() const
5260 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005261
Eric Fiselier84006862016-06-02 00:15:35 +00005262#if _LIBCPP_STD_VER > 14
5263 _LIBCPP_INLINE_VISIBILITY
5264 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5265 { return __weak_this_; }
5266
5267 _LIBCPP_INLINE_VISIBILITY
5268 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5269 { return __weak_this_; }
5270#endif // _LIBCPP_STD_VER > 14
5271
Howard Hinnantc51e1022010-05-11 19:42:16 +00005272 template <class _Up> friend class shared_ptr;
5273};
5274
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005275template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00005276struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005277{
5278 typedef shared_ptr<_Tp> argument_type;
5279 typedef size_t result_type;
Eric Fiselier698a97b2017-01-21 00:02:12 +00005280
Howard Hinnant756c69b2010-09-22 16:48:34 +00005281 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005282 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005283 {
5284 return hash<_Tp*>()(__ptr.get());
5285 }
5286};
5287
Howard Hinnantc834c512011-11-29 18:15:50 +00005288template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005289inline _LIBCPP_INLINE_VISIBILITY
5290basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005291operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005292
Eric Fiselier9b492672016-06-18 02:12:53 +00005293
5294#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005295
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005296class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005297{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005298 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005299public:
5300 void lock() _NOEXCEPT;
5301 void unlock() _NOEXCEPT;
5302
5303private:
5304 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5305 __sp_mut(const __sp_mut&);
5306 __sp_mut& operator=(const __sp_mut&);
5307
Howard Hinnant8331b762013-03-06 23:30:19 +00005308 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005309};
5310
Howard Hinnant8331b762013-03-06 23:30:19 +00005311_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005312
5313template <class _Tp>
5314inline _LIBCPP_INLINE_VISIBILITY
5315bool
5316atomic_is_lock_free(const shared_ptr<_Tp>*)
5317{
5318 return false;
5319}
5320
5321template <class _Tp>
5322shared_ptr<_Tp>
5323atomic_load(const shared_ptr<_Tp>* __p)
5324{
5325 __sp_mut& __m = __get_sp_mut(__p);
5326 __m.lock();
5327 shared_ptr<_Tp> __q = *__p;
5328 __m.unlock();
5329 return __q;
5330}
5331
5332template <class _Tp>
5333inline _LIBCPP_INLINE_VISIBILITY
5334shared_ptr<_Tp>
5335atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5336{
5337 return atomic_load(__p);
5338}
5339
5340template <class _Tp>
5341void
5342atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5343{
5344 __sp_mut& __m = __get_sp_mut(__p);
5345 __m.lock();
5346 __p->swap(__r);
5347 __m.unlock();
5348}
5349
5350template <class _Tp>
5351inline _LIBCPP_INLINE_VISIBILITY
5352void
5353atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5354{
5355 atomic_store(__p, __r);
5356}
5357
5358template <class _Tp>
5359shared_ptr<_Tp>
5360atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5361{
5362 __sp_mut& __m = __get_sp_mut(__p);
5363 __m.lock();
5364 __p->swap(__r);
5365 __m.unlock();
5366 return __r;
5367}
5368
5369template <class _Tp>
5370inline _LIBCPP_INLINE_VISIBILITY
5371shared_ptr<_Tp>
5372atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5373{
5374 return atomic_exchange(__p, __r);
5375}
5376
5377template <class _Tp>
5378bool
5379atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5380{
Marshall Clow4201ee82016-05-18 17:50:13 +00005381 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005382 __sp_mut& __m = __get_sp_mut(__p);
5383 __m.lock();
5384 if (__p->__owner_equivalent(*__v))
5385 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005386 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005387 *__p = __w;
5388 __m.unlock();
5389 return true;
5390 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005391 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005392 *__v = *__p;
5393 __m.unlock();
5394 return false;
5395}
5396
5397template <class _Tp>
5398inline _LIBCPP_INLINE_VISIBILITY
5399bool
5400atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5401{
5402 return atomic_compare_exchange_strong(__p, __v, __w);
5403}
5404
5405template <class _Tp>
5406inline _LIBCPP_INLINE_VISIBILITY
5407bool
5408atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5409 shared_ptr<_Tp> __w, memory_order, memory_order)
5410{
5411 return atomic_compare_exchange_strong(__p, __v, __w);
5412}
5413
5414template <class _Tp>
5415inline _LIBCPP_INLINE_VISIBILITY
5416bool
5417atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5418 shared_ptr<_Tp> __w, memory_order, memory_order)
5419{
5420 return atomic_compare_exchange_weak(__p, __v, __w);
5421}
5422
Eric Fiselier9b492672016-06-18 02:12:53 +00005423#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005424
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005425//enum class
Eric Fiselierab6bb302017-01-05 01:15:42 +00005426#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5427# ifndef _LIBCPP_CXX03_LANG
5428enum class pointer_safety : unsigned char {
5429 relaxed,
5430 preferred,
5431 strict
5432};
5433# endif
5434#else
Howard Hinnant8331b762013-03-06 23:30:19 +00005435struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005436{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005437 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005438 {
5439 relaxed,
5440 preferred,
5441 strict
5442 };
5443
Howard Hinnant49e145e2012-10-30 19:06:59 +00005444 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005445
Howard Hinnant756c69b2010-09-22 16:48:34 +00005446 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2fdd22b2017-01-05 01:28:40 +00005447 pointer_safety() : __v_() {}
5448
5449 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005450 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005451 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005452 operator int() const {return __v_;}
5453};
Eric Fiselierab6bb302017-01-05 01:15:42 +00005454#endif
5455
5456#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
5457 defined(_LIBCPP_BUILDING_MEMORY)
5458_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5459#else
5460// This function is only offered in C++03 under ABI v1.
5461# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5462inline _LIBCPP_INLINE_VISIBILITY
5463pointer_safety get_pointer_safety() _NOEXCEPT {
5464 return pointer_safety::relaxed;
5465}
5466# endif
5467#endif
5468
Howard Hinnantc51e1022010-05-11 19:42:16 +00005469
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005470_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5471_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5472_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005473_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005474
5475template <class _Tp>
5476inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005477_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005478undeclare_reachable(_Tp* __p)
5479{
5480 return static_cast<_Tp*>(__undeclare_reachable(__p));
5481}
5482
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005483_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005484
Marshall Clow8982dcd2015-07-13 20:04:56 +00005485// --- Helper for container swap --
5486template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005487inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005488void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5489#if _LIBCPP_STD_VER >= 14
5490 _NOEXCEPT
5491#else
5492 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5493#endif
5494{
5495 __swap_allocator(__a1, __a2,
5496 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5497}
5498
5499template <typename _Alloc>
5500_LIBCPP_INLINE_VISIBILITY
5501void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5502#if _LIBCPP_STD_VER >= 14
5503 _NOEXCEPT
5504#else
5505 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5506#endif
5507{
5508 using _VSTD::swap;
5509 swap(__a1, __a2);
5510}
5511
5512template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005513inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005514void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5515
Marshall Clowff91de82015-08-18 19:51:37 +00005516template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005517struct __noexcept_move_assign_container : public integral_constant<bool,
5518 _Traits::propagate_on_container_move_assignment::value
5519#if _LIBCPP_STD_VER > 14
5520 || _Traits::is_always_equal::value
5521#else
5522 && is_nothrow_move_assignable<_Alloc>::value
5523#endif
5524 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005525
Marshall Clowa591b9a2016-07-11 21:38:08 +00005526
5527#ifndef _LIBCPP_HAS_NO_VARIADICS
5528template <class _Tp, class _Alloc>
5529struct __temp_value {
5530 typedef allocator_traits<_Alloc> _Traits;
5531
5532 typename aligned_storage<sizeof(_Tp), alignof(_Tp)>::type __v;
5533 _Alloc &__a;
5534
5535 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5536 _Tp & get() { return *__addr(); }
5537
5538 template<class... _Args>
5539 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc)
5540 { _Traits::construct(__a, __addr(), _VSTD::forward<_Args>(__args)...); }
5541
5542 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5543 };
5544#endif
5545
Howard Hinnantc51e1022010-05-11 19:42:16 +00005546_LIBCPP_END_NAMESPACE_STD
5547
5548#endif // _LIBCPP_MEMORY