blob: daa17403eec3e9176ac41608528d072df83461ea [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 Hinnant59f73012013-11-13 00:39:22 +00002108#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002109
2110 _LIBCPP_INLINE_VISIBILITY
2111 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2112 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2113 is_nothrow_copy_constructible<_T2>::value)
2114 : __first_(__p.first()),
2115 __second_(__p.second()) {}
2116
2117 _LIBCPP_INLINE_VISIBILITY
2118 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2119 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2120 is_nothrow_copy_assignable<_T2>::value)
2121 {
2122 __first_ = __p.first();
2123 __second_ = __p.second();
2124 return *this;
2125 }
2126
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002127 _LIBCPP_INLINE_VISIBILITY
2128 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant719bda32011-05-28 14:41:13 +00002129 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2130 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002131 : __first_(_VSTD::forward<_T1>(__p.first())),
2132 __second_(_VSTD::forward<_T2>(__p.second())) {}
2133
2134 _LIBCPP_INLINE_VISIBILITY
2135 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2136 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2137 is_nothrow_move_assignable<_T2>::value)
2138 {
2139 __first_ = _VSTD::forward<_T1>(__p.first());
2140 __second_ = _VSTD::forward<_T2>(__p.second());
2141 return *this;
2142 }
2143
Howard Hinnant59f73012013-11-13 00:39:22 +00002144#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002145
Howard Hinnant83b1c052011-12-19 17:58:44 +00002146#ifndef _LIBCPP_HAS_NO_VARIADICS
2147
2148 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2149 _LIBCPP_INLINE_VISIBILITY
2150 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2151 tuple<_Args1...> __first_args,
2152 tuple<_Args2...> __second_args,
2153 __tuple_indices<_I1...>,
2154 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002155 : __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2156 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002157 {}
2158
2159#endif // _LIBCPP_HAS_NO_VARIADICS
2160
Howard Hinnant719bda32011-05-28 14:41:13 +00002161 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2162 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002163
Howard Hinnant719bda32011-05-28 14:41:13 +00002164 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2165 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002166
2167 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002168 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002169 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002170 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002171 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002172 swap(__first_, __x.__first_);
2173 swap(__second_, __x.__second_);
2174 }
2175};
2176
2177template <class _T1, class _T2>
2178class __libcpp_compressed_pair_imp<_T1, _T2, 1>
2179 : private _T1
2180{
2181private:
2182 _T2 __second_;
2183public:
2184 typedef _T1 _T1_param;
2185 typedef _T2 _T2_param;
2186
2187 typedef _T1& _T1_reference;
2188 typedef typename remove_reference<_T2>::type& _T2_reference;
2189
2190 typedef const _T1& _T1_const_reference;
2191 typedef const typename remove_reference<_T2>::type& _T2_const_reference;
2192
Marshall Clowac92bfe2015-07-16 03:05:06 +00002193 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002194 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002195 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002196 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002197 : __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002198 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002199 : _T1(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002200
Howard Hinnant59f73012013-11-13 00:39:22 +00002201#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002202
2203 _LIBCPP_INLINE_VISIBILITY
2204 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2205 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2206 is_nothrow_copy_constructible<_T2>::value)
2207 : _T1(__p.first()), __second_(__p.second()) {}
2208
2209 _LIBCPP_INLINE_VISIBILITY
2210 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2211 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2212 is_nothrow_copy_assignable<_T2>::value)
2213 {
2214 _T1::operator=(__p.first());
2215 __second_ = __p.second();
2216 return *this;
2217 }
2218
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002219 _LIBCPP_INLINE_VISIBILITY
2220 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant719bda32011-05-28 14:41:13 +00002221 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2222 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002223 : _T1(_VSTD::move(__p.first())), __second_(_VSTD::forward<_T2>(__p.second())) {}
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002224
2225 _LIBCPP_INLINE_VISIBILITY
2226 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2227 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2228 is_nothrow_move_assignable<_T2>::value)
2229 {
2230 _T1::operator=(_VSTD::move(__p.first()));
2231 __second_ = _VSTD::forward<_T2>(__p.second());
2232 return *this;
2233 }
2234
Howard Hinnant59f73012013-11-13 00:39:22 +00002235#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002236
Howard Hinnant83b1c052011-12-19 17:58:44 +00002237#ifndef _LIBCPP_HAS_NO_VARIADICS
2238
2239 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2240 _LIBCPP_INLINE_VISIBILITY
2241 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2242 tuple<_Args1...> __first_args,
2243 tuple<_Args2...> __second_args,
2244 __tuple_indices<_I1...>,
2245 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002246 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2247 __second_(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002248 {}
2249
2250#endif // _LIBCPP_HAS_NO_VARIADICS
2251
Howard Hinnant719bda32011-05-28 14:41:13 +00002252 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2253 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254
Howard Hinnant719bda32011-05-28 14:41:13 +00002255 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return __second_;}
2256 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return __second_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002257
2258 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002259 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002260 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002261 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002262 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002263 swap(__second_, __x.__second_);
2264 }
2265};
2266
2267template <class _T1, class _T2>
2268class __libcpp_compressed_pair_imp<_T1, _T2, 2>
2269 : private _T2
2270{
2271private:
2272 _T1 __first_;
2273public:
2274 typedef _T1 _T1_param;
2275 typedef _T2 _T2_param;
2276
2277 typedef typename remove_reference<_T1>::type& _T1_reference;
2278 typedef _T2& _T2_reference;
2279
2280 typedef const typename remove_reference<_T1>::type& _T1_const_reference;
2281 typedef const _T2& _T2_const_reference;
2282
Marshall Clowac92bfe2015-07-16 03:05:06 +00002283 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002284 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002285 : __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002286 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Marshall Clowac92bfe2015-07-16 03:05:06 +00002287 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002288 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnant719bda32011-05-28 14:41:13 +00002289 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2290 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002291 : _T2(_VSTD::forward<_T2_param>(__t2)), __first_(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002292
Howard Hinnant59f73012013-11-13 00:39:22 +00002293#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002294
2295 _LIBCPP_INLINE_VISIBILITY
2296 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2297 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2298 is_nothrow_copy_constructible<_T2>::value)
2299 : _T2(__p.second()), __first_(__p.first()) {}
2300
2301 _LIBCPP_INLINE_VISIBILITY
2302 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2303 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2304 is_nothrow_copy_assignable<_T2>::value)
2305 {
2306 _T2::operator=(__p.second());
2307 __first_ = __p.first();
2308 return *this;
2309 }
2310
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002311 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002312 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002313 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2314 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002315 : _T2(_VSTD::forward<_T2>(__p.second())), __first_(_VSTD::move(__p.first())) {}
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002316
2317 _LIBCPP_INLINE_VISIBILITY
2318 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2319 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2320 is_nothrow_move_assignable<_T2>::value)
2321 {
2322 _T2::operator=(_VSTD::forward<_T2>(__p.second()));
2323 __first_ = _VSTD::move(__p.first());
2324 return *this;
2325 }
2326
Howard Hinnant59f73012013-11-13 00:39:22 +00002327#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002328
Howard Hinnant83b1c052011-12-19 17:58:44 +00002329#ifndef _LIBCPP_HAS_NO_VARIADICS
2330
2331 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2332 _LIBCPP_INLINE_VISIBILITY
2333 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2334 tuple<_Args1...> __first_args,
2335 tuple<_Args2...> __second_args,
2336 __tuple_indices<_I1...>,
2337 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002338 : _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...),
2339 __first_(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002340
2341 {}
2342
2343#endif // _LIBCPP_HAS_NO_VARIADICS
2344
Howard Hinnant719bda32011-05-28 14:41:13 +00002345 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return __first_;}
2346 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return __first_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002347
Howard Hinnant719bda32011-05-28 14:41:13 +00002348 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2349 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002350
2351 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp& __x)
Howard Hinnant719bda32011-05-28 14:41:13 +00002352 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002353 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002354 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002355 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002356 swap(__first_, __x.__first_);
2357 }
2358};
2359
2360template <class _T1, class _T2>
2361class __libcpp_compressed_pair_imp<_T1, _T2, 3>
2362 : private _T1,
2363 private _T2
2364{
2365public:
2366 typedef _T1 _T1_param;
2367 typedef _T2 _T2_param;
2368
2369 typedef _T1& _T1_reference;
2370 typedef _T2& _T2_reference;
2371
2372 typedef const _T1& _T1_const_reference;
2373 typedef const _T2& _T2_const_reference;
2374
2375 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
2376 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002377 : _T1(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002378 _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002379 : _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002380 _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002381 : _T1(_VSTD::forward<_T1_param>(__t1)), _T2(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002382
Howard Hinnant59f73012013-11-13 00:39:22 +00002383#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002384
2385 _LIBCPP_INLINE_VISIBILITY
2386 __libcpp_compressed_pair_imp(const __libcpp_compressed_pair_imp& __p)
2387 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2388 is_nothrow_copy_constructible<_T2>::value)
2389 : _T1(__p.first()), _T2(__p.second()) {}
2390
2391 _LIBCPP_INLINE_VISIBILITY
2392 __libcpp_compressed_pair_imp& operator=(const __libcpp_compressed_pair_imp& __p)
2393 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2394 is_nothrow_copy_assignable<_T2>::value)
2395 {
2396 _T1::operator=(__p.first());
2397 _T2::operator=(__p.second());
2398 return *this;
2399 }
2400
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002401 _LIBCPP_INLINE_VISIBILITY
2402 __libcpp_compressed_pair_imp(__libcpp_compressed_pair_imp&& __p)
Howard Hinnant719bda32011-05-28 14:41:13 +00002403 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2404 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002405 : _T1(_VSTD::move(__p.first())), _T2(_VSTD::move(__p.second())) {}
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002406
2407 _LIBCPP_INLINE_VISIBILITY
2408 __libcpp_compressed_pair_imp& operator=(__libcpp_compressed_pair_imp&& __p)
2409 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2410 is_nothrow_move_assignable<_T2>::value)
2411 {
2412 _T1::operator=(_VSTD::move(__p.first()));
2413 _T2::operator=(_VSTD::move(__p.second()));
2414 return *this;
2415 }
2416
Howard Hinnant59f73012013-11-13 00:39:22 +00002417#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002418
Howard Hinnant83b1c052011-12-19 17:58:44 +00002419#ifndef _LIBCPP_HAS_NO_VARIADICS
2420
2421 template <class... _Args1, class... _Args2, size_t... _I1, size_t... _I2>
2422 _LIBCPP_INLINE_VISIBILITY
2423 __libcpp_compressed_pair_imp(piecewise_construct_t __pc,
2424 tuple<_Args1...> __first_args,
2425 tuple<_Args2...> __second_args,
2426 __tuple_indices<_I1...>,
2427 __tuple_indices<_I2...>)
Marshall Clow60e4aa72014-06-24 00:46:19 +00002428 : _T1(_VSTD::forward<_Args1>(_VSTD::get<_I1>(__first_args))...),
2429 _T2(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
Howard Hinnant83b1c052011-12-19 17:58:44 +00002430 {}
2431
2432#endif // _LIBCPP_HAS_NO_VARIADICS
2433
Howard Hinnant719bda32011-05-28 14:41:13 +00002434 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return *this;}
2435 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002436
Howard Hinnant719bda32011-05-28 14:41:13 +00002437 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return *this;}
2438 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002439
Howard Hinnant28b24882011-12-01 20:21:04 +00002440 _LIBCPP_INLINE_VISIBILITY void swap(__libcpp_compressed_pair_imp&)
Howard Hinnant719bda32011-05-28 14:41:13 +00002441 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002442 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002443 {
2444 }
2445};
2446
2447template <class _T1, class _T2>
2448class __compressed_pair
2449 : private __libcpp_compressed_pair_imp<_T1, _T2>
2450{
2451 typedef __libcpp_compressed_pair_imp<_T1, _T2> base;
2452public:
2453 typedef typename base::_T1_param _T1_param;
2454 typedef typename base::_T2_param _T2_param;
2455
2456 typedef typename base::_T1_reference _T1_reference;
2457 typedef typename base::_T2_reference _T2_reference;
2458
2459 typedef typename base::_T1_const_reference _T1_const_reference;
2460 typedef typename base::_T2_const_reference _T2_const_reference;
2461
2462 _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002463 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002464 : base(_VSTD::forward<_T1_param>(__t1)) {}
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002465 _LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002466 : base(_VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002467 _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002468 : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002469
Howard Hinnant59f73012013-11-13 00:39:22 +00002470#if defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002471
2472 _LIBCPP_INLINE_VISIBILITY
2473 __compressed_pair(const __compressed_pair& __p)
2474 _NOEXCEPT_(is_nothrow_copy_constructible<_T1>::value &&
2475 is_nothrow_copy_constructible<_T2>::value)
2476 : base(__p) {}
2477
2478 _LIBCPP_INLINE_VISIBILITY
2479 __compressed_pair& operator=(const __compressed_pair& __p)
2480 _NOEXCEPT_(is_nothrow_copy_assignable<_T1>::value &&
2481 is_nothrow_copy_assignable<_T2>::value)
2482 {
2483 base::operator=(__p);
2484 return *this;
2485 }
2486
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00002488 __compressed_pair(__compressed_pair&& __p)
Howard Hinnant719bda32011-05-28 14:41:13 +00002489 _NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
2490 is_nothrow_move_constructible<_T2>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002491 : base(_VSTD::move(__p)) {}
Howard Hinnantd3a657f2011-07-01 19:24:36 +00002492
2493 _LIBCPP_INLINE_VISIBILITY
2494 __compressed_pair& operator=(__compressed_pair&& __p)
2495 _NOEXCEPT_(is_nothrow_move_assignable<_T1>::value &&
2496 is_nothrow_move_assignable<_T2>::value)
2497 {
2498 base::operator=(_VSTD::move(__p));
2499 return *this;
2500 }
Howard Hinnant83b1c052011-12-19 17:58:44 +00002501
Howard Hinnant59f73012013-11-13 00:39:22 +00002502#endif // defined(_LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnant866c4b82013-05-06 16:58:36 +00002503
Howard Hinnant83b1c052011-12-19 17:58:44 +00002504#ifndef _LIBCPP_HAS_NO_VARIADICS
2505
2506 template <class... _Args1, class... _Args2>
2507 _LIBCPP_INLINE_VISIBILITY
2508 __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2509 tuple<_Args2...> __second_args)
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002510 : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
Howard Hinnant83b1c052011-12-19 17:58:44 +00002511 typename __make_tuple_indices<sizeof...(_Args1)>::type(),
2512 typename __make_tuple_indices<sizeof...(_Args2) >::type())
2513 {}
2514
2515#endif // _LIBCPP_HAS_NO_VARIADICS
2516
Howard Hinnant719bda32011-05-28 14:41:13 +00002517 _LIBCPP_INLINE_VISIBILITY _T1_reference first() _NOEXCEPT {return base::first();}
2518 _LIBCPP_INLINE_VISIBILITY _T1_const_reference first() const _NOEXCEPT {return base::first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519
Howard Hinnant719bda32011-05-28 14:41:13 +00002520 _LIBCPP_INLINE_VISIBILITY _T2_reference second() _NOEXCEPT {return base::second();}
2521 _LIBCPP_INLINE_VISIBILITY _T2_const_reference second() const _NOEXCEPT {return base::second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002522
Howard Hinnant719bda32011-05-28 14:41:13 +00002523 _LIBCPP_INLINE_VISIBILITY void swap(__compressed_pair& __x)
2524 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002525 __is_nothrow_swappable<_T2>::value)
Howard Hinnant719bda32011-05-28 14:41:13 +00002526 {base::swap(__x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002527};
2528
2529template <class _T1, class _T2>
2530inline _LIBCPP_INLINE_VISIBILITY
2531void
2532swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
Howard Hinnant719bda32011-05-28 14:41:13 +00002533 _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
Marshall Clow9c1d91f2014-06-30 15:35:09 +00002534 __is_nothrow_swappable<_T2>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002535 {__x.swap(__y);}
2536
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002537// __same_or_less_cv_qualified
2538
2539template <class _Ptr1, class _Ptr2,
2540 bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
2541 typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
2542 >::value
2543 >
2544struct __same_or_less_cv_qualified_imp
2545 : is_convertible<_Ptr1, _Ptr2> {};
2546
2547template <class _Ptr1, class _Ptr2>
2548struct __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
2549 : false_type {};
2550
Marshall Clowdf296162014-04-26 05:19:48 +00002551template <class _Ptr1, class _Ptr2, bool = is_pointer<_Ptr1>::value ||
2552 is_same<_Ptr1, _Ptr2>::value ||
2553 __has_element_type<_Ptr1>::value>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002554struct __same_or_less_cv_qualified
2555 : __same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
2556
2557template <class _Ptr1, class _Ptr2>
Marshall Clowdf296162014-04-26 05:19:48 +00002558struct __same_or_less_cv_qualified<_Ptr1, _Ptr2, false>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002559 : false_type {};
2560
2561// default_delete
2562
Howard Hinnantc51e1022010-05-11 19:42:16 +00002563template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002564struct _LIBCPP_TYPE_VIS_ONLY default_delete
Howard Hinnantc51e1022010-05-11 19:42:16 +00002565{
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002566#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2567 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2568#else
2569 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2570#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002571 template <class _Up>
2572 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up>&,
Howard Hinnant719bda32011-05-28 14:41:13 +00002573 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
2574 _LIBCPP_INLINE_VISIBILITY void operator() (_Tp* __ptr) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002575 {
2576 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Howard Hinnant14015fd2013-04-24 19:44:26 +00002577 static_assert(!is_void<_Tp>::value, "default_delete can not delete incomplete type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002578 delete __ptr;
2579 }
2580};
2581
2582template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002583struct _LIBCPP_TYPE_VIS_ONLY default_delete<_Tp[]>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002584{
Howard Hinnant4500ca52011-12-18 21:19:44 +00002585public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002586#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
2587 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default;
2588#else
2589 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT {}
2590#endif
Howard Hinnant4500ca52011-12-18 21:19:44 +00002591 template <class _Up>
2592 _LIBCPP_INLINE_VISIBILITY default_delete(const default_delete<_Up[]>&,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002593 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) _NOEXCEPT {}
Howard Hinnant4500ca52011-12-18 21:19:44 +00002594 template <class _Up>
2595 _LIBCPP_INLINE_VISIBILITY
2596 void operator() (_Up* __ptr,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002597 typename enable_if<__same_or_less_cv_qualified<_Up*, _Tp*>::value>::type* = 0) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002598 {
2599 static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
Marshall Clowff0e4132016-02-25 16:50:51 +00002600 static_assert(!is_void<_Tp>::value, "default_delete can not delete void type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002601 delete [] __ptr;
2602 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002603};
2604
2605template <class _Tp, class _Dp = default_delete<_Tp> >
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002606class _LIBCPP_TYPE_VIS_ONLY unique_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00002607{
2608public:
2609 typedef _Tp element_type;
2610 typedef _Dp deleter_type;
2611 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2612private:
2613 __compressed_pair<pointer, deleter_type> __ptr_;
2614
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002615#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002616 unique_ptr(unique_ptr&);
2617 template <class _Up, class _Ep>
2618 unique_ptr(unique_ptr<_Up, _Ep>&);
2619 unique_ptr& operator=(unique_ptr&);
2620 template <class _Up, class _Ep>
2621 unique_ptr& operator=(unique_ptr<_Up, _Ep>&);
Howard Hinnant74279a52010-09-04 23:28:19 +00002622#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002623
2624 struct __nat {int __for_bool_;};
2625
2626 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2627 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2628public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002629 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002630 : __ptr_(pointer())
2631 {
2632 static_assert(!is_pointer<deleter_type>::value,
2633 "unique_ptr constructed with null function pointer deleter");
2634 }
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002635 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _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 Hinnant719bda32011-05-28 14:41:13 +00002641 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002642 : __ptr_(_VSTD::move(__p))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002643 {
2644 static_assert(!is_pointer<deleter_type>::value,
2645 "unique_ptr constructed with null function pointer deleter");
2646 }
2647
Howard Hinnant74279a52010-09-04 23:28:19 +00002648#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002649 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
2650 is_reference<deleter_type>::value,
2651 deleter_type,
2652 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002653 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002654 : __ptr_(__p, __d) {}
2655
2656 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002657 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002658 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659 {
2660 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2661 }
Howard Hinnant719bda32011-05-28 14:41:13 +00002662 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002663 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002664 template <class _Up, class _Ep>
2665 _LIBCPP_INLINE_VISIBILITY
2666 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2667 typename enable_if
2668 <
2669 !is_array<_Up>::value &&
2670 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2671 is_convertible<_Ep, deleter_type>::value &&
2672 (
2673 !is_reference<deleter_type>::value ||
2674 is_same<deleter_type, _Ep>::value
2675 ),
2676 __nat
Howard Hinnant719bda32011-05-28 14:41:13 +00002677 >::type = __nat()) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002678 : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002679
2680 template <class _Up>
2681 _LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
2682 typename enable_if<
2683 is_convertible<_Up*, _Tp*>::value &&
2684 is_same<_Dp, default_delete<_Tp> >::value,
2685 __nat
Howard Hinnant719bda32011-05-28 14:41:13 +00002686 >::type = __nat()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002687 : __ptr_(__p.release())
2688 {
2689 }
2690
Howard Hinnant719bda32011-05-28 14:41:13 +00002691 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002692 {
2693 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002694 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695 return *this;
2696 }
2697
2698 template <class _Up, class _Ep>
2699 _LIBCPP_INLINE_VISIBILITY
2700 typename enable_if
2701 <
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002702 !is_array<_Up>::value &&
2703 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2704 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002705 unique_ptr&
2706 >::type
Howard Hinnant719bda32011-05-28 14:41:13 +00002707 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002708 {
2709 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002710 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002711 return *this;
2712 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002713#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002714
2715 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2716 {
2717 return __rv<unique_ptr>(*this);
2718 }
2719
2720 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002721 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002722
2723 template <class _Up, class _Ep>
Eric Fiselier6d8c8ed2015-08-28 05:07:06 +00002724 _LIBCPP_INLINE_VISIBILITY
2725 typename enable_if<
2726 !is_array<_Up>::value &&
2727 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2728 is_assignable<deleter_type&, _Ep&>::value,
2729 unique_ptr&
2730 >::type
2731 operator=(unique_ptr<_Up, _Ep> __u)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002732 {
2733 reset(__u.release());
Eric Fiselier6d8c8ed2015-08-28 05:07:06 +00002734 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002735 return *this;
2736 }
2737
2738 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002739 : __ptr_(_VSTD::move(__p), _VSTD::move(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002740
2741 template <class _Up>
2742 _LIBCPP_INLINE_VISIBILITY
2743 typename enable_if<
2744 is_convertible<_Up*, _Tp*>::value &&
2745 is_same<_Dp, default_delete<_Tp> >::value,
2746 unique_ptr&
2747 >::type
2748 operator=(auto_ptr<_Up> __p)
2749 {reset(__p.release()); return *this;}
2750
Howard Hinnant74279a52010-09-04 23:28:19 +00002751#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002752 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2753
Howard Hinnant719bda32011-05-28 14:41:13 +00002754 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002755 {
2756 reset();
2757 return *this;
2758 }
2759
2760 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator*() const
2761 {return *__ptr_.first();}
Howard Hinnant719bda32011-05-28 14:41:13 +00002762 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return __ptr_.first();}
2763 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2764 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2765 {return __ptr_.second();}
2766 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2767 {return __ptr_.second();}
Howard Hinnant86a291f2012-02-21 21:46:43 +00002768 _LIBCPP_INLINE_VISIBILITY
2769 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2770 {return __ptr_.first() != nullptr;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002771
Howard Hinnant719bda32011-05-28 14:41:13 +00002772 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002773 {
2774 pointer __t = __ptr_.first();
2775 __ptr_.first() = pointer();
2776 return __t;
2777 }
2778
Howard Hinnant719bda32011-05-28 14:41:13 +00002779 _LIBCPP_INLINE_VISIBILITY void reset(pointer __p = pointer()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002780 {
2781 pointer __tmp = __ptr_.first();
2782 __ptr_.first() = __p;
2783 if (__tmp)
2784 __ptr_.second()(__tmp);
2785 }
2786
Howard Hinnant719bda32011-05-28 14:41:13 +00002787 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) _NOEXCEPT
2788 {__ptr_.swap(__u.__ptr_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002789};
2790
2791template <class _Tp, class _Dp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002792class _LIBCPP_TYPE_VIS_ONLY unique_ptr<_Tp[], _Dp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793{
2794public:
2795 typedef _Tp element_type;
2796 typedef _Dp deleter_type;
2797 typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
2798private:
2799 __compressed_pair<pointer, deleter_type> __ptr_;
2800
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002801#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002802 unique_ptr(unique_ptr&);
2803 template <class _Up>
2804 unique_ptr(unique_ptr<_Up>&);
2805 unique_ptr& operator=(unique_ptr&);
2806 template <class _Up>
2807 unique_ptr& operator=(unique_ptr<_Up>&);
Howard Hinnant74279a52010-09-04 23:28:19 +00002808#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002809
2810 struct __nat {int __for_bool_;};
2811
2812 typedef typename remove_reference<deleter_type>::type& _Dp_reference;
2813 typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference;
2814public:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002815 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816 : __ptr_(pointer())
2817 {
2818 static_assert(!is_pointer<deleter_type>::value,
2819 "unique_ptr constructed with null function pointer deleter");
2820 }
Howard Hinnantb5fffe82012-07-07 20:56:04 +00002821 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822 : __ptr_(pointer())
2823 {
2824 static_assert(!is_pointer<deleter_type>::value,
2825 "unique_ptr constructed with null function pointer deleter");
2826 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002827#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00002828 template <class _Pp>
2829 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(_Pp __p,
2830 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat()) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002831 : __ptr_(__p)
2832 {
2833 static_assert(!is_pointer<deleter_type>::value,
2834 "unique_ptr constructed with null function pointer deleter");
2835 }
2836
Logan Chiend435f8b2014-01-31 09:30:46 +00002837 template <class _Pp>
Howard Hinnantc834c512011-11-29 18:15:50 +00002838 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p, typename conditional<
Howard Hinnantc51e1022010-05-11 19:42:16 +00002839 is_reference<deleter_type>::value,
2840 deleter_type,
Logan Chiend435f8b2014-01-31 09:30:46 +00002841 typename add_lvalue_reference<const deleter_type>::type>::type __d,
2842 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002843 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002844 : __ptr_(__p, __d) {}
2845
2846 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename conditional<
2847 is_reference<deleter_type>::value,
2848 deleter_type,
2849 typename add_lvalue_reference<const deleter_type>::type>::type __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002850 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002851 : __ptr_(pointer(), __d) {}
2852
Logan Chiend435f8b2014-01-31 09:30:46 +00002853 template <class _Pp>
2854 _LIBCPP_INLINE_VISIBILITY unique_ptr(_Pp __p,
2855 typename remove_reference<deleter_type>::type&& __d,
2856 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, __nat>::type = __nat())
Howard Hinnant719bda32011-05-28 14:41:13 +00002857 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002858 : __ptr_(__p, _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002859 {
2860 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2861 }
2862
2863 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, typename remove_reference<deleter_type>::type&& __d)
Howard Hinnant719bda32011-05-28 14:41:13 +00002864 _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002865 : __ptr_(pointer(), _VSTD::move(__d))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002866 {
2867 static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");
2868 }
2869
Howard Hinnant719bda32011-05-28 14:41:13 +00002870 _LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002871 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002872
Howard Hinnant719bda32011-05-28 14:41:13 +00002873 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002874 {
2875 reset(__u.release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002876 __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002877 return *this;
2878 }
Howard Hinnant4500ca52011-12-18 21:19:44 +00002879
2880 template <class _Up, class _Ep>
2881 _LIBCPP_INLINE_VISIBILITY
2882 unique_ptr(unique_ptr<_Up, _Ep>&& __u,
2883 typename enable_if
2884 <
2885 is_array<_Up>::value &&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002886 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value
Howard Hinnant4500ca52011-12-18 21:19:44 +00002887 && is_convertible<_Ep, deleter_type>::value &&
2888 (
2889 !is_reference<deleter_type>::value ||
2890 is_same<deleter_type, _Ep>::value
2891 ),
2892 __nat
2893 >::type = __nat()
2894 ) _NOEXCEPT
2895 : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {}
2896
2897
2898 template <class _Up, class _Ep>
2899 _LIBCPP_INLINE_VISIBILITY
2900 typename enable_if
2901 <
2902 is_array<_Up>::value &&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00002903 __same_or_less_cv_qualified<typename unique_ptr<_Up, _Ep>::pointer, pointer>::value &&
2904 is_assignable<deleter_type&, _Ep&&>::value,
Howard Hinnant4500ca52011-12-18 21:19:44 +00002905 unique_ptr&
2906 >::type
2907 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2908 {
2909 reset(__u.release());
2910 __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2911 return *this;
2912 }
Howard Hinnant74279a52010-09-04 23:28:19 +00002913#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002914
2915 _LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p)
2916 : __ptr_(__p)
2917 {
2918 static_assert(!is_pointer<deleter_type>::value,
2919 "unique_ptr constructed with null function pointer deleter");
2920 }
2921
2922 _LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002923 : __ptr_(__p, _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002924
2925 _LIBCPP_INLINE_VISIBILITY unique_ptr(nullptr_t, deleter_type __d)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002926 : __ptr_(pointer(), _VSTD::forward<deleter_type>(__d)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927
2928 _LIBCPP_INLINE_VISIBILITY operator __rv<unique_ptr>()
2929 {
2930 return __rv<unique_ptr>(*this);
2931 }
2932
2933 _LIBCPP_INLINE_VISIBILITY unique_ptr(__rv<unique_ptr> __u)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002934 : __ptr_(__u->release(), _VSTD::forward<deleter_type>(__u->get_deleter())) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002935
2936 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(__rv<unique_ptr> __u)
2937 {
2938 reset(__u->release());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002939 __ptr_.second() = _VSTD::forward<deleter_type>(__u->get_deleter());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002940 return *this;
2941 }
2942
Howard Hinnant74279a52010-09-04 23:28:19 +00002943#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002944 _LIBCPP_INLINE_VISIBILITY ~unique_ptr() {reset();}
2945
Howard Hinnant719bda32011-05-28 14:41:13 +00002946 _LIBCPP_INLINE_VISIBILITY unique_ptr& operator=(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002947 {
2948 reset();
2949 return *this;
2950 }
2951
2952 _LIBCPP_INLINE_VISIBILITY typename add_lvalue_reference<_Tp>::type operator[](size_t __i) const
2953 {return __ptr_.first()[__i];}
Howard Hinnant719bda32011-05-28 14:41:13 +00002954 _LIBCPP_INLINE_VISIBILITY pointer get() const _NOEXCEPT {return __ptr_.first();}
2955 _LIBCPP_INLINE_VISIBILITY _Dp_reference get_deleter() _NOEXCEPT
2956 {return __ptr_.second();}
2957 _LIBCPP_INLINE_VISIBILITY _Dp_const_reference get_deleter() const _NOEXCEPT
2958 {return __ptr_.second();}
Howard Hinnant86a291f2012-02-21 21:46:43 +00002959 _LIBCPP_INLINE_VISIBILITY
2960 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
2961 {return __ptr_.first() != nullptr;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002962
Howard Hinnant719bda32011-05-28 14:41:13 +00002963 _LIBCPP_INLINE_VISIBILITY pointer release() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002964 {
2965 pointer __t = __ptr_.first();
2966 __ptr_.first() = pointer();
2967 return __t;
2968 }
2969
Logan Chiend435f8b2014-01-31 09:30:46 +00002970 template <class _Pp>
2971 _LIBCPP_INLINE_VISIBILITY
2972 typename enable_if<__same_or_less_cv_qualified<_Pp, pointer>::value, void>::type
2973 reset(_Pp __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002974 {
2975 pointer __tmp = __ptr_.first();
2976 __ptr_.first() = __p;
2977 if (__tmp)
2978 __ptr_.second()(__tmp);
2979 }
Marshall Clowff0e4132016-02-25 16:50:51 +00002980 _LIBCPP_INLINE_VISIBILITY void reset(nullptr_t = nullptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002981 {
2982 pointer __tmp = __ptr_.first();
2983 __ptr_.first() = nullptr;
2984 if (__tmp)
2985 __ptr_.second()(__tmp);
2986 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002987
2988 _LIBCPP_INLINE_VISIBILITY void swap(unique_ptr& __u) {__ptr_.swap(__u.__ptr_);}
2989private:
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002990
Howard Hinnant74279a52010-09-04 23:28:19 +00002991#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00002992 template <class _Up>
2993 explicit unique_ptr(_Up);
2994 template <class _Up>
2995 unique_ptr(_Up __u,
2996 typename conditional<
2997 is_reference<deleter_type>::value,
2998 deleter_type,
2999 typename add_lvalue_reference<const deleter_type>::type>::type,
3000 typename enable_if
3001 <
3002 is_convertible<_Up, pointer>::value,
3003 __nat
3004 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00003005#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00003006};
3007
3008template <class _Tp, class _Dp>
3009inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6bfed252016-04-21 23:38:59 +00003010typename enable_if<
3011 __is_swappable<_Dp>::value,
3012 void
3013>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00003014swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003015
3016template <class _T1, class _D1, class _T2, class _D2>
3017inline _LIBCPP_INLINE_VISIBILITY
3018bool
3019operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
3020
3021template <class _T1, class _D1, class _T2, class _D2>
3022inline _LIBCPP_INLINE_VISIBILITY
3023bool
3024operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
3025
3026template <class _T1, class _D1, class _T2, class _D2>
3027inline _LIBCPP_INLINE_VISIBILITY
3028bool
Howard Hinnantb17caf92012-02-21 21:02:58 +00003029operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
3030{
3031 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3032 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
Eric Fiselierf8898c82015-02-05 23:01:40 +00003033 typedef typename common_type<_P1, _P2>::type _Vp;
3034 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00003035}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003036
3037template <class _T1, class _D1, class _T2, class _D2>
3038inline _LIBCPP_INLINE_VISIBILITY
3039bool
3040operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
3041
3042template <class _T1, class _D1, class _T2, class _D2>
3043inline _LIBCPP_INLINE_VISIBILITY
3044bool
3045operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
3046
3047template <class _T1, class _D1, class _T2, class _D2>
3048inline _LIBCPP_INLINE_VISIBILITY
3049bool
3050operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
3051
Howard Hinnantb17caf92012-02-21 21:02:58 +00003052template <class _T1, class _D1>
3053inline _LIBCPP_INLINE_VISIBILITY
3054bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003055operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003056{
3057 return !__x;
3058}
3059
3060template <class _T1, class _D1>
3061inline _LIBCPP_INLINE_VISIBILITY
3062bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003063operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003064{
3065 return !__x;
3066}
3067
3068template <class _T1, class _D1>
3069inline _LIBCPP_INLINE_VISIBILITY
3070bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003071operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003072{
3073 return static_cast<bool>(__x);
3074}
3075
3076template <class _T1, class _D1>
3077inline _LIBCPP_INLINE_VISIBILITY
3078bool
Howard Hinnantb5fffe82012-07-07 20:56:04 +00003079operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
Howard Hinnantb17caf92012-02-21 21:02:58 +00003080{
3081 return static_cast<bool>(__x);
3082}
3083
3084template <class _T1, class _D1>
3085inline _LIBCPP_INLINE_VISIBILITY
3086bool
3087operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3088{
3089 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3090 return less<_P1>()(__x.get(), nullptr);
3091}
3092
3093template <class _T1, class _D1>
3094inline _LIBCPP_INLINE_VISIBILITY
3095bool
3096operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3097{
3098 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
3099 return less<_P1>()(nullptr, __x.get());
3100}
3101
3102template <class _T1, class _D1>
3103inline _LIBCPP_INLINE_VISIBILITY
3104bool
3105operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3106{
3107 return nullptr < __x;
3108}
3109
3110template <class _T1, class _D1>
3111inline _LIBCPP_INLINE_VISIBILITY
3112bool
3113operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3114{
3115 return __x < nullptr;
3116}
3117
3118template <class _T1, class _D1>
3119inline _LIBCPP_INLINE_VISIBILITY
3120bool
3121operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3122{
3123 return !(nullptr < __x);
3124}
3125
3126template <class _T1, class _D1>
3127inline _LIBCPP_INLINE_VISIBILITY
3128bool
3129operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3130{
3131 return !(__x < nullptr);
3132}
3133
3134template <class _T1, class _D1>
3135inline _LIBCPP_INLINE_VISIBILITY
3136bool
3137operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
3138{
3139 return !(__x < nullptr);
3140}
3141
3142template <class _T1, class _D1>
3143inline _LIBCPP_INLINE_VISIBILITY
3144bool
3145operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
3146{
3147 return !(nullptr < __x);
3148}
3149
Howard Hinnant9e028f12012-05-01 15:37:54 +00003150#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3151
3152template <class _Tp, class _Dp>
3153inline _LIBCPP_INLINE_VISIBILITY
3154unique_ptr<_Tp, _Dp>
3155move(unique_ptr<_Tp, _Dp>& __t)
3156{
3157 return unique_ptr<_Tp, _Dp>(__rv<unique_ptr<_Tp, _Dp> >(__t));
3158}
3159
3160#endif
3161
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003162#if _LIBCPP_STD_VER > 11
3163
3164template<class _Tp>
3165struct __unique_if
3166{
3167 typedef unique_ptr<_Tp> __unique_single;
3168};
3169
3170template<class _Tp>
3171struct __unique_if<_Tp[]>
3172{
3173 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
3174};
3175
3176template<class _Tp, size_t _Np>
3177struct __unique_if<_Tp[_Np]>
3178{
3179 typedef void __unique_array_known_bound;
3180};
3181
3182template<class _Tp, class... _Args>
Marshall Clow724e3df2013-07-02 20:06:09 +00003183inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003184typename __unique_if<_Tp>::__unique_single
3185make_unique(_Args&&... __args)
3186{
3187 return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
3188}
3189
3190template<class _Tp>
Marshall Clow724e3df2013-07-02 20:06:09 +00003191inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow9e8f4a92013-07-01 18:16:03 +00003192typename __unique_if<_Tp>::__unique_array_unknown_bound
3193make_unique(size_t __n)
3194{
3195 typedef typename remove_extent<_Tp>::type _Up;
3196 return unique_ptr<_Tp>(new _Up[__n]());
3197}
3198
3199template<class _Tp, class... _Args>
3200 typename __unique_if<_Tp>::__unique_array_known_bound
3201 make_unique(_Args&&...) = delete;
3202
3203#endif // _LIBCPP_STD_VER > 11
3204
Howard Hinnant6a855442013-07-03 17:39:28 +00003205template <class _Size>
3206inline _LIBCPP_INLINE_VISIBILITY
3207_Size
3208__loadword(const void* __p)
3209{
3210 _Size __r;
3211 std::memcpy(&__r, __p, sizeof(__r));
3212 return __r;
3213}
3214
Howard Hinnantf1973402011-12-10 20:28:56 +00003215// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
3216// is 64 bits. This is because cityhash64 uses 64bit x 64bit
3217// multiplication, which can be very slow on 32-bit systems.
Howard Hinnantad71c232011-12-05 00:08:45 +00003218template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
Howard Hinnantf1973402011-12-10 20:28:56 +00003219struct __murmur2_or_cityhash;
Howard Hinnantad71c232011-12-05 00:08:45 +00003220
3221template <class _Size>
Howard Hinnantf1973402011-12-10 20:28:56 +00003222struct __murmur2_or_cityhash<_Size, 32>
Howard Hinnantad71c232011-12-05 00:08:45 +00003223{
3224 _Size operator()(const void* __key, _Size __len);
3225};
3226
Howard Hinnantf1973402011-12-10 20:28:56 +00003227// murmur2
Howard Hinnantad71c232011-12-05 00:08:45 +00003228template <class _Size>
3229_Size
Marshall Clow91c7f642016-01-11 19:27:10 +00003230__murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantad71c232011-12-05 00:08:45 +00003231{
3232 const _Size __m = 0x5bd1e995;
3233 const _Size __r = 24;
3234 _Size __h = __len;
3235 const unsigned char* __data = static_cast<const unsigned char*>(__key);
3236 for (; __len >= 4; __data += 4, __len -= 4)
3237 {
Howard Hinnant6a855442013-07-03 17:39:28 +00003238 _Size __k = __loadword<_Size>(__data);
Howard Hinnantad71c232011-12-05 00:08:45 +00003239 __k *= __m;
3240 __k ^= __k >> __r;
3241 __k *= __m;
3242 __h *= __m;
3243 __h ^= __k;
3244 }
3245 switch (__len)
3246 {
3247 case 3:
3248 __h ^= __data[2] << 16;
3249 case 2:
3250 __h ^= __data[1] << 8;
3251 case 1:
3252 __h ^= __data[0];
3253 __h *= __m;
3254 }
3255 __h ^= __h >> 13;
3256 __h *= __m;
3257 __h ^= __h >> 15;
3258 return __h;
3259}
3260
3261template <class _Size>
Howard Hinnantf1973402011-12-10 20:28:56 +00003262struct __murmur2_or_cityhash<_Size, 64>
Howard Hinnantad71c232011-12-05 00:08:45 +00003263{
3264 _Size operator()(const void* __key, _Size __len);
Howard Hinnantf1973402011-12-10 20:28:56 +00003265
3266 private:
3267 // Some primes between 2^63 and 2^64.
3268 static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
3269 static const _Size __k1 = 0xb492b66fbe98f273ULL;
3270 static const _Size __k2 = 0x9ae16a3b2f90404fULL;
3271 static const _Size __k3 = 0xc949d7c7509e6557ULL;
3272
3273 static _Size __rotate(_Size __val, int __shift) {
3274 return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
3275 }
3276
3277 static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
3278 return (__val >> __shift) | (__val << (64 - __shift));
3279 }
3280
3281 static _Size __shift_mix(_Size __val) {
3282 return __val ^ (__val >> 47);
3283 }
3284
3285 static _Size __hash_len_16(_Size __u, _Size __v) {
3286 const _Size __mul = 0x9ddfea08eb382d69ULL;
3287 _Size __a = (__u ^ __v) * __mul;
3288 __a ^= (__a >> 47);
3289 _Size __b = (__v ^ __a) * __mul;
3290 __b ^= (__b >> 47);
3291 __b *= __mul;
3292 return __b;
3293 }
3294
3295 static _Size __hash_len_0_to_16(const char* __s, _Size __len) {
3296 if (__len > 8) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003297 const _Size __a = __loadword<_Size>(__s);
3298 const _Size __b = __loadword<_Size>(__s + __len - 8);
Howard Hinnantf1973402011-12-10 20:28:56 +00003299 return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
3300 }
3301 if (__len >= 4) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003302 const uint32_t __a = __loadword<uint32_t>(__s);
3303 const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
Howard Hinnantf1973402011-12-10 20:28:56 +00003304 return __hash_len_16(__len + (__a << 3), __b);
3305 }
3306 if (__len > 0) {
3307 const unsigned char __a = __s[0];
3308 const unsigned char __b = __s[__len >> 1];
3309 const unsigned char __c = __s[__len - 1];
3310 const uint32_t __y = static_cast<uint32_t>(__a) +
3311 (static_cast<uint32_t>(__b) << 8);
3312 const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
3313 return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
3314 }
3315 return __k2;
3316 }
3317
3318 static _Size __hash_len_17_to_32(const char *__s, _Size __len) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003319 const _Size __a = __loadword<_Size>(__s) * __k1;
3320 const _Size __b = __loadword<_Size>(__s + 8);
3321 const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;
3322 const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;
Howard Hinnantf1973402011-12-10 20:28:56 +00003323 return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
3324 __a + __rotate(__b ^ __k3, 20) - __c + __len);
3325 }
3326
3327 // Return a 16-byte hash for 48 bytes. Quick and dirty.
3328 // Callers do best to use "random-looking" values for a and b.
3329 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3330 _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b) {
3331 __a += __w;
3332 __b = __rotate(__b + __a + __z, 21);
3333 const _Size __c = __a;
3334 __a += __x;
3335 __a += __y;
3336 __b += __rotate(__a, 44);
3337 return pair<_Size, _Size>(__a + __z, __b + __c);
3338 }
3339
3340 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
3341 static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
3342 const char* __s, _Size __a, _Size __b) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003343 return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),
3344 __loadword<_Size>(__s + 8),
3345 __loadword<_Size>(__s + 16),
3346 __loadword<_Size>(__s + 24),
Howard Hinnantf1973402011-12-10 20:28:56 +00003347 __a,
3348 __b);
3349 }
3350
3351 // Return an 8-byte hash for 33 to 64 bytes.
3352 static _Size __hash_len_33_to_64(const char *__s, size_t __len) {
Howard Hinnant6a855442013-07-03 17:39:28 +00003353 _Size __z = __loadword<_Size>(__s + 24);
3354 _Size __a = __loadword<_Size>(__s) +
3355 (__len + __loadword<_Size>(__s + __len - 16)) * __k0;
Howard Hinnantf1973402011-12-10 20:28:56 +00003356 _Size __b = __rotate(__a + __z, 52);
3357 _Size __c = __rotate(__a, 37);
Howard Hinnant6a855442013-07-03 17:39:28 +00003358 __a += __loadword<_Size>(__s + 8);
Howard Hinnantf1973402011-12-10 20:28:56 +00003359 __c += __rotate(__a, 7);
Howard Hinnant6a855442013-07-03 17:39:28 +00003360 __a += __loadword<_Size>(__s + 16);
Howard Hinnantf1973402011-12-10 20:28:56 +00003361 _Size __vf = __a + __z;
3362 _Size __vs = __b + __rotate(__a, 31) + __c;
Howard Hinnant6a855442013-07-03 17:39:28 +00003363 __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);
3364 __z += __loadword<_Size>(__s + __len - 8);
Howard Hinnantf1973402011-12-10 20:28:56 +00003365 __b = __rotate(__a + __z, 52);
3366 __c = __rotate(__a, 37);
Howard Hinnant6a855442013-07-03 17:39:28 +00003367 __a += __loadword<_Size>(__s + __len - 24);
Howard Hinnantf1973402011-12-10 20:28:56 +00003368 __c += __rotate(__a, 7);
Howard Hinnant6a855442013-07-03 17:39:28 +00003369 __a += __loadword<_Size>(__s + __len - 16);
Howard Hinnantf1973402011-12-10 20:28:56 +00003370 _Size __wf = __a + __z;
3371 _Size __ws = __b + __rotate(__a, 31) + __c;
3372 _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
3373 return __shift_mix(__r * __k0 + __vs) * __k2;
3374 }
Howard Hinnantad71c232011-12-05 00:08:45 +00003375};
3376
Howard Hinnantf1973402011-12-10 20:28:56 +00003377// cityhash64
Howard Hinnantad71c232011-12-05 00:08:45 +00003378template <class _Size>
3379_Size
Marshall Clow91c7f642016-01-11 19:27:10 +00003380__murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantad71c232011-12-05 00:08:45 +00003381{
Howard Hinnantf1973402011-12-10 20:28:56 +00003382 const char* __s = static_cast<const char*>(__key);
3383 if (__len <= 32) {
3384 if (__len <= 16) {
3385 return __hash_len_0_to_16(__s, __len);
3386 } else {
3387 return __hash_len_17_to_32(__s, __len);
Howard Hinnantad71c232011-12-05 00:08:45 +00003388 }
Howard Hinnantf1973402011-12-10 20:28:56 +00003389 } else if (__len <= 64) {
3390 return __hash_len_33_to_64(__s, __len);
3391 }
3392
3393 // For strings over 64 bytes we hash the end first, and then as we
3394 // loop we keep 56 bytes of state: v, w, x, y, and z.
Howard Hinnant6a855442013-07-03 17:39:28 +00003395 _Size __x = __loadword<_Size>(__s + __len - 40);
3396 _Size __y = __loadword<_Size>(__s + __len - 16) +
3397 __loadword<_Size>(__s + __len - 56);
3398 _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,
3399 __loadword<_Size>(__s + __len - 24));
Howard Hinnantf1973402011-12-10 20:28:56 +00003400 pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
3401 pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
Howard Hinnant6a855442013-07-03 17:39:28 +00003402 __x = __x * __k1 + __loadword<_Size>(__s);
Howard Hinnantf1973402011-12-10 20:28:56 +00003403
3404 // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
3405 __len = (__len - 1) & ~static_cast<_Size>(63);
3406 do {
Howard Hinnant6a855442013-07-03 17:39:28 +00003407 __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;
3408 __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;
Howard Hinnantf1973402011-12-10 20:28:56 +00003409 __x ^= __w.second;
Howard Hinnant6a855442013-07-03 17:39:28 +00003410 __y += __v.first + __loadword<_Size>(__s + 40);
Howard Hinnantf1973402011-12-10 20:28:56 +00003411 __z = __rotate(__z + __w.first, 33) * __k1;
3412 __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
3413 __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
Howard Hinnant6a855442013-07-03 17:39:28 +00003414 __y + __loadword<_Size>(__s + 16));
Howard Hinnantf1973402011-12-10 20:28:56 +00003415 std::swap(__z, __x);
3416 __s += 64;
3417 __len -= 64;
3418 } while (__len != 0);
3419 return __hash_len_16(
3420 __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
3421 __hash_len_16(__v.second, __w.second) + __x);
Howard Hinnantad71c232011-12-05 00:08:45 +00003422}
3423
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003424template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
3425struct __scalar_hash;
3426
3427template <class _Tp>
3428struct __scalar_hash<_Tp, 0>
3429 : public unary_function<_Tp, size_t>
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003430{
Howard Hinnant756c69b2010-09-22 16:48:34 +00003431 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003432 size_t operator()(_Tp __v) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003433 {
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003434 union
3435 {
3436 _Tp __t;
3437 size_t __a;
3438 } __u;
3439 __u.__a = 0;
3440 __u.__t = __v;
3441 return __u.__a;
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003442 }
3443};
3444
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003445template <class _Tp>
3446struct __scalar_hash<_Tp, 1>
3447 : public unary_function<_Tp, size_t>
3448{
3449 _LIBCPP_INLINE_VISIBILITY
3450 size_t operator()(_Tp __v) const _NOEXCEPT
3451 {
3452 union
3453 {
3454 _Tp __t;
3455 size_t __a;
3456 } __u;
3457 __u.__t = __v;
3458 return __u.__a;
3459 }
3460};
3461
3462template <class _Tp>
3463struct __scalar_hash<_Tp, 2>
3464 : public unary_function<_Tp, size_t>
3465{
3466 _LIBCPP_INLINE_VISIBILITY
3467 size_t operator()(_Tp __v) const _NOEXCEPT
3468 {
3469 union
3470 {
3471 _Tp __t;
3472 struct
3473 {
3474 size_t __a;
3475 size_t __b;
Eric Fiseliere012bf22015-07-18 20:40:46 +00003476 } __s;
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003477 } __u;
3478 __u.__t = __v;
Howard Hinnantf1973402011-12-10 20:28:56 +00003479 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003480 }
3481};
3482
3483template <class _Tp>
3484struct __scalar_hash<_Tp, 3>
3485 : public unary_function<_Tp, size_t>
3486{
3487 _LIBCPP_INLINE_VISIBILITY
3488 size_t operator()(_Tp __v) const _NOEXCEPT
3489 {
3490 union
3491 {
3492 _Tp __t;
3493 struct
3494 {
3495 size_t __a;
3496 size_t __b;
3497 size_t __c;
Eric Fiseliere012bf22015-07-18 20:40:46 +00003498 } __s;
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003499 } __u;
3500 __u.__t = __v;
Howard Hinnantf1973402011-12-10 20:28:56 +00003501 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003502 }
3503};
3504
3505template <class _Tp>
3506struct __scalar_hash<_Tp, 4>
3507 : public unary_function<_Tp, size_t>
3508{
3509 _LIBCPP_INLINE_VISIBILITY
3510 size_t operator()(_Tp __v) const _NOEXCEPT
3511 {
3512 union
3513 {
3514 _Tp __t;
3515 struct
3516 {
3517 size_t __a;
3518 size_t __b;
3519 size_t __c;
3520 size_t __d;
Eric Fiseliere012bf22015-07-18 20:40:46 +00003521 } __s;
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003522 } __u;
3523 __u.__t = __v;
Howard Hinnantf1973402011-12-10 20:28:56 +00003524 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003525 }
3526};
3527
3528template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003529struct _LIBCPP_TYPE_VIS_ONLY hash<_Tp*>
Howard Hinnant9fa30202012-07-30 01:40:57 +00003530 : public unary_function<_Tp*, size_t>
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003531{
Howard Hinnant9fa30202012-07-30 01:40:57 +00003532 _LIBCPP_INLINE_VISIBILITY
3533 size_t operator()(_Tp* __v) const _NOEXCEPT
3534 {
3535 union
3536 {
3537 _Tp* __t;
3538 size_t __a;
3539 } __u;
3540 __u.__t = __v;
3541 return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
3542 }
Howard Hinnant8cf1f942011-12-03 21:11:36 +00003543};
3544
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003545template <class _Tp, class _Dp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003546struct _LIBCPP_TYPE_VIS_ONLY hash<unique_ptr<_Tp, _Dp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003547{
3548 typedef unique_ptr<_Tp, _Dp> argument_type;
3549 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003551 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00003552 {
3553 typedef typename argument_type::pointer pointer;
3554 return hash<pointer>()(__ptr.get());
3555 }
3556};
3557
Howard Hinnantc51e1022010-05-11 19:42:16 +00003558struct __destruct_n
3559{
3560private:
3561 size_t size;
3562
3563 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003564 _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003565 {for (size_t __i = 0; __i < size; ++__i, ++__p) __p->~_Tp();}
3566
3567 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003568 _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003569 {}
3570
Howard Hinnant719bda32011-05-28 14:41:13 +00003571 _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572 {++size;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003573 _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003574 {}
3575
Howard Hinnant719bda32011-05-28 14:41:13 +00003576 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003577 {size = __s;}
Howard Hinnant719bda32011-05-28 14:41:13 +00003578 _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003579 {}
3580public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003581 _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
3582 : size(__s) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003583
3584 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003585 _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003586 {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003587
3588 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003589 _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003590 {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003591
3592 template <class _Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00003593 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003594 {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003595};
3596
3597template <class _Alloc>
3598class __allocator_destructor
3599{
3600 typedef allocator_traits<_Alloc> __alloc_traits;
3601public:
3602 typedef typename __alloc_traits::pointer pointer;
3603 typedef typename __alloc_traits::size_type size_type;
3604private:
3605 _Alloc& __alloc_;
3606 size_type __s_;
3607public:
3608 _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
Howard Hinnant719bda32011-05-28 14:41:13 +00003609 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003610 : __alloc_(__a), __s_(__s) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00003611 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003612 void operator()(pointer __p) _NOEXCEPT
3613 {__alloc_traits::deallocate(__alloc_, __p, __s_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003614};
3615
3616template <class _InputIterator, class _ForwardIterator>
3617_ForwardIterator
3618uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
3619{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003620 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003621#ifndef _LIBCPP_NO_EXCEPTIONS
3622 _ForwardIterator __s = __r;
3623 try
3624 {
3625#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003626 for (; __f != __l; ++__f, (void) ++__r)
3627 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003628#ifndef _LIBCPP_NO_EXCEPTIONS
3629 }
3630 catch (...)
3631 {
3632 for (; __s != __r; ++__s)
3633 __s->~value_type();
3634 throw;
3635 }
3636#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003637 return __r;
3638}
3639
3640template <class _InputIterator, class _Size, class _ForwardIterator>
3641_ForwardIterator
3642uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
3643{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003644 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003645#ifndef _LIBCPP_NO_EXCEPTIONS
3646 _ForwardIterator __s = __r;
3647 try
3648 {
3649#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003650 for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3651 ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003652#ifndef _LIBCPP_NO_EXCEPTIONS
3653 }
3654 catch (...)
3655 {
3656 for (; __s != __r; ++__s)
3657 __s->~value_type();
3658 throw;
3659 }
3660#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003661 return __r;
3662}
3663
3664template <class _ForwardIterator, class _Tp>
3665void
3666uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
3667{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003668 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003669#ifndef _LIBCPP_NO_EXCEPTIONS
3670 _ForwardIterator __s = __f;
3671 try
3672 {
3673#endif
3674 for (; __f != __l; ++__f)
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003675 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003676#ifndef _LIBCPP_NO_EXCEPTIONS
3677 }
3678 catch (...)
3679 {
3680 for (; __s != __f; ++__s)
3681 __s->~value_type();
3682 throw;
3683 }
3684#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003685}
3686
3687template <class _ForwardIterator, class _Size, class _Tp>
Howard Hinnant3c811092010-11-18 16:13:03 +00003688_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003689uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
3690{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003691 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003692#ifndef _LIBCPP_NO_EXCEPTIONS
3693 _ForwardIterator __s = __f;
3694 try
3695 {
3696#endif
Marshall Clow5e9cb8f2015-05-19 15:01:48 +00003697 for (; __n > 0; ++__f, (void) --__n)
3698 ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
Howard Hinnant2fa038c2011-12-29 17:45:35 +00003699#ifndef _LIBCPP_NO_EXCEPTIONS
3700 }
3701 catch (...)
3702 {
3703 for (; __s != __f; ++__s)
3704 __s->~value_type();
3705 throw;
3706 }
3707#endif
Howard Hinnant3c811092010-11-18 16:13:03 +00003708 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003709}
3710
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003711#if _LIBCPP_STD_VER > 14
3712
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003713template <class _Tp>
3714inline _LIBCPP_INLINE_VISIBILITY
3715void destroy_at(_Tp* __loc) {
3716 _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3717 __loc->~_Tp();
3718}
3719
3720template <class _ForwardIterator>
3721inline _LIBCPP_INLINE_VISIBILITY
3722void destroy(_ForwardIterator __first, _ForwardIterator __last) {
3723 for (; __first != __last; ++__first)
3724 _VSTD::destroy_at(_VSTD::addressof(*__first));
3725}
3726
3727template <class _ForwardIterator, class _Size>
3728inline _LIBCPP_INLINE_VISIBILITY
3729_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3730 for (; __n > 0; (void)++__first, --__n)
3731 _VSTD::destroy_at(_VSTD::addressof(*__first));
3732 return __first;
3733}
3734
Eric Fiselier290c07c2016-10-11 21:13:44 +00003735template <class _ForwardIterator>
3736inline _LIBCPP_INLINE_VISIBILITY
3737void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3738 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3739 auto __idx = __first;
3740#ifndef _LIBCPP_NO_EXCEPTIONS
3741 try {
3742#endif
3743 for (; __idx != __last; ++__idx)
3744 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3745#ifndef _LIBCPP_NO_EXCEPTIONS
3746 } catch (...) {
3747 _VSTD::destroy(__first, __idx);
3748 throw;
3749 }
3750#endif
3751}
3752
3753template <class _ForwardIterator, class _Size>
3754inline _LIBCPP_INLINE_VISIBILITY
3755_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3756 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3757 auto __idx = __first;
3758#ifndef _LIBCPP_NO_EXCEPTIONS
3759 try {
3760#endif
3761 for (; __n > 0; (void)++__idx, --__n)
3762 ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3763 return __idx;
3764#ifndef _LIBCPP_NO_EXCEPTIONS
3765 } catch (...) {
3766 _VSTD::destroy(__first, __idx);
3767 throw;
3768 }
3769#endif
3770}
3771
3772
3773template <class _ForwardIterator>
3774inline _LIBCPP_INLINE_VISIBILITY
3775void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3776 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3777 auto __idx = __first;
3778#ifndef _LIBCPP_NO_EXCEPTIONS
3779 try {
3780#endif
3781 for (; __idx != __last; ++__idx)
3782 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3783#ifndef _LIBCPP_NO_EXCEPTIONS
3784 } catch (...) {
3785 _VSTD::destroy(__first, __idx);
3786 throw;
3787 }
3788#endif
3789}
3790
3791template <class _ForwardIterator, class _Size>
3792inline _LIBCPP_INLINE_VISIBILITY
3793_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3794 using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3795 auto __idx = __first;
3796#ifndef _LIBCPP_NO_EXCEPTIONS
3797 try {
3798#endif
3799 for (; __n > 0; (void)++__idx, --__n)
3800 ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3801 return __idx;
3802#ifndef _LIBCPP_NO_EXCEPTIONS
3803 } catch (...) {
3804 _VSTD::destroy(__first, __idx);
3805 throw;
3806 }
3807#endif
3808}
3809
3810
3811template <class _InputIt, class _ForwardIt>
3812inline _LIBCPP_INLINE_VISIBILITY
3813_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3814 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3815 auto __idx = __first_res;
3816#ifndef _LIBCPP_NO_EXCEPTIONS
3817 try {
3818#endif
3819 for (; __first != __last; (void)++__idx, ++__first)
3820 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3821 return __idx;
3822#ifndef _LIBCPP_NO_EXCEPTIONS
3823 } catch (...) {
3824 _VSTD::destroy(__first_res, __idx);
3825 throw;
3826 }
3827#endif
3828}
3829
3830template <class _InputIt, class _Size, class _ForwardIt>
3831inline _LIBCPP_INLINE_VISIBILITY
3832pair<_InputIt, _ForwardIt>
3833uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3834 using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3835 auto __idx = __first_res;
3836#ifndef _LIBCPP_NO_EXCEPTIONS
3837 try {
3838#endif
3839 for (; __n > 0; ++__idx, (void)++__first, --__n)
3840 ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3841 return {__first, __idx};
3842#ifndef _LIBCPP_NO_EXCEPTIONS
3843 } catch (...) {
3844 _VSTD::destroy(__first_res, __idx);
3845 throw;
3846 }
3847#endif
3848}
3849
3850
Eric Fiselier383f6cf2016-07-24 03:51:39 +00003851#endif // _LIBCPP_STD_VER > 14
3852
Howard Hinnant756c69b2010-09-22 16:48:34 +00003853class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00003854 : public std::exception
3855{
3856public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003857 virtual ~bad_weak_ptr() _NOEXCEPT;
3858 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003859};
3860
Marshall Clow8fea1612016-08-25 15:09:01 +00003861_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
3862void __throw_bad_weak_ptr()
3863{
3864#ifndef _LIBCPP_NO_EXCEPTIONS
3865 throw bad_weak_ptr();
3866#else
3867 _VSTD::abort();
3868#endif
3869}
3870
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003871template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003872
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003873class _LIBCPP_TYPE_VIS __shared_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003874{
3875 __shared_count(const __shared_count&);
3876 __shared_count& operator=(const __shared_count&);
3877
3878protected:
3879 long __shared_owners_;
3880 virtual ~__shared_count();
3881private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003882 virtual void __on_zero_shared() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003883
3884public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003885 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003886 explicit __shared_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003887 : __shared_owners_(__refs) {}
3888
Howard Hinnant719bda32011-05-28 14:41:13 +00003889 void __add_shared() _NOEXCEPT;
3890 bool __release_shared() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003891 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier89659d12015-07-07 00:27:16 +00003892 long use_count() const _NOEXCEPT {
3893 return __libcpp_relaxed_load(&__shared_owners_) + 1;
3894 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003895};
3896
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003897class _LIBCPP_TYPE_VIS __shared_weak_count
Howard Hinnantc51e1022010-05-11 19:42:16 +00003898 : private __shared_count
3899{
3900 long __shared_weak_owners_;
3901
3902public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003903 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003904 explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003905 : __shared_count(__refs),
3906 __shared_weak_owners_(__refs) {}
3907protected:
3908 virtual ~__shared_weak_count();
3909
3910public:
Howard Hinnant719bda32011-05-28 14:41:13 +00003911 void __add_shared() _NOEXCEPT;
3912 void __add_weak() _NOEXCEPT;
3913 void __release_shared() _NOEXCEPT;
3914 void __release_weak() _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00003915 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00003916 long use_count() const _NOEXCEPT {return __shared_count::use_count();}
3917 __shared_weak_count* lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003918
Howard Hinnant807d6332013-02-25 15:50:36 +00003919 // Define the function out only if we build static libc++ without RTTI.
3920 // Otherwise we may break clients who need to compile their projects with
3921 // -fno-rtti and yet link against a libc++.dylib compiled
3922 // without -fno-rtti.
3923#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
Howard Hinnant719bda32011-05-28 14:41:13 +00003924 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant807d6332013-02-25 15:50:36 +00003925#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003926private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003927 virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003928};
3929
3930template <class _Tp, class _Dp, class _Alloc>
3931class __shared_ptr_pointer
3932 : public __shared_weak_count
3933{
3934 __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
3935public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00003936 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003937 __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003938 : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003939
Howard Hinnant72f73582010-08-11 17:04:31 +00003940#ifndef _LIBCPP_NO_RTTI
Howard Hinnant719bda32011-05-28 14:41:13 +00003941 virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
Howard Hinnant72f73582010-08-11 17:04:31 +00003942#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003943
3944private:
Howard Hinnant719bda32011-05-28 14:41:13 +00003945 virtual void __on_zero_shared() _NOEXCEPT;
3946 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003947};
3948
Howard Hinnant72f73582010-08-11 17:04:31 +00003949#ifndef _LIBCPP_NO_RTTI
3950
Howard Hinnantc51e1022010-05-11 19:42:16 +00003951template <class _Tp, class _Dp, class _Alloc>
3952const void*
Howard Hinnant719bda32011-05-28 14:41:13 +00003953__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003954{
Marshall Clowa8dfc942014-11-17 19:05:50 +00003955 return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003956}
3957
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003958#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00003959
Howard Hinnantc51e1022010-05-11 19:42:16 +00003960template <class _Tp, class _Dp, class _Alloc>
3961void
Howard Hinnant719bda32011-05-28 14:41:13 +00003962__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003963{
3964 __data_.first().second()(__data_.first().first());
3965 __data_.first().second().~_Dp();
3966}
3967
3968template <class _Tp, class _Dp, class _Alloc>
3969void
Howard Hinnant719bda32011-05-28 14:41:13 +00003970__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003971{
Eric Fiselierf8898c82015-02-05 23:01:40 +00003972 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
3973 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003974 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3975
Eric Fiselierf8898c82015-02-05 23:01:40 +00003976 _Al __a(__data_.second());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003977 __data_.second().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00003978 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003979}
3980
3981template <class _Tp, class _Alloc>
3982class __shared_ptr_emplace
3983 : public __shared_weak_count
3984{
3985 __compressed_pair<_Alloc, _Tp> __data_;
3986public:
3987#ifndef _LIBCPP_HAS_NO_VARIADICS
3988
Howard Hinnant756c69b2010-09-22 16:48:34 +00003989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003990 __shared_ptr_emplace(_Alloc __a)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003991 : __data_(_VSTD::move(__a)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003992
3993 template <class ..._Args>
Howard Hinnant756c69b2010-09-22 16:48:34 +00003994 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003995 __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
Howard Hinnant83b1c052011-12-19 17:58:44 +00003996 : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3997 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003998
3999#else // _LIBCPP_HAS_NO_VARIADICS
4000
Howard Hinnant756c69b2010-09-22 16:48:34 +00004001 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004002 __shared_ptr_emplace(_Alloc __a)
4003 : __data_(__a) {}
4004
4005 template <class _A0>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004006 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004007 __shared_ptr_emplace(_Alloc __a, _A0& __a0)
4008 : __data_(__a, _Tp(__a0)) {}
4009
4010 template <class _A0, class _A1>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004011 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004012 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
4013 : __data_(__a, _Tp(__a0, __a1)) {}
4014
4015 template <class _A0, class _A1, class _A2>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004016 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004017 __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
4018 : __data_(__a, _Tp(__a0, __a1, __a2)) {}
4019
4020#endif // _LIBCPP_HAS_NO_VARIADICS
4021
4022private:
Howard Hinnant719bda32011-05-28 14:41:13 +00004023 virtual void __on_zero_shared() _NOEXCEPT;
4024 virtual void __on_zero_shared_weak() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004025public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00004026 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004027 _Tp* get() _NOEXCEPT {return &__data_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004028};
4029
4030template <class _Tp, class _Alloc>
4031void
Howard Hinnant719bda32011-05-28 14:41:13 +00004032__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004033{
4034 __data_.second().~_Tp();
4035}
4036
4037template <class _Tp, class _Alloc>
4038void
Howard Hinnant719bda32011-05-28 14:41:13 +00004039__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004040{
Eric Fiselierf8898c82015-02-05 23:01:40 +00004041 typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
4042 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004043 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselierf8898c82015-02-05 23:01:40 +00004044 _Al __a(__data_.first());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004045 __data_.first().~_Alloc();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004046 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004047}
4048
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004049template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004050
4051template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004052class _LIBCPP_TYPE_VIS_ONLY shared_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00004053{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004054public:
4055 typedef _Tp element_type;
Eric Fiselierae5b6672016-06-27 01:02:43 +00004056#if _LIBCPP_STD_VER > 14
4057 typedef weak_ptr<_Tp> weak_type;
4058#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004059private:
4060 element_type* __ptr_;
4061 __shared_weak_count* __cntrl_;
4062
4063 struct __nat {int __for_bool_;};
4064public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004065 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004066 _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004068 _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
Logan Chiend435f8b2014-01-31 09:30:46 +00004069 template<class _Yp>
4070 explicit shared_ptr(_Yp* __p,
4071 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
4072 template<class _Yp, class _Dp>
4073 shared_ptr(_Yp* __p, _Dp __d,
4074 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
4075 template<class _Yp, class _Dp, class _Alloc>
4076 shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4077 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004078 template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
4079 template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004080 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
4081 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004082 shared_ptr(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004083 template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004084 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004085 shared_ptr(const shared_ptr<_Yp>& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004086 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
4087 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00004088#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004089 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004090 shared_ptr(shared_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004091 template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00004092 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type = __nat())
4093 _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00004094#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004095 template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004096 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type= __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00004097#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004098 template<class _Yp>
4099 shared_ptr(auto_ptr<_Yp>&& __r,
4100 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004101#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004102 template<class _Yp>
4103 shared_ptr(auto_ptr<_Yp> __r,
4104 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004105#endif
Howard Hinnant74279a52010-09-04 23:28:19 +00004106#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004107 template <class _Yp, class _Dp>
4108 shared_ptr(unique_ptr<_Yp, _Dp>&&,
4109 typename enable_if
4110 <
4111 !is_lvalue_reference<_Dp>::value &&
4112 !is_array<_Yp>::value &&
4113 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4114 __nat
4115 >::type = __nat());
4116 template <class _Yp, class _Dp>
4117 shared_ptr(unique_ptr<_Yp, _Dp>&&,
4118 typename enable_if
4119 <
4120 is_lvalue_reference<_Dp>::value &&
4121 !is_array<_Yp>::value &&
4122 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4123 __nat
4124 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00004125#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004126 template <class _Yp, class _Dp>
4127 shared_ptr(unique_ptr<_Yp, _Dp>,
4128 typename enable_if
4129 <
4130 !is_lvalue_reference<_Dp>::value &&
4131 !is_array<_Yp>::value &&
4132 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4133 __nat
4134 >::type = __nat());
4135 template <class _Yp, class _Dp>
4136 shared_ptr(unique_ptr<_Yp, _Dp>,
4137 typename enable_if
4138 <
4139 is_lvalue_reference<_Dp>::value &&
4140 !is_array<_Yp>::value &&
4141 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4142 __nat
4143 >::type = __nat());
Howard Hinnant74279a52010-09-04 23:28:19 +00004144#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004145
4146 ~shared_ptr();
4147
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004148 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004149 shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004150 template<class _Yp>
4151 typename enable_if
4152 <
4153 is_convertible<_Yp*, element_type*>::value,
4154 shared_ptr&
4155 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004157 operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
Howard Hinnant74279a52010-09-04 23:28:19 +00004158#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004159 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004160 shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004161 template<class _Yp>
4162 typename enable_if
4163 <
4164 is_convertible<_Yp*, element_type*>::value,
4165 shared_ptr<_Tp>&
4166 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004168 operator=(shared_ptr<_Yp>&& __r);
4169 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00004170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004171 typename enable_if
4172 <
4173 !is_array<_Yp>::value &&
4174 is_convertible<_Yp*, element_type*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004175 shared_ptr
4176 >::type&
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004177 operator=(auto_ptr<_Yp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00004178#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004179 template<class _Yp>
Eric Fiselier6585c752016-04-21 22:54:21 +00004180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004181 typename enable_if
4182 <
4183 !is_array<_Yp>::value &&
4184 is_convertible<_Yp*, element_type*>::value,
4185 shared_ptr&
4186 >::type
4187 operator=(auto_ptr<_Yp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004188#endif
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004189 template <class _Yp, class _Dp>
4190 typename enable_if
4191 <
4192 !is_array<_Yp>::value &&
4193 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4194 shared_ptr&
4195 >::type
Howard Hinnant74279a52010-09-04 23:28:19 +00004196#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004197 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004198 operator=(unique_ptr<_Yp, _Dp>&& __r);
Howard Hinnant74279a52010-09-04 23:28:19 +00004199#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov03192872015-12-09 22:32:36 +00004200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004201 operator=(unique_ptr<_Yp, _Dp> __r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004202#endif
4203
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004204 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004205 void swap(shared_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004206 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004207 void reset() _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004208 template<class _Yp>
4209 typename enable_if
4210 <
4211 is_convertible<_Yp*, element_type*>::value,
4212 void
4213 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004214 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004215 reset(_Yp* __p);
4216 template<class _Yp, class _Dp>
4217 typename enable_if
4218 <
4219 is_convertible<_Yp*, element_type*>::value,
4220 void
4221 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004222 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004223 reset(_Yp* __p, _Dp __d);
4224 template<class _Yp, class _Dp, class _Alloc>
4225 typename enable_if
4226 <
4227 is_convertible<_Yp*, element_type*>::value,
4228 void
4229 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004231 reset(_Yp* __p, _Dp __d, _Alloc __a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004232
Howard Hinnant756c69b2010-09-22 16:48:34 +00004233 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004234 element_type* get() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004235 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004236 typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
4237 {return *__ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004238 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004239 element_type* operator->() const _NOEXCEPT {return __ptr_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004241 long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004243 bool unique() const _NOEXCEPT {return use_count() == 1;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00004244 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +00004245 _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
Howard Hinnantc834c512011-11-29 18:15:50 +00004246 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00004248 bool owner_before(shared_ptr<_Up> const& __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00004249 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnantc834c512011-11-29 18:15:50 +00004250 template <class _Up>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00004252 bool owner_before(weak_ptr<_Up> const& __p) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00004253 {return __cntrl_ < __p.__cntrl_;}
Howard Hinnant9fa30202012-07-30 01:40:57 +00004254 _LIBCPP_INLINE_VISIBILITY
4255 bool
4256 __owner_equivalent(const shared_ptr& __p) const
4257 {return __cntrl_ == __p.__cntrl_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004258
Howard Hinnant72f73582010-08-11 17:04:31 +00004259#ifndef _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004260 template <class _Dp>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00004262 _Dp* __get_deleter() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004263 {return (_Dp*)(__cntrl_ ? __cntrl_->__get_deleter(typeid(_Dp)) : 0);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004264#endif // _LIBCPP_NO_RTTI
Howard Hinnantc51e1022010-05-11 19:42:16 +00004265
4266#ifndef _LIBCPP_HAS_NO_VARIADICS
4267
4268 template<class ..._Args>
4269 static
4270 shared_ptr<_Tp>
4271 make_shared(_Args&& ...__args);
4272
4273 template<class _Alloc, class ..._Args>
4274 static
4275 shared_ptr<_Tp>
4276 allocate_shared(const _Alloc& __a, _Args&& ...__args);
4277
4278#else // _LIBCPP_HAS_NO_VARIADICS
4279
4280 static shared_ptr<_Tp> make_shared();
4281
4282 template<class _A0>
4283 static shared_ptr<_Tp> make_shared(_A0&);
4284
4285 template<class _A0, class _A1>
4286 static shared_ptr<_Tp> make_shared(_A0&, _A1&);
4287
4288 template<class _A0, class _A1, class _A2>
4289 static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
4290
4291 template<class _Alloc>
4292 static shared_ptr<_Tp>
4293 allocate_shared(const _Alloc& __a);
4294
4295 template<class _Alloc, class _A0>
4296 static shared_ptr<_Tp>
4297 allocate_shared(const _Alloc& __a, _A0& __a0);
4298
4299 template<class _Alloc, class _A0, class _A1>
4300 static shared_ptr<_Tp>
4301 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
4302
4303 template<class _Alloc, class _A0, class _A1, class _A2>
4304 static shared_ptr<_Tp>
4305 allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
4306
4307#endif // _LIBCPP_HAS_NO_VARIADICS
4308
4309private:
4310
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004311 template <class _Yp, class _OrigPtr>
Howard Hinnant756c69b2010-09-22 16:48:34 +00004312 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004313 void
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004314 __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
4315 _OrigPtr* __ptr) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004316 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004317 typedef typename remove_cv<_Yp>::type _RawYp;
Eric Fiselier84006862016-06-02 00:15:35 +00004318 if (__e && __e->__weak_this_.expired())
Marshall Clow99442fc2015-06-19 15:54:13 +00004319 {
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004320 __e->__weak_this_ = shared_ptr<_RawYp>(*this,
4321 const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
Marshall Clow99442fc2015-06-19 15:54:13 +00004322 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004323 }
4324
Howard Hinnant756c69b2010-09-22 16:48:34 +00004325 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004326 void __enable_weak_this(const volatile void*, const volatile void*) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00004327
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004328 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
4329 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004330};
4331
4332template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004333inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004334_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004335shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004336 : __ptr_(0),
4337 __cntrl_(0)
4338{
4339}
4340
4341template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004342inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00004343_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00004344shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004345 : __ptr_(0),
4346 __cntrl_(0)
4347{
4348}
4349
4350template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004351template<class _Yp>
4352shared_ptr<_Tp>::shared_ptr(_Yp* __p,
4353 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004354 : __ptr_(__p)
4355{
4356 unique_ptr<_Yp> __hold(__p);
4357 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4358 __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>());
4359 __hold.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004360 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004361}
4362
4363template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004364template<class _Yp, class _Dp>
4365shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
4366 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004367 : __ptr_(__p)
4368{
4369#ifndef _LIBCPP_NO_EXCEPTIONS
4370 try
4371 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004372#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004373 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4374 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004375 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004376#ifndef _LIBCPP_NO_EXCEPTIONS
4377 }
4378 catch (...)
4379 {
4380 __d(__p);
4381 throw;
4382 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004383#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004384}
4385
4386template<class _Tp>
4387template<class _Dp>
4388shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
4389 : __ptr_(0)
4390{
4391#ifndef _LIBCPP_NO_EXCEPTIONS
4392 try
4393 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004394#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004395 typedef __shared_ptr_pointer<nullptr_t, _Dp, allocator<_Tp> > _CntrlBlk;
4396 __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>());
4397#ifndef _LIBCPP_NO_EXCEPTIONS
4398 }
4399 catch (...)
4400 {
4401 __d(__p);
4402 throw;
4403 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004404#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004405}
4406
4407template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004408template<class _Yp, class _Dp, class _Alloc>
4409shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
4410 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004411 : __ptr_(__p)
4412{
4413#ifndef _LIBCPP_NO_EXCEPTIONS
4414 try
4415 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004416#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004417 typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004418 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004419 typedef __allocator_destructor<_A2> _D2;
4420 _A2 __a2(__a);
4421 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004422 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4423 _CntrlBlk(__p, __d, __a);
4424 __cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004425 __enable_weak_this(__p, __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004426#ifndef _LIBCPP_NO_EXCEPTIONS
4427 }
4428 catch (...)
4429 {
4430 __d(__p);
4431 throw;
4432 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004433#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004434}
4435
4436template<class _Tp>
4437template<class _Dp, class _Alloc>
4438shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
4439 : __ptr_(0)
4440{
4441#ifndef _LIBCPP_NO_EXCEPTIONS
4442 try
4443 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004444#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004445 typedef __shared_ptr_pointer<nullptr_t, _Dp, _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(__p, __d, __a);
4452 __cntrl_ = _VSTD::addressof(*__hold2.release());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004453#ifndef _LIBCPP_NO_EXCEPTIONS
4454 }
4455 catch (...)
4456 {
4457 __d(__p);
4458 throw;
4459 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004460#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004461}
4462
4463template<class _Tp>
4464template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004465inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004466shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004467 : __ptr_(__p),
4468 __cntrl_(__r.__cntrl_)
4469{
4470 if (__cntrl_)
4471 __cntrl_->__add_shared();
4472}
4473
4474template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004475inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004476shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004477 : __ptr_(__r.__ptr_),
4478 __cntrl_(__r.__cntrl_)
4479{
4480 if (__cntrl_)
4481 __cntrl_->__add_shared();
4482}
4483
4484template<class _Tp>
4485template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004486inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004487shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4488 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004489 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004490 : __ptr_(__r.__ptr_),
4491 __cntrl_(__r.__cntrl_)
4492{
4493 if (__cntrl_)
4494 __cntrl_->__add_shared();
4495}
4496
Howard Hinnant74279a52010-09-04 23:28:19 +00004497#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004498
4499template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004500inline
Howard Hinnant719bda32011-05-28 14:41:13 +00004501shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004502 : __ptr_(__r.__ptr_),
4503 __cntrl_(__r.__cntrl_)
4504{
4505 __r.__ptr_ = 0;
4506 __r.__cntrl_ = 0;
4507}
4508
4509template<class _Tp>
4510template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004511inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004512shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4513 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00004514 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004515 : __ptr_(__r.__ptr_),
4516 __cntrl_(__r.__cntrl_)
4517{
4518 __r.__ptr_ = 0;
4519 __r.__cntrl_ = 0;
4520}
4521
Howard Hinnant74279a52010-09-04 23:28:19 +00004522#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004523
4524template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004525template<class _Yp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004526#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Logan Chiend435f8b2014-01-31 09:30:46 +00004527shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004528#else
Logan Chiend435f8b2014-01-31 09:30:46 +00004529shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004530#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004531 typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004532 : __ptr_(__r.get())
4533{
4534 typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
4535 __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004536 __enable_weak_this(__r.get(), __r.get());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004537 __r.release();
4538}
4539
4540template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004541template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004542#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004543shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4544#else
4545shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4546#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004547 typename enable_if
4548 <
4549 !is_lvalue_reference<_Dp>::value &&
4550 !is_array<_Yp>::value &&
4551 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4552 __nat
4553 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004554 : __ptr_(__r.get())
4555{
Marshall Clow35cde742015-05-10 13:59:45 +00004556#if _LIBCPP_STD_VER > 11
4557 if (__ptr_ == nullptr)
4558 __cntrl_ = nullptr;
4559 else
4560#endif
4561 {
4562 typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk;
4563 __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004564 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004565 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004566 __r.release();
4567}
4568
4569template<class _Tp>
Logan Chiend435f8b2014-01-31 09:30:46 +00004570template <class _Yp, class _Dp>
Howard Hinnant74279a52010-09-04 23:28:19 +00004571#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004572shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
4573#else
4574shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
4575#endif
Logan Chiend435f8b2014-01-31 09:30:46 +00004576 typename enable_if
4577 <
4578 is_lvalue_reference<_Dp>::value &&
4579 !is_array<_Yp>::value &&
4580 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
4581 __nat
4582 >::type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004583 : __ptr_(__r.get())
4584{
Marshall Clow35cde742015-05-10 13:59:45 +00004585#if _LIBCPP_STD_VER > 11
4586 if (__ptr_ == nullptr)
4587 __cntrl_ = nullptr;
4588 else
4589#endif
4590 {
4591 typedef __shared_ptr_pointer<_Yp*,
4592 reference_wrapper<typename remove_reference<_Dp>::type>,
4593 allocator<_Yp> > _CntrlBlk;
4594 __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004595 __enable_weak_this(__r.get(), __r.get());
Marshall Clow35cde742015-05-10 13:59:45 +00004596 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004597 __r.release();
4598}
4599
4600#ifndef _LIBCPP_HAS_NO_VARIADICS
4601
4602template<class _Tp>
4603template<class ..._Args>
4604shared_ptr<_Tp>
4605shared_ptr<_Tp>::make_shared(_Args&& ...__args)
4606{
4607 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4608 typedef allocator<_CntrlBlk> _A2;
4609 typedef __allocator_destructor<_A2> _D2;
4610 _A2 __a2;
4611 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004612 ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004613 shared_ptr<_Tp> __r;
4614 __r.__ptr_ = __hold2.get()->get();
4615 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004616 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004617 return __r;
4618}
4619
4620template<class _Tp>
4621template<class _Alloc, class ..._Args>
4622shared_ptr<_Tp>
4623shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
4624{
4625 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004626 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004627 typedef __allocator_destructor<_A2> _D2;
4628 _A2 __a2(__a);
4629 unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004630 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4631 _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004632 shared_ptr<_Tp> __r;
4633 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004634 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004635 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004636 return __r;
4637}
4638
4639#else // _LIBCPP_HAS_NO_VARIADICS
4640
4641template<class _Tp>
4642shared_ptr<_Tp>
4643shared_ptr<_Tp>::make_shared()
4644{
4645 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4646 typedef allocator<_CntrlBlk> _Alloc2;
4647 typedef __allocator_destructor<_Alloc2> _D2;
4648 _Alloc2 __alloc2;
4649 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4650 ::new(__hold2.get()) _CntrlBlk(__alloc2);
4651 shared_ptr<_Tp> __r;
4652 __r.__ptr_ = __hold2.get()->get();
4653 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004654 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004655 return __r;
4656}
4657
4658template<class _Tp>
4659template<class _A0>
4660shared_ptr<_Tp>
4661shared_ptr<_Tp>::make_shared(_A0& __a0)
4662{
4663 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4664 typedef allocator<_CntrlBlk> _Alloc2;
4665 typedef __allocator_destructor<_Alloc2> _D2;
4666 _Alloc2 __alloc2;
4667 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4668 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
4669 shared_ptr<_Tp> __r;
4670 __r.__ptr_ = __hold2.get()->get();
4671 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004672 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004673 return __r;
4674}
4675
4676template<class _Tp>
4677template<class _A0, class _A1>
4678shared_ptr<_Tp>
4679shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
4680{
4681 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4682 typedef allocator<_CntrlBlk> _Alloc2;
4683 typedef __allocator_destructor<_Alloc2> _D2;
4684 _Alloc2 __alloc2;
4685 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4686 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
4687 shared_ptr<_Tp> __r;
4688 __r.__ptr_ = __hold2.get()->get();
4689 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004690 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004691 return __r;
4692}
4693
4694template<class _Tp>
4695template<class _A0, class _A1, class _A2>
4696shared_ptr<_Tp>
4697shared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
4698{
4699 typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
4700 typedef allocator<_CntrlBlk> _Alloc2;
4701 typedef __allocator_destructor<_Alloc2> _D2;
4702 _Alloc2 __alloc2;
4703 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4704 ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
4705 shared_ptr<_Tp> __r;
4706 __r.__ptr_ = __hold2.get()->get();
4707 __r.__cntrl_ = __hold2.release();
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004708 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004709 return __r;
4710}
4711
4712template<class _Tp>
4713template<class _Alloc>
4714shared_ptr<_Tp>
4715shared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
4716{
4717 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004718 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004719 typedef __allocator_destructor<_Alloc2> _D2;
4720 _Alloc2 __alloc2(__a);
4721 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004722 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4723 _CntrlBlk(__a);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004724 shared_ptr<_Tp> __r;
4725 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004726 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004727 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004728 return __r;
4729}
4730
4731template<class _Tp>
4732template<class _Alloc, class _A0>
4733shared_ptr<_Tp>
4734shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
4735{
4736 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004737 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004738 typedef __allocator_destructor<_Alloc2> _D2;
4739 _Alloc2 __alloc2(__a);
4740 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004741 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4742 _CntrlBlk(__a, __a0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004743 shared_ptr<_Tp> __r;
4744 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004745 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004746 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004747 return __r;
4748}
4749
4750template<class _Tp>
4751template<class _Alloc, class _A0, class _A1>
4752shared_ptr<_Tp>
4753shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
4754{
4755 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004756 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004757 typedef __allocator_destructor<_Alloc2> _D2;
4758 _Alloc2 __alloc2(__a);
4759 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004760 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4761 _CntrlBlk(__a, __a0, __a1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004762 shared_ptr<_Tp> __r;
4763 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004764 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004765 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004766 return __r;
4767}
4768
4769template<class _Tp>
4770template<class _Alloc, class _A0, class _A1, class _A2>
4771shared_ptr<_Tp>
4772shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
4773{
4774 typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004775 typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004776 typedef __allocator_destructor<_Alloc2> _D2;
4777 _Alloc2 __alloc2(__a);
4778 unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004779 ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4780 _CntrlBlk(__a, __a0, __a1, __a2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004781 shared_ptr<_Tp> __r;
4782 __r.__ptr_ = __hold2.get()->get();
Eric Fiselier6bd814f2014-10-23 04:12:28 +00004783 __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
Eric Fiselierf16c93f2016-06-26 23:56:32 +00004784 __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004785 return __r;
4786}
4787
4788#endif // _LIBCPP_HAS_NO_VARIADICS
4789
4790template<class _Tp>
4791shared_ptr<_Tp>::~shared_ptr()
4792{
4793 if (__cntrl_)
4794 __cntrl_->__release_shared();
4795}
4796
4797template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004798inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004799shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004800shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004801{
4802 shared_ptr(__r).swap(*this);
4803 return *this;
4804}
4805
4806template<class _Tp>
4807template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004808inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004809typename enable_if
4810<
4811 is_convertible<_Yp*, _Tp*>::value,
4812 shared_ptr<_Tp>&
4813>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00004814shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004815{
4816 shared_ptr(__r).swap(*this);
4817 return *this;
4818}
4819
Howard Hinnant74279a52010-09-04 23:28:19 +00004820#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004821
4822template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004823inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004824shared_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00004825shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004826{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004827 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004828 return *this;
4829}
4830
4831template<class _Tp>
4832template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004833inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004834typename enable_if
4835<
4836 is_convertible<_Yp*, _Tp*>::value,
4837 shared_ptr<_Tp>&
4838>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004839shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
4840{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004841 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004842 return *this;
4843}
4844
4845template<class _Tp>
4846template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004847inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004848typename enable_if
4849<
4850 !is_array<_Yp>::value &&
4851 is_convertible<_Yp*, _Tp*>::value,
Howard Hinnant25bcaf62013-09-13 23:56:00 +00004852 shared_ptr<_Tp>
4853>::type&
Howard Hinnantc51e1022010-05-11 19:42:16 +00004854shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
4855{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004856 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004857 return *this;
4858}
4859
4860template<class _Tp>
4861template <class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004862inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004863typename enable_if
4864<
4865 !is_array<_Yp>::value &&
4866 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4867 shared_ptr<_Tp>&
4868>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004869shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
4870{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004871 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004872 return *this;
4873}
4874
Howard Hinnant74279a52010-09-04 23:28:19 +00004875#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004876
4877template<class _Tp>
4878template<class _Yp>
4879inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004880typename enable_if
4881<
4882 !is_array<_Yp>::value &&
4883 is_convertible<_Yp*, _Tp*>::value,
4884 shared_ptr<_Tp>&
4885>::type
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004886shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004887{
4888 shared_ptr(__r).swap(*this);
4889 return *this;
4890}
4891
4892template<class _Tp>
4893template <class _Yp, class _Dp>
4894inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004895typename enable_if
4896<
4897 !is_array<_Yp>::value &&
4898 is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, _Tp*>::value,
4899 shared_ptr<_Tp>&
4900>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004901shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
4902{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004903 shared_ptr(_VSTD::move(__r)).swap(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004904 return *this;
4905}
4906
Howard Hinnant74279a52010-09-04 23:28:19 +00004907#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00004908
4909template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004910inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004911void
Howard Hinnant719bda32011-05-28 14:41:13 +00004912shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004913{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004914 _VSTD::swap(__ptr_, __r.__ptr_);
4915 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004916}
4917
4918template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004919inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004920void
Howard Hinnant719bda32011-05-28 14:41:13 +00004921shared_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004922{
4923 shared_ptr().swap(*this);
4924}
4925
4926template<class _Tp>
4927template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004928inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004929typename enable_if
4930<
4931 is_convertible<_Yp*, _Tp*>::value,
4932 void
4933>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004934shared_ptr<_Tp>::reset(_Yp* __p)
4935{
4936 shared_ptr(__p).swap(*this);
4937}
4938
4939template<class _Tp>
4940template<class _Yp, class _Dp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004941inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004942typename enable_if
4943<
4944 is_convertible<_Yp*, _Tp*>::value,
4945 void
4946>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004947shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
4948{
4949 shared_ptr(__p, __d).swap(*this);
4950}
4951
4952template<class _Tp>
4953template<class _Yp, class _Dp, class _Alloc>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00004954inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004955typename enable_if
4956<
4957 is_convertible<_Yp*, _Tp*>::value,
4958 void
4959>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004960shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
4961{
4962 shared_ptr(__p, __d, __a).swap(*this);
4963}
4964
4965#ifndef _LIBCPP_HAS_NO_VARIADICS
4966
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004967template<class _Tp, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004968inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004969typename enable_if
4970<
4971 !is_array<_Tp>::value,
4972 shared_ptr<_Tp>
4973>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004974make_shared(_Args&& ...__args)
4975{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004976 return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004977}
4978
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004979template<class _Tp, class _Alloc, class ..._Args>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004980inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00004981typename enable_if
4982<
4983 !is_array<_Tp>::value,
4984 shared_ptr<_Tp>
4985>::type
Howard Hinnantc51e1022010-05-11 19:42:16 +00004986allocate_shared(const _Alloc& __a, _Args&& ...__args)
4987{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004988 return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004989}
4990
4991#else // _LIBCPP_HAS_NO_VARIADICS
4992
4993template<class _Tp>
4994inline _LIBCPP_INLINE_VISIBILITY
4995shared_ptr<_Tp>
4996make_shared()
4997{
4998 return shared_ptr<_Tp>::make_shared();
4999}
5000
5001template<class _Tp, class _A0>
5002inline _LIBCPP_INLINE_VISIBILITY
5003shared_ptr<_Tp>
5004make_shared(_A0& __a0)
5005{
5006 return shared_ptr<_Tp>::make_shared(__a0);
5007}
5008
5009template<class _Tp, class _A0, class _A1>
5010inline _LIBCPP_INLINE_VISIBILITY
5011shared_ptr<_Tp>
5012make_shared(_A0& __a0, _A1& __a1)
5013{
5014 return shared_ptr<_Tp>::make_shared(__a0, __a1);
5015}
5016
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005017template<class _Tp, class _A0, class _A1, class _A2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00005018inline _LIBCPP_INLINE_VISIBILITY
5019shared_ptr<_Tp>
5020make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
5021{
5022 return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
5023}
5024
5025template<class _Tp, class _Alloc>
5026inline _LIBCPP_INLINE_VISIBILITY
5027shared_ptr<_Tp>
5028allocate_shared(const _Alloc& __a)
5029{
5030 return shared_ptr<_Tp>::allocate_shared(__a);
5031}
5032
5033template<class _Tp, class _Alloc, class _A0>
5034inline _LIBCPP_INLINE_VISIBILITY
5035shared_ptr<_Tp>
5036allocate_shared(const _Alloc& __a, _A0& __a0)
5037{
5038 return shared_ptr<_Tp>::allocate_shared(__a, __a0);
5039}
5040
5041template<class _Tp, class _Alloc, class _A0, class _A1>
5042inline _LIBCPP_INLINE_VISIBILITY
5043shared_ptr<_Tp>
5044allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
5045{
5046 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
5047}
5048
5049template<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
5050inline _LIBCPP_INLINE_VISIBILITY
5051shared_ptr<_Tp>
5052allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
5053{
5054 return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
5055}
5056
5057#endif // _LIBCPP_HAS_NO_VARIADICS
5058
5059template<class _Tp, class _Up>
5060inline _LIBCPP_INLINE_VISIBILITY
5061bool
Howard Hinnant719bda32011-05-28 14:41:13 +00005062operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005063{
5064 return __x.get() == __y.get();
5065}
5066
5067template<class _Tp, class _Up>
5068inline _LIBCPP_INLINE_VISIBILITY
5069bool
Howard Hinnant719bda32011-05-28 14:41:13 +00005070operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005071{
5072 return !(__x == __y);
5073}
5074
5075template<class _Tp, class _Up>
5076inline _LIBCPP_INLINE_VISIBILITY
5077bool
Howard Hinnant719bda32011-05-28 14:41:13 +00005078operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005079{
Eric Fiselierf8898c82015-02-05 23:01:40 +00005080 typedef typename common_type<_Tp*, _Up*>::type _Vp;
5081 return less<_Vp>()(__x.get(), __y.get());
Howard Hinnantb17caf92012-02-21 21:02:58 +00005082}
5083
5084template<class _Tp, class _Up>
5085inline _LIBCPP_INLINE_VISIBILITY
5086bool
5087operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
5088{
5089 return __y < __x;
5090}
5091
5092template<class _Tp, class _Up>
5093inline _LIBCPP_INLINE_VISIBILITY
5094bool
5095operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
5096{
5097 return !(__y < __x);
5098}
5099
5100template<class _Tp, class _Up>
5101inline _LIBCPP_INLINE_VISIBILITY
5102bool
5103operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
5104{
5105 return !(__x < __y);
5106}
5107
5108template<class _Tp>
5109inline _LIBCPP_INLINE_VISIBILITY
5110bool
5111operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5112{
5113 return !__x;
5114}
5115
5116template<class _Tp>
5117inline _LIBCPP_INLINE_VISIBILITY
5118bool
5119operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5120{
5121 return !__x;
5122}
5123
5124template<class _Tp>
5125inline _LIBCPP_INLINE_VISIBILITY
5126bool
5127operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5128{
5129 return static_cast<bool>(__x);
5130}
5131
5132template<class _Tp>
5133inline _LIBCPP_INLINE_VISIBILITY
5134bool
5135operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5136{
5137 return static_cast<bool>(__x);
5138}
5139
5140template<class _Tp>
5141inline _LIBCPP_INLINE_VISIBILITY
5142bool
5143operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5144{
5145 return less<_Tp*>()(__x.get(), nullptr);
5146}
5147
5148template<class _Tp>
5149inline _LIBCPP_INLINE_VISIBILITY
5150bool
5151operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5152{
5153 return less<_Tp*>()(nullptr, __x.get());
5154}
5155
5156template<class _Tp>
5157inline _LIBCPP_INLINE_VISIBILITY
5158bool
5159operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5160{
5161 return nullptr < __x;
5162}
5163
5164template<class _Tp>
5165inline _LIBCPP_INLINE_VISIBILITY
5166bool
5167operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5168{
5169 return __x < nullptr;
5170}
5171
5172template<class _Tp>
5173inline _LIBCPP_INLINE_VISIBILITY
5174bool
5175operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5176{
5177 return !(nullptr < __x);
5178}
5179
5180template<class _Tp>
5181inline _LIBCPP_INLINE_VISIBILITY
5182bool
5183operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5184{
5185 return !(__x < nullptr);
5186}
5187
5188template<class _Tp>
5189inline _LIBCPP_INLINE_VISIBILITY
5190bool
5191operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
5192{
5193 return !(__x < nullptr);
5194}
5195
5196template<class _Tp>
5197inline _LIBCPP_INLINE_VISIBILITY
5198bool
5199operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
5200{
5201 return !(nullptr < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005202}
5203
5204template<class _Tp>
5205inline _LIBCPP_INLINE_VISIBILITY
5206void
Howard Hinnant719bda32011-05-28 14:41:13 +00005207swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005208{
5209 __x.swap(__y);
5210}
5211
5212template<class _Tp, class _Up>
5213inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005214typename enable_if
5215<
5216 !is_array<_Tp>::value && !is_array<_Up>::value,
5217 shared_ptr<_Tp>
5218>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005219static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005220{
5221 return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
5222}
5223
5224template<class _Tp, class _Up>
5225inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005226typename enable_if
5227<
5228 !is_array<_Tp>::value && !is_array<_Up>::value,
5229 shared_ptr<_Tp>
5230>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005231dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005232{
5233 _Tp* __p = dynamic_cast<_Tp*>(__r.get());
5234 return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
5235}
5236
5237template<class _Tp, class _Up>
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005238typename enable_if
5239<
5240 is_array<_Tp>::value == is_array<_Up>::value,
5241 shared_ptr<_Tp>
5242>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005243const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005244{
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005245 typedef typename remove_extent<_Tp>::type _RTp;
5246 return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00005247}
5248
Howard Hinnant72f73582010-08-11 17:04:31 +00005249#ifndef _LIBCPP_NO_RTTI
5250
Howard Hinnantc51e1022010-05-11 19:42:16 +00005251template<class _Dp, class _Tp>
5252inline _LIBCPP_INLINE_VISIBILITY
5253_Dp*
Howard Hinnant719bda32011-05-28 14:41:13 +00005254get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005255{
5256 return __p.template __get_deleter<_Dp>();
5257}
5258
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005259#endif // _LIBCPP_NO_RTTI
Howard Hinnant72f73582010-08-11 17:04:31 +00005260
Howard Hinnantc51e1022010-05-11 19:42:16 +00005261template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005262class _LIBCPP_TYPE_VIS_ONLY weak_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +00005263{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005264public:
5265 typedef _Tp element_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005266private:
5267 element_type* __ptr_;
5268 __shared_weak_count* __cntrl_;
5269
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005270public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005271 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005272 _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005273 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00005274 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5275 _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005277 weak_ptr(weak_ptr const& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005278 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant719bda32011-05-28 14:41:13 +00005279 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5280 _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005281
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005282#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005284 weak_ptr(weak_ptr&& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005285 template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005286 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
5287 _NOEXCEPT;
5288#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005289 ~weak_ptr();
5290
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005291 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005292 weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005293 template<class _Yp>
5294 typename enable_if
5295 <
5296 is_convertible<_Yp*, element_type*>::value,
5297 weak_ptr&
5298 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005300 operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
5301
5302#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5303
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005305 weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
5306 template<class _Yp>
5307 typename enable_if
5308 <
5309 is_convertible<_Yp*, element_type*>::value,
5310 weak_ptr&
5311 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005312 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005313 operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
5314
5315#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5316
5317 template<class _Yp>
5318 typename enable_if
5319 <
5320 is_convertible<_Yp*, element_type*>::value,
5321 weak_ptr&
5322 >::type
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005324 operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005325
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005326 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005327 void swap(weak_ptr& __r) _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005328 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005329 void reset() _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005330
Howard Hinnant756c69b2010-09-22 16:48:34 +00005331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005332 long use_count() const _NOEXCEPT
5333 {return __cntrl_ ? __cntrl_->use_count() : 0;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005335 bool expired() const _NOEXCEPT
5336 {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
5337 shared_ptr<_Tp> lock() const _NOEXCEPT;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005338 template<class _Up>
5339 _LIBCPP_INLINE_VISIBILITY
5340 bool owner_before(const shared_ptr<_Up>& __r) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00005341 {return __cntrl_ < __r.__cntrl_;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005342 template<class _Up>
5343 _LIBCPP_INLINE_VISIBILITY
5344 bool owner_before(const weak_ptr<_Up>& __r) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00005345 {return __cntrl_ < __r.__cntrl_;}
5346
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005347 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY weak_ptr;
5348 template <class _Up> friend class _LIBCPP_TYPE_VIS_ONLY shared_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005349};
5350
5351template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005352inline
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005353_LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005354weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005355 : __ptr_(0),
5356 __cntrl_(0)
5357{
5358}
5359
5360template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005361inline
Howard Hinnant719bda32011-05-28 14:41:13 +00005362weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005363 : __ptr_(__r.__ptr_),
5364 __cntrl_(__r.__cntrl_)
5365{
5366 if (__cntrl_)
5367 __cntrl_->__add_weak();
5368}
5369
5370template<class _Tp>
5371template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005372inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005373weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005374 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005375 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005376 : __ptr_(__r.__ptr_),
5377 __cntrl_(__r.__cntrl_)
5378{
5379 if (__cntrl_)
5380 __cntrl_->__add_weak();
5381}
5382
5383template<class _Tp>
5384template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005385inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005386weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
Howard Hinnant3a085e82011-05-22 00:09:02 +00005387 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
Howard Hinnant719bda32011-05-28 14:41:13 +00005388 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005389 : __ptr_(__r.__ptr_),
5390 __cntrl_(__r.__cntrl_)
5391{
5392 if (__cntrl_)
5393 __cntrl_->__add_weak();
5394}
5395
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005396#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5397
5398template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005399inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005400weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
5401 : __ptr_(__r.__ptr_),
5402 __cntrl_(__r.__cntrl_)
5403{
5404 __r.__ptr_ = 0;
5405 __r.__cntrl_ = 0;
5406}
5407
5408template<class _Tp>
5409template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005410inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005411weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
5412 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
5413 _NOEXCEPT
5414 : __ptr_(__r.__ptr_),
5415 __cntrl_(__r.__cntrl_)
5416{
5417 __r.__ptr_ = 0;
5418 __r.__cntrl_ = 0;
5419}
5420
5421#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5422
Howard Hinnantc51e1022010-05-11 19:42:16 +00005423template<class _Tp>
5424weak_ptr<_Tp>::~weak_ptr()
5425{
5426 if (__cntrl_)
5427 __cntrl_->__release_weak();
5428}
5429
5430template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005431inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005432weak_ptr<_Tp>&
Howard Hinnant719bda32011-05-28 14:41:13 +00005433weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005434{
5435 weak_ptr(__r).swap(*this);
5436 return *this;
5437}
5438
5439template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005440template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005441inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005442typename enable_if
5443<
5444 is_convertible<_Yp*, _Tp*>::value,
5445 weak_ptr<_Tp>&
5446>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005447weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005448{
5449 weak_ptr(__r).swap(*this);
5450 return *this;
5451}
5452
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005453#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
5454
5455template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005456inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005457weak_ptr<_Tp>&
5458weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
5459{
5460 weak_ptr(_VSTD::move(__r)).swap(*this);
5461 return *this;
5462}
5463
Howard Hinnantc51e1022010-05-11 19:42:16 +00005464template<class _Tp>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005465template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005466inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005467typename enable_if
5468<
5469 is_convertible<_Yp*, _Tp*>::value,
5470 weak_ptr<_Tp>&
5471>::type
5472weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
5473{
5474 weak_ptr(_VSTD::move(__r)).swap(*this);
5475 return *this;
5476}
5477
5478#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
5479
5480template<class _Tp>
5481template<class _Yp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005482inline
Howard Hinnant741c8fa2012-01-02 17:56:02 +00005483typename enable_if
5484<
5485 is_convertible<_Yp*, _Tp*>::value,
5486 weak_ptr<_Tp>&
5487>::type
Howard Hinnant719bda32011-05-28 14:41:13 +00005488weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005489{
5490 weak_ptr(__r).swap(*this);
5491 return *this;
5492}
5493
5494template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005495inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005496void
Howard Hinnant719bda32011-05-28 14:41:13 +00005497weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005498{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005499 _VSTD::swap(__ptr_, __r.__ptr_);
5500 _VSTD::swap(__cntrl_, __r.__cntrl_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005501}
5502
5503template<class _Tp>
5504inline _LIBCPP_INLINE_VISIBILITY
5505void
Howard Hinnant719bda32011-05-28 14:41:13 +00005506swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005507{
5508 __x.swap(__y);
5509}
5510
5511template<class _Tp>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00005512inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00005513void
Howard Hinnant719bda32011-05-28 14:41:13 +00005514weak_ptr<_Tp>::reset() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005515{
5516 weak_ptr().swap(*this);
5517}
5518
5519template<class _Tp>
5520template<class _Yp>
5521shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5522 typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat>::type)
5523 : __ptr_(__r.__ptr_),
5524 __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
5525{
5526 if (__cntrl_ == 0)
Marshall Clow8fea1612016-08-25 15:09:01 +00005527 __throw_bad_weak_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00005528}
5529
5530template<class _Tp>
5531shared_ptr<_Tp>
Howard Hinnant719bda32011-05-28 14:41:13 +00005532weak_ptr<_Tp>::lock() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00005533{
5534 shared_ptr<_Tp> __r;
5535 __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
5536 if (__r.__cntrl_)
5537 __r.__ptr_ = __ptr_;
5538 return __r;
5539}
5540
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005541#if _LIBCPP_STD_VER > 14
5542template <class _Tp = void> struct owner_less;
5543#else
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005544template <class _Tp> struct owner_less;
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005545#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00005546
5547template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005548struct _LIBCPP_TYPE_VIS_ONLY owner_less<shared_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005549 : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005550{
5551 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005553 bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5554 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005556 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5557 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005559 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5560 {return __x.owner_before(__y);}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005561};
Howard Hinnantc51e1022010-05-11 19:42:16 +00005562
5563template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005564struct _LIBCPP_TYPE_VIS_ONLY owner_less<weak_ptr<_Tp> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00005565 : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
5566{
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005567 typedef bool result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005569 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5570 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005572 bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const
5573 {return __x.owner_before(__y);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005574 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005575 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const
5576 {return __x.owner_before(__y);}
5577};
5578
Marshall Clow3c2d8a42015-11-12 15:56:44 +00005579#if _LIBCPP_STD_VER > 14
5580template <>
5581struct _LIBCPP_TYPE_VIS_ONLY owner_less<void>
5582{
5583 template <class _Tp, class _Up>
5584 _LIBCPP_INLINE_VISIBILITY
5585 bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const
5586 {return __x.owner_before(__y);}
5587 template <class _Tp, class _Up>
5588 _LIBCPP_INLINE_VISIBILITY
5589 bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const
5590 {return __x.owner_before(__y);}
5591 template <class _Tp, class _Up>
5592 _LIBCPP_INLINE_VISIBILITY
5593 bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const
5594 {return __x.owner_before(__y);}
5595 template <class _Tp, class _Up>
5596 _LIBCPP_INLINE_VISIBILITY
5597 bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const
5598 {return __x.owner_before(__y);}
5599 typedef void is_transparent;
5600};
5601#endif
5602
Howard Hinnantc51e1022010-05-11 19:42:16 +00005603template<class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005604class _LIBCPP_TYPE_VIS_ONLY enable_shared_from_this
Howard Hinnantc51e1022010-05-11 19:42:16 +00005605{
5606 mutable weak_ptr<_Tp> __weak_this_;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005607protected:
Howard Hinnantb5fffe82012-07-07 20:56:04 +00005608 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant719bda32011-05-28 14:41:13 +00005609 enable_shared_from_this() _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005610 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005611 enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005612 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005613 enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
5614 {return *this;}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005616 ~enable_shared_from_this() {}
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005617public:
Howard Hinnant756c69b2010-09-22 16:48:34 +00005618 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005619 shared_ptr<_Tp> shared_from_this()
5620 {return shared_ptr<_Tp>(__weak_this_);}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005621 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005622 shared_ptr<_Tp const> shared_from_this() const
5623 {return shared_ptr<const _Tp>(__weak_this_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00005624
Eric Fiselier84006862016-06-02 00:15:35 +00005625#if _LIBCPP_STD_VER > 14
5626 _LIBCPP_INLINE_VISIBILITY
5627 weak_ptr<_Tp> weak_from_this() _NOEXCEPT
5628 { return __weak_this_; }
5629
5630 _LIBCPP_INLINE_VISIBILITY
5631 weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
5632 { return __weak_this_; }
5633#endif // _LIBCPP_STD_VER > 14
5634
Howard Hinnantc51e1022010-05-11 19:42:16 +00005635 template <class _Up> friend class shared_ptr;
5636};
5637
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005638template <class _Tp>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005639struct _LIBCPP_TYPE_VIS_ONLY hash<shared_ptr<_Tp> >
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005640{
5641 typedef shared_ptr<_Tp> argument_type;
5642 typedef size_t result_type;
Howard Hinnant756c69b2010-09-22 16:48:34 +00005643 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant719bda32011-05-28 14:41:13 +00005644 result_type operator()(const argument_type& __ptr) const _NOEXCEPT
Howard Hinnant36b31ae2010-06-03 16:42:57 +00005645 {
5646 return hash<_Tp*>()(__ptr.get());
5647 }
5648};
5649
Howard Hinnantc834c512011-11-29 18:15:50 +00005650template<class _CharT, class _Traits, class _Yp>
Howard Hinnantdc095972011-07-18 15:51:59 +00005651inline _LIBCPP_INLINE_VISIBILITY
5652basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00005653operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
Howard Hinnantdc095972011-07-18 15:51:59 +00005654
Eric Fiselier9b492672016-06-18 02:12:53 +00005655
5656#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005657
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005658class _LIBCPP_TYPE_VIS __sp_mut
Howard Hinnant9fa30202012-07-30 01:40:57 +00005659{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005660 void* __lx;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005661public:
5662 void lock() _NOEXCEPT;
5663 void unlock() _NOEXCEPT;
5664
5665private:
5666 _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5667 __sp_mut(const __sp_mut&);
5668 __sp_mut& operator=(const __sp_mut&);
5669
Howard Hinnant8331b762013-03-06 23:30:19 +00005670 friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005671};
5672
Howard Hinnant8331b762013-03-06 23:30:19 +00005673_LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005674
5675template <class _Tp>
5676inline _LIBCPP_INLINE_VISIBILITY
5677bool
5678atomic_is_lock_free(const shared_ptr<_Tp>*)
5679{
5680 return false;
5681}
5682
5683template <class _Tp>
5684shared_ptr<_Tp>
5685atomic_load(const shared_ptr<_Tp>* __p)
5686{
5687 __sp_mut& __m = __get_sp_mut(__p);
5688 __m.lock();
5689 shared_ptr<_Tp> __q = *__p;
5690 __m.unlock();
5691 return __q;
5692}
5693
5694template <class _Tp>
5695inline _LIBCPP_INLINE_VISIBILITY
5696shared_ptr<_Tp>
5697atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5698{
5699 return atomic_load(__p);
5700}
5701
5702template <class _Tp>
5703void
5704atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5705{
5706 __sp_mut& __m = __get_sp_mut(__p);
5707 __m.lock();
5708 __p->swap(__r);
5709 __m.unlock();
5710}
5711
5712template <class _Tp>
5713inline _LIBCPP_INLINE_VISIBILITY
5714void
5715atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5716{
5717 atomic_store(__p, __r);
5718}
5719
5720template <class _Tp>
5721shared_ptr<_Tp>
5722atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5723{
5724 __sp_mut& __m = __get_sp_mut(__p);
5725 __m.lock();
5726 __p->swap(__r);
5727 __m.unlock();
5728 return __r;
5729}
5730
5731template <class _Tp>
5732inline _LIBCPP_INLINE_VISIBILITY
5733shared_ptr<_Tp>
5734atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5735{
5736 return atomic_exchange(__p, __r);
5737}
5738
5739template <class _Tp>
5740bool
5741atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5742{
Marshall Clow4201ee82016-05-18 17:50:13 +00005743 shared_ptr<_Tp> __temp;
Howard Hinnant9fa30202012-07-30 01:40:57 +00005744 __sp_mut& __m = __get_sp_mut(__p);
5745 __m.lock();
5746 if (__p->__owner_equivalent(*__v))
5747 {
Marshall Clow4201ee82016-05-18 17:50:13 +00005748 _VSTD::swap(__temp, *__p);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005749 *__p = __w;
5750 __m.unlock();
5751 return true;
5752 }
Marshall Clow4201ee82016-05-18 17:50:13 +00005753 _VSTD::swap(__temp, *__v);
Howard Hinnant9fa30202012-07-30 01:40:57 +00005754 *__v = *__p;
5755 __m.unlock();
5756 return false;
5757}
5758
5759template <class _Tp>
5760inline _LIBCPP_INLINE_VISIBILITY
5761bool
5762atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5763{
5764 return atomic_compare_exchange_strong(__p, __v, __w);
5765}
5766
5767template <class _Tp>
5768inline _LIBCPP_INLINE_VISIBILITY
5769bool
5770atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5771 shared_ptr<_Tp> __w, memory_order, memory_order)
5772{
5773 return atomic_compare_exchange_strong(__p, __v, __w);
5774}
5775
5776template <class _Tp>
5777inline _LIBCPP_INLINE_VISIBILITY
5778bool
5779atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5780 shared_ptr<_Tp> __w, memory_order, memory_order)
5781{
5782 return atomic_compare_exchange_weak(__p, __v, __w);
5783}
5784
Eric Fiselier9b492672016-06-18 02:12:53 +00005785#endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
Howard Hinnant9fa30202012-07-30 01:40:57 +00005786
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005787//enum class
Howard Hinnant8331b762013-03-06 23:30:19 +00005788struct _LIBCPP_TYPE_VIS pointer_safety
Howard Hinnantc51e1022010-05-11 19:42:16 +00005789{
Howard Hinnant49e145e2012-10-30 19:06:59 +00005790 enum __lx
Howard Hinnantc51e1022010-05-11 19:42:16 +00005791 {
5792 relaxed,
5793 preferred,
5794 strict
5795 };
5796
Howard Hinnant49e145e2012-10-30 19:06:59 +00005797 __lx __v_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005798
Howard Hinnant756c69b2010-09-22 16:48:34 +00005799 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant49e145e2012-10-30 19:06:59 +00005800 pointer_safety(__lx __v) : __v_(__v) {}
Howard Hinnant756c69b2010-09-22 16:48:34 +00005801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00005802 operator int() const {return __v_;}
5803};
5804
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005805_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5806_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5807_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
5808_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5809_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005810
5811template <class _Tp>
5812inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005813_Tp*
Howard Hinnantc51e1022010-05-11 19:42:16 +00005814undeclare_reachable(_Tp* __p)
5815{
5816 return static_cast<_Tp*>(__undeclare_reachable(__p));
5817}
5818
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00005819_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005820
Marshall Clow8982dcd2015-07-13 20:04:56 +00005821// --- Helper for container swap --
5822template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005823inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005824void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5825#if _LIBCPP_STD_VER >= 14
5826 _NOEXCEPT
5827#else
5828 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5829#endif
5830{
5831 __swap_allocator(__a1, __a2,
5832 integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5833}
5834
5835template <typename _Alloc>
5836_LIBCPP_INLINE_VISIBILITY
5837void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5838#if _LIBCPP_STD_VER >= 14
5839 _NOEXCEPT
5840#else
5841 _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5842#endif
5843{
5844 using _VSTD::swap;
5845 swap(__a1, __a2);
5846}
5847
5848template <typename _Alloc>
Eric Fiselier6585c752016-04-21 22:54:21 +00005849inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8982dcd2015-07-13 20:04:56 +00005850void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5851
Marshall Clowff91de82015-08-18 19:51:37 +00005852template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00005853struct __noexcept_move_assign_container : public integral_constant<bool,
5854 _Traits::propagate_on_container_move_assignment::value
5855#if _LIBCPP_STD_VER > 14
5856 || _Traits::is_always_equal::value
5857#else
5858 && is_nothrow_move_assignable<_Alloc>::value
5859#endif
5860 > {};
Marshall Clow8982dcd2015-07-13 20:04:56 +00005861
Marshall Clowa591b9a2016-07-11 21:38:08 +00005862
5863#ifndef _LIBCPP_HAS_NO_VARIADICS
5864template <class _Tp, class _Alloc>
5865struct __temp_value {
5866 typedef allocator_traits<_Alloc> _Traits;
5867
5868 typename aligned_storage<sizeof(_Tp), alignof(_Tp)>::type __v;
5869 _Alloc &__a;
5870
5871 _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5872 _Tp & get() { return *__addr(); }
5873
5874 template<class... _Args>
5875 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc)
5876 { _Traits::construct(__a, __addr(), _VSTD::forward<_Args>(__args)...); }
5877
5878 ~__temp_value() { _Traits::destroy(__a, __addr()); }
5879 };
5880#endif
5881
Howard Hinnantc51e1022010-05-11 19:42:16 +00005882_LIBCPP_END_NAMESPACE_STD
5883
5884#endif // _LIBCPP_MEMORY