blob: 24361cc8e7f1f8cb9545a94b281a5ad38d3bcd9d [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- memory ------------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_MEMORY
12#define _LIBCPP_MEMORY
13
14/*
15 memory synopsis
16
17namespace std
18{
19
20struct allocator_arg_t { };
21constexpr allocator_arg_t allocator_arg = allocator_arg_t();
22
23template <class T, class Alloc> struct uses_allocator;
24
25template <class Ptr>
26struct pointer_traits
27{
28 typedef Ptr pointer;
29 typedef <details> element_type;
30 typedef <details> difference_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000031
Howard Hinnantc51e1022010-05-11 19:42:16 +000032 template <class U> using rebind = <details>;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000033
Howard Hinnantc51e1022010-05-11 19:42:16 +000034 static pointer pointer_to(<details>);
35};
36
Howard Hinnant719bda32011-05-28 14:41:13 +000037template <class T>
38struct pointer_traits<T*>
39{
40 typedef T* pointer;
41 typedef T element_type;
42 typedef ptrdiff_t difference_type;
43
44 template <class U> using rebind = U*;
45
46 static pointer pointer_to(<details>) noexcept;
47};
48
Howard Hinnantc51e1022010-05-11 19:42:16 +000049template <class Alloc>
50struct allocator_traits
51{
52 typedef Alloc allocator_type;
53 typedef typename allocator_type::value_type
54 value_type;
55
56 typedef Alloc::pointer | value_type* pointer;
57 typedef Alloc::const_pointer
58 | pointer_traits<pointer>::rebind<const value_type>
59 const_pointer;
60 typedef Alloc::void_pointer
61 | pointer_traits<pointer>::rebind<void>
62 void_pointer;
63 typedef Alloc::const_void_pointer
64 | pointer_traits<pointer>::rebind<const void>
65 const_void_pointer;
66 typedef Alloc::difference_type
Howard Hinnant8e4e6002010-11-18 01:40:00 +000067 | pointer_traits<pointer>::difference_type
68 difference_type;
69 typedef Alloc::size_type
70 | make_unsigned<difference_type>::type
71 size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000072 typedef Alloc::propagate_on_container_copy_assignment
73 | false_type propagate_on_container_copy_assignment;
74 typedef Alloc::propagate_on_container_move_assignment
75 | false_type propagate_on_container_move_assignment;
76 typedef Alloc::propagate_on_container_swap
77 | false_type propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +000078 typedef Alloc::is_always_equal
79 | is_empty is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +000080
81 template <class T> using rebind_alloc = Alloc::rebind<U>::other | Alloc<T, Args...>;
82 template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
83
84 static pointer allocate(allocator_type& a, size_type n);
85 static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint);
86
Howard Hinnant719bda32011-05-28 14:41:13 +000087 static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
89 template <class T, class... Args>
90 static void construct(allocator_type& a, T* p, Args&&... args);
91
92 template <class T>
93 static void destroy(allocator_type& a, T* p);
94
Marshall Clow4f834b52013-08-27 20:22:15 +000095 static size_type max_size(const allocator_type& a); // noexcept in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000096
97 static allocator_type
98 select_on_container_copy_construction(const allocator_type& a);
99};
100
101template <>
102class allocator<void>
103{
104public:
105 typedef void* pointer;
106 typedef const void* const_pointer;
107 typedef void value_type;
108
109 template <class _Up> struct rebind {typedef allocator<_Up> other;};
110};
111
112template <class T>
113class allocator
114{
115public:
116 typedef size_t size_type;
117 typedef ptrdiff_t difference_type;
118 typedef T* pointer;
119 typedef const T* const_pointer;
120 typedef typename add_lvalue_reference<T>::type reference;
121 typedef typename add_lvalue_reference<const T>::type const_reference;
122 typedef T value_type;
123
124 template <class U> struct rebind {typedef allocator<U> other;};
125
Howard Hinnant719bda32011-05-28 14:41:13 +0000126 allocator() noexcept;
127 allocator(const allocator&) noexcept;
128 template <class U> allocator(const allocator<U>&) noexcept;
129 ~allocator();
130 pointer address(reference x) const noexcept;
131 const_pointer address(const_reference x) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132 pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
Howard Hinnant719bda32011-05-28 14:41:13 +0000133 void deallocate(pointer p, size_type n) noexcept;
134 size_type max_size() const noexcept;
135 template<class U, class... Args>
136 void construct(U* p, Args&&... args);
137 template <class U>
138 void destroy(U* p);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000139};
140
141template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000142bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000143
144template <class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000145bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000146
147template <class OutputIterator, class T>
148class raw_storage_iterator
149 : public iterator<output_iterator_tag,
150 T, // purposefully not C++03
151 ptrdiff_t, // purposefully not C++03
152 T*, // purposefully not C++03
153 raw_storage_iterator&> // purposefully not C++03
154{
155public:
156 explicit raw_storage_iterator(OutputIterator x);
157 raw_storage_iterator& operator*();
158 raw_storage_iterator& operator=(const T& element);
159 raw_storage_iterator& operator++();
160 raw_storage_iterator operator++(int);
161};
162
Howard Hinnant719bda32011-05-28 14:41:13 +0000163template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
164template <class T> void return_temporary_buffer(T* p) noexcept;
165
166template <class T> T* addressof(T& r) noexcept;
Marshall Clow78dbe462016-11-14 18:22:19 +0000167template <class T> T* addressof(const T&& r) noexcept = delete;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000168
169template <class InputIterator, class ForwardIterator>
170ForwardIterator
171uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
172
Howard Hinnant719bda32011-05-28 14:41:13 +0000173template <class InputIterator, class Size, class ForwardIterator>
174ForwardIterator
175uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
176
Howard Hinnantc51e1022010-05-11 19:42:16 +0000177template <class ForwardIterator, class T>
178void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
179
180template <class ForwardIterator, class Size, class T>
Howard Hinnant3c811092010-11-18 16:13:03 +0000181ForwardIterator
182uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000183
Eric Fiselier383f6cf2016-07-24 03:51:39 +0000184template <class T>
185void destroy_at(T* location);
186
187template <class ForwardIterator>
188 void destroy(ForwardIterator first, ForwardIterator last);
189
190template <class ForwardIterator, class Size>
191 ForwardIterator destroy_n(ForwardIterator first, Size n);
192
193template <class InputIterator, class ForwardIterator>
194 ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
195
196template <class InputIterator, class Size, class ForwardIterator>
197 pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
198
199template <class ForwardIterator>
200 void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
201
202template <class ForwardIterator, class Size>
203 ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
204
205template <class ForwardIterator>
206 void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
207
208template <class ForwardIterator, class Size>
209 ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
210
Howard Hinnantc51e1022010-05-11 19:42:16 +0000211template <class Y> struct auto_ptr_ref {};
212
213template<class X>
214class auto_ptr
215{
216public:
217 typedef X element_type;
218
219 explicit auto_ptr(X* p =0) throw();
220 auto_ptr(auto_ptr&) throw();
221 template<class Y> auto_ptr(auto_ptr<Y>&) throw();
222 auto_ptr& operator=(auto_ptr&) throw();
223 template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
224 auto_ptr& operator=(auto_ptr_ref<X> r) throw();
225 ~auto_ptr() throw();
226
227 typename add_lvalue_reference<X>::type operator*() const throw();
228 X* operator->() const throw();
229 X* get() const throw();
230 X* release() throw();
231 void reset(X* p =0) throw();
232
233 auto_ptr(auto_ptr_ref<X>) throw();
234 template<class Y> operator auto_ptr_ref<Y>() throw();
235 template<class Y> operator auto_ptr<Y>() throw();
236};
237
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000238template <class T>
239struct default_delete
240{
Howard Hinnant719bda32011-05-28 14:41:13 +0000241 constexpr default_delete() noexcept = default;
242 template <class U> default_delete(const default_delete<U>&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000243
Howard Hinnant719bda32011-05-28 14:41:13 +0000244 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000245};
246
247template <class T>
248struct default_delete<T[]>
249{
Howard Hinnant719bda32011-05-28 14:41:13 +0000250 constexpr default_delete() noexcept = default;
251 void operator()(T*) const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000252 template <class U> void operator()(U*) const = delete;
253};
254
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000255template <class T, class D = default_delete<T>>
256class unique_ptr
257{
258public:
259 typedef see below pointer;
260 typedef T element_type;
261 typedef D deleter_type;
262
263 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000264 constexpr unique_ptr() noexcept;
265 explicit unique_ptr(pointer p) noexcept;
266 unique_ptr(pointer p, see below d1) noexcept;
267 unique_ptr(pointer p, see below d2) noexcept;
268 unique_ptr(unique_ptr&& u) noexcept;
269 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000270 template <class U, class E>
Howard Hinnant719bda32011-05-28 14:41:13 +0000271 unique_ptr(unique_ptr<U, E>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000272 template <class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000273 unique_ptr(auto_ptr<U>&& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000274
275 // destructor
276 ~unique_ptr();
277
278 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000279 unique_ptr& operator=(unique_ptr&& u) noexcept;
280 template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
281 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000282
283 // observers
284 typename add_lvalue_reference<T>::type operator*() const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000285 pointer operator->() const noexcept;
286 pointer get() const noexcept;
287 deleter_type& get_deleter() noexcept;
288 const deleter_type& get_deleter() const noexcept;
289 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000290
291 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000292 pointer release() noexcept;
293 void reset(pointer p = pointer()) noexcept;
294 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000295};
296
297template <class T, class D>
298class unique_ptr<T[], D>
299{
300public:
301 typedef implementation-defined pointer;
302 typedef T element_type;
303 typedef D deleter_type;
304
305 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000306 constexpr unique_ptr() noexcept;
307 explicit unique_ptr(pointer p) noexcept;
308 unique_ptr(pointer p, see below d) noexcept;
309 unique_ptr(pointer p, see below d) noexcept;
310 unique_ptr(unique_ptr&& u) noexcept;
311 unique_ptr(nullptr_t) noexcept : unique_ptr() { }
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000312
313 // destructor
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000314 ~unique_ptr();
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000315
316 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000317 unique_ptr& operator=(unique_ptr&& u) noexcept;
318 unique_ptr& operator=(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000319
320 // observers
321 T& operator[](size_t i) const;
Howard Hinnant719bda32011-05-28 14:41:13 +0000322 pointer get() const noexcept;
323 deleter_type& get_deleter() noexcept;
324 const deleter_type& get_deleter() const noexcept;
325 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000326
327 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000328 pointer release() noexcept;
329 void reset(pointer p = pointer()) noexcept;
330 void reset(nullptr_t) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000331 template <class U> void reset(U) = delete;
Howard Hinnant719bda32011-05-28 14:41:13 +0000332 void swap(unique_ptr& u) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000333};
334
335template <class T, class D>
Howard Hinnant719bda32011-05-28 14:41:13 +0000336 void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000337
338template <class T1, class D1, class T2, class D2>
339 bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
340template <class T1, class D1, class T2, class D2>
341 bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
342template <class T1, class D1, class T2, class D2>
343 bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
344template <class T1, class D1, class T2, class D2>
345 bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
346template <class T1, class D1, class T2, class D2>
347 bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
348template <class T1, class D1, class T2, class D2>
349 bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
350
Howard Hinnant719bda32011-05-28 14:41:13 +0000351template <class T, class D>
352 bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
353template <class T, class D>
354 bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
355template <class T, class D>
356 bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
357template <class T, class D>
358 bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
359
360template <class T, class D>
361 bool operator<(const unique_ptr<T, D>& x, nullptr_t);
362template <class T, class D>
363 bool operator<(nullptr_t, const unique_ptr<T, D>& y);
364template <class T, class D>
365 bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
366template <class T, class D>
367 bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
368template <class T, class D>
369 bool operator>(const unique_ptr<T, D>& x, nullptr_t);
370template <class T, class D>
371 bool operator>(nullptr_t, const unique_ptr<T, D>& y);
372template <class T, class D>
373 bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
374template <class T, class D>
375 bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
376
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000377class bad_weak_ptr
378 : public std::exception
379{
Howard Hinnant719bda32011-05-28 14:41:13 +0000380 bad_weak_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000381};
382
Marshall Clow9e8f4a92013-07-01 18:16:03 +0000383template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
384template<class T> unique_ptr<T> make_unique(size_t n); // C++14
385template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
386
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000387template<class T>
388class shared_ptr
389{
390public:
391 typedef T element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +0000392 typedef weak_ptr<T> weak_type; // C++17
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000393
394 // constructors:
Howard Hinnant719bda32011-05-28 14:41:13 +0000395 constexpr shared_ptr() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000396 template<class Y> explicit shared_ptr(Y* p);
397 template<class Y, class D> shared_ptr(Y* p, D d);
398 template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
399 template <class D> shared_ptr(nullptr_t p, D d);
400 template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
Howard Hinnant719bda32011-05-28 14:41:13 +0000401 template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
402 shared_ptr(const shared_ptr& r) noexcept;
403 template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
404 shared_ptr(shared_ptr&& r) noexcept;
405 template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000406 template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
407 template<class Y> shared_ptr(auto_ptr<Y>&& r);
408 template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
409 shared_ptr(nullptr_t) : shared_ptr() { }
410
411 // destructor:
412 ~shared_ptr();
413
414 // assignment:
Howard Hinnant719bda32011-05-28 14:41:13 +0000415 shared_ptr& operator=(const shared_ptr& r) noexcept;
416 template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
417 shared_ptr& operator=(shared_ptr&& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000418 template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
419 template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r);
420 template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
421
422 // modifiers:
Howard Hinnant719bda32011-05-28 14:41:13 +0000423 void swap(shared_ptr& r) noexcept;
424 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000425 template<class Y> void reset(Y* p);
426 template<class Y, class D> void reset(Y* p, D d);
427 template<class Y, class D, class A> void reset(Y* p, D d, A a);
428
Howard Hinnant719bda32011-05-28 14:41:13 +0000429 // observers:
430 T* get() const noexcept;
431 T& operator*() const noexcept;
432 T* operator->() const noexcept;
433 long use_count() const noexcept;
434 bool unique() const noexcept;
435 explicit operator bool() const noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000436 template<class U> bool owner_before(shared_ptr<U> const& b) const;
437 template<class U> bool owner_before(weak_ptr<U> const& b) const;
438};
439
440// shared_ptr comparisons:
441template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000442 bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000443template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000444 bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000445template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000446 bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000447template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000448 bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000449template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000450 bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000451template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000452 bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
453
454template <class T>
455 bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
456template <class T>
457 bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
458template <class T>
459 bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
460template <class T>
461 bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
462template <class T>
463 bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
464template <class T>
465bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
466template <class T>
467 bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
468template <class T>
469 bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
470template <class T>
471 bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
472template <class T>
473 bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
474template <class T>
475 bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
476template <class T>
477 bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000478
479// shared_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000480template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000481
482// shared_ptr casts:
483template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000484 shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000485template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000486 shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000487template<class T, class U>
Howard Hinnant719bda32011-05-28 14:41:13 +0000488 shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000489
490// shared_ptr I/O:
491template<class E, class T, class Y>
492 basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
493
494// shared_ptr get_deleter:
Howard Hinnant719bda32011-05-28 14:41:13 +0000495template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000496
497template<class T, class... Args>
498 shared_ptr<T> make_shared(Args&&... args);
499template<class T, class A, class... Args>
500 shared_ptr<T> allocate_shared(const A& a, Args&&... args);
501
502template<class T>
503class weak_ptr
504{
505public:
506 typedef T element_type;
507
508 // constructors
Howard Hinnant719bda32011-05-28 14:41:13 +0000509 constexpr weak_ptr() noexcept;
510 template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
511 weak_ptr(weak_ptr const& r) noexcept;
512 template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000513 weak_ptr(weak_ptr&& r) noexcept; // C++14
514 template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000515
516 // destructor
517 ~weak_ptr();
518
519 // assignment
Howard Hinnant719bda32011-05-28 14:41:13 +0000520 weak_ptr& operator=(weak_ptr const& r) noexcept;
521 template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
522 template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
Marshall Clowe9b0a5c2014-03-05 03:12:04 +0000523 weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
524 template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000525
526 // modifiers
Howard Hinnant719bda32011-05-28 14:41:13 +0000527 void swap(weak_ptr& r) noexcept;
528 void reset() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000529
530 // observers
Howard Hinnant719bda32011-05-28 14:41:13 +0000531 long use_count() const noexcept;
532 bool expired() const noexcept;
533 shared_ptr<T> lock() const noexcept;
Marshall Clow495e4a62013-09-03 14:37:50 +0000534 template<class U> bool owner_before(shared_ptr<U> const& b) const;
535 template<class U> bool owner_before(weak_ptr<U> const& b) const;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000536};
537
538// weak_ptr specialized algorithms:
Howard Hinnant719bda32011-05-28 14:41:13 +0000539template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000540
541// class owner_less:
542template<class T> struct owner_less;
543
544template<class T>
545struct owner_less<shared_ptr<T>>
546 : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
547{
548 typedef bool result_type;
549 bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const;
550 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
551 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
552};
553
554template<class T>
555struct owner_less<weak_ptr<T>>
556 : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
557{
558 typedef bool result_type;
559 bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const;
560 bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const;
561 bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const;
562};
563
564template<class T>
565class enable_shared_from_this
566{
567protected:
Howard Hinnant719bda32011-05-28 14:41:13 +0000568 constexpr enable_shared_from_this() noexcept;
569 enable_shared_from_this(enable_shared_from_this const&) noexcept;
570 enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000571 ~enable_shared_from_this();
572public:
573 shared_ptr<T> shared_from_this();
574 shared_ptr<T const> shared_from_this() const;
575};
576
577template<class T>
578 bool atomic_is_lock_free(const shared_ptr<T>* p);
579template<class T>
580 shared_ptr<T> atomic_load(const shared_ptr<T>* p);
581template<class T>
582 shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
583template<class T>
584 void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
585template<class T>
586 void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
587template<class T>
588 shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
589template<class T>
590 shared_ptr<T>
591 atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
592template<class T>
593 bool
594 atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
595template<class T>
596 bool
597 atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
598template<class T>
599 bool
600 atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
601 shared_ptr<T> w, memory_order success,
602 memory_order failure);
603template<class T>
604 bool
605 atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
606 shared_ptr<T> w, memory_order success,
607 memory_order failure);
608// Hash support
609template <class T> struct hash;
610template <class T, class D> struct hash<unique_ptr<T, D> >;
611template <class T> struct hash<shared_ptr<T> >;
612
613// Pointer safety
614enum class pointer_safety { relaxed, preferred, strict };
615void declare_reachable(void *p);
616template <class T> T *undeclare_reachable(T *p);
617void declare_no_pointers(char *p, size_t n);
618void undeclare_no_pointers(char *p, size_t n);
Howard Hinnant719bda32011-05-28 14:41:13 +0000619pointer_safety get_pointer_safety() noexcept;
Howard Hinnantd26c01d2010-08-19 18:39:17 +0000620
Howard Hinnantc51e1022010-05-11 19:42:16 +0000621void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
622
623} // std
624
625*/
626
627#include <__config>
628#include <type_traits>
629#include <typeinfo>
630#include <cstddef>
631#include <cstdint>
632#include <new>
633#include <utility>
634#include <limits>
635#include <iterator>
636#include <__functional_base>
Howard Hinnantdc095972011-07-18 15:51:59 +0000637#include <iosfwd>
Howard Hinnant83b1c052011-12-19 17:58:44 +0000638#include <tuple>
Eric Fiselier2db2bd52016-05-07 03:12:24 +0000639#include <stdexcept>
Howard Hinnantd2cab2f2012-02-15 00:41:34 +0000640#include <cstring>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000641
Eric Fiselier8020b6c2015-08-19 17:21:46 +0000642#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +0000643# include <atomic>
644#endif
645
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000646#include <__undef_min_max>
Saleem Abdulrasooldcf27a62015-02-13 22:15:32 +0000647#include <__undef___deallocate>
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000648
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000649#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000650#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000651#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000652
653_LIBCPP_BEGIN_NAMESPACE_STD
654
Eric Fiselier89659d12015-07-07 00:27:16 +0000655template <class _ValueType>
656inline _LIBCPP_ALWAYS_INLINE
657_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
658#if !defined(_LIBCPP_HAS_NO_THREADS) && \
659 defined(__ATOMIC_RELAXED) && \
660 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
661 return __atomic_load_n(__value, __ATOMIC_RELAXED);
662#else
663 return *__value;
664#endif
665}
666
Kuba Breckade9d6792016-09-04 09:55:12 +0000667template <class _ValueType>
668inline _LIBCPP_ALWAYS_INLINE
669_ValueType __libcpp_acquire_load(_ValueType const* __value) {
670#if !defined(_LIBCPP_HAS_NO_THREADS) && \
671 defined(__ATOMIC_ACQUIRE) && \
672 (__has_builtin(__atomic_load_n) || _GNUC_VER >= 407)
673 return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
674#else
675 return *__value;
676#endif
677}
678
Marshall Clow78dbe462016-11-14 18:22:19 +0000679// addressof moved to <type_traits>
Douglas Gregor13034442011-06-22 22:17:44 +0000680
Howard Hinnantc51e1022010-05-11 19:42:16 +0000681template <class _Tp> class allocator;
682
683template <>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000684class _LIBCPP_TYPE_VIS_ONLY allocator<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000685{
686public:
687 typedef void* pointer;
688 typedef const void* const_pointer;
689 typedef void value_type;
690
691 template <class _Up> struct rebind {typedef allocator<_Up> other;};
692};
693
Howard Hinnant9f771052012-01-19 23:15:22 +0000694template <>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000695class _LIBCPP_TYPE_VIS_ONLY allocator<const void>
Howard Hinnant9f771052012-01-19 23:15:22 +0000696{
697public:
698 typedef const void* pointer;
699 typedef const void* const_pointer;
700 typedef const void value_type;
701
702 template <class _Up> struct rebind {typedef allocator<_Up> other;};
703};
704
Howard Hinnantc51e1022010-05-11 19:42:16 +0000705// pointer_traits
706
707template <class _Tp>
708struct __has_element_type
709{
710private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000711 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000712 template <class _Up> static __two __test(...);
713 template <class _Up> static char __test(typename _Up::element_type* = 0);
714public:
715 static const bool value = sizeof(__test<_Tp>(0)) == 1;
716};
717
718template <class _Ptr, bool = __has_element_type<_Ptr>::value>
719struct __pointer_traits_element_type;
720
721template <class _Ptr>
722struct __pointer_traits_element_type<_Ptr, true>
723{
724 typedef typename _Ptr::element_type type;
725};
726
727#ifndef _LIBCPP_HAS_NO_VARIADICS
728
729template <template <class, class...> class _Sp, class _Tp, class ..._Args>
730struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
731{
732 typedef typename _Sp<_Tp, _Args...>::element_type type;
733};
734
735template <template <class, class...> class _Sp, class _Tp, class ..._Args>
736struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
737{
738 typedef _Tp type;
739};
740
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000741#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000742
743template <template <class> class _Sp, class _Tp>
744struct __pointer_traits_element_type<_Sp<_Tp>, true>
745{
746 typedef typename _Sp<_Tp>::element_type type;
747};
748
749template <template <class> class _Sp, class _Tp>
750struct __pointer_traits_element_type<_Sp<_Tp>, false>
751{
752 typedef _Tp type;
753};
754
755template <template <class, class> class _Sp, class _Tp, class _A0>
756struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
757{
758 typedef typename _Sp<_Tp, _A0>::element_type type;
759};
760
761template <template <class, class> class _Sp, class _Tp, class _A0>
762struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
763{
764 typedef _Tp type;
765};
766
767template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
768struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
769{
770 typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
771};
772
773template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
774struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
775{
776 typedef _Tp type;
777};
778
779template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
780 class _A1, class _A2>
781struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
782{
783 typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
784};
785
786template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
787 class _A1, class _A2>
788struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
789{
790 typedef _Tp type;
791};
792
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000793#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000794
795template <class _Tp>
796struct __has_difference_type
797{
798private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000799 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000800 template <class _Up> static __two __test(...);
801 template <class _Up> static char __test(typename _Up::difference_type* = 0);
802public:
803 static const bool value = sizeof(__test<_Tp>(0)) == 1;
804};
805
806template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
807struct __pointer_traits_difference_type
808{
809 typedef ptrdiff_t type;
810};
811
812template <class _Ptr>
813struct __pointer_traits_difference_type<_Ptr, true>
814{
815 typedef typename _Ptr::difference_type type;
816};
817
818template <class _Tp, class _Up>
819struct __has_rebind
820{
821private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000822 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000823 template <class _Xp> static __two __test(...);
824 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
825public:
826 static const bool value = sizeof(__test<_Tp>(0)) == 1;
827};
828
829template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
830struct __pointer_traits_rebind
831{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000832#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000833 typedef typename _Tp::template rebind<_Up> type;
834#else
835 typedef typename _Tp::template rebind<_Up>::other type;
836#endif
837};
838
839#ifndef _LIBCPP_HAS_NO_VARIADICS
840
841template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
842struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
843{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000844#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000845 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
846#else
847 typedef typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
848#endif
849};
850
851template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
852struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
853{
854 typedef _Sp<_Up, _Args...> type;
855};
856
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000857#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000858
859template <template <class> class _Sp, class _Tp, class _Up>
860struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
861{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000862#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000863 typedef typename _Sp<_Tp>::template rebind<_Up> type;
864#else
865 typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
866#endif
867};
868
869template <template <class> class _Sp, class _Tp, class _Up>
870struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
871{
872 typedef _Sp<_Up> type;
873};
874
875template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
876struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
877{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000878#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000879 typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
880#else
881 typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
882#endif
883};
884
885template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
886struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
887{
888 typedef _Sp<_Up, _A0> type;
889};
890
891template <template <class, class, class> class _Sp, class _Tp, class _A0,
892 class _A1, class _Up>
893struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
894{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000895#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000896 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
897#else
898 typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
899#endif
900};
901
902template <template <class, class, class> class _Sp, class _Tp, class _A0,
903 class _A1, class _Up>
904struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
905{
906 typedef _Sp<_Up, _A0, _A1> type;
907};
908
909template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
910 class _A1, class _A2, class _Up>
911struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
912{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000913#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
915#else
916 typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
917#endif
918};
919
920template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
921 class _A1, class _A2, class _Up>
922struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
923{
924 typedef _Sp<_Up, _A0, _A1, _A2> type;
925};
926
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000927#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928
929template <class _Ptr>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000930struct _LIBCPP_TYPE_VIS_ONLY pointer_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +0000931{
932 typedef _Ptr pointer;
933 typedef typename __pointer_traits_element_type<pointer>::type element_type;
934 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
935
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000936#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantf09283d2011-05-11 20:21:19 +0000937 template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000938#else
939 template <class _Up> struct rebind
940 {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000941#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942
943private:
944 struct __nat {};
945public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000946 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947 static pointer pointer_to(typename conditional<is_void<element_type>::value,
948 __nat, element_type>::type& __r)
949 {return pointer::pointer_to(__r);}
950};
951
952template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000953struct _LIBCPP_TYPE_VIS_ONLY pointer_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000954{
955 typedef _Tp* pointer;
956 typedef _Tp element_type;
957 typedef ptrdiff_t difference_type;
958
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000959#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000960 template <class _Up> using rebind = _Up*;
961#else
962 template <class _Up> struct rebind {typedef _Up* other;};
963#endif
964
965private:
966 struct __nat {};
967public:
Howard Hinnant756c69b2010-09-22 16:48:34 +0000968 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000969 static pointer pointer_to(typename conditional<is_void<element_type>::value,
Howard Hinnant719bda32011-05-28 14:41:13 +0000970 __nat, element_type>::type& __r) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000971 {return _VSTD::addressof(__r);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972};
973
Eric Fiselierae3ab842015-08-23 02:56:05 +0000974template <class _From, class _To>
975struct __rebind_pointer {
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000976#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierae3ab842015-08-23 02:56:05 +0000977 typedef typename pointer_traits<_From>::template rebind<_To> type;
978#else
979 typedef typename pointer_traits<_From>::template rebind<_To>::other type;
980#endif
981};
982
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983// allocator_traits
984
985namespace __has_pointer_type_imp
986{
Howard Hinnant6e260172013-09-21 01:45:05 +0000987 template <class _Up> static __two __test(...);
988 template <class _Up> static char __test(typename _Up::pointer* = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000989}
990
991template <class _Tp>
992struct __has_pointer_type
Howard Hinnant6e260172013-09-21 01:45:05 +0000993 : public integral_constant<bool, sizeof(__has_pointer_type_imp::__test<_Tp>(0)) == 1>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000994{
995};
996
997namespace __pointer_type_imp
998{
999
1000template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
1001struct __pointer_type
1002{
1003 typedef typename _Dp::pointer type;
1004};
1005
1006template <class _Tp, class _Dp>
1007struct __pointer_type<_Tp, _Dp, false>
1008{
1009 typedef _Tp* type;
1010};
1011
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001012} // __pointer_type_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013
1014template <class _Tp, class _Dp>
1015struct __pointer_type
1016{
1017 typedef typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
1018};
1019
1020template <class _Tp>
1021struct __has_const_pointer
1022{
1023private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001024 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025 template <class _Up> static __two __test(...);
1026 template <class _Up> static char __test(typename _Up::const_pointer* = 0);
1027public:
1028 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1029};
1030
1031template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
1032struct __const_pointer
1033{
1034 typedef typename _Alloc::const_pointer type;
1035};
1036
1037template <class _Tp, class _Ptr, class _Alloc>
1038struct __const_pointer<_Tp, _Ptr, _Alloc, false>
1039{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001040#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
1042#else
1043 typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
1044#endif
1045};
1046
1047template <class _Tp>
1048struct __has_void_pointer
1049{
1050private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001051 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001052 template <class _Up> static __two __test(...);
1053 template <class _Up> static char __test(typename _Up::void_pointer* = 0);
1054public:
1055 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1056};
1057
1058template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
1059struct __void_pointer
1060{
1061 typedef typename _Alloc::void_pointer type;
1062};
1063
1064template <class _Ptr, class _Alloc>
1065struct __void_pointer<_Ptr, _Alloc, false>
1066{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001067#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068 typedef typename pointer_traits<_Ptr>::template rebind<void> type;
1069#else
1070 typedef typename pointer_traits<_Ptr>::template rebind<void>::other type;
1071#endif
1072};
1073
1074template <class _Tp>
1075struct __has_const_void_pointer
1076{
1077private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001078 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079 template <class _Up> static __two __test(...);
1080 template <class _Up> static char __test(typename _Up::const_void_pointer* = 0);
1081public:
1082 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1083};
1084
1085template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
1086struct __const_void_pointer
1087{
1088 typedef typename _Alloc::const_void_pointer type;
1089};
1090
1091template <class _Ptr, class _Alloc>
1092struct __const_void_pointer<_Ptr, _Alloc, false>
1093{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001094#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095 typedef typename pointer_traits<_Ptr>::template rebind<const void> type;
1096#else
1097 typedef typename pointer_traits<_Ptr>::template rebind<const void>::other type;
1098#endif
1099};
1100
Howard Hinnantc834c512011-11-29 18:15:50 +00001101template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001102inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001103_Tp*
1104__to_raw_pointer(_Tp* __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001105{
1106 return __p;
1107}
1108
1109template <class _Pointer>
1110inline _LIBCPP_INLINE_VISIBILITY
1111typename pointer_traits<_Pointer>::element_type*
Howard Hinnant719bda32011-05-28 14:41:13 +00001112__to_raw_pointer(_Pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001113{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001114 return _VSTD::__to_raw_pointer(__p.operator->());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115}
1116
1117template <class _Tp>
1118struct __has_size_type
1119{
1120private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001121 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122 template <class _Up> static __two __test(...);
1123 template <class _Up> static char __test(typename _Up::size_type* = 0);
1124public:
1125 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1126};
1127
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001128template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129struct __size_type
1130{
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001131 typedef typename make_unsigned<_DiffType>::type type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132};
1133
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001134template <class _Alloc, class _DiffType>
1135struct __size_type<_Alloc, _DiffType, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136{
1137 typedef typename _Alloc::size_type type;
1138};
1139
1140template <class _Tp>
1141struct __has_propagate_on_container_copy_assignment
1142{
1143private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001144 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001145 template <class _Up> static __two __test(...);
1146 template <class _Up> static char __test(typename _Up::propagate_on_container_copy_assignment* = 0);
1147public:
1148 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1149};
1150
1151template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
1152struct __propagate_on_container_copy_assignment
1153{
1154 typedef false_type type;
1155};
1156
1157template <class _Alloc>
1158struct __propagate_on_container_copy_assignment<_Alloc, true>
1159{
1160 typedef typename _Alloc::propagate_on_container_copy_assignment type;
1161};
1162
1163template <class _Tp>
1164struct __has_propagate_on_container_move_assignment
1165{
1166private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001167 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168 template <class _Up> static __two __test(...);
1169 template <class _Up> static char __test(typename _Up::propagate_on_container_move_assignment* = 0);
1170public:
1171 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1172};
1173
1174template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
1175struct __propagate_on_container_move_assignment
1176{
1177 typedef false_type type;
1178};
1179
1180template <class _Alloc>
1181struct __propagate_on_container_move_assignment<_Alloc, true>
1182{
1183 typedef typename _Alloc::propagate_on_container_move_assignment type;
1184};
1185
1186template <class _Tp>
1187struct __has_propagate_on_container_swap
1188{
1189private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001190 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191 template <class _Up> static __two __test(...);
1192 template <class _Up> static char __test(typename _Up::propagate_on_container_swap* = 0);
1193public:
1194 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1195};
1196
1197template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
1198struct __propagate_on_container_swap
1199{
1200 typedef false_type type;
1201};
1202
1203template <class _Alloc>
1204struct __propagate_on_container_swap<_Alloc, true>
1205{
1206 typedef typename _Alloc::propagate_on_container_swap type;
1207};
1208
Marshall Clow0b587562015-06-02 16:34:03 +00001209template <class _Tp>
1210struct __has_is_always_equal
1211{
1212private:
1213 struct __two {char __lx; char __lxx;};
1214 template <class _Up> static __two __test(...);
1215 template <class _Up> static char __test(typename _Up::is_always_equal* = 0);
1216public:
1217 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1218};
1219
1220template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
1221struct __is_always_equal
1222{
1223 typedef typename _VSTD::is_empty<_Alloc>::type type;
1224};
1225
1226template <class _Alloc>
1227struct __is_always_equal<_Alloc, true>
1228{
1229 typedef typename _Alloc::is_always_equal type;
1230};
1231
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
1233struct __has_rebind_other
1234{
1235private:
Howard Hinnant49e145e2012-10-30 19:06:59 +00001236 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237 template <class _Xp> static __two __test(...);
1238 template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
1239public:
1240 static const bool value = sizeof(__test<_Tp>(0)) == 1;
1241};
1242
1243template <class _Tp, class _Up>
1244struct __has_rebind_other<_Tp, _Up, false>
1245{
1246 static const bool value = false;
1247};
1248
1249template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
1250struct __allocator_traits_rebind
1251{
1252 typedef typename _Tp::template rebind<_Up>::other type;
1253};
1254
1255#ifndef _LIBCPP_HAS_NO_VARIADICS
1256
1257template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1258struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
1259{
1260 typedef typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
1261};
1262
1263template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
1264struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
1265{
1266 typedef _Alloc<_Up, _Args...> type;
1267};
1268
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001269#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270
1271template <template <class> class _Alloc, class _Tp, class _Up>
1272struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
1273{
1274 typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
1275};
1276
1277template <template <class> class _Alloc, class _Tp, class _Up>
1278struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
1279{
1280 typedef _Alloc<_Up> type;
1281};
1282
Howard Hinnantc51e1022010-05-11 19:42:16 +00001283template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1284struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
1285{
1286 typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
1287};
1288
1289template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
1290struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
1291{
1292 typedef _Alloc<_Up, _A0> type;
1293};
1294
Howard Hinnantc51e1022010-05-11 19:42:16 +00001295template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1296 class _A1, class _Up>
1297struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
1298{
1299 typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
1300};
1301
1302template <template <class, class, class> class _Alloc, class _Tp, class _A0,
1303 class _A1, class _Up>
1304struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
1305{
1306 typedef _Alloc<_Up, _A0, _A1> type;
1307};
1308
Howard Hinnantc51e1022010-05-11 19:42:16 +00001309template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1310 class _A1, class _A2, class _Up>
1311struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
1312{
1313 typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
1314};
1315
1316template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
1317 class _A1, class _A2, class _Up>
1318struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
1319{
1320 typedef _Alloc<_Up, _A0, _A1, _A2> type;
1321};
1322
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001323#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001324
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001325#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326
1327template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1328auto
1329__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1330 -> decltype(__a.allocate(__sz, __p), true_type());
1331
1332template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1333auto
1334__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
1335 -> false_type;
1336
1337template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1338struct __has_allocate_hint
1339 : integral_constant<bool,
1340 is_same<
1341 decltype(__has_allocate_hint_test(declval<_Alloc>(),
1342 declval<_SizeType>(),
1343 declval<_ConstVoidPtr>())),
1344 true_type>::value>
1345{
1346};
1347
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001348#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001349
1350template <class _Alloc, class _SizeType, class _ConstVoidPtr>
1351struct __has_allocate_hint
1352 : true_type
1353{
1354};
1355
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001356#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001357
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001358#if !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001359
1360template <class _Alloc, class _Tp, class ..._Args>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001361decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1362 _VSTD::declval<_Args>()...),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001363 true_type())
1364__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
1365
1366template <class _Alloc, class _Pointer, class ..._Args>
1367false_type
1368__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
1369
1370template <class _Alloc, class _Pointer, class ..._Args>
1371struct __has_construct
1372 : integral_constant<bool,
1373 is_same<
1374 decltype(__has_construct_test(declval<_Alloc>(),
1375 declval<_Pointer>(),
1376 declval<_Args>()...)),
1377 true_type>::value>
1378{
1379};
1380
1381template <class _Alloc, class _Pointer>
1382auto
1383__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
1384 -> decltype(__a.destroy(__p), true_type());
1385
1386template <class _Alloc, class _Pointer>
1387auto
1388__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
1389 -> false_type;
1390
1391template <class _Alloc, class _Pointer>
1392struct __has_destroy
1393 : integral_constant<bool,
1394 is_same<
1395 decltype(__has_destroy_test(declval<_Alloc>(),
1396 declval<_Pointer>())),
1397 true_type>::value>
1398{
1399};
1400
1401template <class _Alloc>
1402auto
1403__has_max_size_test(_Alloc&& __a)
1404 -> decltype(__a.max_size(), true_type());
1405
1406template <class _Alloc>
1407auto
1408__has_max_size_test(const volatile _Alloc& __a)
1409 -> false_type;
1410
1411template <class _Alloc>
1412struct __has_max_size
1413 : integral_constant<bool,
1414 is_same<
1415 decltype(__has_max_size_test(declval<_Alloc&>())),
1416 true_type>::value>
1417{
1418};
1419
1420template <class _Alloc>
1421auto
1422__has_select_on_container_copy_construction_test(_Alloc&& __a)
1423 -> decltype(__a.select_on_container_copy_construction(), true_type());
1424
1425template <class _Alloc>
1426auto
1427__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
1428 -> false_type;
1429
1430template <class _Alloc>
1431struct __has_select_on_container_copy_construction
1432 : integral_constant<bool,
1433 is_same<
1434 decltype(__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
1435 true_type>::value>
1436{
1437};
1438
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001439#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001440
1441#ifndef _LIBCPP_HAS_NO_VARIADICS
1442
1443template <class _Alloc, class _Pointer, class ..._Args>
1444struct __has_construct
1445 : false_type
1446{
1447};
1448
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001449#else // _LIBCPP_HAS_NO_VARIADICS
1450
1451template <class _Alloc, class _Pointer, class _Args>
1452struct __has_construct
1453 : false_type
1454{
1455};
1456
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001457#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001458
1459template <class _Alloc, class _Pointer>
1460struct __has_destroy
1461 : false_type
1462{
1463};
1464
1465template <class _Alloc>
1466struct __has_max_size
1467 : true_type
1468{
1469};
1470
1471template <class _Alloc>
1472struct __has_select_on_container_copy_construction
1473 : false_type
1474{
1475};
1476
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001477#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001478
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001479template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1480struct __alloc_traits_difference_type
1481{
1482 typedef typename pointer_traits<_Ptr>::difference_type type;
1483};
1484
1485template <class _Alloc, class _Ptr>
1486struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1487{
1488 typedef typename _Alloc::difference_type type;
1489};
1490
Howard Hinnantc51e1022010-05-11 19:42:16 +00001491template <class _Alloc>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001492struct _LIBCPP_TYPE_VIS_ONLY allocator_traits
Howard Hinnantc51e1022010-05-11 19:42:16 +00001493{
1494 typedef _Alloc allocator_type;
1495 typedef typename allocator_type::value_type value_type;
1496
1497 typedef typename __pointer_type<value_type, allocator_type>::type pointer;
1498 typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
1499 typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
1500 typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
1501
Howard Hinnant8e4e6002010-11-18 01:40:00 +00001502 typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1503 typedef typename __size_type<allocator_type, difference_type>::type size_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001504
1505 typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
1506 propagate_on_container_copy_assignment;
1507 typedef typename __propagate_on_container_move_assignment<allocator_type>::type
1508 propagate_on_container_move_assignment;
1509 typedef typename __propagate_on_container_swap<allocator_type>::type
1510 propagate_on_container_swap;
Marshall Clow0b587562015-06-02 16:34:03 +00001511 typedef typename __is_always_equal<allocator_type>::type
1512 is_always_equal;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001513
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001514#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001515 template <class _Tp> using rebind_alloc =
Howard Hinnantf09283d2011-05-11 20:21:19 +00001516 typename __allocator_traits_rebind<allocator_type, _Tp>::type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001517 template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001518#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001519 template <class _Tp> struct rebind_alloc
1520 {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
1521 template <class _Tp> struct rebind_traits
1522 {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001523#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001524
Howard Hinnant756c69b2010-09-22 16:48:34 +00001525 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001526 static pointer allocate(allocator_type& __a, size_type __n)
1527 {return __a.allocate(__n);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001528 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529 static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
1530 {return allocate(__a, __n, __hint,
1531 __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
1532
Howard Hinnant756c69b2010-09-22 16:48:34 +00001533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001534 static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001535 {__a.deallocate(__p, __n);}
1536
1537#ifndef _LIBCPP_HAS_NO_VARIADICS
1538 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540 static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
Marshall Clow3996b8b2014-11-11 19:22:33 +00001541 {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001542 __a, __p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001543#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001544 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001546 static void construct(allocator_type& __a, _Tp* __p)
1547 {
1548 ::new ((void*)__p) _Tp();
1549 }
1550 template <class _Tp, class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001552 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
1553 {
1554 ::new ((void*)__p) _Tp(__a0);
1555 }
1556 template <class _Tp, class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1559 const _A1& __a1)
1560 {
1561 ::new ((void*)__p) _Tp(__a0, __a1);
1562 }
1563 template <class _Tp, class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001564 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001565 static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
1566 const _A1& __a1, const _A2& __a2)
1567 {
1568 ::new ((void*)__p) _Tp(__a0, __a1, __a2);
1569 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001570#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001571
1572 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001574 static void destroy(allocator_type& __a, _Tp* __p)
1575 {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
1576
Howard Hinnant756c69b2010-09-22 16:48:34 +00001577 _LIBCPP_INLINE_VISIBILITY
Marshall Clow4f834b52013-08-27 20:22:15 +00001578 static size_type max_size(const allocator_type& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579 {return __max_size(__has_max_size<const allocator_type>(), __a);}
1580
Howard Hinnant756c69b2010-09-22 16:48:34 +00001581 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001582 static allocator_type
1583 select_on_container_copy_construction(const allocator_type& __a)
1584 {return select_on_container_copy_construction(
1585 __has_select_on_container_copy_construction<const allocator_type>(),
1586 __a);}
1587
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001588 template <class _Ptr>
1589 _LIBCPP_INLINE_VISIBILITY
1590 static
1591 void
1592 __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
1593 {
1594 for (; __begin1 != __end1; ++__begin1, ++__begin2)
1595 construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
1596 }
1597
1598 template <class _Tp>
1599 _LIBCPP_INLINE_VISIBILITY
1600 static
1601 typename enable_if
1602 <
1603 (is_same<allocator_type, allocator<_Tp> >::value
1604 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1605 is_trivially_move_constructible<_Tp>::value,
1606 void
1607 >::type
1608 __construct_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1609 {
1610 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001611 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001612 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001613 _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001614 __begin2 += _Np;
1615 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001616 }
1617
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001618 template <class _Iter, class _Ptr>
1619 _LIBCPP_INLINE_VISIBILITY
1620 static
1621 void
1622 __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1623 {
1624 for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1625 construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1626 }
1627
1628 template <class _Tp>
1629 _LIBCPP_INLINE_VISIBILITY
1630 static
1631 typename enable_if
1632 <
1633 (is_same<allocator_type, allocator<_Tp> >::value
1634 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1635 is_trivially_move_constructible<_Tp>::value,
1636 void
1637 >::type
1638 __construct_range_forward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
1639 {
1640 typedef typename remove_const<_Tp>::type _Vp;
1641 ptrdiff_t _Np = __end1 - __begin1;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001642 if (_Np > 0)
Marshall Clow426d6d22015-06-02 13:04:18 +00001643 {
Marshall Clow4907e8f2015-05-31 03:13:31 +00001644 _VSTD::memcpy(const_cast<_Vp*>(__begin2), __begin1, _Np * sizeof(_Tp));
Marshall Clow426d6d22015-06-02 13:04:18 +00001645 __begin2 += _Np;
1646 }
Eric Fiselieracfe6f02015-03-31 16:54:19 +00001647 }
1648
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001649 template <class _Ptr>
1650 _LIBCPP_INLINE_VISIBILITY
1651 static
1652 void
1653 __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
1654 {
1655 while (__end1 != __begin1)
Howard Hinnant5adee4c2013-01-11 20:36:59 +00001656 {
1657 construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
1658 --__end2;
1659 }
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001660 }
1661
1662 template <class _Tp>
1663 _LIBCPP_INLINE_VISIBILITY
1664 static
1665 typename enable_if
1666 <
1667 (is_same<allocator_type, allocator<_Tp> >::value
1668 || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
1669 is_trivially_move_constructible<_Tp>::value,
1670 void
1671 >::type
1672 __construct_backward(allocator_type& __a, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
1673 {
1674 ptrdiff_t _Np = __end1 - __begin1;
1675 __end2 -= _Np;
Marshall Clow4907e8f2015-05-31 03:13:31 +00001676 if (_Np > 0)
1677 _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
Howard Hinnantd2cab2f2012-02-15 00:41:34 +00001678 }
1679
Howard Hinnantc51e1022010-05-11 19:42:16 +00001680private:
1681
Howard Hinnant756c69b2010-09-22 16:48:34 +00001682 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001683 static pointer allocate(allocator_type& __a, size_type __n,
1684 const_void_pointer __hint, true_type)
1685 {return __a.allocate(__n, __hint);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001686 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001687 static pointer allocate(allocator_type& __a, size_type __n,
Howard Hinnant28b24882011-12-01 20:21:04 +00001688 const_void_pointer, false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001689 {return __a.allocate(__n);}
1690
1691#ifndef _LIBCPP_HAS_NO_VARIADICS
1692 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001693 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001694 static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001695 {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001696 template <class _Tp, class... _Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001697 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001698 static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
1699 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001700 ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001701 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001702#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703
1704 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001705 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001706 static void __destroy(true_type, allocator_type& __a, _Tp* __p)
1707 {__a.destroy(__p);}
1708 template <class _Tp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00001709 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710 static void __destroy(false_type, allocator_type&, _Tp* __p)
1711 {
1712 __p->~_Tp();
1713 }
1714
Howard Hinnant756c69b2010-09-22 16:48:34 +00001715 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001716 static size_type __max_size(true_type, const allocator_type& __a)
1717 {return __a.max_size();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001718 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001719 static size_type __max_size(false_type, const allocator_type&)
Marshall Clow33daa162015-10-25 19:34:04 +00001720 {return numeric_limits<size_type>::max() / sizeof(value_type);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001721
Howard Hinnant756c69b2010-09-22 16:48:34 +00001722 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001723 static allocator_type
1724 select_on_container_copy_construction(true_type, const allocator_type& __a)
1725 {return __a.select_on_container_copy_construction();}
Howard Hinnant756c69b2010-09-22 16:48:34 +00001726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001727 static allocator_type
1728 select_on_container_copy_construction(false_type, const allocator_type& __a)
1729 {return __a;}
1730};
1731
Marshall Clow940e01c2015-04-07 05:21:38 +00001732template <class _Traits, class _Tp>
1733struct __rebind_alloc_helper
1734{
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001735#ifndef _LIBCPP_CXX03_LANG
Marshall Clow35cde742015-05-10 13:59:45 +00001736 typedef typename _Traits::template rebind_alloc<_Tp> type;
Marshall Clow940e01c2015-04-07 05:21:38 +00001737#else
1738 typedef typename _Traits::template rebind_alloc<_Tp>::other type;
1739#endif
1740};
1741
Howard Hinnantc51e1022010-05-11 19:42:16 +00001742// allocator
1743
1744template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001745class _LIBCPP_TYPE_VIS_ONLY allocator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001746{
1747public:
1748 typedef size_t size_type;
1749 typedef ptrdiff_t difference_type;
1750 typedef _Tp* pointer;
1751 typedef const _Tp* const_pointer;
1752 typedef _Tp& reference;
1753 typedef const _Tp& const_reference;
1754 typedef _Tp value_type;
1755
Howard Hinnant4931e092011-06-02 21:38:57 +00001756 typedef true_type propagate_on_container_move_assignment;
Marshall Clow0b587562015-06-02 16:34:03 +00001757 typedef true_type is_always_equal;
Howard Hinnant4931e092011-06-02 21:38:57 +00001758
Howard Hinnantc51e1022010-05-11 19:42:16 +00001759 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1760
Howard Hinnant719bda32011-05-28 14:41:13 +00001761 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1762 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1763 _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001764 {return _VSTD::addressof(__x);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001765 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001766 {return _VSTD::addressof(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001767 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001768 {
1769 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001770 __throw_length_error("allocator<T>::allocate(size_t n)"
1771 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001772 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
1773 }
Howard Hinnant719bda32011-05-28 14:41:13 +00001774 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith1f1c1472014-06-04 19:54:15 +00001775 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant719bda32011-05-28 14:41:13 +00001776 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1777 {return size_type(~0) / sizeof(_Tp);}
Howard Hinnant74279a52010-09-04 23:28:19 +00001778#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001779 template <class _Up, class... _Args>
1780 _LIBCPP_INLINE_VISIBILITY
1781 void
1782 construct(_Up* __p, _Args&&... __args)
1783 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001784 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001785 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001786#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001787 _LIBCPP_INLINE_VISIBILITY
1788 void
1789 construct(pointer __p)
1790 {
1791 ::new((void*)__p) _Tp();
1792 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001793# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001794
Howard Hinnantc51e1022010-05-11 19:42:16 +00001795 template <class _A0>
1796 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001797 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001798 construct(pointer __p, _A0& __a0)
1799 {
1800 ::new((void*)__p) _Tp(__a0);
1801 }
1802 template <class _A0>
1803 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001804 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00001805 construct(pointer __p, const _A0& __a0)
1806 {
1807 ::new((void*)__p) _Tp(__a0);
1808 }
Michael J. Spencer8d8164e2010-12-10 19:47:54 +00001809# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001810 template <class _A0, class _A1>
1811 _LIBCPP_INLINE_VISIBILITY
1812 void
1813 construct(pointer __p, _A0& __a0, _A1& __a1)
1814 {
1815 ::new((void*)__p) _Tp(__a0, __a1);
1816 }
1817 template <class _A0, class _A1>
1818 _LIBCPP_INLINE_VISIBILITY
1819 void
1820 construct(pointer __p, const _A0& __a0, _A1& __a1)
1821 {
1822 ::new((void*)__p) _Tp(__a0, __a1);
1823 }
1824 template <class _A0, class _A1>
1825 _LIBCPP_INLINE_VISIBILITY
1826 void
1827 construct(pointer __p, _A0& __a0, const _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, const _A1& __a1)
1835 {
1836 ::new((void*)__p) _Tp(__a0, __a1);
1837 }
Howard Hinnant74279a52010-09-04 23:28:19 +00001838#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001839 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1840};
1841
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001842template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001843class _LIBCPP_TYPE_VIS_ONLY allocator<const _Tp>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001844{
1845public:
1846 typedef size_t size_type;
1847 typedef ptrdiff_t difference_type;
1848 typedef const _Tp* pointer;
1849 typedef const _Tp* const_pointer;
1850 typedef const _Tp& reference;
1851 typedef const _Tp& const_reference;
Howard Hinnant9bc95e22013-06-07 01:56:37 +00001852 typedef const _Tp value_type;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001853
1854 typedef true_type propagate_on_container_move_assignment;
Marshall Clowac12fcc2015-07-01 21:23:40 +00001855 typedef true_type is_always_equal;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001856
1857 template <class _Up> struct rebind {typedef allocator<_Up> other;};
1858
1859 _LIBCPP_INLINE_VISIBILITY allocator() _NOEXCEPT {}
1860 template <class _Up> _LIBCPP_INLINE_VISIBILITY allocator(const allocator<_Up>&) _NOEXCEPT {}
1861 _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1862 {return _VSTD::addressof(__x);}
1863 _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
Marshall Clow813ffb32016-03-03 12:04:39 +00001864 {
1865 if (__n > max_size())
Marshall Clowed3e2292016-08-25 17:47:09 +00001866 __throw_length_error("allocator<const T>::allocate(size_t n)"
1867 " 'n' exceeds maximum supported size");
Marshall Clow813ffb32016-03-03 12:04:39 +00001868 return static_cast<pointer>(_VSTD::__allocate(__n * sizeof(_Tp)));
Eric Fiselier2db2bd52016-05-07 03:12:24 +00001869 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001870 _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) _NOEXCEPT
Richard Smith1f1c1472014-06-04 19:54:15 +00001871 {_VSTD::__deallocate((void*)__p);}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001872 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
1873 {return size_type(~0) / sizeof(_Tp);}
1874#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1875 template <class _Up, class... _Args>
1876 _LIBCPP_INLINE_VISIBILITY
1877 void
1878 construct(_Up* __p, _Args&&... __args)
1879 {
1880 ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
1881 }
1882#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1883 _LIBCPP_INLINE_VISIBILITY
1884 void
1885 construct(pointer __p)
1886 {
1887 ::new((void*)__p) _Tp();
1888 }
1889# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant9e028f12012-05-01 15:37:54 +00001890
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001891 template <class _A0>
1892 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001893 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001894 construct(pointer __p, _A0& __a0)
1895 {
1896 ::new((void*)__p) _Tp(__a0);
1897 }
1898 template <class _A0>
1899 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9e028f12012-05-01 15:37:54 +00001900 void
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001901 construct(pointer __p, const _A0& __a0)
1902 {
1903 ::new((void*)__p) _Tp(__a0);
1904 }
Howard Hinnant741c8fa2012-01-02 17:56:02 +00001905# endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1906 template <class _A0, class _A1>
1907 _LIBCPP_INLINE_VISIBILITY
1908 void
1909 construct(pointer __p, _A0& __a0, _A1& __a1)
1910 {
1911 ::new((void*)__p) _Tp(__a0, __a1);
1912 }
1913 template <class _A0, class _A1>
1914 _LIBCPP_INLINE_VISIBILITY
1915 void
1916 construct(pointer __p, const _A0& __a0, _A1& __a1)
1917 {
1918 ::new((void*)__p) _Tp(__a0, __a1);
1919 }
1920 template <class _A0, class _A1>
1921 _LIBCPP_INLINE_VISIBILITY
1922 void
1923 construct(pointer __p, _A0& __a0, const _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, const _A1& __a1)
1931 {
1932 ::new((void*)__p) _Tp(__a0, __a1);
1933 }
1934#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1935 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
1936};
1937
Howard Hinnantc51e1022010-05-11 19:42:16 +00001938template <class _Tp, class _Up>
1939inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001940bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001941
1942template <class _Tp, class _Up>
1943inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001944bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001945
1946template <class _OutputIterator, class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001947class _LIBCPP_TYPE_VIS_ONLY raw_storage_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001948 : public iterator<output_iterator_tag,
1949 _Tp, // purposefully not C++03
1950 ptrdiff_t, // purposefully not C++03
1951 _Tp*, // purposefully not C++03
1952 raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
1953{
1954private:
1955 _OutputIterator __x_;
1956public:
1957 _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
1958 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
1959 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
1960 {::new(&*__x_) _Tp(__element); return *this;}
Marshall Clow5c3f09e2015-10-25 18:58:07 +00001961#if _LIBCPP_STD_VER >= 14
1962 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
1963 {::new(&*__x_) _Tp(_VSTD::move(__element)); return *this;}
1964#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001965 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
1966 _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
1967 {raw_storage_iterator __t(*this); ++__x_; return __t;}
Marshall Clowb949f1b2015-05-10 13:14:08 +00001968#if _LIBCPP_STD_VER >= 14
1969 _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
1970#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001971};
1972
1973template <class _Tp>
1974pair<_Tp*, ptrdiff_t>
Howard Hinnant719bda32011-05-28 14:41:13 +00001975get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001976{
1977 pair<_Tp*, ptrdiff_t> __r(0, 0);
1978 const ptrdiff_t __m = (~ptrdiff_t(0) ^
1979 ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
1980 / sizeof(_Tp);
1981 if (__n > __m)
1982 __n = __m;
1983 while (__n > 0)
1984 {
1985 __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
1986 if (__r.first)
1987 {
1988 __r.second = __n;
1989 break;
1990 }
1991 __n /= 2;
1992 }
1993 return __r;
1994}
1995
1996template <class _Tp>
1997inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00001998void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001999
2000template <class _Tp>
2001struct auto_ptr_ref
2002{
2003 _Tp* __ptr_;
2004};
2005
2006template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002007class _LIBCPP_TYPE_VIS_ONLY auto_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002008{
2009private:
2010 _Tp* __ptr_;
2011public:
2012 typedef _Tp element_type;
2013
2014 _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
2015 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
2016 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
2017 : __ptr_(__p.release()) {}
2018 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
2019 {reset(__p.release()); return *this;}
2020 template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
2021 {reset(__p.release()); return *this;}
2022 _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
2023 {reset(__p.__ptr_); return *this;}
2024 _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
2025
2026 _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
2027 {return *__ptr_;}
2028 _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
2029 _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
2030 _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
2031 {
2032 _Tp* __t = __ptr_;
2033 __ptr_ = 0;
2034 return __t;
2035 }
2036 _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
2037 {
2038 if (__ptr_ != __p)
2039 delete __ptr_;
2040 __ptr_ = __p;
2041 }
2042
2043 _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
2044 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
2045 {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
2046 template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
2047 {return auto_ptr<_Up>(release());}
2048};
2049
2050template <>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002051class _LIBCPP_TYPE_VIS_ONLY auto_ptr<void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002052{
2053public:
2054 typedef void element_type;
2055};
2056
Howard Hinnantc51e1022010-05-11 19:42:16 +00002057template <class _T1, class _T2, bool = is_same<typename remove_cv<_T1>::type,
2058 typename remove_cv<_T2>::type>::value,
Howard Hinnantd0aabf82011-12-11 20:31:33 +00002059 bool = is_empty<_T1>::value
Eric Fiselierf7394302015-06-13 07:08:02 +00002060 && !__libcpp_is_final<_T1>::value,
Howard Hinnantd0aabf82011-12-11 20:31:33 +00002061 bool = is_empty<_T2>::value
Eric Fiselierf7394302015-06-13 07:08:02 +00002062 && !__libcpp_is_final<_T2>::value
Howard Hinnantd0aabf82011-12-11 20:31:33 +00002063 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00002064struct __libcpp_compressed_pair_switch;
2065
2066template <class _T1, class _T2, bool IsSame>
2067struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, false> {enum {value = 0};};
2068
2069template <class _T1, class _T2, bool IsSame>
2070struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, true, false> {enum {value = 1};};
2071
2072template <class _T1, class _T2, bool IsSame>
2073struct __libcpp_compressed_pair_switch<_T1, _T2, IsSame, false, true> {enum {value = 2};};
2074
2075template <class _T1, class _T2>
2076struct __libcpp_compressed_pair_switch<_T1, _T2, false, true, true> {enum {value = 3};};
2077
2078template <class _T1, class _T2>
2079struct __libcpp_compressed_pair_switch<_T1, _T2, true, true, true> {enum {value = 1};};
2080
2081template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
2082class __libcpp_compressed_pair_imp;
2083
2084template <class _T1, class _T2>
2085class __libcpp_compressed_pair_imp<_T1, _T2, 0>
2086{
2087private:
2088 _T1 __first_;
2089 _T2 __second_;
2090public:
2091 typedef _T1 _T1_param;
2092 typedef _T2 _T2_param;
2093
2094 typedef typename remove_reference<_T1>::type& _T1_reference;
2095 typedef typename remove_reference<_T2>::type& _T2_reference;
2096
2097 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2098 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2099
Marshall Clowac92bfe2015-07-16 03:05:06 +00002100 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_(), __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002101 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002102 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002103 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002104 : __first_(), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002106 : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002107
Howard Hinnant83b1c052011-12-19 17:58:44 +00002108#ifndef _LIBCPP_HAS_NO_VARIADICS
2109
2110 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2111 _LIBCPP_INLINE_VISIBILITY
2112 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2113 tuple<_Args1...> __first_args,
2114 tuple<_Args2...> __second_args,
2115 __tuple_indices<_I1...>,
2116 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002117 : __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2118 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002119 {}
2120
2121#endif // _LIBCPP_HAS_NO_VARIADICS
2122
Howard Hinnant719bda32011-05-28 14:41:13 +00002123 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2124 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002125
Howard Hinnant719bda32011-05-28 14:41:13 +00002126 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2127 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002128
2129 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002130 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002131 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002132 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002133 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002134 swap(__first_, __x.__first_);
2135 swap(__second_, __x.__second_);
2136 }
2137};
2138
2139template <class _T1, class _T2>
2140class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2141 : private _T1
2142{
2143private:
2144 _T2 __second_;
2145public:
2146 typedef _T1 _T1_param;
2147 typedef _T2 _T2_param;
2148
2149 typedef _T1& _T1_reference;
2150 typedef typename remove_reference<_T2>::type& _T2_reference;
2151
2152 typedef const _T1& _T1_const_reference;
2153 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2154
Marshall Clowac92bfe2015-07-16 03:05:06 +00002155 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002156 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002157 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002158 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002159 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002160 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002161 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002162
Howard Hinnant83b1c052011-12-19 17:58:44 +00002163#ifndef _LIBCPP_HAS_NO_VARIADICS
2164
2165 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2166 _LIBCPP_INLINE_VISIBILITY
2167 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2168 tuple<_Args1...> __first_args,
2169 tuple<_Args2...> __second_args,
2170 __tuple_indices<_I1...>,
2171 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002172 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2173 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002174 {}
2175
2176#endif // _LIBCPP_HAS_NO_VARIADICS
2177
Howard Hinnant719bda32011-05-28 14:41:13 +00002178 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2179 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002180
Howard Hinnant719bda32011-05-28 14:41:13 +00002181 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2182 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183
2184 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002185 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002186 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002187 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002188 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002189 swap(__second_, __x.__second_);
2190 }
2191};
2192
2193template <class _T1, class _T2>
2194class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2195 : private _T2
2196{
2197private:
2198 _T1 __first_;
2199public:
2200 typedef _T1 _T1_param;
2201 typedef _T2 _T2_param;
2202
2203 typedef typename remove_reference<_T1>::type& _T1_reference;
2204 typedef _T2& _T2_reference;
2205
2206 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2207 typedef const _T2& _T2_const_reference;
2208
Marshall Clowac92bfe2015-07-16 03:05:06 +00002209 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002210 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002211 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002213 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002214 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant719bda32011-05-28 14:41:13 +00002215 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2216 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002217 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002218
Howard Hinnant83b1c052011-12-19 17:58:44 +00002219#ifndef _LIBCPP_HAS_NO_VARIADICS
2220
2221 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2222 _LIBCPP_INLINE_VISIBILITY
2223 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2224 tuple<_Args1...> __first_args,
2225 tuple<_Args2...> __second_args,
2226 __tuple_indices<_I1...>,
2227 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002228 : _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...),
2229 __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002230
2231 {}
2232
2233#endif // _LIBCPP_HAS_NO_VARIADICS
2234
Howard Hinnant719bda32011-05-28 14:41:13 +00002235 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2236 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237
Howard Hinnant719bda32011-05-28 14:41:13 +00002238 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2239 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240
2241 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002242 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002243 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002245 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002246 swap(__first_, __x.__first_);
2247 }
2248};
2249
2250template <class _T1, class _T2>
2251class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2252 : private _T1,
2253 private _T2
2254{
2255public:
2256 typedef _T1 _T1_param;
2257 typedef _T2 _T2_param;
2258
2259 typedef _T1& _T1_reference;
2260 typedef _T2& _T2_reference;
2261
2262 typedef const _T1& _T1_const_reference;
2263 typedef const _T2& _T2_const_reference;
2264
2265 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2266 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002267 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002268 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002269 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002270 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002271 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002272
Howard Hinnant83b1c052011-12-19 17:58:44 +00002273#ifndef _LIBCPP_HAS_NO_VARIADICS
2274
2275 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2276 _LIBCPP_INLINE_VISIBILITY
2277 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2278 tuple<_Args1...> __first_args,
2279 tuple<_Args2...> __second_args,
2280 __tuple_indices<_I1...>,
2281 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002282 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2283 _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002284 {}
2285
2286#endif // _LIBCPP_HAS_NO_VARIADICS
2287
Howard Hinnant719bda32011-05-28 14:41:13 +00002288 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2289 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002290
Howard Hinnant719bda32011-05-28 14:41:13 +00002291 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2292 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002293
Howard Hinnant28b24882011-12-01 20:21:04 +00002294 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant719bda32011-05-28 14:41:13 +00002295 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002296 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002297 {
2298 }
2299};
2300
2301template <class _T1, class _T2>
2302class __compressed_pair
2303 : private __libcpp_compressed_pair_imp<_T1, _T2>
2304{
2305 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2306public:
2307 typedef typename base::_T1_param _T1_param;
2308 typedef typename base::_T2_param _T2_param;
2309
2310 typedef typename base::_T1_reference _T1_reference;
2311 typedef typename base::_T2_reference _T2_reference;
2312
2313 typedef typename base::_T1_const_reference _T1_const_reference;
2314 typedef typename base::_T2_const_reference _T2_const_reference;
2315
2316 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002317 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002318 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002319 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002320 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002321 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002322 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002323
Howard Hinnant83b1c052011-12-19 17:58:44 +00002324#ifndef _LIBCPP_HAS_NO_VARIADICS
2325
2326 template <class... _Args1, class... _Args2>
2327 _LIBCPP_INLINE_VISIBILITY
2328 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2329 tuple<_Args2...> __second_args)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002330 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant83b1c052011-12-19 17:58:44 +00002331 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2332 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2333 {}
2334
2335#endif // _LIBCPP_HAS_NO_VARIADICS
2336
Howard Hinnant719bda32011-05-28 14:41:13 +00002337 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2338 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002339
Howard Hinnant719bda32011-05-28 14:41:13 +00002340 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2341 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002342
Howard Hinnant719bda32011-05-28 14:41:13 +00002343 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2344 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002345 __is_nothrow_swappable<_T2>::value)
Howard Hinnant719bda32011-05-28 14:41:13 +00002346 {base::swap(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002347};
2348
2349template <class _T1, class _T2>
2350inline _LIBCPP_INLINE_VISIBILITY
2351void
2352swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant719bda32011-05-28 14:41:13 +00002353 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002354 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002355 {__x.swap(__y);}
2356
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002357// __same_or_less_cv_qualified
2358
2359template <class _Ptr1, class _Ptr2,
2360 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2361 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2362 >::value
2363 >
2364struct __same_or_less_cv_qualified_imp
2365 : is_convertible<_Ptr1, _Ptr2> {};
2366
2367template <class _Ptr1, class _Ptr2>
2368struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2369 : false_type {};
2370
Marshall Clowdf296162014-04-26 05:19:48 +00002371template <class _Ptr1, class _Ptr2, bool = is_pointer<_Ptr1>::value ||
2372 is_same<_Ptr1, _Ptr2>::value ||
2373 __has_element_type<_Ptr1>::value>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002374struct __same_or_less_cv_qualified
2375 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2376
2377template <class _Ptr1, class _Ptr2>
Marshall Clowdf296162014-04-26 05:19:48 +00002378struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, false>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002379 : false_type {};
2380
2381// default_delete
2382
Howard Hinnantc51e1022010-05-11 19:42:16 +00002383template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002384struct _LIBCPP_TYPE_VIS_ONLY default_delete
Howard Hinnantc51e1022010-05-11 19:42:16 +00002385{
Eric Fiselier07b2b552016-11-18 06:42:17 +00002386#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002387 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2388#else
2389 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2390#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002391 template <class _Up>
2392 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant719bda32011-05-28 14:41:13 +00002393 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2394 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002395 {
2396 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant14015fd2013-04-24 19:44:26 +00002397 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002398 delete __ptr;
2399 }
2400};
2401
2402template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002403struct _LIBCPP_TYPE_VIS_ONLY default_delete<_Tp[]>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002404{
Howard Hinnant4500ca52011-12-18 21:19:44 +00002405public:
Eric Fiselier07b2b552016-11-18 06:42:17 +00002406#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002407 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2408#else
2409 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2410#endif
Howard Hinnant4500ca52011-12-18 21:19:44 +00002411 template <class _Up>
2412 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002413 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant4500ca52011-12-18 21:19:44 +00002414 template <class _Up>
2415 _LIBCPP_INLINE_VISIBILITY
2416 void operator() (_Up* __ptr,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002417 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002418 {
2419 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Marshall Clowff0e4132016-02-25 16:50:51 +00002420 static_assert(!is_void<_Tp>::value, "default_delete can not delete void type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002421 delete [] __ptr;
2422 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002423};
2424
2425template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002426class _LIBCPP_TYPE_VIS_ONLY unique_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002427{
2428public:
2429 typedef _Tp element_type;
2430 typedef _Dp deleter_type;
2431 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2432private:
2433 __compressed_pair<pointer, deleter_type> __ptr_;
2434
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002435#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002436 unique_ptr(unique_ptr&);
2437 template <class _Up, class _Ep>
2438 unique_ptr(unique_ptr<_Up, _Ep>&);
2439 unique_ptr& operator=(unique_ptr&);
2440 template <class _Up, class _Ep>
2441 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant74279a52010-09-04 23:28:19 +00002442#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002443
2444 struct __nat {int __for_bool_;};
2445
2446 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2447 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2448public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002449 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002450 : __ptr_(pointer())
2451 {
2452 static_assert(!is_pointer<deleter_type>::value,
2453 "unique_ptr constructed with null function pointer deleter");
2454 }
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002455 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002456 : __ptr_(pointer())
2457 {
2458 static_assert(!is_pointer<deleter_type>::value,
2459 "unique_ptr constructed with null function pointer deleter");
2460 }
Howard Hinnant719bda32011-05-28 14:41:13 +00002461 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002462 : __ptr_(_VSTD::move(__p))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002463 {
2464 static_assert(!is_pointer<deleter_type>::value,
2465 "unique_ptr constructed with null function pointer deleter");
2466 }
2467
Howard Hinnant74279a52010-09-04 23:28:19 +00002468#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002469 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2470 is_reference<deleter_type>::value,
2471 deleter_type,
2472 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002473 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002474 : __ptr_(__p, __d) {}
2475
2476 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002477 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002478 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002479 {
2480 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2481 }
Howard Hinnant719bda32011-05-28 14:41:13 +00002482 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002483 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002484 template <class _Up, class _Ep>
2485 _LIBCPP_INLINE_VISIBILITY
2486 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2487 typename enable_if
2488 <
2489 !is_array<_Up>::value &&
2490 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2491 is_convertible<_Ep, deleter_type>::value &&
2492 (
2493 !is_reference<deleter_type>::value ||
2494 is_same<deleter_type, _Ep>::value
2495 ),
2496 __nat
Howard Hinnant719bda32011-05-28 14:41:13 +00002497 >::type = __nat()) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002498 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002499
2500 template <class _Up>
2501 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2502 typename enable_if<
2503 is_convertible<_Up*, _Tp*>::value &&
2504 is_same<_Dp, default_delete<_Tp> >::value,
2505 __nat
Howard Hinnant719bda32011-05-28 14:41:13 +00002506 >::type = __nat()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002507 : __ptr_(__p.release())
2508 {
2509 }
2510
Howard Hinnant719bda32011-05-28 14:41:13 +00002511 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002512 {
2513 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002514 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002515 return *this;
2516 }
2517
2518 template <class _Up, class _Ep>
2519 _LIBCPP_INLINE_VISIBILITY
2520 typename enable_if
2521 <
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002522 !is_array<_Up>::value &&
2523 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2524 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002525 unique_ptr&
2526 >::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002527 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002528 {
2529 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002530 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002531 return *this;
2532 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002533#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002534
2535 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2536 {
2537 return __rv<unique_ptr>(*this);
2538 }
2539
2540 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002541 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002542
2543 template <class _Up, class _Ep>
Eric Fiselier6d8c8ed2015-08-28 05:07:06 +00002544 _LIBCPP_INLINE_VISIBILITY
2545 typename enable_if<
2546 !is_array<_Up>::value &&
2547 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2548 is_assignable<deleter_type&, _Ep&>::value,
2549 unique_ptr&
2550 >::type
2551 operator=(unique_ptr<_Up, _Ep> __u)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002552 {
2553 reset(__u.release());
Eric Fiselier6d8c8ed2015-08-28 05:07:06 +00002554 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555 return *this;
2556 }
2557
2558 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002559 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002560
2561 template <class _Up>
2562 _LIBCPP_INLINE_VISIBILITY
2563 typename enable_if<
2564 is_convertible<_Up*, _Tp*>::value &&
2565 is_same<_Dp, default_delete<_Tp> >::value,
2566 unique_ptr&
2567 >::type
2568 operator=(auto_ptr<_Up> __p)
2569 {reset(__p.release()); return *this;}
2570
Howard Hinnant74279a52010-09-04 23:28:19 +00002571#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002572 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2573
Howard Hinnant719bda32011-05-28 14:41:13 +00002574 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002575 {
2576 reset();
2577 return *this;
2578 }
2579
2580 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2581 {return *__ptr_.first();}
Howard Hinnant719bda32011-05-28 14:41:13 +00002582 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2583 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2584 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2585 {return __ptr_.second();}
2586 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2587 {return __ptr_.second();}
Howard Hinnant86a291f2012-02-21 21:46:43 +00002588 _LIBCPP_INLINE_VISIBILITY
2589 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2590 {return __ptr_.first() != nullptr;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002591
Howard Hinnant719bda32011-05-28 14:41:13 +00002592 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002593 {
2594 pointer __t = __ptr_.first();
2595 __ptr_.first() = pointer();
2596 return __t;
2597 }
2598
Howard Hinnant719bda32011-05-28 14:41:13 +00002599 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002600 {
2601 pointer __tmp = __ptr_.first();
2602 __ptr_.first() = __p;
2603 if (__tmp)
2604 __ptr_.second()(__tmp);
2605 }
2606
Howard Hinnant719bda32011-05-28 14:41:13 +00002607 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2608 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002609};
2610
2611template <class _Tp, class _Dp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002612class _LIBCPP_TYPE_VIS_ONLY unique_ptr<_Tp[], _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002613{
2614public:
2615 typedef _Tp element_type;
2616 typedef _Dp deleter_type;
2617 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2618private:
2619 __compressed_pair<pointer, deleter_type> __ptr_;
2620
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002621#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002622 unique_ptr(unique_ptr&);
2623 template <class _Up>
2624 unique_ptr(unique_ptr<_Up>&);
2625 unique_ptr& operator=(unique_ptr&);
2626 template <class _Up>
2627 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant74279a52010-09-04 23:28:19 +00002628#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002629
2630 struct __nat {int __for_bool_;};
2631
2632 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2633 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2634public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002635 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002636 : __ptr_(pointer())
2637 {
2638 static_assert(!is_pointer<deleter_type>::value,
2639 "unique_ptr constructed with null function pointer deleter");
2640 }
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002641 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002642 : __ptr_(pointer())
2643 {
2644 static_assert(!is_pointer<deleter_type>::value,
2645 "unique_ptr constructed with null function pointer deleter");
2646 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002647#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00002648 template <class _Pp>
2649 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p,
2650 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002651 : __ptr_(__p)
2652 {
2653 static_assert(!is_pointer<deleter_type>::value,
2654 "unique_ptr constructed with null function pointer deleter");
2655 }
2656
Logan Chiend435f8b2014-01-31 09:30:46 +00002657 template <class _Pp>
Howard Hinnantc834c512011-11-29 18:15:50 +00002658 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659 is_reference<deleter_type>::value,
2660 deleter_type,
Logan Chiend435f8b2014-01-31 09:30:46 +00002661 typename add_lvalue_reference<const deleter_type>::type>::type __d,
2662 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002663 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002664 : __ptr_(__p, __d) {}
2665
2666 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2667 is_reference<deleter_type>::value,
2668 deleter_type,
2669 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002670 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002671 : __ptr_(pointer(), __d) {}
2672
Logan Chiend435f8b2014-01-31 09:30:46 +00002673 template <class _Pp>
2674 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p,
2675 typename remove_reference<deleter_type>::type&& __d,
2676 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002677 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002678 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002679 {
2680 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2681 }
2682
2683 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002684 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002685 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002686 {
2687 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2688 }
2689
Howard Hinnant719bda32011-05-28 14:41:13 +00002690 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002691 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002692
Howard Hinnant719bda32011-05-28 14:41:13 +00002693 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002694 {
2695 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002696 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002697 return *this;
2698 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002699
2700 template <class _Up, class _Ep>
2701 _LIBCPP_INLINE_VISIBILITY
2702 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2703 typename enable_if
2704 <
2705 is_array<_Up>::value &&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002706 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant4500ca52011-12-18 21:19:44 +00002707 && is_convertible<_Ep, deleter_type>::value &&
2708 (
2709 !is_reference<deleter_type>::value ||
2710 is_same<deleter_type, _Ep>::value
2711 ),
2712 __nat
2713 >::type = __nat()
2714 ) _NOEXCEPT
2715 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2716
2717
2718 template <class _Up, class _Ep>
2719 _LIBCPP_INLINE_VISIBILITY
2720 typename enable_if
2721 <
2722 is_array<_Up>::value &&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002723 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2724 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant4500ca52011-12-18 21:19:44 +00002725 unique_ptr&
2726 >::type
2727 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2728 {
2729 reset(__u.release());
2730 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2731 return *this;
2732 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002733#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002734
2735 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2736 : __ptr_(__p)
2737 {
2738 static_assert(!is_pointer<deleter_type>::value,
2739 "unique_ptr constructed with null function pointer deleter");
2740 }
2741
2742 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002743 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002744
2745 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002746 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002747
2748 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2749 {
2750 return __rv<unique_ptr>(*this);
2751 }
2752
2753 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002754 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002755
2756 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2757 {
2758 reset(__u->release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002759 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002760 return *this;
2761 }
2762
Howard Hinnant74279a52010-09-04 23:28:19 +00002763#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002764 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2765
Howard Hinnant719bda32011-05-28 14:41:13 +00002766 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002767 {
2768 reset();
2769 return *this;
2770 }
2771
2772 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2773 {return __ptr_.first()[__i];}
Howard Hinnant719bda32011-05-28 14:41:13 +00002774 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2775 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2776 {return __ptr_.second();}
2777 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2778 {return __ptr_.second();}
Howard Hinnant86a291f2012-02-21 21:46:43 +00002779 _LIBCPP_INLINE_VISIBILITY
2780 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2781 {return __ptr_.first() != nullptr;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002782
Howard Hinnant719bda32011-05-28 14:41:13 +00002783 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002784 {
2785 pointer __t = __ptr_.first();
2786 __ptr_.first() = pointer();
2787 return __t;
2788 }
2789
Logan Chiend435f8b2014-01-31 09:30:46 +00002790 template <class _Pp>
2791 _LIBCPP_INLINE_VISIBILITY
2792 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, void>::type
2793 reset(_Pp __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002794 {
2795 pointer __tmp = __ptr_.first();
2796 __ptr_.first() = __p;
2797 if (__tmp)
2798 __ptr_.second()(__tmp);
2799 }
Marshall Clowff0e4132016-02-25 16:50:51 +00002800 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t = nullptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002801 {
2802 pointer __tmp = __ptr_.first();
2803 __ptr_.first() = nullptr;
2804 if (__tmp)
2805 __ptr_.second()(__tmp);
2806 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002807
2808 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2809private:
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002810
Howard Hinnant74279a52010-09-04 23:28:19 +00002811#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002812 template <class _Up>
2813 explicit unique_ptr(_Up);
2814 template <class _Up>
2815 unique_ptr(_Up __u,
2816 typename conditional<
2817 is_reference<deleter_type>::value,
2818 deleter_type,
2819 typename add_lvalue_reference<const deleter_type>::type>::type,
2820 typename enable_if
2821 <
2822 is_convertible<_Up, pointer>::value,
2823 __nat
2824 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00002825#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002826};
2827
2828template <class _Tp, class _Dp>
2829inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00002830typename enable_if<
2831 __is_swappable<_Dp>::value,
2832 void
2833>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002834swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002835
2836template <class _T1, class _D1, class _T2, class _D2>
2837inline _LIBCPP_INLINE_VISIBILITY
2838bool
2839operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
2840
2841template <class _T1, class _D1, class _T2, class _D2>
2842inline _LIBCPP_INLINE_VISIBILITY
2843bool
2844operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
2845
2846template <class _T1, class _D1, class _T2, class _D2>
2847inline _LIBCPP_INLINE_VISIBILITY
2848bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00002849operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
2850{
2851 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2852 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00002853 typedef typename common_type<_P1, _P2>::type _Vp;
2854 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00002855}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002856
2857template <class _T1, class _D1, class _T2, class _D2>
2858inline _LIBCPP_INLINE_VISIBILITY
2859bool
2860operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
2861
2862template <class _T1, class _D1, class _T2, class _D2>
2863inline _LIBCPP_INLINE_VISIBILITY
2864bool
2865operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
2866
2867template <class _T1, class _D1, class _T2, class _D2>
2868inline _LIBCPP_INLINE_VISIBILITY
2869bool
2870operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
2871
Howard Hinnantb17caf92012-02-21 21:02:58 +00002872template <class _T1, class _D1>
2873inline _LIBCPP_INLINE_VISIBILITY
2874bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002875operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002876{
2877 return !__x;
2878}
2879
2880template <class _T1, class _D1>
2881inline _LIBCPP_INLINE_VISIBILITY
2882bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002883operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002884{
2885 return !__x;
2886}
2887
2888template <class _T1, class _D1>
2889inline _LIBCPP_INLINE_VISIBILITY
2890bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002891operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002892{
2893 return static_cast<bool>(__x);
2894}
2895
2896template <class _T1, class _D1>
2897inline _LIBCPP_INLINE_VISIBILITY
2898bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002899operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00002900{
2901 return static_cast<bool>(__x);
2902}
2903
2904template <class _T1, class _D1>
2905inline _LIBCPP_INLINE_VISIBILITY
2906bool
2907operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2908{
2909 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2910 return less<_P1>()(__x.get(), nullptr);
2911}
2912
2913template <class _T1, class _D1>
2914inline _LIBCPP_INLINE_VISIBILITY
2915bool
2916operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2917{
2918 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
2919 return less<_P1>()(nullptr, __x.get());
2920}
2921
2922template <class _T1, class _D1>
2923inline _LIBCPP_INLINE_VISIBILITY
2924bool
2925operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2926{
2927 return nullptr < __x;
2928}
2929
2930template <class _T1, class _D1>
2931inline _LIBCPP_INLINE_VISIBILITY
2932bool
2933operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2934{
2935 return __x < nullptr;
2936}
2937
2938template <class _T1, class _D1>
2939inline _LIBCPP_INLINE_VISIBILITY
2940bool
2941operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2942{
2943 return !(nullptr < __x);
2944}
2945
2946template <class _T1, class _D1>
2947inline _LIBCPP_INLINE_VISIBILITY
2948bool
2949operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2950{
2951 return !(__x < nullptr);
2952}
2953
2954template <class _T1, class _D1>
2955inline _LIBCPP_INLINE_VISIBILITY
2956bool
2957operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
2958{
2959 return !(__x < nullptr);
2960}
2961
2962template <class _T1, class _D1>
2963inline _LIBCPP_INLINE_VISIBILITY
2964bool
2965operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
2966{
2967 return !(nullptr < __x);
2968}
2969
Howard Hinnant9e028f12012-05-01 15:37:54 +00002970#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2971
2972template <class _Tp, class _Dp>
2973inline _LIBCPP_INLINE_VISIBILITY
2974unique_ptr<_Tp, _Dp>
2975move(unique_ptr<_Tp, _Dp>& __t)
2976{
2977 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
2978}
2979
2980#endif
2981
Marshall Clow9e8f4a92013-07-01 18:16:03 +00002982#if _LIBCPP_STD_VER > 11
2983
2984template<class _Tp>
2985struct __unique_if
2986{
2987 typedef unique_ptr<_Tp> __unique_single;
2988};
2989
2990template<class _Tp>
2991struct __unique_if<_Tp[]>
2992{
2993 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
2994};
2995
2996template<class _Tp, size_t _Np>
2997struct __unique_if<_Tp[_Np]>
2998{
2999 typedef void __unique_array_known_bound;
3000};
3001
3002template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003003inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003004typename __unique_if<_Tp>::__unique_single
3005make_unique(_Args&&... __args)
3006{
3007 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3008}
3009
3010template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003011inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003012typename __unique_if<_Tp>::__unique_array_unknown_bound
3013make_unique(size_t __n)
3014{
3015 typedef typename remove_extent<_Tp>::type _Up;
3016 return unique_ptr<_Tp>(new _Up[__n]());
3017}
3018
3019template<class _Tp, class... _Args>
3020 typename __unique_if<_Tp>::__unique_array_known_bound
3021 make_unique(_Args&&...) = delete;
3022
3023#endif // _LIBCPP_STD_VER > 11
3024
Howard Hinnant6a855442013-07-03 17:39:28 +00003025template <class _Size>
3026inline _LIBCPP_INLINE_VISIBILITY
3027_Size
3028__loadword(const void* __p)
3029{
3030 _Size __r;
3031 std::memcpy(&__r, __p, sizeof(__r));
3032 return __r;
3033}
3034
Howard Hinnantf1973402011-12-10 20:28:56 +00003035// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
3036// is 64 bits. This is because cityhash64 uses 64bit x 64bit
3037// multiplication, which can be very slow on 32-bit systems.
Howard Hinnantad71c232011-12-05 00:08:45 +00003038template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantf1973402011-12-10 20:28:56 +00003039struct __murmur2_or_cityhash;
Howard Hinnantad71c232011-12-05 00:08:45 +00003040
3041template <class _Size>
Howard Hinnantf1973402011-12-10 20:28:56 +00003042struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnantad71c232011-12-05 00:08:45 +00003043{
3044 _Size operator()(const void* __key, _Size __len);
3045};
3046
Howard Hinnantf1973402011-12-10 20:28:56 +00003047// murmur2
Howard Hinnantad71c232011-12-05 00:08:45 +00003048template <class _Size>
3049_Size
Marshall Clow91c7f642016-01-11 19:27:10 +00003050__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantad71c232011-12-05 00:08:45 +00003051{
3052 const _Size __m = 0x5bd1e995;
3053 const _Size __r = 24;
3054 _Size __h = __len;
3055 const unsigned char* __data = static_cast<const unsigned char*>(__key);
3056 for (; __len >= 4; __data += 4, __len -= 4)
3057 {
Howard Hinnant6a855442013-07-03 17:39:28 +00003058 _Size __k = __loadword<_Size>(__data);
Howard Hinnantad71c232011-12-05 00:08:45 +00003059 __k *= __m;
3060 __k ^= __k >> __r;
3061 __k *= __m;
3062 __h *= __m;
3063 __h ^= __k;
3064 }
3065 switch (__len)
3066 {
3067 case 3:
3068 __h ^= __data[2] << 16;
3069 case 2:
3070 __h ^= __data[1] << 8;
3071 case 1:
3072 __h ^= __data[0];
3073 __h *= __m;
3074 }
3075 __h ^= __h >> 13;
3076 __h *= __m;
3077 __h ^= __h >> 15;
3078 return __h;
3079}
3080
3081template <class _Size>
Howard Hinnantf1973402011-12-10 20:28:56 +00003082struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnantad71c232011-12-05 00:08:45 +00003083{
3084 _Size operator()(const void* __key, _Size __len);
Howard Hinnantf1973402011-12-10 20:28:56 +00003085
3086 private:
3087 // Some primes between 2^63 and 2^64.
3088 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
3089 static const _Size __k1 = 0xb492b66fbe98f273ULL;
3090 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
3091 static const _Size __k3 = 0xc949d7c7509e6557ULL;
3092
3093 static _Size __rotate(_Size __val, int __shift) {
3094 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
3095 }
3096
3097 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
3098 return (__val >> __shift) | (__val << (64 - __shift));
3099 }
3100
3101 static _Size __shift_mix(_Size __val) {
3102 return __val ^ (__val >> 47);
3103 }
3104
3105 static _Size __hash_len_16(_Size __u, _Size __v) {
3106 const _Size __mul = 0x9ddfea08eb382d69ULL;
3107 _Size __a = (__u ^ __v) * __mul;
3108 __a ^= (__a >> 47);
3109 _Size __b = (__v ^ __a) * __mul;
3110 __b ^= (__b >> 47);
3111 __b *= __mul;
3112 return __b;
3113 }
3114
3115 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
3116 if (__len > 8) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003117 const _Size __a = __loadword<_Size>(__s);
3118 const _Size __b = __loadword<_Size>(__s + __len - 8);
Howard Hinnantf1973402011-12-10 20:28:56 +00003119 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
3120 }
3121 if (__len >= 4) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003122 const uint32_t __a = __loadword<uint32_t>(__s);
3123 const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
Howard Hinnantf1973402011-12-10 20:28:56 +00003124 return __hash_len_16(__len + (__a << 3), __b);
3125 }
3126 if (__len > 0) {
3127 const unsigned char __a = __s[0];
3128 const unsigned char __b = __s[__len >> 1];
3129 const unsigned char __c = __s[__len - 1];
3130 const uint32_t __y = static_cast<uint32_t>(__a) +
3131 (static_cast<uint32_t>(__b) << 8);
3132 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3133 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3134 }
3135 return __k2;
3136 }
3137
3138 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003139 const _Size __a = __loadword<_Size>(__s) * __k1;
3140 const _Size __b = __loadword<_Size>(__s + 8);
3141 const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;
3142 const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;
Howard Hinnantf1973402011-12-10 20:28:56 +00003143 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3144 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3145 }
3146
3147 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3148 // Callers do best to use "random-looking" values for a and b.
3149 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3150 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3151 __a += __w;
3152 __b = __rotate(__b + __a + __z, 21);
3153 const _Size __c = __a;
3154 __a += __x;
3155 __a += __y;
3156 __b += __rotate(__a, 44);
3157 return pair<_Size, _Size>(__a + __z, __b + __c);
3158 }
3159
3160 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3161 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3162 const char* __s, _Size __a, _Size __b) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003163 return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),
3164 __loadword<_Size>(__s + 8),
3165 __loadword<_Size>(__s + 16),
3166 __loadword<_Size>(__s + 24),
Howard Hinnantf1973402011-12-10 20:28:56 +00003167 __a,
3168 __b);
3169 }
3170
3171 // Return an 8-byte hash for 33 to 64 bytes.
3172 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003173 _Size __z = __loadword<_Size>(__s + 24);
3174 _Size __a = __loadword<_Size>(__s) +
3175 (__len + __loadword<_Size>(__s + __len - 16)) * __k0;
Howard Hinnantf1973402011-12-10 20:28:56 +00003176 _Size __b = __rotate(__a + __z, 52);
3177 _Size __c = __rotate(__a, 37);
Howard Hinnant6a855442013-07-03 17:39:28 +00003178 __a += __loadword<_Size>(__s + 8);
Howard Hinnantf1973402011-12-10 20:28:56 +00003179 __c += __rotate(__a, 7);
Howard Hinnant6a855442013-07-03 17:39:28 +00003180 __a += __loadword<_Size>(__s + 16);
Howard Hinnantf1973402011-12-10 20:28:56 +00003181 _Size __vf = __a + __z;
3182 _Size __vs = __b + __rotate(__a, 31) + __c;
Howard Hinnant6a855442013-07-03 17:39:28 +00003183 __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);
3184 __z += __loadword<_Size>(__s + __len - 8);
Howard Hinnantf1973402011-12-10 20:28:56 +00003185 __b = __rotate(__a + __z, 52);
3186 __c = __rotate(__a, 37);
Howard Hinnant6a855442013-07-03 17:39:28 +00003187 __a += __loadword<_Size>(__s + __len - 24);
Howard Hinnantf1973402011-12-10 20:28:56 +00003188 __c += __rotate(__a, 7);
Howard Hinnant6a855442013-07-03 17:39:28 +00003189 __a += __loadword<_Size>(__s + __len - 16);
Howard Hinnantf1973402011-12-10 20:28:56 +00003190 _Size __wf = __a + __z;
3191 _Size __ws = __b + __rotate(__a, 31) + __c;
3192 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3193 return __shift_mix(__r * __k0 + __vs) * __k2;
3194 }
Howard Hinnantad71c232011-12-05 00:08:45 +00003195};
3196
Howard Hinnantf1973402011-12-10 20:28:56 +00003197// cityhash64
Howard Hinnantad71c232011-12-05 00:08:45 +00003198template <class _Size>
3199_Size
Marshall Clow91c7f642016-01-11 19:27:10 +00003200__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantad71c232011-12-05 00:08:45 +00003201{
Howard Hinnantf1973402011-12-10 20:28:56 +00003202 const char* __s = static_cast<const char*>(__key);
3203 if (__len <= 32) {
3204 if (__len <= 16) {
3205 return __hash_len_0_to_16(__s, __len);
3206 } else {
3207 return __hash_len_17_to_32(__s, __len);
Howard Hinnantad71c232011-12-05 00:08:45 +00003208 }
Howard Hinnantf1973402011-12-10 20:28:56 +00003209 } else if (__len <= 64) {
3210 return __hash_len_33_to_64(__s, __len);
3211 }
3212
3213 // For strings over 64 bytes we hash the end first, and then as we
3214 // loop we keep 56 bytes of state: v, w, x, y, and z.
Howard Hinnant6a855442013-07-03 17:39:28 +00003215 _Size __x = __loadword<_Size>(__s + __len - 40);
3216 _Size __y = __loadword<_Size>(__s + __len - 16) +
3217 __loadword<_Size>(__s + __len - 56);
3218 _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,
3219 __loadword<_Size>(__s + __len - 24));
Howard Hinnantf1973402011-12-10 20:28:56 +00003220 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3221 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
Howard Hinnant6a855442013-07-03 17:39:28 +00003222 __x = __x * __k1 + __loadword<_Size>(__s);
Howard Hinnantf1973402011-12-10 20:28:56 +00003223
3224 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3225 __len = (__len - 1) & ~static_cast<_Size>(63);
3226 do {
Howard Hinnant6a855442013-07-03 17:39:28 +00003227 __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;
3228 __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;
Howard Hinnantf1973402011-12-10 20:28:56 +00003229 __x ^= __w.second;
Howard Hinnant6a855442013-07-03 17:39:28 +00003230 __y += __v.first + __loadword<_Size>(__s + 40);
Howard Hinnantf1973402011-12-10 20:28:56 +00003231 __z = __rotate(__z + __w.first, 33) * __k1;
3232 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3233 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
Howard Hinnant6a855442013-07-03 17:39:28 +00003234 __y + __loadword<_Size>(__s + 16));
Howard Hinnantf1973402011-12-10 20:28:56 +00003235 std::swap(__z, __x);
3236 __s += 64;
3237 __len -= 64;
3238 } while (__len != 0);
3239 return __hash_len_16(
3240 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3241 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnantad71c232011-12-05 00:08:45 +00003242}
3243
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003244template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3245struct __scalar_hash;
3246
3247template <class _Tp>
3248struct __scalar_hash<_Tp, 0>
3249 : public unary_function<_Tp, size_t>
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003250{
Howard Hinnant756c69b2010-09-22 16:48:34 +00003251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003252 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003253 {
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003254 union
3255 {
3256 _Tp __t;
3257 size_t __a;
3258 } __u;
3259 __u.__a = 0;
3260 __u.__t = __v;
3261 return __u.__a;
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003262 }
3263};
3264
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003265template <class _Tp>
3266struct __scalar_hash<_Tp, 1>
3267 : public unary_function<_Tp, size_t>
3268{
3269 _LIBCPP_INLINE_VISIBILITY
3270 size_t operator()(_Tp __v) const _NOEXCEPT
3271 {
3272 union
3273 {
3274 _Tp __t;
3275 size_t __a;
3276 } __u;
3277 __u.__t = __v;
3278 return __u.__a;
3279 }
3280};
3281
3282template <class _Tp>
3283struct __scalar_hash<_Tp, 2>
3284 : public unary_function<_Tp, size_t>
3285{
3286 _LIBCPP_INLINE_VISIBILITY
3287 size_t operator()(_Tp __v) const _NOEXCEPT
3288 {
3289 union
3290 {
3291 _Tp __t;
3292 struct
3293 {
3294 size_t __a;
3295 size_t __b;
Eric Fiseliere012bf22015-07-18 20:40:46 +00003296 } __s;
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003297 } __u;
3298 __u.__t = __v;
Howard Hinnantf1973402011-12-10 20:28:56 +00003299 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003300 }
3301};
3302
3303template <class _Tp>
3304struct __scalar_hash<_Tp, 3>
3305 : public unary_function<_Tp, size_t>
3306{
3307 _LIBCPP_INLINE_VISIBILITY
3308 size_t operator()(_Tp __v) const _NOEXCEPT
3309 {
3310 union
3311 {
3312 _Tp __t;
3313 struct
3314 {
3315 size_t __a;
3316 size_t __b;
3317 size_t __c;
Eric Fiseliere012bf22015-07-18 20:40:46 +00003318 } __s;
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003319 } __u;
3320 __u.__t = __v;
Howard Hinnantf1973402011-12-10 20:28:56 +00003321 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003322 }
3323};
3324
3325template <class _Tp>
3326struct __scalar_hash<_Tp, 4>
3327 : public unary_function<_Tp, size_t>
3328{
3329 _LIBCPP_INLINE_VISIBILITY
3330 size_t operator()(_Tp __v) const _NOEXCEPT
3331 {
3332 union
3333 {
3334 _Tp __t;
3335 struct
3336 {
3337 size_t __a;
3338 size_t __b;
3339 size_t __c;
3340 size_t __d;
Eric Fiseliere012bf22015-07-18 20:40:46 +00003341 } __s;
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003342 } __u;
3343 __u.__t = __v;
Howard Hinnantf1973402011-12-10 20:28:56 +00003344 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003345 }
3346};
3347
3348template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003349struct _LIBCPP_TYPE_VIS_ONLY hash<_Tp*>
Howard Hinnant9fa30202012-07-30 01:40:57 +00003350 : public unary_function<_Tp*, size_t>
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003351{
Howard Hinnant9fa30202012-07-30 01:40:57 +00003352 _LIBCPP_INLINE_VISIBILITY
3353 size_t operator()(_Tp* __v) const _NOEXCEPT
3354 {
3355 union
3356 {
3357 _Tp* __t;
3358 size_t __a;
3359 } __u;
3360 __u.__t = __v;
3361 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
3362 }
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003363};
3364
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003365template <class _Tp, class _Dp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003366struct _LIBCPP_TYPE_VIS_ONLY hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003367{
3368 typedef unique_ptr<_Tp, _Dp> argument_type;
3369 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003371 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003372 {
3373 typedef typename argument_type::pointer pointer;
3374 return hash<pointer>()(__ptr.get());
3375 }
3376};
3377
Howard Hinnantc51e1022010-05-11 19:42:16 +00003378struct __destruct_n
3379{
3380private:
3381 size_t size;
3382
3383 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003384 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003385 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3386
3387 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003388 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003389 {}
3390
Howard Hinnant719bda32011-05-28 14:41:13 +00003391 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003392 {++size;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003393 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003394 {}
3395
Howard Hinnant719bda32011-05-28 14:41:13 +00003396 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003397 {size = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003398 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003399 {}
3400public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003401 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3402 : size(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003403
3404 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003405 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003406 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003407
3408 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003409 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003410 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003411
3412 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003413 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003414 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003415};
3416
3417template <class _Alloc>
3418class __allocator_destructor
3419{
3420 typedef allocator_traits<_Alloc> __alloc_traits;
3421public:
3422 typedef typename __alloc_traits::pointer pointer;
3423 typedef typename __alloc_traits::size_type size_type;
3424private:
3425 _Alloc& __alloc_;
3426 size_type __s_;
3427public:
3428 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003429 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003430 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003431 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003432 void operator()(pointer __p) _NOEXCEPT
3433 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003434};
3435
3436template <class _InputIterator, class _ForwardIterator>
3437_ForwardIterator
3438uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3439{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003440 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003441#ifndef _LIBCPP_NO_EXCEPTIONS
3442 _ForwardIterator __s = __r;
3443 try
3444 {
3445#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003446 for (; __f != __l; ++__f, (void) ++__r)
3447 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003448#ifndef _LIBCPP_NO_EXCEPTIONS
3449 }
3450 catch (...)
3451 {
3452 for (; __s != __r; ++__s)
3453 __s->~value_type();
3454 throw;
3455 }
3456#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003457 return __r;
3458}
3459
3460template <class _InputIterator, class _Size, class _ForwardIterator>
3461_ForwardIterator
3462uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3463{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003464 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003465#ifndef _LIBCPP_NO_EXCEPTIONS
3466 _ForwardIterator __s = __r;
3467 try
3468 {
3469#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003470 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3471 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003472#ifndef _LIBCPP_NO_EXCEPTIONS
3473 }
3474 catch (...)
3475 {
3476 for (; __s != __r; ++__s)
3477 __s->~value_type();
3478 throw;
3479 }
3480#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003481 return __r;
3482}
3483
3484template <class _ForwardIterator, class _Tp>
3485void
3486uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3487{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003488 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003489#ifndef _LIBCPP_NO_EXCEPTIONS
3490 _ForwardIterator __s = __f;
3491 try
3492 {
3493#endif
3494 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003495 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003496#ifndef _LIBCPP_NO_EXCEPTIONS
3497 }
3498 catch (...)
3499 {
3500 for (; __s != __f; ++__s)
3501 __s->~value_type();
3502 throw;
3503 }
3504#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003505}
3506
3507template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003508_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003509uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3510{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003511 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003512#ifndef _LIBCPP_NO_EXCEPTIONS
3513 _ForwardIterator __s = __f;
3514 try
3515 {
3516#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003517 for (; __n > 0; ++__f, (void) --__n)
3518 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003519#ifndef _LIBCPP_NO_EXCEPTIONS
3520 }
3521 catch (...)
3522 {
3523 for (; __s != __f; ++__s)
3524 __s->~value_type();
3525 throw;
3526 }
3527#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003528 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003529}
3530
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003531#if _LIBCPP_STD_VER > 14
3532
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003533template <class _Tp>
3534inline _LIBCPP_INLINE_VISIBILITY
3535void destroy_at(_Tp* __loc) {
3536 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3537 __loc->~_Tp();
3538}
3539
3540template <class _ForwardIterator>
3541inline _LIBCPP_INLINE_VISIBILITY
3542void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3543 for (; __first != __last; ++__first)
3544 _VSTD::destroy_at(_VSTD::addressof(*__first));
3545}
3546
3547template <class _ForwardIterator, class _Size>
3548inline _LIBCPP_INLINE_VISIBILITY
3549_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3550 for (; __n > 0; (void)++__first, --__n)
3551 _VSTD::destroy_at(_VSTD::addressof(*__first));
3552 return __first;
3553}
3554
Eric Fiselier290c07c2016-10-11 21:13:44 +00003555template <class _ForwardIterator>
3556inline _LIBCPP_INLINE_VISIBILITY
3557void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3558 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3559 auto __idx = __first;
3560#ifndef _LIBCPP_NO_EXCEPTIONS
3561 try {
3562#endif
3563 for (; __idx != __last; ++__idx)
3564 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3565#ifndef _LIBCPP_NO_EXCEPTIONS
3566 } catch (...) {
3567 _VSTD::destroy(__first, __idx);
3568 throw;
3569 }
3570#endif
3571}
3572
3573template <class _ForwardIterator, class _Size>
3574inline _LIBCPP_INLINE_VISIBILITY
3575_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3576 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3577 auto __idx = __first;
3578#ifndef _LIBCPP_NO_EXCEPTIONS
3579 try {
3580#endif
3581 for (; __n > 0; (void)++__idx, --__n)
3582 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3583 return __idx;
3584#ifndef _LIBCPP_NO_EXCEPTIONS
3585 } catch (...) {
3586 _VSTD::destroy(__first, __idx);
3587 throw;
3588 }
3589#endif
3590}
3591
3592
3593template <class _ForwardIterator>
3594inline _LIBCPP_INLINE_VISIBILITY
3595void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3596 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3597 auto __idx = __first;
3598#ifndef _LIBCPP_NO_EXCEPTIONS
3599 try {
3600#endif
3601 for (; __idx != __last; ++__idx)
3602 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3603#ifndef _LIBCPP_NO_EXCEPTIONS
3604 } catch (...) {
3605 _VSTD::destroy(__first, __idx);
3606 throw;
3607 }
3608#endif
3609}
3610
3611template <class _ForwardIterator, class _Size>
3612inline _LIBCPP_INLINE_VISIBILITY
3613_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3614 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3615 auto __idx = __first;
3616#ifndef _LIBCPP_NO_EXCEPTIONS
3617 try {
3618#endif
3619 for (; __n > 0; (void)++__idx, --__n)
3620 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3621 return __idx;
3622#ifndef _LIBCPP_NO_EXCEPTIONS
3623 } catch (...) {
3624 _VSTD::destroy(__first, __idx);
3625 throw;
3626 }
3627#endif
3628}
3629
3630
3631template <class _InputIt, class _ForwardIt>
3632inline _LIBCPP_INLINE_VISIBILITY
3633_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3634 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3635 auto __idx = __first_res;
3636#ifndef _LIBCPP_NO_EXCEPTIONS
3637 try {
3638#endif
3639 for (; __first != __last; (void)++__idx, ++__first)
3640 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3641 return __idx;
3642#ifndef _LIBCPP_NO_EXCEPTIONS
3643 } catch (...) {
3644 _VSTD::destroy(__first_res, __idx);
3645 throw;
3646 }
3647#endif
3648}
3649
3650template <class _InputIt, class _Size, class _ForwardIt>
3651inline _LIBCPP_INLINE_VISIBILITY
3652pair<_InputIt, _ForwardIt>
3653uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3654 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3655 auto __idx = __first_res;
3656#ifndef _LIBCPP_NO_EXCEPTIONS
3657 try {
3658#endif
3659 for (; __n > 0; ++__idx, (void)++__first, --__n)
3660 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3661 return {__first, __idx};
3662#ifndef _LIBCPP_NO_EXCEPTIONS
3663 } catch (...) {
3664 _VSTD::destroy(__first_res, __idx);
3665 throw;
3666 }
3667#endif
3668}
3669
3670
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003671#endif // _LIBCPP_STD_VER > 14
3672
Howard Hinnant756c69b2010-09-22 16:48:34 +00003673class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003674 : public std::exception
3675{
3676public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003677 virtual ~bad_weak_ptr() _NOEXCEPT;
3678 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003679};
3680
Marshall Clow8fea1612016-08-25 15:09:01 +00003681_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
3682void __throw_bad_weak_ptr()
3683{
3684#ifndef _LIBCPP_NO_EXCEPTIONS
3685 throw bad_weak_ptr();
3686#else
3687 _VSTD::abort();
3688#endif
3689}
3690
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003691template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003692
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003693class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003694{
3695 __shared_count(const __shared_count&);
3696 __shared_count& operator=(const __shared_count&);
3697
3698protected:
3699 long __shared_owners_;
3700 virtual ~__shared_count();
3701private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003702 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003703
3704public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003705 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003706 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003707 : __shared_owners_(__refs) {}
3708
Howard Hinnant719bda32011-05-28 14:41:13 +00003709 void __add_shared() _NOEXCEPT;
3710 bool __release_shared() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003711 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003712 long use_count() const _NOEXCEPT {
3713 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3714 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003715};
3716
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003717class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003718 : private __shared_count
3719{
3720 long __shared_weak_owners_;
3721
3722public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003723 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003724 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003725 : __shared_count(__refs),
3726 __shared_weak_owners_(__refs) {}
3727protected:
3728 virtual ~__shared_weak_count();
3729
3730public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003731 void __add_shared() _NOEXCEPT;
3732 void __add_weak() _NOEXCEPT;
3733 void __release_shared() _NOEXCEPT;
3734 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003735 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003736 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3737 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003738
Howard Hinnant807d6332013-02-25 15:50:36 +00003739 // Define the function out only if we build static libc++ without RTTI.
3740 // Otherwise we may break clients who need to compile their projects with
3741 // -fno-rtti and yet link against a libc++.dylib compiled
3742 // without -fno-rtti.
3743#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003744 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003745#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003746private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003747 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003748};
3749
3750template <class _Tp, class _Dp, class _Alloc>
3751class __shared_ptr_pointer
3752 : public __shared_weak_count
3753{
3754 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3755public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003756 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003757 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003758 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003759
Howard Hinnant72f73582010-08-11 17:04:31 +00003760#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003761 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003762#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003763
3764private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003765 virtual void __on_zero_shared() _NOEXCEPT;
3766 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003767};
3768
Howard Hinnant72f73582010-08-11 17:04:31 +00003769#ifndef _LIBCPP_NO_RTTI
3770
Howard Hinnantc51e1022010-05-11 19:42:16 +00003771template <class _Tp, class _Dp, class _Alloc>
3772const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003773__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003774{
Marshall Clowa8dfc942014-11-17 19:05:50 +00003775 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003776}
3777
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003778#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003779
Howard Hinnantc51e1022010-05-11 19:42:16 +00003780template <class _Tp, class _Dp, class _Alloc>
3781void
Howard Hinnant719bda32011-05-28 14:41:13 +00003782__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003783{
3784 __data_.first().second()(__data_.first().first());
3785 __data_.first().second().~_Dp();
3786}
3787
3788template <class _Tp, class _Dp, class _Alloc>
3789void
Howard Hinnant719bda32011-05-28 14:41:13 +00003790__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003791{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003792 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3793 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003794 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3795
Eric Fiselierf8898c82015-02-05 23:01:40 +00003796 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003797 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003798 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003799}
3800
3801template <class _Tp, class _Alloc>
3802class __shared_ptr_emplace
3803 : public __shared_weak_count
3804{
3805 __compressed_pair<_Alloc, _Tp> __data_;
3806public:
3807#ifndef _LIBCPP_HAS_NO_VARIADICS
3808
Howard Hinnant756c69b2010-09-22 16:48:34 +00003809 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003810 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003811 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003812
3813 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003814 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003815 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003816 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3817 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003818
3819#else // _LIBCPP_HAS_NO_VARIADICS
3820
Howard Hinnant756c69b2010-09-22 16:48:34 +00003821 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003822 __shared_ptr_emplace(_Alloc __a)
3823 : __data_(__a) {}
3824
3825 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003826 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003827 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
3828 : __data_(__a, _Tp(__a0)) {}
3829
3830 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003832 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
3833 : __data_(__a, _Tp(__a0, __a1)) {}
3834
3835 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003836 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003837 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
3838 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
3839
3840#endif // _LIBCPP_HAS_NO_VARIADICS
3841
3842private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003843 virtual void __on_zero_shared() _NOEXCEPT;
3844 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003845public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003847 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003848};
3849
3850template <class _Tp, class _Alloc>
3851void
Howard Hinnant719bda32011-05-28 14:41:13 +00003852__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003853{
3854 __data_.second().~_Tp();
3855}
3856
3857template <class _Tp, class _Alloc>
3858void
Howard Hinnant719bda32011-05-28 14:41:13 +00003859__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003860{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003861 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
3862 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003863 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003864 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003865 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003866 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003867}
3868
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003869template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003870
3871template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003872class _LIBCPP_TYPE_VIS_ONLY shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003873{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003874public:
3875 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00003876#if _LIBCPP_STD_VER > 14
3877 typedef weak_ptr<_Tp> weak_type;
3878#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003879private:
3880 element_type* __ptr_;
3881 __shared_weak_count* __cntrl_;
3882
3883 struct __nat {int __for_bool_;};
3884public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003885 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003886 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003887 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003888 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00003889 template<class _Yp>
3890 explicit shared_ptr(_Yp* __p,
3891 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3892 template<class _Yp, class _Dp>
3893 shared_ptr(_Yp* __p, _Dp __d,
3894 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3895 template<class _Yp, class _Dp, class _Alloc>
3896 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
3897 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003898 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
3899 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003900 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003902 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003903 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003904 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003905 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003906 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3907 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003908#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003909 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003910 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003911 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00003912 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
3913 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003914#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003915 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003916 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003917#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003918 template<class _Yp>
3919 shared_ptr(auto_ptr<_Yp>&& __r,
3920 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003921#else
Logan Chiend435f8b2014-01-31 09:30:46 +00003922 template<class _Yp>
3923 shared_ptr(auto_ptr<_Yp> __r,
3924 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003925#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00003926#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003927 template <class _Yp, class _Dp>
3928 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3929 typename enable_if
3930 <
3931 !is_lvalue_reference<_Dp>::value &&
3932 !is_array<_Yp>::value &&
3933 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3934 __nat
3935 >::type = __nat());
3936 template <class _Yp, class _Dp>
3937 shared_ptr(unique_ptr<_Yp, _Dp>&&,
3938 typename enable_if
3939 <
3940 is_lvalue_reference<_Dp>::value &&
3941 !is_array<_Yp>::value &&
3942 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3943 __nat
3944 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003945#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00003946 template <class _Yp, class _Dp>
3947 shared_ptr(unique_ptr<_Yp, _Dp>,
3948 typename enable_if
3949 <
3950 !is_lvalue_reference<_Dp>::value &&
3951 !is_array<_Yp>::value &&
3952 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3953 __nat
3954 >::type = __nat());
3955 template <class _Yp, class _Dp>
3956 shared_ptr(unique_ptr<_Yp, _Dp>,
3957 typename enable_if
3958 <
3959 is_lvalue_reference<_Dp>::value &&
3960 !is_array<_Yp>::value &&
3961 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
3962 __nat
3963 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003964#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003965
3966 ~shared_ptr();
3967
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003968 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003969 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003970 template<class _Yp>
3971 typename enable_if
3972 <
3973 is_convertible<_Yp*, element_type*>::value,
3974 shared_ptr&
3975 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003976 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003977 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00003978#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003979 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003980 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003981 template<class _Yp>
3982 typename enable_if
3983 <
3984 is_convertible<_Yp*, element_type*>::value,
3985 shared_ptr<_Tp>&
3986 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00003987 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003988 operator=(shared_ptr<_Yp>&& __r);
3989 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00003990 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003991 typename enable_if
3992 <
3993 !is_array<_Yp>::value &&
3994 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00003995 shared_ptr
3996 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003997 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00003998#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant741c8fa2012-01-02 17:56:02 +00003999 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00004000 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004001 typename enable_if
4002 <
4003 !is_array<_Yp>::value &&
4004 is_convertible<_Yp*, element_type*>::value,
4005 shared_ptr&
4006 >::type
4007 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004008#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004009 template <class _Yp, class _Dp>
4010 typename enable_if
4011 <
4012 !is_array<_Yp>::value &&
4013 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4014 shared_ptr&
4015 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00004016#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004017 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004018 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00004019#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00004020 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004021 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004022#endif
4023
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004024 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004025 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004026 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004027 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004028 template<class _Yp>
4029 typename enable_if
4030 <
4031 is_convertible<_Yp*, element_type*>::value,
4032 void
4033 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004034 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004035 reset(_Yp* __p);
4036 template<class _Yp, class _Dp>
4037 typename enable_if
4038 <
4039 is_convertible<_Yp*, element_type*>::value,
4040 void
4041 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004042 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004043 reset(_Yp* __p, _Dp __d);
4044 template<class _Yp, class _Dp, class _Alloc>
4045 typename enable_if
4046 <
4047 is_convertible<_Yp*, element_type*>::value,
4048 void
4049 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004050 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004051 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004052
Howard Hinnant756c69b2010-09-22 16:48:34 +00004053 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004054 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004055 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004056 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
4057 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004058 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004059 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004060 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004061 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004063 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004064 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00004065 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00004066 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00004068 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00004069 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00004070 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004071 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00004072 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00004073 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00004074 _LIBCPP_INLINE_VISIBILITY
4075 bool
4076 __owner_equivalent(const shared_ptr& __p) const
4077 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004078
Howard Hinnant72f73582010-08-11 17:04:31 +00004079#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004080 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004081 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004082 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004083 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004084#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004085
4086#ifndef _LIBCPP_HAS_NO_VARIADICS
4087
4088 template<class ..._Args>
4089 static
4090 shared_ptr<_Tp>
4091 make_shared(_Args&& ...__args);
4092
4093 template<class _Alloc, class ..._Args>
4094 static
4095 shared_ptr<_Tp>
4096 allocate_shared(const _Alloc& __a, _Args&& ...__args);
4097
4098#else // _LIBCPP_HAS_NO_VARIADICS
4099
4100 static shared_ptr<_Tp> make_shared();
4101
4102 template<class _A0>
4103 static shared_ptr<_Tp> make_shared(_A0&);
4104
4105 template<class _A0, class _A1>
4106 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
4107
4108 template<class _A0, class _A1, class _A2>
4109 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
4110
4111 template<class _Alloc>
4112 static shared_ptr<_Tp>
4113 allocate_shared(const _Alloc& __a);
4114
4115 template<class _Alloc, class _A0>
4116 static shared_ptr<_Tp>
4117 allocate_shared(const _Alloc& __a, _A0& __a0);
4118
4119 template<class _Alloc, class _A0, class _A1>
4120 static shared_ptr<_Tp>
4121 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
4122
4123 template<class _Alloc, class _A0, class _A1, class _A2>
4124 static shared_ptr<_Tp>
4125 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
4126
4127#endif // _LIBCPP_HAS_NO_VARIADICS
4128
4129private:
4130
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004131 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004133 void
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004134 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
4135 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004136 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004137 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00004138 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00004139 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004140 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
4141 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00004142 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004143 }
4144
Howard Hinnant756c69b2010-09-22 16:48:34 +00004145 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004146 void __enable_weak_this(const volatile void*, const volatile void*) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004147
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004148 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
4149 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004150};
4151
4152template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004153inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004154_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004155shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004156 : __ptr_(0),
4157 __cntrl_(0)
4158{
4159}
4160
4161template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004162inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004163_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004164shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004165 : __ptr_(0),
4166 __cntrl_(0)
4167{
4168}
4169
4170template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004171template<class _Yp>
4172shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4173 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004174 : __ptr_(__p)
4175{
4176 unique_ptr<_Yp> __hold(__p);
4177 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4178 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
4179 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004180 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004181}
4182
4183template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004184template<class _Yp, class _Dp>
4185shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4186 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004187 : __ptr_(__p)
4188{
4189#ifndef _LIBCPP_NO_EXCEPTIONS
4190 try
4191 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004192#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004193 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4194 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004195 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004196#ifndef _LIBCPP_NO_EXCEPTIONS
4197 }
4198 catch (...)
4199 {
4200 __d(__p);
4201 throw;
4202 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004203#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004204}
4205
4206template<class _Tp>
4207template<class _Dp>
4208shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4209 : __ptr_(0)
4210{
4211#ifndef _LIBCPP_NO_EXCEPTIONS
4212 try
4213 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004214#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004215 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4216 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4217#ifndef _LIBCPP_NO_EXCEPTIONS
4218 }
4219 catch (...)
4220 {
4221 __d(__p);
4222 throw;
4223 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004224#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004225}
4226
4227template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004228template<class _Yp, class _Dp, class _Alloc>
4229shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4230 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004231 : __ptr_(__p)
4232{
4233#ifndef _LIBCPP_NO_EXCEPTIONS
4234 try
4235 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004236#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004237 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004238 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004239 typedef __allocator_destructor<_A2> _D2;
4240 _A2 __a2(__a);
4241 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004242 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4243 _CntrlBlk(__p, __d, __a);
4244 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004245 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004246#ifndef _LIBCPP_NO_EXCEPTIONS
4247 }
4248 catch (...)
4249 {
4250 __d(__p);
4251 throw;
4252 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004253#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004254}
4255
4256template<class _Tp>
4257template<class _Dp, class _Alloc>
4258shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4259 : __ptr_(0)
4260{
4261#ifndef _LIBCPP_NO_EXCEPTIONS
4262 try
4263 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004264#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004265 typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004266 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004267 typedef __allocator_destructor<_A2> _D2;
4268 _A2 __a2(__a);
4269 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004270 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4271 _CntrlBlk(__p, __d, __a);
4272 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004273#ifndef _LIBCPP_NO_EXCEPTIONS
4274 }
4275 catch (...)
4276 {
4277 __d(__p);
4278 throw;
4279 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004280#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004281}
4282
4283template<class _Tp>
4284template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004285inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004286shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004287 : __ptr_(__p),
4288 __cntrl_(__r.__cntrl_)
4289{
4290 if (__cntrl_)
4291 __cntrl_->__add_shared();
4292}
4293
4294template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004295inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004296shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004297 : __ptr_(__r.__ptr_),
4298 __cntrl_(__r.__cntrl_)
4299{
4300 if (__cntrl_)
4301 __cntrl_->__add_shared();
4302}
4303
4304template<class _Tp>
4305template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004306inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004307shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4308 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004309 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004310 : __ptr_(__r.__ptr_),
4311 __cntrl_(__r.__cntrl_)
4312{
4313 if (__cntrl_)
4314 __cntrl_->__add_shared();
4315}
4316
Howard Hinnant74279a52010-09-04 23:28:19 +00004317#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004318
4319template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004320inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004321shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004322 : __ptr_(__r.__ptr_),
4323 __cntrl_(__r.__cntrl_)
4324{
4325 __r.__ptr_ = 0;
4326 __r.__cntrl_ = 0;
4327}
4328
4329template<class _Tp>
4330template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004331inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004332shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4333 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004334 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004335 : __ptr_(__r.__ptr_),
4336 __cntrl_(__r.__cntrl_)
4337{
4338 __r.__ptr_ = 0;
4339 __r.__cntrl_ = 0;
4340}
4341
Howard Hinnant74279a52010-09-04 23:28:19 +00004342#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004343
4344template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004345template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004346#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004347shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004348#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004349shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004350#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004351 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004352 : __ptr_(__r.get())
4353{
4354 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4355 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004356 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004357 __r.release();
4358}
4359
4360template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004361template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004362#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004363shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4364#else
4365shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4366#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004367 typename enable_if
4368 <
4369 !is_lvalue_reference<_Dp>::value &&
4370 !is_array<_Yp>::value &&
4371 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4372 __nat
4373 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004374 : __ptr_(__r.get())
4375{
Marshall Clow35cde742015-05-10 13:59:45 +00004376#if _LIBCPP_STD_VER > 11
4377 if (__ptr_ == nullptr)
4378 __cntrl_ = nullptr;
4379 else
4380#endif
4381 {
4382 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4383 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004384 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004385 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004386 __r.release();
4387}
4388
4389template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004390template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004391#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004392shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4393#else
4394shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4395#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004396 typename enable_if
4397 <
4398 is_lvalue_reference<_Dp>::value &&
4399 !is_array<_Yp>::value &&
4400 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4401 __nat
4402 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004403 : __ptr_(__r.get())
4404{
Marshall Clow35cde742015-05-10 13:59:45 +00004405#if _LIBCPP_STD_VER > 11
4406 if (__ptr_ == nullptr)
4407 __cntrl_ = nullptr;
4408 else
4409#endif
4410 {
4411 typedef __shared_ptr_pointer<_Yp*,
4412 reference_wrapper<typename remove_reference<_Dp>::type>,
4413 allocator<_Yp> > _CntrlBlk;
4414 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004415 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004416 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004417 __r.release();
4418}
4419
4420#ifndef _LIBCPP_HAS_NO_VARIADICS
4421
4422template<class _Tp>
4423template<class ..._Args>
4424shared_ptr<_Tp>
4425shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4426{
4427 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4428 typedef allocator<_CntrlBlk> _A2;
4429 typedef __allocator_destructor<_A2> _D2;
4430 _A2 __a2;
4431 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004432 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004433 shared_ptr<_Tp> __r;
4434 __r.__ptr_ = __hold2.get()->get();
4435 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004436 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004437 return __r;
4438}
4439
4440template<class _Tp>
4441template<class _Alloc, class ..._Args>
4442shared_ptr<_Tp>
4443shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4444{
4445 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004446 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004447 typedef __allocator_destructor<_A2> _D2;
4448 _A2 __a2(__a);
4449 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004450 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4451 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004452 shared_ptr<_Tp> __r;
4453 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004454 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004455 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004456 return __r;
4457}
4458
4459#else // _LIBCPP_HAS_NO_VARIADICS
4460
4461template<class _Tp>
4462shared_ptr<_Tp>
4463shared_ptr<_Tp>::make_shared()
4464{
4465 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4466 typedef allocator<_CntrlBlk> _Alloc2;
4467 typedef __allocator_destructor<_Alloc2> _D2;
4468 _Alloc2 __alloc2;
4469 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4470 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4471 shared_ptr<_Tp> __r;
4472 __r.__ptr_ = __hold2.get()->get();
4473 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004474 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004475 return __r;
4476}
4477
4478template<class _Tp>
4479template<class _A0>
4480shared_ptr<_Tp>
4481shared_ptr<_Tp>::make_shared(_A0& __a0)
4482{
4483 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4484 typedef allocator<_CntrlBlk> _Alloc2;
4485 typedef __allocator_destructor<_Alloc2> _D2;
4486 _Alloc2 __alloc2;
4487 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4488 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4489 shared_ptr<_Tp> __r;
4490 __r.__ptr_ = __hold2.get()->get();
4491 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004492 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004493 return __r;
4494}
4495
4496template<class _Tp>
4497template<class _A0, class _A1>
4498shared_ptr<_Tp>
4499shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4500{
4501 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4502 typedef allocator<_CntrlBlk> _Alloc2;
4503 typedef __allocator_destructor<_Alloc2> _D2;
4504 _Alloc2 __alloc2;
4505 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4506 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4507 shared_ptr<_Tp> __r;
4508 __r.__ptr_ = __hold2.get()->get();
4509 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004510 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004511 return __r;
4512}
4513
4514template<class _Tp>
4515template<class _A0, class _A1, class _A2>
4516shared_ptr<_Tp>
4517shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4518{
4519 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4520 typedef allocator<_CntrlBlk> _Alloc2;
4521 typedef __allocator_destructor<_Alloc2> _D2;
4522 _Alloc2 __alloc2;
4523 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4524 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4525 shared_ptr<_Tp> __r;
4526 __r.__ptr_ = __hold2.get()->get();
4527 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004528 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004529 return __r;
4530}
4531
4532template<class _Tp>
4533template<class _Alloc>
4534shared_ptr<_Tp>
4535shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4536{
4537 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004538 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004539 typedef __allocator_destructor<_Alloc2> _D2;
4540 _Alloc2 __alloc2(__a);
4541 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004542 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4543 _CntrlBlk(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004544 shared_ptr<_Tp> __r;
4545 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004546 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004547 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004548 return __r;
4549}
4550
4551template<class _Tp>
4552template<class _Alloc, class _A0>
4553shared_ptr<_Tp>
4554shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4555{
4556 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004557 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004558 typedef __allocator_destructor<_Alloc2> _D2;
4559 _Alloc2 __alloc2(__a);
4560 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004561 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4562 _CntrlBlk(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004563 shared_ptr<_Tp> __r;
4564 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004565 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004566 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004567 return __r;
4568}
4569
4570template<class _Tp>
4571template<class _Alloc, class _A0, class _A1>
4572shared_ptr<_Tp>
4573shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4574{
4575 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004576 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004577 typedef __allocator_destructor<_Alloc2> _D2;
4578 _Alloc2 __alloc2(__a);
4579 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004580 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4581 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004582 shared_ptr<_Tp> __r;
4583 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004584 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004585 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004586 return __r;
4587}
4588
4589template<class _Tp>
4590template<class _Alloc, class _A0, class _A1, class _A2>
4591shared_ptr<_Tp>
4592shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4593{
4594 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004595 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004596 typedef __allocator_destructor<_Alloc2> _D2;
4597 _Alloc2 __alloc2(__a);
4598 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004599 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4600 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004601 shared_ptr<_Tp> __r;
4602 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004603 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004604 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004605 return __r;
4606}
4607
4608#endif // _LIBCPP_HAS_NO_VARIADICS
4609
4610template<class _Tp>
4611shared_ptr<_Tp>::~shared_ptr()
4612{
4613 if (__cntrl_)
4614 __cntrl_->__release_shared();
4615}
4616
4617template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004618inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004619shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004620shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004621{
4622 shared_ptr(__r).swap(*this);
4623 return *this;
4624}
4625
4626template<class _Tp>
4627template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004628inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004629typename enable_if
4630<
4631 is_convertible<_Yp*, _Tp*>::value,
4632 shared_ptr<_Tp>&
4633>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004634shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004635{
4636 shared_ptr(__r).swap(*this);
4637 return *this;
4638}
4639
Howard Hinnant74279a52010-09-04 23:28:19 +00004640#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004641
4642template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004643inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004644shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004645shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004646{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004647 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004648 return *this;
4649}
4650
4651template<class _Tp>
4652template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004653inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004654typename enable_if
4655<
4656 is_convertible<_Yp*, _Tp*>::value,
4657 shared_ptr<_Tp>&
4658>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004659shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4660{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004661 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004662 return *this;
4663}
4664
4665template<class _Tp>
4666template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004667inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004668typename enable_if
4669<
4670 !is_array<_Yp>::value &&
4671 is_convertible<_Yp*, _Tp*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004672 shared_ptr<_Tp>
4673>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004674shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4675{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004676 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004677 return *this;
4678}
4679
4680template<class _Tp>
4681template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004682inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004683typename enable_if
4684<
4685 !is_array<_Yp>::value &&
4686 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4687 shared_ptr<_Tp>&
4688>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004689shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4690{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004691 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004692 return *this;
4693}
4694
Howard Hinnant74279a52010-09-04 23:28:19 +00004695#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004696
4697template<class _Tp>
4698template<class _Yp>
4699inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004700typename enable_if
4701<
4702 !is_array<_Yp>::value &&
4703 is_convertible<_Yp*, _Tp*>::value,
4704 shared_ptr<_Tp>&
4705>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004706shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004707{
4708 shared_ptr(__r).swap(*this);
4709 return *this;
4710}
4711
4712template<class _Tp>
4713template <class _Yp, class _Dp>
4714inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004715typename enable_if
4716<
4717 !is_array<_Yp>::value &&
4718 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4719 shared_ptr<_Tp>&
4720>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004721shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4722{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004723 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004724 return *this;
4725}
4726
Howard Hinnant74279a52010-09-04 23:28:19 +00004727#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004728
4729template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004730inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004731void
Howard Hinnant719bda32011-05-28 14:41:13 +00004732shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004733{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004734 _VSTD::swap(__ptr_, __r.__ptr_);
4735 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004736}
4737
4738template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004739inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004740void
Howard Hinnant719bda32011-05-28 14:41:13 +00004741shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004742{
4743 shared_ptr().swap(*this);
4744}
4745
4746template<class _Tp>
4747template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004748inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004749typename enable_if
4750<
4751 is_convertible<_Yp*, _Tp*>::value,
4752 void
4753>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004754shared_ptr<_Tp>::reset(_Yp* __p)
4755{
4756 shared_ptr(__p).swap(*this);
4757}
4758
4759template<class _Tp>
4760template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004761inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004762typename enable_if
4763<
4764 is_convertible<_Yp*, _Tp*>::value,
4765 void
4766>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004767shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4768{
4769 shared_ptr(__p, __d).swap(*this);
4770}
4771
4772template<class _Tp>
4773template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004774inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004775typename enable_if
4776<
4777 is_convertible<_Yp*, _Tp*>::value,
4778 void
4779>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004780shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4781{
4782 shared_ptr(__p, __d, __a).swap(*this);
4783}
4784
4785#ifndef _LIBCPP_HAS_NO_VARIADICS
4786
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004787template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004788inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004789typename enable_if
4790<
4791 !is_array<_Tp>::value,
4792 shared_ptr<_Tp>
4793>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004794make_shared(_Args&& ...__args)
4795{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004796 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004797}
4798
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004799template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004800inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004801typename enable_if
4802<
4803 !is_array<_Tp>::value,
4804 shared_ptr<_Tp>
4805>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004806allocate_shared(const _Alloc& __a, _Args&& ...__args)
4807{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004808 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004809}
4810
4811#else // _LIBCPP_HAS_NO_VARIADICS
4812
4813template<class _Tp>
4814inline _LIBCPP_INLINE_VISIBILITY
4815shared_ptr<_Tp>
4816make_shared()
4817{
4818 return shared_ptr<_Tp>::make_shared();
4819}
4820
4821template<class _Tp, class _A0>
4822inline _LIBCPP_INLINE_VISIBILITY
4823shared_ptr<_Tp>
4824make_shared(_A0& __a0)
4825{
4826 return shared_ptr<_Tp>::make_shared(__a0);
4827}
4828
4829template<class _Tp, class _A0, class _A1>
4830inline _LIBCPP_INLINE_VISIBILITY
4831shared_ptr<_Tp>
4832make_shared(_A0& __a0, _A1& __a1)
4833{
4834 return shared_ptr<_Tp>::make_shared(__a0, __a1);
4835}
4836
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004837template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004838inline _LIBCPP_INLINE_VISIBILITY
4839shared_ptr<_Tp>
4840make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4841{
4842 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
4843}
4844
4845template<class _Tp, class _Alloc>
4846inline _LIBCPP_INLINE_VISIBILITY
4847shared_ptr<_Tp>
4848allocate_shared(const _Alloc& __a)
4849{
4850 return shared_ptr<_Tp>::allocate_shared(__a);
4851}
4852
4853template<class _Tp, class _Alloc, class _A0>
4854inline _LIBCPP_INLINE_VISIBILITY
4855shared_ptr<_Tp>
4856allocate_shared(const _Alloc& __a, _A0& __a0)
4857{
4858 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
4859}
4860
4861template<class _Tp, class _Alloc, class _A0, class _A1>
4862inline _LIBCPP_INLINE_VISIBILITY
4863shared_ptr<_Tp>
4864allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4865{
4866 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
4867}
4868
4869template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
4870inline _LIBCPP_INLINE_VISIBILITY
4871shared_ptr<_Tp>
4872allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4873{
4874 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
4875}
4876
4877#endif // _LIBCPP_HAS_NO_VARIADICS
4878
4879template<class _Tp, class _Up>
4880inline _LIBCPP_INLINE_VISIBILITY
4881bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004882operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004883{
4884 return __x.get() == __y.get();
4885}
4886
4887template<class _Tp, class _Up>
4888inline _LIBCPP_INLINE_VISIBILITY
4889bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004890operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004891{
4892 return !(__x == __y);
4893}
4894
4895template<class _Tp, class _Up>
4896inline _LIBCPP_INLINE_VISIBILITY
4897bool
Howard Hinnant719bda32011-05-28 14:41:13 +00004898operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004899{
Eric Fiselierf8898c82015-02-05 23:01:40 +00004900 typedef typename common_type<_Tp*, _Up*>::type _Vp;
4901 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00004902}
4903
4904template<class _Tp, class _Up>
4905inline _LIBCPP_INLINE_VISIBILITY
4906bool
4907operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4908{
4909 return __y < __x;
4910}
4911
4912template<class _Tp, class _Up>
4913inline _LIBCPP_INLINE_VISIBILITY
4914bool
4915operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4916{
4917 return !(__y < __x);
4918}
4919
4920template<class _Tp, class _Up>
4921inline _LIBCPP_INLINE_VISIBILITY
4922bool
4923operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
4924{
4925 return !(__x < __y);
4926}
4927
4928template<class _Tp>
4929inline _LIBCPP_INLINE_VISIBILITY
4930bool
4931operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4932{
4933 return !__x;
4934}
4935
4936template<class _Tp>
4937inline _LIBCPP_INLINE_VISIBILITY
4938bool
4939operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4940{
4941 return !__x;
4942}
4943
4944template<class _Tp>
4945inline _LIBCPP_INLINE_VISIBILITY
4946bool
4947operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4948{
4949 return static_cast<bool>(__x);
4950}
4951
4952template<class _Tp>
4953inline _LIBCPP_INLINE_VISIBILITY
4954bool
4955operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4956{
4957 return static_cast<bool>(__x);
4958}
4959
4960template<class _Tp>
4961inline _LIBCPP_INLINE_VISIBILITY
4962bool
4963operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4964{
4965 return less<_Tp*>()(__x.get(), nullptr);
4966}
4967
4968template<class _Tp>
4969inline _LIBCPP_INLINE_VISIBILITY
4970bool
4971operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4972{
4973 return less<_Tp*>()(nullptr, __x.get());
4974}
4975
4976template<class _Tp>
4977inline _LIBCPP_INLINE_VISIBILITY
4978bool
4979operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4980{
4981 return nullptr < __x;
4982}
4983
4984template<class _Tp>
4985inline _LIBCPP_INLINE_VISIBILITY
4986bool
4987operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
4988{
4989 return __x < nullptr;
4990}
4991
4992template<class _Tp>
4993inline _LIBCPP_INLINE_VISIBILITY
4994bool
4995operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
4996{
4997 return !(nullptr < __x);
4998}
4999
5000template<class _Tp>
5001inline _LIBCPP_INLINE_VISIBILITY
5002bool
5003operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5004{
5005 return !(__x < nullptr);
5006}
5007
5008template<class _Tp>
5009inline _LIBCPP_INLINE_VISIBILITY
5010bool
5011operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5012{
5013 return !(__x < nullptr);
5014}
5015
5016template<class _Tp>
5017inline _LIBCPP_INLINE_VISIBILITY
5018bool
5019operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5020{
5021 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005022}
5023
5024template<class _Tp>
5025inline _LIBCPP_INLINE_VISIBILITY
5026void
Howard Hinnant719bda32011-05-28 14:41:13 +00005027swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005028{
5029 __x.swap(__y);
5030}
5031
5032template<class _Tp, class _Up>
5033inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005034typename enable_if
5035<
5036 !is_array<_Tp>::value && !is_array<_Up>::value,
5037 shared_ptr<_Tp>
5038>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005039static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005040{
5041 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
5042}
5043
5044template<class _Tp, class _Up>
5045inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005046typename enable_if
5047<
5048 !is_array<_Tp>::value && !is_array<_Up>::value,
5049 shared_ptr<_Tp>
5050>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005051dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005052{
5053 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
5054 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
5055}
5056
5057template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005058typename enable_if
5059<
5060 is_array<_Tp>::value == is_array<_Up>::value,
5061 shared_ptr<_Tp>
5062>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005063const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005064{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005065 typedef typename remove_extent<_Tp>::type _RTp;
5066 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00005067}
5068
Howard Hinnant72f73582010-08-11 17:04:31 +00005069#ifndef _LIBCPP_NO_RTTI
5070
Howard Hinnantc51e1022010-05-11 19:42:16 +00005071template<class _Dp, class _Tp>
5072inline _LIBCPP_INLINE_VISIBILITY
5073_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00005074get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005075{
5076 return __p.template __get_deleter<_Dp>();
5077}
5078
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005079#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00005080
Howard Hinnantc51e1022010-05-11 19:42:16 +00005081template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005082class _LIBCPP_TYPE_VIS_ONLY weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00005083{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005084public:
5085 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005086private:
5087 element_type* __ptr_;
5088 __shared_weak_count* __cntrl_;
5089
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005090public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005091 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005092 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005093 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00005094 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5095 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005096 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005097 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005098 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00005099 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5100 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005101
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005102#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005103 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005104 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005105 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005106 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5107 _NOEXCEPT;
5108#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005109 ~weak_ptr();
5110
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005111 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005112 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005113 template<class _Yp>
5114 typename enable_if
5115 <
5116 is_convertible<_Yp*, element_type*>::value,
5117 weak_ptr&
5118 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005119 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005120 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
5121
5122#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5123
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005125 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
5126 template<class _Yp>
5127 typename enable_if
5128 <
5129 is_convertible<_Yp*, element_type*>::value,
5130 weak_ptr&
5131 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005133 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
5134
5135#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5136
5137 template<class _Yp>
5138 typename enable_if
5139 <
5140 is_convertible<_Yp*, element_type*>::value,
5141 weak_ptr&
5142 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005143 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005144 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005145
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005146 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005147 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005148 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005149 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005150
Howard Hinnant756c69b2010-09-22 16:48:34 +00005151 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005152 long use_count() const _NOEXCEPT
5153 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005154 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005155 bool expired() const _NOEXCEPT
5156 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5157 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005158 template<class _Up>
5159 _LIBCPP_INLINE_VISIBILITY
5160 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00005161 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005162 template<class _Up>
5163 _LIBCPP_INLINE_VISIBILITY
5164 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00005165 {return __cntrl_ < __r.__cntrl_;}
5166
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005167 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
5168 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005169};
5170
5171template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005172inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005173_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005174weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005175 : __ptr_(0),
5176 __cntrl_(0)
5177{
5178}
5179
5180template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005181inline
Howard Hinnant719bda32011-05-28 14:41:13 +00005182weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005183 : __ptr_(__r.__ptr_),
5184 __cntrl_(__r.__cntrl_)
5185{
5186 if (__cntrl_)
5187 __cntrl_->__add_weak();
5188}
5189
5190template<class _Tp>
5191template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005192inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005193weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005194 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005195 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005196 : __ptr_(__r.__ptr_),
5197 __cntrl_(__r.__cntrl_)
5198{
5199 if (__cntrl_)
5200 __cntrl_->__add_weak();
5201}
5202
5203template<class _Tp>
5204template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005205inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005206weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005207 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005208 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005209 : __ptr_(__r.__ptr_),
5210 __cntrl_(__r.__cntrl_)
5211{
5212 if (__cntrl_)
5213 __cntrl_->__add_weak();
5214}
5215
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005216#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5217
5218template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005219inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005220weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5221 : __ptr_(__r.__ptr_),
5222 __cntrl_(__r.__cntrl_)
5223{
5224 __r.__ptr_ = 0;
5225 __r.__cntrl_ = 0;
5226}
5227
5228template<class _Tp>
5229template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005230inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005231weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5232 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5233 _NOEXCEPT
5234 : __ptr_(__r.__ptr_),
5235 __cntrl_(__r.__cntrl_)
5236{
5237 __r.__ptr_ = 0;
5238 __r.__cntrl_ = 0;
5239}
5240
5241#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5242
Howard Hinnantc51e1022010-05-11 19:42:16 +00005243template<class _Tp>
5244weak_ptr<_Tp>::~weak_ptr()
5245{
5246 if (__cntrl_)
5247 __cntrl_->__release_weak();
5248}
5249
5250template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005251inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005252weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005253weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005254{
5255 weak_ptr(__r).swap(*this);
5256 return *this;
5257}
5258
5259template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005260template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005261inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005262typename enable_if
5263<
5264 is_convertible<_Yp*, _Tp*>::value,
5265 weak_ptr<_Tp>&
5266>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005267weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005268{
5269 weak_ptr(__r).swap(*this);
5270 return *this;
5271}
5272
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005273#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5274
5275template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005276inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005277weak_ptr<_Tp>&
5278weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5279{
5280 weak_ptr(_VSTD::move(__r)).swap(*this);
5281 return *this;
5282}
5283
Howard Hinnantc51e1022010-05-11 19:42:16 +00005284template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005285template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005286inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005287typename enable_if
5288<
5289 is_convertible<_Yp*, _Tp*>::value,
5290 weak_ptr<_Tp>&
5291>::type
5292weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5293{
5294 weak_ptr(_VSTD::move(__r)).swap(*this);
5295 return *this;
5296}
5297
5298#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5299
5300template<class _Tp>
5301template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005302inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005303typename enable_if
5304<
5305 is_convertible<_Yp*, _Tp*>::value,
5306 weak_ptr<_Tp>&
5307>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005308weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005309{
5310 weak_ptr(__r).swap(*this);
5311 return *this;
5312}
5313
5314template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005315inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005316void
Howard Hinnant719bda32011-05-28 14:41:13 +00005317weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005318{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005319 _VSTD::swap(__ptr_, __r.__ptr_);
5320 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005321}
5322
5323template<class _Tp>
5324inline _LIBCPP_INLINE_VISIBILITY
5325void
Howard Hinnant719bda32011-05-28 14:41:13 +00005326swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005327{
5328 __x.swap(__y);
5329}
5330
5331template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005332inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005333void
Howard Hinnant719bda32011-05-28 14:41:13 +00005334weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005335{
5336 weak_ptr().swap(*this);
5337}
5338
5339template<class _Tp>
5340template<class _Yp>
5341shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5342 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
5343 : __ptr_(__r.__ptr_),
5344 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5345{
5346 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00005347 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00005348}
5349
5350template<class _Tp>
5351shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005352weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005353{
5354 shared_ptr<_Tp> __r;
5355 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5356 if (__r.__cntrl_)
5357 __r.__ptr_ = __ptr_;
5358 return __r;
5359}
5360
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005361#if _LIBCPP_STD_VER > 14
5362template <class _Tp = void> struct owner_less;
5363#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005364template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005365#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005366
5367template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005368struct _LIBCPP_TYPE_VIS_ONLY owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005369 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005370{
5371 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005373 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5374 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005376 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5377 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005379 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5380 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005381};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005382
5383template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005384struct _LIBCPP_TYPE_VIS_ONLY owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005385 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5386{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005387 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005388 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005389 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5390 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005392 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5393 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005395 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5396 {return __x.owner_before(__y);}
5397};
5398
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005399#if _LIBCPP_STD_VER > 14
5400template <>
5401struct _LIBCPP_TYPE_VIS_ONLY owner_less<void>
5402{
5403 template <class _Tp, class _Up>
5404 _LIBCPP_INLINE_VISIBILITY
5405 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const
5406 {return __x.owner_before(__y);}
5407 template <class _Tp, class _Up>
5408 _LIBCPP_INLINE_VISIBILITY
5409 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const
5410 {return __x.owner_before(__y);}
5411 template <class _Tp, class _Up>
5412 _LIBCPP_INLINE_VISIBILITY
5413 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const
5414 {return __x.owner_before(__y);}
5415 template <class _Tp, class _Up>
5416 _LIBCPP_INLINE_VISIBILITY
5417 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const
5418 {return __x.owner_before(__y);}
5419 typedef void is_transparent;
5420};
5421#endif
5422
Howard Hinnantc51e1022010-05-11 19:42:16 +00005423template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005424class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005425{
5426 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005427protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005428 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005429 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005431 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005433 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5434 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005435 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005436 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005437public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005438 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005439 shared_ptr<_Tp> shared_from_this()
5440 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005441 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005442 shared_ptr<_Tp const> shared_from_this() const
5443 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005444
Eric Fiselier84006862016-06-02 00:15:35 +00005445#if _LIBCPP_STD_VER > 14
5446 _LIBCPP_INLINE_VISIBILITY
5447 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5448 { return __weak_this_; }
5449
5450 _LIBCPP_INLINE_VISIBILITY
5451 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5452 { return __weak_this_; }
5453#endif // _LIBCPP_STD_VER > 14
5454
Howard Hinnantc51e1022010-05-11 19:42:16 +00005455 template <class _Up> friend class shared_ptr;
5456};
5457
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005458template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005459struct _LIBCPP_TYPE_VIS_ONLY hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005460{
5461 typedef shared_ptr<_Tp> argument_type;
5462 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005463 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005464 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005465 {
5466 return hash<_Tp*>()(__ptr.get());
5467 }
5468};
5469
Howard Hinnantc834c512011-11-29 18:15:50 +00005470template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005471inline _LIBCPP_INLINE_VISIBILITY
5472basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005473operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005474
Eric Fiselier9b492672016-06-18 02:12:53 +00005475
5476#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005477
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005478class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005479{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005480 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005481public:
5482 void lock() _NOEXCEPT;
5483 void unlock() _NOEXCEPT;
5484
5485private:
5486 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5487 __sp_mut(const __sp_mut&);
5488 __sp_mut& operator=(const __sp_mut&);
5489
Howard Hinnant8331b762013-03-06 23:30:19 +00005490 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005491};
5492
Howard Hinnant8331b762013-03-06 23:30:19 +00005493_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005494
5495template <class _Tp>
5496inline _LIBCPP_INLINE_VISIBILITY
5497bool
5498atomic_is_lock_free(const shared_ptr<_Tp>*)
5499{
5500 return false;
5501}
5502
5503template <class _Tp>
5504shared_ptr<_Tp>
5505atomic_load(const shared_ptr<_Tp>* __p)
5506{
5507 __sp_mut& __m = __get_sp_mut(__p);
5508 __m.lock();
5509 shared_ptr<_Tp> __q = *__p;
5510 __m.unlock();
5511 return __q;
5512}
5513
5514template <class _Tp>
5515inline _LIBCPP_INLINE_VISIBILITY
5516shared_ptr<_Tp>
5517atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5518{
5519 return atomic_load(__p);
5520}
5521
5522template <class _Tp>
5523void
5524atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5525{
5526 __sp_mut& __m = __get_sp_mut(__p);
5527 __m.lock();
5528 __p->swap(__r);
5529 __m.unlock();
5530}
5531
5532template <class _Tp>
5533inline _LIBCPP_INLINE_VISIBILITY
5534void
5535atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5536{
5537 atomic_store(__p, __r);
5538}
5539
5540template <class _Tp>
5541shared_ptr<_Tp>
5542atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5543{
5544 __sp_mut& __m = __get_sp_mut(__p);
5545 __m.lock();
5546 __p->swap(__r);
5547 __m.unlock();
5548 return __r;
5549}
5550
5551template <class _Tp>
5552inline _LIBCPP_INLINE_VISIBILITY
5553shared_ptr<_Tp>
5554atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5555{
5556 return atomic_exchange(__p, __r);
5557}
5558
5559template <class _Tp>
5560bool
5561atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5562{
Marshall Clow4201ee82016-05-18 17:50:13 +00005563 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005564 __sp_mut& __m = __get_sp_mut(__p);
5565 __m.lock();
5566 if (__p->__owner_equivalent(*__v))
5567 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005568 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005569 *__p = __w;
5570 __m.unlock();
5571 return true;
5572 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005573 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005574 *__v = *__p;
5575 __m.unlock();
5576 return false;
5577}
5578
5579template <class _Tp>
5580inline _LIBCPP_INLINE_VISIBILITY
5581bool
5582atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5583{
5584 return atomic_compare_exchange_strong(__p, __v, __w);
5585}
5586
5587template <class _Tp>
5588inline _LIBCPP_INLINE_VISIBILITY
5589bool
5590atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5591 shared_ptr<_Tp> __w, memory_order, memory_order)
5592{
5593 return atomic_compare_exchange_strong(__p, __v, __w);
5594}
5595
5596template <class _Tp>
5597inline _LIBCPP_INLINE_VISIBILITY
5598bool
5599atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5600 shared_ptr<_Tp> __w, memory_order, memory_order)
5601{
5602 return atomic_compare_exchange_weak(__p, __v, __w);
5603}
5604
Eric Fiselier9b492672016-06-18 02:12:53 +00005605#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005606
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005607//enum class
Howard Hinnant8331b762013-03-06 23:30:19 +00005608struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005609{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005610 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005611 {
5612 relaxed,
5613 preferred,
5614 strict
5615 };
5616
Howard Hinnant49e145e2012-10-30 19:06:59 +00005617 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005618
Howard Hinnant756c69b2010-09-22 16:48:34 +00005619 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005620 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005621 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005622 operator int() const {return __v_;}
5623};
5624
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005625_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5626_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5627_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
5628_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5629_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005630
5631template <class _Tp>
5632inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005633_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005634undeclare_reachable(_Tp* __p)
5635{
5636 return static_cast<_Tp*>(__undeclare_reachable(__p));
5637}
5638
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005639_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005640
Marshall Clow8982dcd2015-07-13 20:04:56 +00005641// --- Helper for container swap --
5642template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005643inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005644void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5645#if _LIBCPP_STD_VER >= 14
5646 _NOEXCEPT
5647#else
5648 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5649#endif
5650{
5651 __swap_allocator(__a1, __a2,
5652 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5653}
5654
5655template <typename _Alloc>
5656_LIBCPP_INLINE_VISIBILITY
5657void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5658#if _LIBCPP_STD_VER >= 14
5659 _NOEXCEPT
5660#else
5661 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5662#endif
5663{
5664 using _VSTD::swap;
5665 swap(__a1, __a2);
5666}
5667
5668template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005669inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005670void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5671
Marshall Clowff91de82015-08-18 19:51:37 +00005672template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005673struct __noexcept_move_assign_container : public integral_constant<bool,
5674 _Traits::propagate_on_container_move_assignment::value
5675#if _LIBCPP_STD_VER > 14
5676 || _Traits::is_always_equal::value
5677#else
5678 && is_nothrow_move_assignable<_Alloc>::value
5679#endif
5680 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005681
Marshall Clowa591b9a2016-07-11 21:38:08 +00005682
5683#ifndef _LIBCPP_HAS_NO_VARIADICS
5684template <class _Tp, class _Alloc>
5685struct __temp_value {
5686 typedef allocator_traits<_Alloc> _Traits;
5687
5688 typename aligned_storage<sizeof(_Tp), alignof(_Tp)>::type __v;
5689 _Alloc &__a;
5690
5691 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5692 _Tp & get() { return *__addr(); }
5693
5694 template<class... _Args>
5695 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc)
5696 { _Traits::construct(__a, __addr(), _VSTD::forward<_Args>(__args)...); }
5697
5698 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5699 };
5700#endif
5701
Howard Hinnantc51e1022010-05-11 19:42:16 +00005702_LIBCPP_END_NAMESPACE_STD
5703
5704#endif // _LIBCPP_MEMORY