Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===-------------------------- memory ------------------------------------===// |
| 3 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_MEMORY |
| 11 | #define _LIBCPP_MEMORY |
| 12 | |
| 13 | /* |
| 14 | memory synopsis |
| 15 | |
| 16 | namespace std |
| 17 | { |
| 18 | |
| 19 | struct allocator_arg_t { }; |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 20 | inline constexpr allocator_arg_t allocator_arg = allocator_arg_t(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 21 | |
| 22 | template <class T, class Alloc> struct uses_allocator; |
| 23 | |
| 24 | template <class Ptr> |
| 25 | struct pointer_traits |
| 26 | { |
| 27 | typedef Ptr pointer; |
| 28 | typedef <details> element_type; |
| 29 | typedef <details> difference_type; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 30 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 31 | template <class U> using rebind = <details>; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 32 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 33 | static pointer pointer_to(<details>); |
| 34 | }; |
| 35 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 36 | template <class T> |
| 37 | struct pointer_traits<T*> |
| 38 | { |
| 39 | typedef T* pointer; |
| 40 | typedef T element_type; |
| 41 | typedef ptrdiff_t difference_type; |
| 42 | |
| 43 | template <class U> using rebind = U*; |
| 44 | |
Louis Dionne | 44e1ea8 | 2018-11-13 17:04:05 +0000 | [diff] [blame] | 45 | static pointer pointer_to(<details>) noexcept; // constexpr in C++20 |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
Eric Fiselier | 45c9aac | 2017-11-22 19:49:21 +0000 | [diff] [blame] | 48 | template <class T> constexpr T* to_address(T* p) noexcept; // C++20 |
Marek Kurdej | 1f0cde9 | 2020-12-06 15:23:46 +0100 | [diff] [blame] | 49 | template <class Ptr> constexpr auto to_address(const Ptr& p) noexcept; // C++20 |
Eric Fiselier | 45c9aac | 2017-11-22 19:49:21 +0000 | [diff] [blame] | 50 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 51 | template <class Alloc> |
| 52 | struct allocator_traits |
| 53 | { |
| 54 | typedef Alloc allocator_type; |
| 55 | typedef typename allocator_type::value_type |
| 56 | value_type; |
| 57 | |
| 58 | typedef Alloc::pointer | value_type* pointer; |
| 59 | typedef Alloc::const_pointer |
| 60 | | pointer_traits<pointer>::rebind<const value_type> |
| 61 | const_pointer; |
| 62 | typedef Alloc::void_pointer |
| 63 | | pointer_traits<pointer>::rebind<void> |
| 64 | void_pointer; |
| 65 | typedef Alloc::const_void_pointer |
| 66 | | pointer_traits<pointer>::rebind<const void> |
| 67 | const_void_pointer; |
| 68 | typedef Alloc::difference_type |
Howard Hinnant | 8e4e600 | 2010-11-18 01:40:00 +0000 | [diff] [blame] | 69 | | pointer_traits<pointer>::difference_type |
| 70 | difference_type; |
| 71 | typedef Alloc::size_type |
| 72 | | make_unsigned<difference_type>::type |
| 73 | size_type; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 74 | typedef Alloc::propagate_on_container_copy_assignment |
| 75 | | false_type propagate_on_container_copy_assignment; |
| 76 | typedef Alloc::propagate_on_container_move_assignment |
| 77 | | false_type propagate_on_container_move_assignment; |
| 78 | typedef Alloc::propagate_on_container_swap |
| 79 | | false_type propagate_on_container_swap; |
Marshall Clow | 0b58756 | 2015-06-02 16:34:03 +0000 | [diff] [blame] | 80 | typedef Alloc::is_always_equal |
| 81 | | is_empty is_always_equal; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 82 | |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 83 | template <class T> using rebind_alloc = Alloc::rebind<T>::other | Alloc<T, Args...>; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 84 | template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>; |
| 85 | |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 86 | static pointer allocate(allocator_type& a, size_type n); // constexpr and [[nodiscard]] in C++20 |
| 87 | static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // constexpr and [[nodiscard]] in C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 88 | |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 89 | static void deallocate(allocator_type& a, pointer p, size_type n) noexcept; // constexpr in C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 90 | |
| 91 | template <class T, class... Args> |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 92 | static void construct(allocator_type& a, T* p, Args&&... args); // constexpr in C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 93 | |
| 94 | template <class T> |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 95 | static void destroy(allocator_type& a, T* p); // constexpr in C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 96 | |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 97 | static size_type max_size(const allocator_type& a); // noexcept in C++14, constexpr in C++20 |
| 98 | static allocator_type select_on_container_copy_construction(const allocator_type& a); // constexpr in C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | template <> |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 102 | class allocator<void> // deprecated in C++17, removed in C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 103 | { |
| 104 | public: |
| 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 | |
| 112 | template <class T> |
| 113 | class allocator |
| 114 | { |
| 115 | public: |
Louis Dionne | c0056af | 2020-08-28 12:31:16 -0400 | [diff] [blame] | 116 | typedef size_t size_type; |
| 117 | typedef ptrdiff_t difference_type; |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 118 | typedef T* pointer; // deprecated in C++17, removed in C++20 |
| 119 | typedef const T* const_pointer; // deprecated in C++17, removed in C++20 |
| 120 | typedef typename add_lvalue_reference<T>::type |
| 121 | reference; // deprecated in C++17, removed in C++20 |
| 122 | typedef typename add_lvalue_reference<const T>::type |
| 123 | const_reference; // deprecated in C++17, removed in C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 124 | |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 125 | typedef T value_type; |
| 126 | |
| 127 | template <class U> struct rebind {typedef allocator<U> other;}; // deprecated in C++17, removed in C++20 |
| 128 | |
| 129 | typedef true_type propagate_on_container_move_assignment; |
| 130 | typedef true_type is_always_equal; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 131 | |
Marshall Clow | bc75976 | 2018-03-20 23:02:53 +0000 | [diff] [blame] | 132 | constexpr allocator() noexcept; // constexpr in C++20 |
| 133 | constexpr allocator(const allocator&) noexcept; // constexpr in C++20 |
| 134 | template <class U> |
| 135 | constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20 |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 136 | ~allocator(); // constexpr in C++20 |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 137 | pointer address(reference x) const noexcept; // deprecated in C++17, removed in C++20 |
| 138 | const_pointer address(const_reference x) const noexcept; // deprecated in C++17, removed in C++20 |
| 139 | T* allocate(size_t n, const void* hint); // deprecated in C++17, removed in C++20 |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 140 | T* allocate(size_t n); // constexpr in C++20 |
| 141 | void deallocate(T* p, size_t n) noexcept; // constexpr in C++20 |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 142 | size_type max_size() const noexcept; // deprecated in C++17, removed in C++20 |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 143 | template<class U, class... Args> |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 144 | void construct(U* p, Args&&... args); // deprecated in C++17, removed in C++20 |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 145 | template <class U> |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 146 | void destroy(U* p); // deprecated in C++17, removed in C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | template <class T, class U> |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 150 | bool operator==(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 151 | |
| 152 | template <class T, class U> |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 153 | bool operator!=(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 154 | |
| 155 | template <class OutputIterator, class T> |
| 156 | class raw_storage_iterator |
| 157 | : public iterator<output_iterator_tag, |
| 158 | T, // purposefully not C++03 |
| 159 | ptrdiff_t, // purposefully not C++03 |
| 160 | T*, // purposefully not C++03 |
| 161 | raw_storage_iterator&> // purposefully not C++03 |
| 162 | { |
| 163 | public: |
| 164 | explicit raw_storage_iterator(OutputIterator x); |
| 165 | raw_storage_iterator& operator*(); |
| 166 | raw_storage_iterator& operator=(const T& element); |
| 167 | raw_storage_iterator& operator++(); |
| 168 | raw_storage_iterator operator++(int); |
| 169 | }; |
| 170 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 171 | template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept; |
| 172 | template <class T> void return_temporary_buffer(T* p) noexcept; |
| 173 | |
| 174 | template <class T> T* addressof(T& r) noexcept; |
Marshall Clow | 78dbe46 | 2016-11-14 18:22:19 +0000 | [diff] [blame] | 175 | template <class T> T* addressof(const T&& r) noexcept = delete; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 176 | |
| 177 | template <class InputIterator, class ForwardIterator> |
| 178 | ForwardIterator |
| 179 | uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result); |
| 180 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 181 | template <class InputIterator, class Size, class ForwardIterator> |
| 182 | ForwardIterator |
| 183 | uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result); |
| 184 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 185 | template <class ForwardIterator, class T> |
| 186 | void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x); |
| 187 | |
| 188 | template <class ForwardIterator, class Size, class T> |
Howard Hinnant | 3c81109 | 2010-11-18 16:13:03 +0000 | [diff] [blame] | 189 | ForwardIterator |
| 190 | uninitialized_fill_n(ForwardIterator first, Size n, const T& x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 191 | |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 192 | template <class T, class ...Args> |
| 193 | constexpr T* construct_at(T* location, Args&& ...args); // since C++20 |
| 194 | |
Eric Fiselier | 383f6cf | 2016-07-24 03:51:39 +0000 | [diff] [blame] | 195 | template <class T> |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 196 | void destroy_at(T* location); // constexpr in C++20 |
Eric Fiselier | 383f6cf | 2016-07-24 03:51:39 +0000 | [diff] [blame] | 197 | |
| 198 | template <class ForwardIterator> |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 199 | void destroy(ForwardIterator first, ForwardIterator last); // constexpr in C++20 |
Eric Fiselier | 383f6cf | 2016-07-24 03:51:39 +0000 | [diff] [blame] | 200 | |
| 201 | template <class ForwardIterator, class Size> |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 202 | ForwardIterator destroy_n(ForwardIterator first, Size n); // constexpr in C++20 |
Eric Fiselier | 383f6cf | 2016-07-24 03:51:39 +0000 | [diff] [blame] | 203 | |
| 204 | template <class InputIterator, class ForwardIterator> |
| 205 | ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result); |
| 206 | |
| 207 | template <class InputIterator, class Size, class ForwardIterator> |
| 208 | pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result); |
| 209 | |
| 210 | template <class ForwardIterator> |
| 211 | void uninitialized_value_construct(ForwardIterator first, ForwardIterator last); |
| 212 | |
| 213 | template <class ForwardIterator, class Size> |
| 214 | ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n); |
| 215 | |
| 216 | template <class ForwardIterator> |
| 217 | void uninitialized_default_construct(ForwardIterator first, ForwardIterator last); |
| 218 | |
| 219 | template <class ForwardIterator, class Size> |
| 220 | ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n); |
| 221 | |
Louis Dionne | 481a266 | 2018-09-23 18:35:00 +0000 | [diff] [blame] | 222 | template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 223 | |
| 224 | template<class X> |
Louis Dionne | 481a266 | 2018-09-23 18:35:00 +0000 | [diff] [blame] | 225 | class auto_ptr // deprecated in C++11, removed in C++17 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 226 | { |
| 227 | public: |
| 228 | typedef X element_type; |
| 229 | |
| 230 | explicit auto_ptr(X* p =0) throw(); |
| 231 | auto_ptr(auto_ptr&) throw(); |
| 232 | template<class Y> auto_ptr(auto_ptr<Y>&) throw(); |
| 233 | auto_ptr& operator=(auto_ptr&) throw(); |
| 234 | template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw(); |
| 235 | auto_ptr& operator=(auto_ptr_ref<X> r) throw(); |
| 236 | ~auto_ptr() throw(); |
| 237 | |
| 238 | typename add_lvalue_reference<X>::type operator*() const throw(); |
| 239 | X* operator->() const throw(); |
| 240 | X* get() const throw(); |
| 241 | X* release() throw(); |
| 242 | void reset(X* p =0) throw(); |
| 243 | |
| 244 | auto_ptr(auto_ptr_ref<X>) throw(); |
| 245 | template<class Y> operator auto_ptr_ref<Y>() throw(); |
| 246 | template<class Y> operator auto_ptr<Y>() throw(); |
| 247 | }; |
| 248 | |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 249 | template <class T> |
| 250 | struct default_delete |
| 251 | { |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 252 | constexpr default_delete() noexcept = default; |
| 253 | template <class U> default_delete(const default_delete<U>&) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 254 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 255 | void operator()(T*) const noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 256 | }; |
| 257 | |
| 258 | template <class T> |
| 259 | struct default_delete<T[]> |
| 260 | { |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 261 | constexpr default_delete() noexcept = default; |
| 262 | void operator()(T*) const noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 263 | template <class U> void operator()(U*) const = delete; |
| 264 | }; |
| 265 | |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 266 | template <class T, class D = default_delete<T>> |
| 267 | class unique_ptr |
| 268 | { |
| 269 | public: |
| 270 | typedef see below pointer; |
| 271 | typedef T element_type; |
| 272 | typedef D deleter_type; |
| 273 | |
| 274 | // constructors |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 275 | constexpr unique_ptr() noexcept; |
| 276 | explicit unique_ptr(pointer p) noexcept; |
| 277 | unique_ptr(pointer p, see below d1) noexcept; |
| 278 | unique_ptr(pointer p, see below d2) noexcept; |
| 279 | unique_ptr(unique_ptr&& u) noexcept; |
| 280 | unique_ptr(nullptr_t) noexcept : unique_ptr() { } |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 281 | template <class U, class E> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 282 | unique_ptr(unique_ptr<U, E>&& u) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 283 | template <class U> |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 284 | unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17 |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 285 | |
| 286 | // destructor |
| 287 | ~unique_ptr(); |
| 288 | |
| 289 | // assignment |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 290 | unique_ptr& operator=(unique_ptr&& u) noexcept; |
| 291 | template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept; |
| 292 | unique_ptr& operator=(nullptr_t) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 293 | |
| 294 | // observers |
| 295 | typename add_lvalue_reference<T>::type operator*() const; |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 296 | pointer operator->() const noexcept; |
| 297 | pointer get() const noexcept; |
| 298 | deleter_type& get_deleter() noexcept; |
| 299 | const deleter_type& get_deleter() const noexcept; |
| 300 | explicit operator bool() const noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 301 | |
| 302 | // modifiers |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 303 | pointer release() noexcept; |
| 304 | void reset(pointer p = pointer()) noexcept; |
| 305 | void swap(unique_ptr& u) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 306 | }; |
| 307 | |
| 308 | template <class T, class D> |
| 309 | class unique_ptr<T[], D> |
| 310 | { |
| 311 | public: |
| 312 | typedef implementation-defined pointer; |
| 313 | typedef T element_type; |
| 314 | typedef D deleter_type; |
| 315 | |
| 316 | // constructors |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 317 | constexpr unique_ptr() noexcept; |
| 318 | explicit unique_ptr(pointer p) noexcept; |
| 319 | unique_ptr(pointer p, see below d) noexcept; |
| 320 | unique_ptr(pointer p, see below d) noexcept; |
| 321 | unique_ptr(unique_ptr&& u) noexcept; |
| 322 | unique_ptr(nullptr_t) noexcept : unique_ptr() { } |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 323 | |
| 324 | // destructor |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 325 | ~unique_ptr(); |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 326 | |
| 327 | // assignment |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 328 | unique_ptr& operator=(unique_ptr&& u) noexcept; |
| 329 | unique_ptr& operator=(nullptr_t) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 330 | |
| 331 | // observers |
| 332 | T& operator[](size_t i) const; |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 333 | pointer get() const noexcept; |
| 334 | deleter_type& get_deleter() noexcept; |
| 335 | const deleter_type& get_deleter() const noexcept; |
| 336 | explicit operator bool() const noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 337 | |
| 338 | // modifiers |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 339 | pointer release() noexcept; |
| 340 | void reset(pointer p = pointer()) noexcept; |
| 341 | void reset(nullptr_t) noexcept; |
Vy Nguyen | e369bd9 | 2020-07-13 12:34:37 -0400 | [diff] [blame] | 342 | template <class U> void reset(U) = delete; |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 343 | void swap(unique_ptr& u) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 344 | }; |
| 345 | |
| 346 | template <class T, class D> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 347 | void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 348 | |
| 349 | template <class T1, class D1, class T2, class D2> |
| 350 | bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); |
| 351 | template <class T1, class D1, class T2, class D2> |
| 352 | bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); |
| 353 | template <class T1, class D1, class T2, class D2> |
| 354 | bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); |
| 355 | template <class T1, class D1, class T2, class D2> |
| 356 | bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); |
| 357 | template <class T1, class D1, class T2, class D2> |
| 358 | bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); |
| 359 | template <class T1, class D1, class T2, class D2> |
| 360 | bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); |
| 361 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 362 | template <class T, class D> |
| 363 | bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept; |
| 364 | template <class T, class D> |
| 365 | bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept; |
| 366 | template <class T, class D> |
| 367 | bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept; |
| 368 | template <class T, class D> |
| 369 | bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept; |
| 370 | |
| 371 | template <class T, class D> |
| 372 | bool operator<(const unique_ptr<T, D>& x, nullptr_t); |
| 373 | template <class T, class D> |
| 374 | bool operator<(nullptr_t, const unique_ptr<T, D>& y); |
| 375 | template <class T, class D> |
| 376 | bool operator<=(const unique_ptr<T, D>& x, nullptr_t); |
| 377 | template <class T, class D> |
| 378 | bool operator<=(nullptr_t, const unique_ptr<T, D>& y); |
| 379 | template <class T, class D> |
| 380 | bool operator>(const unique_ptr<T, D>& x, nullptr_t); |
| 381 | template <class T, class D> |
| 382 | bool operator>(nullptr_t, const unique_ptr<T, D>& y); |
| 383 | template <class T, class D> |
| 384 | bool operator>=(const unique_ptr<T, D>& x, nullptr_t); |
| 385 | template <class T, class D> |
| 386 | bool operator>=(nullptr_t, const unique_ptr<T, D>& y); |
| 387 | |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 388 | class bad_weak_ptr |
| 389 | : public std::exception |
| 390 | { |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 391 | bad_weak_ptr() noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 392 | }; |
| 393 | |
Marshall Clow | 9e8f4a9 | 2013-07-01 18:16:03 +0000 | [diff] [blame] | 394 | template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14 |
| 395 | template<class T> unique_ptr<T> make_unique(size_t n); // C++14 |
| 396 | template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N] |
| 397 | |
Marshall Clow | e52a324 | 2017-11-27 15:51:36 +0000 | [diff] [blame] | 398 | template<class E, class T, class Y, class D> |
| 399 | basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p); |
| 400 | |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 401 | template<class T> |
| 402 | class shared_ptr |
| 403 | { |
| 404 | public: |
| 405 | typedef T element_type; |
Eric Fiselier | ae5b667 | 2016-06-27 01:02:43 +0000 | [diff] [blame] | 406 | typedef weak_ptr<T> weak_type; // C++17 |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 407 | |
| 408 | // constructors: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 409 | constexpr shared_ptr() noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 410 | template<class Y> explicit shared_ptr(Y* p); |
| 411 | template<class Y, class D> shared_ptr(Y* p, D d); |
| 412 | template<class Y, class D, class A> shared_ptr(Y* p, D d, A a); |
| 413 | template <class D> shared_ptr(nullptr_t p, D d); |
| 414 | template <class D, class A> shared_ptr(nullptr_t p, D d, A a); |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 415 | template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept; |
| 416 | shared_ptr(const shared_ptr& r) noexcept; |
| 417 | template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept; |
| 418 | shared_ptr(shared_ptr&& r) noexcept; |
| 419 | template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 420 | template<class Y> explicit shared_ptr(const weak_ptr<Y>& r); |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 421 | template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17 |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 422 | template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r); |
| 423 | shared_ptr(nullptr_t) : shared_ptr() { } |
| 424 | |
| 425 | // destructor: |
| 426 | ~shared_ptr(); |
| 427 | |
| 428 | // assignment: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 429 | shared_ptr& operator=(const shared_ptr& r) noexcept; |
| 430 | template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept; |
| 431 | shared_ptr& operator=(shared_ptr&& r) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 432 | template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r); |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 433 | template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17 |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 434 | template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r); |
| 435 | |
| 436 | // modifiers: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 437 | void swap(shared_ptr& r) noexcept; |
| 438 | void reset() noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 439 | template<class Y> void reset(Y* p); |
| 440 | template<class Y, class D> void reset(Y* p, D d); |
| 441 | template<class Y, class D, class A> void reset(Y* p, D d, A a); |
| 442 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 443 | // observers: |
| 444 | T* get() const noexcept; |
| 445 | T& operator*() const noexcept; |
| 446 | T* operator->() const noexcept; |
| 447 | long use_count() const noexcept; |
| 448 | bool unique() const noexcept; |
| 449 | explicit operator bool() const noexcept; |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 450 | template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept; |
| 451 | template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 452 | }; |
| 453 | |
Logan Smith | a5e4d7e | 2020-05-07 12:07:01 -0400 | [diff] [blame] | 454 | template<class T> |
| 455 | shared_ptr(weak_ptr<T>) -> shared_ptr<T>; |
| 456 | template<class T, class D> |
| 457 | shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>; |
| 458 | |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 459 | // shared_ptr comparisons: |
| 460 | template<class T, class U> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 461 | bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 462 | template<class T, class U> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 463 | bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 464 | template<class T, class U> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 465 | bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 466 | template<class T, class U> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 467 | bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 468 | template<class T, class U> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 469 | bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 470 | template<class T, class U> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 471 | bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept; |
| 472 | |
| 473 | template <class T> |
| 474 | bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept; |
| 475 | template <class T> |
| 476 | bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept; |
| 477 | template <class T> |
| 478 | bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept; |
| 479 | template <class T> |
| 480 | bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept; |
| 481 | template <class T> |
| 482 | bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept; |
| 483 | template <class T> |
| 484 | bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept; |
| 485 | template <class T> |
| 486 | bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept; |
| 487 | template <class T> |
| 488 | bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept; |
| 489 | template <class T> |
| 490 | bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept; |
| 491 | template <class T> |
| 492 | bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept; |
| 493 | template <class T> |
| 494 | bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept; |
| 495 | template <class T> |
| 496 | bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 497 | |
| 498 | // shared_ptr specialized algorithms: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 499 | template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 500 | |
| 501 | // shared_ptr casts: |
| 502 | template<class T, class U> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 503 | shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 504 | template<class T, class U> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 505 | shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 506 | template<class T, class U> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 507 | shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 508 | |
| 509 | // shared_ptr I/O: |
| 510 | template<class E, class T, class Y> |
| 511 | basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p); |
| 512 | |
| 513 | // shared_ptr get_deleter: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 514 | template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 515 | |
| 516 | template<class T, class... Args> |
| 517 | shared_ptr<T> make_shared(Args&&... args); |
| 518 | template<class T, class A, class... Args> |
| 519 | shared_ptr<T> allocate_shared(const A& a, Args&&... args); |
| 520 | |
| 521 | template<class T> |
| 522 | class weak_ptr |
| 523 | { |
| 524 | public: |
| 525 | typedef T element_type; |
| 526 | |
| 527 | // constructors |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 528 | constexpr weak_ptr() noexcept; |
| 529 | template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept; |
| 530 | weak_ptr(weak_ptr const& r) noexcept; |
| 531 | template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept; |
Marshall Clow | e9b0a5c | 2014-03-05 03:12:04 +0000 | [diff] [blame] | 532 | weak_ptr(weak_ptr&& r) noexcept; // C++14 |
| 533 | template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14 |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 534 | |
| 535 | // destructor |
| 536 | ~weak_ptr(); |
| 537 | |
| 538 | // assignment |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 539 | weak_ptr& operator=(weak_ptr const& r) noexcept; |
| 540 | template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept; |
| 541 | template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept; |
Marshall Clow | e9b0a5c | 2014-03-05 03:12:04 +0000 | [diff] [blame] | 542 | weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14 |
| 543 | template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14 |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 544 | |
| 545 | // modifiers |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 546 | void swap(weak_ptr& r) noexcept; |
| 547 | void reset() noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 548 | |
| 549 | // observers |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 550 | long use_count() const noexcept; |
| 551 | bool expired() const noexcept; |
| 552 | shared_ptr<T> lock() const noexcept; |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 553 | template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept; |
| 554 | template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 555 | }; |
| 556 | |
Logan Smith | a5e4d7e | 2020-05-07 12:07:01 -0400 | [diff] [blame] | 557 | template<class T> |
| 558 | weak_ptr(shared_ptr<T>) -> weak_ptr<T>; |
| 559 | |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 560 | // weak_ptr specialized algorithms: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 561 | template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 562 | |
| 563 | // class owner_less: |
| 564 | template<class T> struct owner_less; |
| 565 | |
| 566 | template<class T> |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 567 | struct owner_less<shared_ptr<T> > |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 568 | : binary_function<shared_ptr<T>, shared_ptr<T>, bool> |
| 569 | { |
| 570 | typedef bool result_type; |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 571 | bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept; |
| 572 | bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept; |
| 573 | bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 574 | }; |
| 575 | |
| 576 | template<class T> |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 577 | struct owner_less<weak_ptr<T> > |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 578 | : binary_function<weak_ptr<T>, weak_ptr<T>, bool> |
| 579 | { |
| 580 | typedef bool result_type; |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 581 | bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept; |
| 582 | bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept; |
| 583 | bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept; |
| 584 | }; |
| 585 | |
| 586 | template <> // Added in C++14 |
| 587 | struct owner_less<void> |
| 588 | { |
| 589 | template <class _Tp, class _Up> |
| 590 | bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept; |
| 591 | template <class _Tp, class _Up> |
| 592 | bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept; |
| 593 | template <class _Tp, class _Up> |
| 594 | bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept; |
| 595 | template <class _Tp, class _Up> |
| 596 | bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept; |
| 597 | |
| 598 | typedef void is_transparent; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 599 | }; |
| 600 | |
| 601 | template<class T> |
| 602 | class enable_shared_from_this |
| 603 | { |
| 604 | protected: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 605 | constexpr enable_shared_from_this() noexcept; |
| 606 | enable_shared_from_this(enable_shared_from_this const&) noexcept; |
| 607 | enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 608 | ~enable_shared_from_this(); |
| 609 | public: |
| 610 | shared_ptr<T> shared_from_this(); |
| 611 | shared_ptr<T const> shared_from_this() const; |
| 612 | }; |
| 613 | |
| 614 | template<class T> |
| 615 | bool atomic_is_lock_free(const shared_ptr<T>* p); |
| 616 | template<class T> |
| 617 | shared_ptr<T> atomic_load(const shared_ptr<T>* p); |
| 618 | template<class T> |
| 619 | shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo); |
| 620 | template<class T> |
| 621 | void atomic_store(shared_ptr<T>* p, shared_ptr<T> r); |
| 622 | template<class T> |
| 623 | void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo); |
| 624 | template<class T> |
| 625 | shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r); |
| 626 | template<class T> |
| 627 | shared_ptr<T> |
| 628 | atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo); |
| 629 | template<class T> |
| 630 | bool |
| 631 | atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w); |
| 632 | template<class T> |
| 633 | bool |
| 634 | atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w); |
| 635 | template<class T> |
| 636 | bool |
| 637 | atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v, |
| 638 | shared_ptr<T> w, memory_order success, |
| 639 | memory_order failure); |
| 640 | template<class T> |
| 641 | bool |
| 642 | atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v, |
| 643 | shared_ptr<T> w, memory_order success, |
| 644 | memory_order failure); |
| 645 | // Hash support |
| 646 | template <class T> struct hash; |
| 647 | template <class T, class D> struct hash<unique_ptr<T, D> >; |
| 648 | template <class T> struct hash<shared_ptr<T> >; |
| 649 | |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 650 | template <class T, class Alloc> |
| 651 | inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value; |
| 652 | |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 653 | // Pointer safety |
| 654 | enum class pointer_safety { relaxed, preferred, strict }; |
| 655 | void declare_reachable(void *p); |
| 656 | template <class T> T *undeclare_reachable(T *p); |
| 657 | void declare_no_pointers(char *p, size_t n); |
| 658 | void undeclare_no_pointers(char *p, size_t n); |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 659 | pointer_safety get_pointer_safety() noexcept; |
Howard Hinnant | d26c01d | 2010-08-19 18:39:17 +0000 | [diff] [blame] | 660 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 661 | void* align(size_t alignment, size_t size, void*& ptr, size_t& space); |
| 662 | |
| 663 | } // std |
| 664 | |
| 665 | */ |
| 666 | |
| 667 | #include <__config> |
Louis Dionne | 73912b2 | 2020-11-04 15:01:25 -0500 | [diff] [blame] | 668 | #include <__availability> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 669 | #include <type_traits> |
| 670 | #include <typeinfo> |
Arthur O'Dwyer | 7deec12 | 2021-03-24 18:19:12 -0400 | [diff] [blame] | 671 | #include <compare> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 672 | #include <cstddef> |
| 673 | #include <cstdint> |
| 674 | #include <new> |
| 675 | #include <utility> |
| 676 | #include <limits> |
| 677 | #include <iterator> |
| 678 | #include <__functional_base> |
Howard Hinnant | dc09597 | 2011-07-18 15:51:59 +0000 | [diff] [blame] | 679 | #include <iosfwd> |
Howard Hinnant | 83b1c05 | 2011-12-19 17:58:44 +0000 | [diff] [blame] | 680 | #include <tuple> |
Eric Fiselier | 2db2bd5 | 2016-05-07 03:12:24 +0000 | [diff] [blame] | 681 | #include <stdexcept> |
Howard Hinnant | d2cab2f | 2012-02-15 00:41:34 +0000 | [diff] [blame] | 682 | #include <cstring> |
Louis Dionne | fa621d7 | 2020-12-10 18:28:13 -0500 | [diff] [blame] | 683 | #include <__memory/allocator_traits.h> |
| 684 | #include <__memory/base.h> |
| 685 | #include <__memory/pointer_traits.h> |
Louis Dionne | 74aef9f | 2020-12-11 12:20:06 -0500 | [diff] [blame] | 686 | #include <__memory/utilities.h> |
Eric Fiselier | 8020b6c | 2015-08-19 17:21:46 +0000 | [diff] [blame] | 687 | #if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER) |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 688 | # include <atomic> |
| 689 | #endif |
Marshall Clow | 0a1e750 | 2018-09-12 19:41:40 +0000 | [diff] [blame] | 690 | #include <version> |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 691 | |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 692 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 693 | #pragma GCC system_header |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 694 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 695 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 696 | _LIBCPP_PUSH_MACROS |
| 697 | #include <__undef_macros> |
| 698 | |
| 699 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 700 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 701 | |
Eric Fiselier | 89659d1 | 2015-07-07 00:27:16 +0000 | [diff] [blame] | 702 | template <class _ValueType> |
Louis Dionne | 16fe295 | 2018-07-11 23:14:33 +0000 | [diff] [blame] | 703 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 89659d1 | 2015-07-07 00:27:16 +0000 | [diff] [blame] | 704 | _ValueType __libcpp_relaxed_load(_ValueType const* __value) { |
| 705 | #if !defined(_LIBCPP_HAS_NO_THREADS) && \ |
| 706 | defined(__ATOMIC_RELAXED) && \ |
Richard Smith | ca47d0f | 2019-04-25 20:02:10 +0000 | [diff] [blame] | 707 | (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC)) |
Eric Fiselier | 89659d1 | 2015-07-07 00:27:16 +0000 | [diff] [blame] | 708 | return __atomic_load_n(__value, __ATOMIC_RELAXED); |
| 709 | #else |
| 710 | return *__value; |
| 711 | #endif |
| 712 | } |
| 713 | |
Kuba Brecka | de9d679 | 2016-09-04 09:55:12 +0000 | [diff] [blame] | 714 | template <class _ValueType> |
Louis Dionne | 16fe295 | 2018-07-11 23:14:33 +0000 | [diff] [blame] | 715 | inline _LIBCPP_INLINE_VISIBILITY |
Kuba Brecka | de9d679 | 2016-09-04 09:55:12 +0000 | [diff] [blame] | 716 | _ValueType __libcpp_acquire_load(_ValueType const* __value) { |
| 717 | #if !defined(_LIBCPP_HAS_NO_THREADS) && \ |
| 718 | defined(__ATOMIC_ACQUIRE) && \ |
Richard Smith | ca47d0f | 2019-04-25 20:02:10 +0000 | [diff] [blame] | 719 | (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC)) |
Kuba Brecka | de9d679 | 2016-09-04 09:55:12 +0000 | [diff] [blame] | 720 | return __atomic_load_n(__value, __ATOMIC_ACQUIRE); |
| 721 | #else |
| 722 | return *__value; |
| 723 | #endif |
| 724 | } |
| 725 | |
Louis Dionne | fa621d7 | 2020-12-10 18:28:13 -0500 | [diff] [blame] | 726 | template <class _Tp> class allocator; |
Marshall Clow | 0be70c3 | 2017-06-14 21:23:57 +0000 | [diff] [blame] | 727 | |
Louis Dionne | fa621d7 | 2020-12-10 18:28:13 -0500 | [diff] [blame] | 728 | #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS) |
| 729 | template <> |
| 730 | class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<void> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 731 | { |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 732 | public: |
Louis Dionne | fa621d7 | 2020-12-10 18:28:13 -0500 | [diff] [blame] | 733 | typedef void* pointer; |
| 734 | typedef const void* const_pointer; |
| 735 | typedef void value_type; |
| 736 | |
| 737 | template <class _Up> struct rebind {typedef allocator<_Up> other;}; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 738 | }; |
| 739 | |
Louis Dionne | fa621d7 | 2020-12-10 18:28:13 -0500 | [diff] [blame] | 740 | template <> |
| 741 | class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 allocator<const void> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 742 | { |
Louis Dionne | fa621d7 | 2020-12-10 18:28:13 -0500 | [diff] [blame] | 743 | public: |
| 744 | typedef const void* pointer; |
| 745 | typedef const void* const_pointer; |
| 746 | typedef const void value_type; |
| 747 | |
| 748 | template <class _Up> struct rebind {typedef allocator<_Up> other;}; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 749 | }; |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 750 | #endif |
Marshall Clow | 940e01c | 2015-04-07 05:21:38 +0000 | [diff] [blame] | 751 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 752 | // allocator |
| 753 | |
| 754 | template <class _Tp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 755 | class _LIBCPP_TEMPLATE_VIS allocator |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 756 | { |
| 757 | public: |
Louis Dionne | f8b6c02 | 2020-09-21 10:36:37 -0400 | [diff] [blame] | 758 | typedef size_t size_type; |
| 759 | typedef ptrdiff_t difference_type; |
| 760 | typedef _Tp value_type; |
| 761 | typedef true_type propagate_on_container_move_assignment; |
| 762 | typedef true_type is_always_equal; |
| 763 | |
| 764 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
| 765 | allocator() _NOEXCEPT { } |
| 766 | |
| 767 | template <class _Up> |
| 768 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
| 769 | allocator(const allocator<_Up>&) _NOEXCEPT { } |
| 770 | |
| 771 | _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
| 772 | _Tp* allocate(size_t __n) { |
| 773 | if (__n > allocator_traits<allocator>::max_size(*this)) |
| 774 | __throw_length_error("allocator<T>::allocate(size_t n)" |
| 775 | " 'n' exceeds maximum supported size"); |
Louis Dionne | 3c989bc | 2020-09-28 17:29:52 -0400 | [diff] [blame] | 776 | if (__libcpp_is_constant_evaluated()) { |
| 777 | return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp))); |
| 778 | } else { |
| 779 | return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp))); |
| 780 | } |
Louis Dionne | f8b6c02 | 2020-09-21 10:36:37 -0400 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
| 784 | void deallocate(_Tp* __p, size_t __n) _NOEXCEPT { |
Louis Dionne | 3c989bc | 2020-09-28 17:29:52 -0400 | [diff] [blame] | 785 | if (__libcpp_is_constant_evaluated()) { |
| 786 | ::operator delete(__p); |
| 787 | } else { |
| 788 | _VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)); |
| 789 | } |
Louis Dionne | f8b6c02 | 2020-09-21 10:36:37 -0400 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | // C++20 Removed members |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 793 | #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS) |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 794 | _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer; |
| 795 | _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer; |
| 796 | _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference; |
| 797 | _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference; |
| 798 | |
Louis Dionne | 481a266 | 2018-09-23 18:35:00 +0000 | [diff] [blame] | 799 | template <class _Up> |
Louis Dionne | f8b6c02 | 2020-09-21 10:36:37 -0400 | [diff] [blame] | 800 | struct _LIBCPP_DEPRECATED_IN_CXX17 rebind { |
| 801 | typedef allocator<_Up> other; |
| 802 | }; |
Marshall Clow | bc75976 | 2018-03-20 23:02:53 +0000 | [diff] [blame] | 803 | |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 804 | _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | f8b6c02 | 2020-09-21 10:36:37 -0400 | [diff] [blame] | 805 | pointer address(reference __x) const _NOEXCEPT { |
| 806 | return _VSTD::addressof(__x); |
| 807 | } |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 808 | _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY |
Louis Dionne | f8b6c02 | 2020-09-21 10:36:37 -0400 | [diff] [blame] | 809 | const_pointer address(const_reference __x) const _NOEXCEPT { |
| 810 | return _VSTD::addressof(__x); |
| 811 | } |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 812 | |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 813 | _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17 |
Louis Dionne | f8b6c02 | 2020-09-21 10:36:37 -0400 | [diff] [blame] | 814 | _Tp* allocate(size_t __n, const void*) { |
| 815 | return allocate(__n); |
| 816 | } |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 817 | |
Louis Dionne | f8b6c02 | 2020-09-21 10:36:37 -0400 | [diff] [blame] | 818 | _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT { |
| 819 | return size_type(~0) / sizeof(_Tp); |
| 820 | } |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 821 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 822 | template <class _Up, class... _Args> |
Louis Dionne | f8b6c02 | 2020-09-21 10:36:37 -0400 | [diff] [blame] | 823 | _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY |
| 824 | void construct(_Up* __p, _Args&&... __args) { |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 825 | ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...); |
Louis Dionne | f8b6c02 | 2020-09-21 10:36:37 -0400 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY |
| 829 | void destroy(pointer __p) { |
| 830 | __p->~_Tp(); |
| 831 | } |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 832 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 833 | }; |
| 834 | |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 835 | template <class _Tp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 836 | class _LIBCPP_TEMPLATE_VIS allocator<const _Tp> |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 837 | { |
| 838 | public: |
Louis Dionne | f8b6c02 | 2020-09-21 10:36:37 -0400 | [diff] [blame] | 839 | typedef size_t size_type; |
| 840 | typedef ptrdiff_t difference_type; |
| 841 | typedef const _Tp value_type; |
| 842 | typedef true_type propagate_on_container_move_assignment; |
| 843 | typedef true_type is_always_equal; |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 844 | |
Marshall Clow | bc75976 | 2018-03-20 23:02:53 +0000 | [diff] [blame] | 845 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Louis Dionne | f8b6c02 | 2020-09-21 10:36:37 -0400 | [diff] [blame] | 846 | allocator() _NOEXCEPT { } |
Marshall Clow | bc75976 | 2018-03-20 23:02:53 +0000 | [diff] [blame] | 847 | |
| 848 | template <class _Up> |
Louis Dionne | 481a266 | 2018-09-23 18:35:00 +0000 | [diff] [blame] | 849 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Louis Dionne | f8b6c02 | 2020-09-21 10:36:37 -0400 | [diff] [blame] | 850 | allocator(const allocator<_Up>&) _NOEXCEPT { } |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 851 | |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 852 | _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Louis Dionne | f8b6c02 | 2020-09-21 10:36:37 -0400 | [diff] [blame] | 853 | const _Tp* allocate(size_t __n) { |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 854 | if (__n > allocator_traits<allocator>::max_size(*this)) |
Marshall Clow | ed3e229 | 2016-08-25 17:47:09 +0000 | [diff] [blame] | 855 | __throw_length_error("allocator<const T>::allocate(size_t n)" |
| 856 | " 'n' exceeds maximum supported size"); |
Louis Dionne | 3c989bc | 2020-09-28 17:29:52 -0400 | [diff] [blame] | 857 | if (__libcpp_is_constant_evaluated()) { |
| 858 | return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp))); |
| 859 | } else { |
| 860 | return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp))); |
| 861 | } |
Eric Fiselier | 2db2bd5 | 2016-05-07 03:12:24 +0000 | [diff] [blame] | 862 | } |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 863 | |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 864 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Louis Dionne | f8b6c02 | 2020-09-21 10:36:37 -0400 | [diff] [blame] | 865 | void deallocate(const _Tp* __p, size_t __n) { |
Louis Dionne | 3c989bc | 2020-09-28 17:29:52 -0400 | [diff] [blame] | 866 | if (__libcpp_is_constant_evaluated()) { |
| 867 | ::operator delete(const_cast<_Tp*>(__p)); |
| 868 | } else { |
| 869 | _VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)); |
| 870 | } |
Louis Dionne | f8b6c02 | 2020-09-21 10:36:37 -0400 | [diff] [blame] | 871 | } |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 872 | |
Louis Dionne | f8b6c02 | 2020-09-21 10:36:37 -0400 | [diff] [blame] | 873 | // C++20 Removed members |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 874 | #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS) |
Louis Dionne | f8b6c02 | 2020-09-21 10:36:37 -0400 | [diff] [blame] | 875 | _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer; |
| 876 | _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer; |
| 877 | _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference; |
| 878 | _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference; |
| 879 | |
| 880 | template <class _Up> |
| 881 | struct _LIBCPP_DEPRECATED_IN_CXX17 rebind { |
| 882 | typedef allocator<_Up> other; |
| 883 | }; |
| 884 | |
| 885 | _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY |
| 886 | const_pointer address(const_reference __x) const _NOEXCEPT { |
| 887 | return _VSTD::addressof(__x); |
| 888 | } |
| 889 | |
| 890 | _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17 |
| 891 | const _Tp* allocate(size_t __n, const void*) { |
| 892 | return allocate(__n); |
| 893 | } |
| 894 | |
| 895 | _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT { |
| 896 | return size_type(~0) / sizeof(_Tp); |
| 897 | } |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 898 | |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 899 | template <class _Up, class... _Args> |
Louis Dionne | f8b6c02 | 2020-09-21 10:36:37 -0400 | [diff] [blame] | 900 | _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY |
| 901 | void construct(_Up* __p, _Args&&... __args) { |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 902 | ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...); |
Louis Dionne | f8b6c02 | 2020-09-21 10:36:37 -0400 | [diff] [blame] | 903 | } |
Howard Hinnant | 9e028f1 | 2012-05-01 15:37:54 +0000 | [diff] [blame] | 904 | |
Louis Dionne | f8b6c02 | 2020-09-21 10:36:37 -0400 | [diff] [blame] | 905 | _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY |
| 906 | void destroy(pointer __p) { |
| 907 | __p->~_Tp(); |
| 908 | } |
Michael Park | 99f9e91 | 2020-03-04 11:27:14 -0500 | [diff] [blame] | 909 | #endif |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 910 | }; |
| 911 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 912 | template <class _Tp, class _Up> |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 913 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 914 | bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 915 | |
| 916 | template <class _Tp, class _Up> |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 917 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 918 | bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 919 | |
Louis Dionne | d665154 | 2020-11-03 12:05:55 -0500 | [diff] [blame] | 920 | template <class _Alloc, class _Ptr> |
| 921 | _LIBCPP_INLINE_VISIBILITY |
| 922 | void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) { |
| 923 | static_assert(__is_cpp17_move_insertable<_Alloc>::value, |
Arthur O'Dwyer | f2016bb | 2020-12-12 11:43:15 -0500 | [diff] [blame] | 924 | "The specified type does not meet the requirements of Cpp17MoveInsertable"); |
Louis Dionne | d665154 | 2020-11-03 12:05:55 -0500 | [diff] [blame] | 925 | typedef allocator_traits<_Alloc> _Traits; |
| 926 | for (; __begin1 != __end1; ++__begin1, (void)++__begin2) { |
| 927 | _Traits::construct(__a, _VSTD::__to_address(__begin2), |
| 928 | #ifdef _LIBCPP_NO_EXCEPTIONS |
| 929 | _VSTD::move(*__begin1) |
| 930 | #else |
| 931 | _VSTD::move_if_noexcept(*__begin1) |
| 932 | #endif |
| 933 | ); |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | template <class _Alloc, class _Tp, typename enable_if< |
| 938 | (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) && |
| 939 | is_trivially_move_constructible<_Tp>::value |
| 940 | >::type> |
| 941 | _LIBCPP_INLINE_VISIBILITY |
| 942 | void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) { |
| 943 | ptrdiff_t _Np = __end1 - __begin1; |
| 944 | if (_Np > 0) { |
| 945 | _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp)); |
| 946 | __begin2 += _Np; |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | template <class _Alloc, class _Iter, class _Ptr> |
| 951 | _LIBCPP_INLINE_VISIBILITY |
| 952 | void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) { |
| 953 | typedef allocator_traits<_Alloc> _Traits; |
| 954 | for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) { |
| 955 | _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1); |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | template <class _Alloc, class _Source, class _Dest, |
| 960 | class _RawSource = typename remove_const<_Source>::type, |
| 961 | class _RawDest = typename remove_const<_Dest>::type, |
| 962 | class = |
| 963 | typename enable_if< |
| 964 | is_trivially_copy_constructible<_Dest>::value && |
| 965 | is_same<_RawSource, _RawDest>::value && |
| 966 | (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value) |
| 967 | >::type> |
| 968 | _LIBCPP_INLINE_VISIBILITY |
| 969 | void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) { |
| 970 | ptrdiff_t _Np = __end1 - __begin1; |
| 971 | if (_Np > 0) { |
| 972 | _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest)); |
| 973 | __begin2 += _Np; |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | template <class _Alloc, class _Ptr> |
| 978 | _LIBCPP_INLINE_VISIBILITY |
| 979 | void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) { |
| 980 | static_assert(__is_cpp17_move_insertable<_Alloc>::value, |
| 981 | "The specified type does not meet the requirements of Cpp17MoveInsertable"); |
| 982 | typedef allocator_traits<_Alloc> _Traits; |
| 983 | while (__end1 != __begin1) { |
| 984 | _Traits::construct(__a, _VSTD::__to_address(__end2 - 1), |
| 985 | #ifdef _LIBCPP_NO_EXCEPTIONS |
| 986 | _VSTD::move(*--__end1) |
| 987 | #else |
| 988 | _VSTD::move_if_noexcept(*--__end1) |
| 989 | #endif |
| 990 | ); |
| 991 | --__end2; |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | template <class _Alloc, class _Tp, class = typename enable_if< |
| 996 | (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) && |
| 997 | is_trivially_move_constructible<_Tp>::value |
| 998 | >::type> |
| 999 | _LIBCPP_INLINE_VISIBILITY |
| 1000 | void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) { |
| 1001 | ptrdiff_t _Np = __end1 - __begin1; |
| 1002 | __end2 -= _Np; |
| 1003 | if (_Np > 0) |
| 1004 | _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp)); |
| 1005 | } |
| 1006 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1007 | template <class _OutputIterator, class _Tp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1008 | class _LIBCPP_TEMPLATE_VIS raw_storage_iterator |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1009 | : public iterator<output_iterator_tag, |
| 1010 | _Tp, // purposefully not C++03 |
| 1011 | ptrdiff_t, // purposefully not C++03 |
| 1012 | _Tp*, // purposefully not C++03 |
| 1013 | raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03 |
| 1014 | { |
| 1015 | private: |
| 1016 | _OutputIterator __x_; |
| 1017 | public: |
| 1018 | _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {} |
| 1019 | _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;} |
| 1020 | _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 1021 | {::new ((void*)_VSTD::addressof(*__x_)) _Tp(__element); return *this;} |
Marshall Clow | 5c3f09e | 2015-10-25 18:58:07 +0000 | [diff] [blame] | 1022 | #if _LIBCPP_STD_VER >= 14 |
| 1023 | _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 1024 | {::new ((void*)_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;} |
Marshall Clow | 5c3f09e | 2015-10-25 18:58:07 +0000 | [diff] [blame] | 1025 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1026 | _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;} |
| 1027 | _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int) |
| 1028 | {raw_storage_iterator __t(*this); ++__x_; return __t;} |
Marshall Clow | b949f1b | 2015-05-10 13:14:08 +0000 | [diff] [blame] | 1029 | #if _LIBCPP_STD_VER >= 14 |
Aditya Kumar | 7c5db69 | 2017-08-20 10:38:55 +0000 | [diff] [blame] | 1030 | _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; } |
Marshall Clow | b949f1b | 2015-05-10 13:14:08 +0000 | [diff] [blame] | 1031 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1032 | }; |
| 1033 | |
| 1034 | template <class _Tp> |
Roman Lebedev | b5959fa | 2018-09-22 17:54:48 +0000 | [diff] [blame] | 1035 | _LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1036 | pair<_Tp*, ptrdiff_t> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1037 | get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1038 | { |
| 1039 | pair<_Tp*, ptrdiff_t> __r(0, 0); |
| 1040 | const ptrdiff_t __m = (~ptrdiff_t(0) ^ |
| 1041 | ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1))) |
| 1042 | / sizeof(_Tp); |
| 1043 | if (__n > __m) |
| 1044 | __n = __m; |
| 1045 | while (__n > 0) |
| 1046 | { |
Eric Fiselier | 6a07b34 | 2018-10-26 17:12:32 +0000 | [diff] [blame] | 1047 | #if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) |
Eric Fiselier | 0f0ed9d | 2019-01-16 01:51:12 +0000 | [diff] [blame] | 1048 | if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp))) |
Richard Smith | 0657be1 | 2018-02-01 22:24:45 +0000 | [diff] [blame] | 1049 | { |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 1050 | align_val_t __al = |
| 1051 | align_val_t(alignment_of<_Tp>::value); |
Richard Smith | 0657be1 | 2018-02-01 22:24:45 +0000 | [diff] [blame] | 1052 | __r.first = static_cast<_Tp*>(::operator new( |
| 1053 | __n * sizeof(_Tp), __al, nothrow)); |
| 1054 | } else { |
| 1055 | __r.first = static_cast<_Tp*>(::operator new( |
| 1056 | __n * sizeof(_Tp), nothrow)); |
| 1057 | } |
| 1058 | #else |
Eric Fiselier | 0f0ed9d | 2019-01-16 01:51:12 +0000 | [diff] [blame] | 1059 | if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp))) |
Richard Smith | 0657be1 | 2018-02-01 22:24:45 +0000 | [diff] [blame] | 1060 | { |
| 1061 | // Since aligned operator new is unavailable, return an empty |
| 1062 | // buffer rather than one with invalid alignment. |
| 1063 | return __r; |
| 1064 | } |
| 1065 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1066 | __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow)); |
Richard Smith | 0657be1 | 2018-02-01 22:24:45 +0000 | [diff] [blame] | 1067 | #endif |
| 1068 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1069 | if (__r.first) |
| 1070 | { |
| 1071 | __r.second = __n; |
| 1072 | break; |
| 1073 | } |
| 1074 | __n /= 2; |
| 1075 | } |
| 1076 | return __r; |
| 1077 | } |
| 1078 | |
| 1079 | template <class _Tp> |
| 1080 | inline _LIBCPP_INLINE_VISIBILITY |
Richard Smith | 0657be1 | 2018-02-01 22:24:45 +0000 | [diff] [blame] | 1081 | void return_temporary_buffer(_Tp* __p) _NOEXCEPT |
| 1082 | { |
Eric Fiselier | 0f0ed9d | 2019-01-16 01:51:12 +0000 | [diff] [blame] | 1083 | _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp)); |
Richard Smith | 0657be1 | 2018-02-01 22:24:45 +0000 | [diff] [blame] | 1084 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1085 | |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 1086 | #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1087 | template <class _Tp> |
Louis Dionne | 481a266 | 2018-09-23 18:35:00 +0000 | [diff] [blame] | 1088 | struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1089 | { |
| 1090 | _Tp* __ptr_; |
| 1091 | }; |
| 1092 | |
| 1093 | template<class _Tp> |
Louis Dionne | 481a266 | 2018-09-23 18:35:00 +0000 | [diff] [blame] | 1094 | class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1095 | { |
| 1096 | private: |
| 1097 | _Tp* __ptr_; |
| 1098 | public: |
| 1099 | typedef _Tp element_type; |
| 1100 | |
Dimitry Andric | 47269ce | 2020-03-13 19:36:26 +0100 | [diff] [blame] | 1101 | _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) _NOEXCEPT : __ptr_(__p) {} |
| 1102 | _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) _NOEXCEPT : __ptr_(__p.release()) {} |
| 1103 | template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1104 | : __ptr_(__p.release()) {} |
Dimitry Andric | 47269ce | 2020-03-13 19:36:26 +0100 | [diff] [blame] | 1105 | _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1106 | {reset(__p.release()); return *this;} |
Dimitry Andric | 47269ce | 2020-03-13 19:36:26 +0100 | [diff] [blame] | 1107 | template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1108 | {reset(__p.release()); return *this;} |
Dimitry Andric | 47269ce | 2020-03-13 19:36:26 +0100 | [diff] [blame] | 1109 | _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1110 | {reset(__p.__ptr_); return *this;} |
Dimitry Andric | 47269ce | 2020-03-13 19:36:26 +0100 | [diff] [blame] | 1111 | _LIBCPP_INLINE_VISIBILITY ~auto_ptr() _NOEXCEPT {delete __ptr_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1112 | |
Dimitry Andric | 47269ce | 2020-03-13 19:36:26 +0100 | [diff] [blame] | 1113 | _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1114 | {return *__ptr_;} |
Dimitry Andric | 47269ce | 2020-03-13 19:36:26 +0100 | [diff] [blame] | 1115 | _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const _NOEXCEPT {return __ptr_;} |
| 1116 | _LIBCPP_INLINE_VISIBILITY _Tp* get() const _NOEXCEPT {return __ptr_;} |
| 1117 | _LIBCPP_INLINE_VISIBILITY _Tp* release() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1118 | { |
| 1119 | _Tp* __t = __ptr_; |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 1120 | __ptr_ = nullptr; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1121 | return __t; |
| 1122 | } |
Dimitry Andric | 47269ce | 2020-03-13 19:36:26 +0100 | [diff] [blame] | 1123 | _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1124 | { |
| 1125 | if (__ptr_ != __p) |
| 1126 | delete __ptr_; |
| 1127 | __ptr_ = __p; |
| 1128 | } |
| 1129 | |
Dimitry Andric | 47269ce | 2020-03-13 19:36:26 +0100 | [diff] [blame] | 1130 | _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) _NOEXCEPT : __ptr_(__p.__ptr_) {} |
| 1131 | template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1132 | {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;} |
Dimitry Andric | 47269ce | 2020-03-13 19:36:26 +0100 | [diff] [blame] | 1133 | template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1134 | {return auto_ptr<_Up>(release());} |
| 1135 | }; |
| 1136 | |
| 1137 | template <> |
Louis Dionne | 481a266 | 2018-09-23 18:35:00 +0000 | [diff] [blame] | 1138 | class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1139 | { |
| 1140 | public: |
| 1141 | typedef void element_type; |
| 1142 | }; |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 1143 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1144 | |
Eric Fiselier | 15eae3b | 2019-12-16 17:00:10 -0500 | [diff] [blame] | 1145 | // Tag used to default initialize one or both of the pair's elements. |
| 1146 | struct __default_init_tag {}; |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1147 | struct __value_init_tag {}; |
Eric Fiselier | 15eae3b | 2019-12-16 17:00:10 -0500 | [diff] [blame] | 1148 | |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1149 | template <class _Tp, int _Idx, |
| 1150 | bool _CanBeEmptyBase = |
| 1151 | is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value> |
| 1152 | struct __compressed_pair_elem { |
| 1153 | typedef _Tp _ParamT; |
| 1154 | typedef _Tp& reference; |
| 1155 | typedef const _Tp& const_reference; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1156 | |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1157 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Eric Fiselier | 15eae3b | 2019-12-16 17:00:10 -0500 | [diff] [blame] | 1158 | __compressed_pair_elem(__default_init_tag) {} |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1159 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 1160 | __compressed_pair_elem(__value_init_tag) : __value_() {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1161 | |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1162 | template <class _Up, class = typename enable_if< |
Eric Fiselier | 209ecde | 2017-04-17 22:32:02 +0000 | [diff] [blame] | 1163 | !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value |
| 1164 | >::type> |
Alex Lorenz | 7613211 | 2017-11-09 17:54:49 +0000 | [diff] [blame] | 1165 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1166 | _LIBCPP_CONSTEXPR explicit |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1167 | __compressed_pair_elem(_Up&& __u) |
Eric Fiselier | b41db9a | 2018-10-01 01:59:37 +0000 | [diff] [blame] | 1168 | : __value_(_VSTD::forward<_Up>(__u)) |
| 1169 | { |
| 1170 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1171 | |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1172 | |
| 1173 | #ifndef _LIBCPP_CXX03_LANG |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1174 | template <class... _Args, size_t... _Indexes> |
| 1175 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 1176 | __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args, |
| 1177 | __tuple_indices<_Indexes...>) |
| 1178 | : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {} |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1179 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1180 | |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1181 | |
Alex Lorenz | 7613211 | 2017-11-09 17:54:49 +0000 | [diff] [blame] | 1182 | _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; } |
| 1183 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1184 | const_reference __get() const _NOEXCEPT { return __value_; } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1185 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1186 | private: |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1187 | _Tp __value_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1188 | }; |
| 1189 | |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1190 | template <class _Tp, int _Idx> |
| 1191 | struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp { |
| 1192 | typedef _Tp _ParamT; |
| 1193 | typedef _Tp& reference; |
| 1194 | typedef const _Tp& const_reference; |
| 1195 | typedef _Tp __value_type; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1196 | |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1197 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR __compressed_pair_elem() = default; |
| 1198 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 1199 | __compressed_pair_elem(__default_init_tag) {} |
| 1200 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 1201 | __compressed_pair_elem(__value_init_tag) : __value_type() {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1202 | |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1203 | template <class _Up, class = typename enable_if< |
Eric Fiselier | 209ecde | 2017-04-17 22:32:02 +0000 | [diff] [blame] | 1204 | !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value |
| 1205 | >::type> |
Alex Lorenz | 7613211 | 2017-11-09 17:54:49 +0000 | [diff] [blame] | 1206 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1207 | _LIBCPP_CONSTEXPR explicit |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1208 | __compressed_pair_elem(_Up&& __u) |
Eric Fiselier | b41db9a | 2018-10-01 01:59:37 +0000 | [diff] [blame] | 1209 | : __value_type(_VSTD::forward<_Up>(__u)) |
| 1210 | {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1211 | |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1212 | #ifndef _LIBCPP_CXX03_LANG |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1213 | template <class... _Args, size_t... _Indexes> |
| 1214 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 1215 | __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args, |
| 1216 | __tuple_indices<_Indexes...>) |
| 1217 | : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {} |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1218 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1219 | |
Alex Lorenz | 7613211 | 2017-11-09 17:54:49 +0000 | [diff] [blame] | 1220 | _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; } |
| 1221 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1222 | const_reference __get() const _NOEXCEPT { return *this; } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1223 | }; |
| 1224 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1225 | template <class _T1, class _T2> |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1226 | class __compressed_pair : private __compressed_pair_elem<_T1, 0>, |
| 1227 | private __compressed_pair_elem<_T2, 1> { |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 1228 | public: |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1229 | // NOTE: This static assert should never fire because __compressed_pair |
| 1230 | // is *almost never* used in a scenario where it's possible for T1 == T2. |
| 1231 | // (The exception is std::function where it is possible that the function |
| 1232 | // object and the allocator have the same type). |
Eric Fiselier | c5adf47 | 2017-04-13 01:13:58 +0000 | [diff] [blame] | 1233 | static_assert((!is_same<_T1, _T2>::value), |
Arthur O'Dwyer | fed1ff6 | 2020-12-01 20:02:46 -0500 | [diff] [blame] | 1234 | "__compressed_pair cannot be instantiated when T1 and T2 are the same type; " |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1235 | "The current implementation is NOT ABI-compatible with the previous " |
| 1236 | "implementation for this configuration"); |
| 1237 | |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 1238 | typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1; |
| 1239 | typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2; |
| 1240 | |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1241 | template <bool _Dummy = true, |
Eric Fiselier | 209ecde | 2017-04-17 22:32:02 +0000 | [diff] [blame] | 1242 | class = typename enable_if< |
| 1243 | __dependent_type<is_default_constructible<_T1>, _Dummy>::value && |
| 1244 | __dependent_type<is_default_constructible<_T2>, _Dummy>::value |
| 1245 | >::type |
| 1246 | > |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1247 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1248 | _LIBCPP_CONSTEXPR __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1249 | |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1250 | template <class _U1, class _U2> |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1251 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1252 | __compressed_pair(_U1&& __t1, _U2&& __t2) |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 1253 | : _Base1(_VSTD::forward<_U1>(__t1)), _Base2(_VSTD::forward<_U2>(__t2)) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1254 | |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1255 | #ifndef _LIBCPP_CXX03_LANG |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1256 | template <class... _Args1, class... _Args2> |
| 1257 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
| 1258 | __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args, |
| 1259 | tuple<_Args2...> __second_args) |
| 1260 | : _Base1(__pc, _VSTD::move(__first_args), |
| 1261 | typename __make_tuple_indices<sizeof...(_Args1)>::type()), |
| 1262 | _Base2(__pc, _VSTD::move(__second_args), |
| 1263 | typename __make_tuple_indices<sizeof...(_Args2)>::type()) {} |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1264 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1265 | |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1266 | _LIBCPP_INLINE_VISIBILITY |
| 1267 | typename _Base1::reference first() _NOEXCEPT { |
| 1268 | return static_cast<_Base1&>(*this).__get(); |
| 1269 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1270 | |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1271 | _LIBCPP_INLINE_VISIBILITY |
| 1272 | typename _Base1::const_reference first() const _NOEXCEPT { |
| 1273 | return static_cast<_Base1 const&>(*this).__get(); |
| 1274 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1275 | |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1276 | _LIBCPP_INLINE_VISIBILITY |
| 1277 | typename _Base2::reference second() _NOEXCEPT { |
| 1278 | return static_cast<_Base2&>(*this).__get(); |
| 1279 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1280 | |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1281 | _LIBCPP_INLINE_VISIBILITY |
| 1282 | typename _Base2::const_reference second() const _NOEXCEPT { |
| 1283 | return static_cast<_Base2 const&>(*this).__get(); |
| 1284 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1285 | |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 1286 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 1287 | static _Base1* __get_first_base(__compressed_pair* __pair) _NOEXCEPT { |
| 1288 | return static_cast<_Base1*>(__pair); |
| 1289 | } |
| 1290 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 1291 | static _Base2* __get_second_base(__compressed_pair* __pair) _NOEXCEPT { |
| 1292 | return static_cast<_Base2*>(__pair); |
| 1293 | } |
| 1294 | |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1295 | _LIBCPP_INLINE_VISIBILITY |
| 1296 | void swap(__compressed_pair& __x) |
| 1297 | _NOEXCEPT_(__is_nothrow_swappable<_T1>::value && |
| 1298 | __is_nothrow_swappable<_T2>::value) |
| 1299 | { |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 1300 | using _VSTD::swap; |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1301 | swap(first(), __x.first()); |
| 1302 | swap(second(), __x.second()); |
| 1303 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1304 | }; |
| 1305 | |
| 1306 | template <class _T1, class _T2> |
| 1307 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 9d35598 | 2017-04-12 23:45:53 +0000 | [diff] [blame] | 1308 | void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y) |
| 1309 | _NOEXCEPT_(__is_nothrow_swappable<_T1>::value && |
| 1310 | __is_nothrow_swappable<_T2>::value) { |
| 1311 | __x.swap(__y); |
| 1312 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1313 | |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 1314 | // default_delete |
| 1315 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1316 | template <class _Tp> |
Eric Fiselier | 6779b06 | 2017-04-16 02:06:25 +0000 | [diff] [blame] | 1317 | struct _LIBCPP_TEMPLATE_VIS default_delete { |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 1318 | static_assert(!is_function<_Tp>::value, |
| 1319 | "default_delete cannot be instantiated for function types"); |
Eric Fiselier | 6779b06 | 2017-04-16 02:06:25 +0000 | [diff] [blame] | 1320 | #ifndef _LIBCPP_CXX03_LANG |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1321 | _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default; |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 1322 | #else |
Eric Fiselier | 6779b06 | 2017-04-16 02:06:25 +0000 | [diff] [blame] | 1323 | _LIBCPP_INLINE_VISIBILITY default_delete() {} |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 1324 | #endif |
Eric Fiselier | 6779b06 | 2017-04-16 02:06:25 +0000 | [diff] [blame] | 1325 | template <class _Up> |
| 1326 | _LIBCPP_INLINE_VISIBILITY |
| 1327 | default_delete(const default_delete<_Up>&, |
| 1328 | typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = |
| 1329 | 0) _NOEXCEPT {} |
| 1330 | |
| 1331 | _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT { |
| 1332 | static_assert(sizeof(_Tp) > 0, |
| 1333 | "default_delete can not delete incomplete type"); |
| 1334 | static_assert(!is_void<_Tp>::value, |
| 1335 | "default_delete can not delete incomplete type"); |
| 1336 | delete __ptr; |
| 1337 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1338 | }; |
| 1339 | |
| 1340 | template <class _Tp> |
Eric Fiselier | 6779b06 | 2017-04-16 02:06:25 +0000 | [diff] [blame] | 1341 | struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> { |
| 1342 | private: |
| 1343 | template <class _Up> |
| 1344 | struct _EnableIfConvertible |
| 1345 | : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {}; |
| 1346 | |
Howard Hinnant | 4500ca5 | 2011-12-18 21:19:44 +0000 | [diff] [blame] | 1347 | public: |
Eric Fiselier | 6779b06 | 2017-04-16 02:06:25 +0000 | [diff] [blame] | 1348 | #ifndef _LIBCPP_CXX03_LANG |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1349 | _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default; |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 1350 | #else |
Eric Fiselier | 6779b06 | 2017-04-16 02:06:25 +0000 | [diff] [blame] | 1351 | _LIBCPP_INLINE_VISIBILITY default_delete() {} |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 1352 | #endif |
Eric Fiselier | 6779b06 | 2017-04-16 02:06:25 +0000 | [diff] [blame] | 1353 | |
| 1354 | template <class _Up> |
| 1355 | _LIBCPP_INLINE_VISIBILITY |
| 1356 | default_delete(const default_delete<_Up[]>&, |
| 1357 | typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {} |
| 1358 | |
| 1359 | template <class _Up> |
| 1360 | _LIBCPP_INLINE_VISIBILITY |
| 1361 | typename _EnableIfConvertible<_Up>::type |
| 1362 | operator()(_Up* __ptr) const _NOEXCEPT { |
| 1363 | static_assert(sizeof(_Tp) > 0, |
| 1364 | "default_delete can not delete incomplete type"); |
| 1365 | static_assert(!is_void<_Tp>::value, |
| 1366 | "default_delete can not delete void type"); |
| 1367 | delete[] __ptr; |
| 1368 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1369 | }; |
| 1370 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1371 | template <class _Deleter> |
| 1372 | struct __unique_ptr_deleter_sfinae { |
| 1373 | static_assert(!is_reference<_Deleter>::value, "incorrect specialization"); |
| 1374 | typedef const _Deleter& __lval_ref_type; |
| 1375 | typedef _Deleter&& __good_rval_ref_type; |
| 1376 | typedef true_type __enable_rval_overload; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1377 | }; |
| 1378 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1379 | template <class _Deleter> |
| 1380 | struct __unique_ptr_deleter_sfinae<_Deleter const&> { |
| 1381 | typedef const _Deleter& __lval_ref_type; |
| 1382 | typedef const _Deleter&& __bad_rval_ref_type; |
| 1383 | typedef false_type __enable_rval_overload; |
| 1384 | }; |
| 1385 | |
| 1386 | template <class _Deleter> |
| 1387 | struct __unique_ptr_deleter_sfinae<_Deleter&> { |
| 1388 | typedef _Deleter& __lval_ref_type; |
| 1389 | typedef _Deleter&& __bad_rval_ref_type; |
| 1390 | typedef false_type __enable_rval_overload; |
| 1391 | }; |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1392 | |
Vy Nguyen | e369bd9 | 2020-07-13 12:34:37 -0400 | [diff] [blame] | 1393 | #if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI) |
| 1394 | # define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi)) |
| 1395 | #else |
| 1396 | # define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI |
| 1397 | #endif |
| 1398 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1399 | template <class _Tp, class _Dp = default_delete<_Tp> > |
Vy Nguyen | e369bd9 | 2020-07-13 12:34:37 -0400 | [diff] [blame] | 1400 | class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr { |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1401 | public: |
| 1402 | typedef _Tp element_type; |
| 1403 | typedef _Dp deleter_type; |
Louis Dionne | 88978f6 | 2021-01-12 11:53:24 -0500 | [diff] [blame] | 1404 | typedef _LIBCPP_NODEBUG_TYPE typename __pointer<_Tp, deleter_type>::type pointer; |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1405 | |
| 1406 | static_assert(!is_rvalue_reference<deleter_type>::value, |
| 1407 | "the specified deleter type cannot be an rvalue reference"); |
| 1408 | |
| 1409 | private: |
| 1410 | __compressed_pair<pointer, deleter_type> __ptr_; |
| 1411 | |
| 1412 | struct __nat { int __for_bool_; }; |
| 1413 | |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1414 | typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE; |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1415 | |
| 1416 | template <bool _Dummy> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1417 | using _LValRefType _LIBCPP_NODEBUG_TYPE = |
Eric Fiselier | 31127cd | 2017-04-16 02:14:31 +0000 | [diff] [blame] | 1418 | typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type; |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1419 | |
| 1420 | template <bool _Dummy> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1421 | using _GoodRValRefType _LIBCPP_NODEBUG_TYPE = |
Eric Fiselier | 31127cd | 2017-04-16 02:14:31 +0000 | [diff] [blame] | 1422 | typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type; |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1423 | |
| 1424 | template <bool _Dummy> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1425 | using _BadRValRefType _LIBCPP_NODEBUG_TYPE = |
Eric Fiselier | 31127cd | 2017-04-16 02:14:31 +0000 | [diff] [blame] | 1426 | typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type; |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1427 | |
| 1428 | template <bool _Dummy, class _Deleter = typename __dependent_type< |
| 1429 | __identity<deleter_type>, _Dummy>::type> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1430 | using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE = |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1431 | typename enable_if<is_default_constructible<_Deleter>::value && |
| 1432 | !is_pointer<_Deleter>::value>::type; |
| 1433 | |
| 1434 | template <class _ArgType> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1435 | using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE = |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1436 | typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type; |
| 1437 | |
| 1438 | template <class _UPtr, class _Up> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1439 | using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if< |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1440 | is_convertible<typename _UPtr::pointer, pointer>::value && |
| 1441 | !is_array<_Up>::value |
| 1442 | >::type; |
| 1443 | |
| 1444 | template <class _UDel> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1445 | using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if< |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1446 | (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) || |
| 1447 | (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value) |
| 1448 | >::type; |
| 1449 | |
| 1450 | template <class _UDel> |
| 1451 | using _EnableIfDeleterAssignable = typename enable_if< |
| 1452 | is_assignable<_Dp&, _UDel&&>::value |
| 1453 | >::type; |
| 1454 | |
| 1455 | public: |
| 1456 | template <bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1457 | class = _EnableIfDeleterDefaultConstructible<_Dummy> > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1458 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1459 | _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {} |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1460 | |
| 1461 | template <bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1462 | class = _EnableIfDeleterDefaultConstructible<_Dummy> > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1463 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1464 | _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {} |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1465 | |
| 1466 | template <bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1467 | class = _EnableIfDeleterDefaultConstructible<_Dummy> > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1468 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1469 | explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {} |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1470 | |
| 1471 | template <bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1472 | class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1473 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1474 | unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1475 | : __ptr_(__p, __d) {} |
| 1476 | |
| 1477 | template <bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1478 | class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1479 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1480 | unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1481 | : __ptr_(__p, _VSTD::move(__d)) { |
| 1482 | static_assert(!is_reference<deleter_type>::value, |
| 1483 | "rvalue deleter bound to reference"); |
| 1484 | } |
| 1485 | |
| 1486 | template <bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1487 | class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1488 | _LIBCPP_INLINE_VISIBILITY |
| 1489 | unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete; |
| 1490 | |
| 1491 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1492 | unique_ptr(unique_ptr&& __u) _NOEXCEPT |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1493 | : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) { |
| 1494 | } |
| 1495 | |
| 1496 | template <class _Up, class _Ep, |
| 1497 | class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>, |
| 1498 | class = _EnableIfDeleterConvertible<_Ep> |
| 1499 | > |
| 1500 | _LIBCPP_INLINE_VISIBILITY |
| 1501 | unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT |
| 1502 | : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {} |
| 1503 | |
| 1504 | #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) |
| 1505 | template <class _Up> |
| 1506 | _LIBCPP_INLINE_VISIBILITY |
| 1507 | unique_ptr(auto_ptr<_Up>&& __p, |
| 1508 | typename enable_if<is_convertible<_Up*, _Tp*>::value && |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1509 | is_same<_Dp, default_delete<_Tp> >::value, |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1510 | __nat>::type = __nat()) _NOEXCEPT |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1511 | : __ptr_(__p.release(), __default_init_tag()) {} |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1512 | #endif |
| 1513 | |
| 1514 | _LIBCPP_INLINE_VISIBILITY |
| 1515 | unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT { |
| 1516 | reset(__u.release()); |
| 1517 | __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter()); |
| 1518 | return *this; |
| 1519 | } |
| 1520 | |
| 1521 | template <class _Up, class _Ep, |
| 1522 | class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>, |
| 1523 | class = _EnableIfDeleterAssignable<_Ep> |
| 1524 | > |
| 1525 | _LIBCPP_INLINE_VISIBILITY |
| 1526 | unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT { |
| 1527 | reset(__u.release()); |
| 1528 | __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter()); |
| 1529 | return *this; |
| 1530 | } |
| 1531 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1532 | #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) |
| 1533 | template <class _Up> |
| 1534 | _LIBCPP_INLINE_VISIBILITY |
| 1535 | typename enable_if<is_convertible<_Up*, _Tp*>::value && |
| 1536 | is_same<_Dp, default_delete<_Tp> >::value, |
| 1537 | unique_ptr&>::type |
| 1538 | operator=(auto_ptr<_Up> __p) { |
| 1539 | reset(__p.release()); |
| 1540 | return *this; |
| 1541 | } |
| 1542 | #endif |
| 1543 | |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1544 | #ifdef _LIBCPP_CXX03_LANG |
| 1545 | unique_ptr(unique_ptr const&) = delete; |
| 1546 | unique_ptr& operator=(unique_ptr const&) = delete; |
| 1547 | #endif |
| 1548 | |
| 1549 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1550 | _LIBCPP_INLINE_VISIBILITY |
| 1551 | ~unique_ptr() { reset(); } |
| 1552 | |
| 1553 | _LIBCPP_INLINE_VISIBILITY |
| 1554 | unique_ptr& operator=(nullptr_t) _NOEXCEPT { |
| 1555 | reset(); |
| 1556 | return *this; |
| 1557 | } |
| 1558 | |
| 1559 | _LIBCPP_INLINE_VISIBILITY |
| 1560 | typename add_lvalue_reference<_Tp>::type |
| 1561 | operator*() const { |
| 1562 | return *__ptr_.first(); |
| 1563 | } |
| 1564 | _LIBCPP_INLINE_VISIBILITY |
| 1565 | pointer operator->() const _NOEXCEPT { |
| 1566 | return __ptr_.first(); |
| 1567 | } |
| 1568 | _LIBCPP_INLINE_VISIBILITY |
| 1569 | pointer get() const _NOEXCEPT { |
| 1570 | return __ptr_.first(); |
| 1571 | } |
| 1572 | _LIBCPP_INLINE_VISIBILITY |
| 1573 | deleter_type& get_deleter() _NOEXCEPT { |
| 1574 | return __ptr_.second(); |
| 1575 | } |
| 1576 | _LIBCPP_INLINE_VISIBILITY |
| 1577 | const deleter_type& get_deleter() const _NOEXCEPT { |
| 1578 | return __ptr_.second(); |
| 1579 | } |
| 1580 | _LIBCPP_INLINE_VISIBILITY |
| 1581 | _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT { |
| 1582 | return __ptr_.first() != nullptr; |
| 1583 | } |
| 1584 | |
| 1585 | _LIBCPP_INLINE_VISIBILITY |
| 1586 | pointer release() _NOEXCEPT { |
| 1587 | pointer __t = __ptr_.first(); |
| 1588 | __ptr_.first() = pointer(); |
| 1589 | return __t; |
| 1590 | } |
| 1591 | |
| 1592 | _LIBCPP_INLINE_VISIBILITY |
| 1593 | void reset(pointer __p = pointer()) _NOEXCEPT { |
| 1594 | pointer __tmp = __ptr_.first(); |
| 1595 | __ptr_.first() = __p; |
| 1596 | if (__tmp) |
| 1597 | __ptr_.second()(__tmp); |
| 1598 | } |
| 1599 | |
| 1600 | _LIBCPP_INLINE_VISIBILITY |
| 1601 | void swap(unique_ptr& __u) _NOEXCEPT { |
| 1602 | __ptr_.swap(__u.__ptr_); |
| 1603 | } |
| 1604 | }; |
| 1605 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1606 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1607 | template <class _Tp, class _Dp> |
Vy Nguyen | e369bd9 | 2020-07-13 12:34:37 -0400 | [diff] [blame] | 1608 | class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> { |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1609 | public: |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1610 | typedef _Tp element_type; |
| 1611 | typedef _Dp deleter_type; |
Louis Dionne | 88978f6 | 2021-01-12 11:53:24 -0500 | [diff] [blame] | 1612 | typedef typename __pointer<_Tp, deleter_type>::type pointer; |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1613 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1614 | private: |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1615 | __compressed_pair<pointer, deleter_type> __ptr_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1616 | |
Eric Fiselier | 31127cd | 2017-04-16 02:14:31 +0000 | [diff] [blame] | 1617 | template <class _From> |
| 1618 | struct _CheckArrayPointerConversion : is_same<_From, pointer> {}; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1619 | |
Eric Fiselier | 31127cd | 2017-04-16 02:14:31 +0000 | [diff] [blame] | 1620 | template <class _FromElem> |
| 1621 | struct _CheckArrayPointerConversion<_FromElem*> |
| 1622 | : integral_constant<bool, |
| 1623 | is_same<_FromElem*, pointer>::value || |
| 1624 | (is_same<pointer, element_type*>::value && |
| 1625 | is_convertible<_FromElem(*)[], element_type(*)[]>::value) |
| 1626 | > |
| 1627 | {}; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1628 | |
Eric Fiselier | 31127cd | 2017-04-16 02:14:31 +0000 | [diff] [blame] | 1629 | typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE; |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1630 | |
| 1631 | template <bool _Dummy> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1632 | using _LValRefType _LIBCPP_NODEBUG_TYPE = |
Eric Fiselier | 31127cd | 2017-04-16 02:14:31 +0000 | [diff] [blame] | 1633 | typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type; |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1634 | |
| 1635 | template <bool _Dummy> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1636 | using _GoodRValRefType _LIBCPP_NODEBUG_TYPE = |
Eric Fiselier | 31127cd | 2017-04-16 02:14:31 +0000 | [diff] [blame] | 1637 | typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type; |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1638 | |
| 1639 | template <bool _Dummy> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1640 | using _BadRValRefType _LIBCPP_NODEBUG_TYPE = |
Eric Fiselier | 31127cd | 2017-04-16 02:14:31 +0000 | [diff] [blame] | 1641 | typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type; |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1642 | |
| 1643 | template <bool _Dummy, class _Deleter = typename __dependent_type< |
| 1644 | __identity<deleter_type>, _Dummy>::type> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1645 | using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE = |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1646 | typename enable_if<is_default_constructible<_Deleter>::value && |
| 1647 | !is_pointer<_Deleter>::value>::type; |
| 1648 | |
| 1649 | template <class _ArgType> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1650 | using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE = |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1651 | typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type; |
| 1652 | |
| 1653 | template <class _Pp> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1654 | using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if< |
Eric Fiselier | 31127cd | 2017-04-16 02:14:31 +0000 | [diff] [blame] | 1655 | _CheckArrayPointerConversion<_Pp>::value |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1656 | >::type; |
| 1657 | |
| 1658 | template <class _UPtr, class _Up, |
| 1659 | class _ElemT = typename _UPtr::element_type> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1660 | using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if< |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1661 | is_array<_Up>::value && |
| 1662 | is_same<pointer, element_type*>::value && |
| 1663 | is_same<typename _UPtr::pointer, _ElemT*>::value && |
| 1664 | is_convertible<_ElemT(*)[], element_type(*)[]>::value |
| 1665 | >::type; |
| 1666 | |
| 1667 | template <class _UDel> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1668 | using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if< |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1669 | (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) || |
| 1670 | (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value) |
| 1671 | >::type; |
| 1672 | |
| 1673 | template <class _UDel> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1674 | using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if< |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1675 | is_assignable<_Dp&, _UDel&&>::value |
| 1676 | >::type; |
| 1677 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1678 | public: |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1679 | template <bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1680 | class = _EnableIfDeleterDefaultConstructible<_Dummy> > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1681 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1682 | _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1683 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1684 | template <bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1685 | class = _EnableIfDeleterDefaultConstructible<_Dummy> > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1686 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1687 | _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1688 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1689 | template <class _Pp, bool _Dummy = true, |
| 1690 | class = _EnableIfDeleterDefaultConstructible<_Dummy>, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1691 | class = _EnableIfPointerConvertible<_Pp> > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1692 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1693 | explicit unique_ptr(_Pp __p) _NOEXCEPT |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1694 | : __ptr_(__p, __default_init_tag()) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1695 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1696 | template <class _Pp, bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1697 | class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >, |
| 1698 | class = _EnableIfPointerConvertible<_Pp> > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1699 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1700 | unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1701 | : __ptr_(__p, __d) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1702 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1703 | template <bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1704 | class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1705 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1706 | unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1707 | : __ptr_(nullptr, __d) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1708 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1709 | template <class _Pp, bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1710 | class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >, |
| 1711 | class = _EnableIfPointerConvertible<_Pp> > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1712 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1713 | unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1714 | : __ptr_(__p, _VSTD::move(__d)) { |
| 1715 | static_assert(!is_reference<deleter_type>::value, |
| 1716 | "rvalue deleter bound to reference"); |
| 1717 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1718 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1719 | template <bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1720 | class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1721 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1722 | unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1723 | : __ptr_(nullptr, _VSTD::move(__d)) { |
| 1724 | static_assert(!is_reference<deleter_type>::value, |
| 1725 | "rvalue deleter bound to reference"); |
| 1726 | } |
Howard Hinnant | 4500ca5 | 2011-12-18 21:19:44 +0000 | [diff] [blame] | 1727 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1728 | template <class _Pp, bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1729 | class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >, |
| 1730 | class = _EnableIfPointerConvertible<_Pp> > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1731 | _LIBCPP_INLINE_VISIBILITY |
| 1732 | unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete; |
Howard Hinnant | 4500ca5 | 2011-12-18 21:19:44 +0000 | [diff] [blame] | 1733 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1734 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1735 | unique_ptr(unique_ptr&& __u) _NOEXCEPT |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1736 | : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) { |
| 1737 | } |
Howard Hinnant | 4500ca5 | 2011-12-18 21:19:44 +0000 | [diff] [blame] | 1738 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1739 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1740 | unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT { |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1741 | reset(__u.release()); |
| 1742 | __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter()); |
| 1743 | return *this; |
| 1744 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1745 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1746 | template <class _Up, class _Ep, |
| 1747 | class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>, |
| 1748 | class = _EnableIfDeleterConvertible<_Ep> |
| 1749 | > |
| 1750 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1751 | unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT |
Eric Fiselier | 3827e6a | 2017-04-17 20:20:27 +0000 | [diff] [blame] | 1752 | : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) { |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1753 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1754 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1755 | template <class _Up, class _Ep, |
| 1756 | class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>, |
| 1757 | class = _EnableIfDeleterAssignable<_Ep> |
| 1758 | > |
| 1759 | _LIBCPP_INLINE_VISIBILITY |
| 1760 | unique_ptr& |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1761 | operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT { |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1762 | reset(__u.release()); |
| 1763 | __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter()); |
| 1764 | return *this; |
| 1765 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1766 | |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1767 | #ifdef _LIBCPP_CXX03_LANG |
| 1768 | unique_ptr(unique_ptr const&) = delete; |
| 1769 | unique_ptr& operator=(unique_ptr const&) = delete; |
| 1770 | #endif |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1771 | |
| 1772 | public: |
| 1773 | _LIBCPP_INLINE_VISIBILITY |
| 1774 | ~unique_ptr() { reset(); } |
| 1775 | |
| 1776 | _LIBCPP_INLINE_VISIBILITY |
| 1777 | unique_ptr& operator=(nullptr_t) _NOEXCEPT { |
| 1778 | reset(); |
| 1779 | return *this; |
| 1780 | } |
| 1781 | |
| 1782 | _LIBCPP_INLINE_VISIBILITY |
| 1783 | typename add_lvalue_reference<_Tp>::type |
| 1784 | operator[](size_t __i) const { |
| 1785 | return __ptr_.first()[__i]; |
| 1786 | } |
| 1787 | _LIBCPP_INLINE_VISIBILITY |
| 1788 | pointer get() const _NOEXCEPT { |
| 1789 | return __ptr_.first(); |
| 1790 | } |
| 1791 | |
| 1792 | _LIBCPP_INLINE_VISIBILITY |
| 1793 | deleter_type& get_deleter() _NOEXCEPT { |
| 1794 | return __ptr_.second(); |
| 1795 | } |
| 1796 | |
| 1797 | _LIBCPP_INLINE_VISIBILITY |
| 1798 | const deleter_type& get_deleter() const _NOEXCEPT { |
| 1799 | return __ptr_.second(); |
| 1800 | } |
| 1801 | _LIBCPP_INLINE_VISIBILITY |
| 1802 | _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT { |
| 1803 | return __ptr_.first() != nullptr; |
| 1804 | } |
| 1805 | |
| 1806 | _LIBCPP_INLINE_VISIBILITY |
| 1807 | pointer release() _NOEXCEPT { |
| 1808 | pointer __t = __ptr_.first(); |
| 1809 | __ptr_.first() = pointer(); |
| 1810 | return __t; |
| 1811 | } |
| 1812 | |
| 1813 | template <class _Pp> |
| 1814 | _LIBCPP_INLINE_VISIBILITY |
| 1815 | typename enable_if< |
Eric Fiselier | 31127cd | 2017-04-16 02:14:31 +0000 | [diff] [blame] | 1816 | _CheckArrayPointerConversion<_Pp>::value |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1817 | >::type |
| 1818 | reset(_Pp __p) _NOEXCEPT { |
| 1819 | pointer __tmp = __ptr_.first(); |
| 1820 | __ptr_.first() = __p; |
| 1821 | if (__tmp) |
| 1822 | __ptr_.second()(__tmp); |
| 1823 | } |
| 1824 | |
| 1825 | _LIBCPP_INLINE_VISIBILITY |
| 1826 | void reset(nullptr_t = nullptr) _NOEXCEPT { |
| 1827 | pointer __tmp = __ptr_.first(); |
| 1828 | __ptr_.first() = nullptr; |
| 1829 | if (__tmp) |
| 1830 | __ptr_.second()(__tmp); |
| 1831 | } |
| 1832 | |
| 1833 | _LIBCPP_INLINE_VISIBILITY |
| 1834 | void swap(unique_ptr& __u) _NOEXCEPT { |
| 1835 | __ptr_.swap(__u.__ptr_); |
| 1836 | } |
| 1837 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1838 | }; |
| 1839 | |
| 1840 | template <class _Tp, class _Dp> |
| 1841 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 6bfed25 | 2016-04-21 23:38:59 +0000 | [diff] [blame] | 1842 | typename enable_if< |
| 1843 | __is_swappable<_Dp>::value, |
| 1844 | void |
| 1845 | >::type |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1846 | swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1847 | |
| 1848 | template <class _T1, class _D1, class _T2, class _D2> |
| 1849 | inline _LIBCPP_INLINE_VISIBILITY |
| 1850 | bool |
| 1851 | operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();} |
| 1852 | |
| 1853 | template <class _T1, class _D1, class _T2, class _D2> |
| 1854 | inline _LIBCPP_INLINE_VISIBILITY |
| 1855 | bool |
| 1856 | operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);} |
| 1857 | |
| 1858 | template <class _T1, class _D1, class _T2, class _D2> |
| 1859 | inline _LIBCPP_INLINE_VISIBILITY |
| 1860 | bool |
Howard Hinnant | b17caf9 | 2012-02-21 21:02:58 +0000 | [diff] [blame] | 1861 | operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) |
| 1862 | { |
| 1863 | typedef typename unique_ptr<_T1, _D1>::pointer _P1; |
| 1864 | typedef typename unique_ptr<_T2, _D2>::pointer _P2; |
Eric Fiselier | f8898c8 | 2015-02-05 23:01:40 +0000 | [diff] [blame] | 1865 | typedef typename common_type<_P1, _P2>::type _Vp; |
| 1866 | return less<_Vp>()(__x.get(), __y.get()); |
Howard Hinnant | b17caf9 | 2012-02-21 21:02:58 +0000 | [diff] [blame] | 1867 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1868 | |
| 1869 | template <class _T1, class _D1, class _T2, class _D2> |
| 1870 | inline _LIBCPP_INLINE_VISIBILITY |
| 1871 | bool |
| 1872 | operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;} |
| 1873 | |
| 1874 | template <class _T1, class _D1, class _T2, class _D2> |
| 1875 | inline _LIBCPP_INLINE_VISIBILITY |
| 1876 | bool |
| 1877 | operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);} |
| 1878 | |
| 1879 | template <class _T1, class _D1, class _T2, class _D2> |
| 1880 | inline _LIBCPP_INLINE_VISIBILITY |
| 1881 | bool |
| 1882 | operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);} |
| 1883 | |
Howard Hinnant | b17caf9 | 2012-02-21 21:02:58 +0000 | [diff] [blame] | 1884 | template <class _T1, class _D1> |
| 1885 | inline _LIBCPP_INLINE_VISIBILITY |
| 1886 | bool |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 1887 | operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT |
Howard Hinnant | b17caf9 | 2012-02-21 21:02:58 +0000 | [diff] [blame] | 1888 | { |
| 1889 | return !__x; |
| 1890 | } |
| 1891 | |
| 1892 | template <class _T1, class _D1> |
| 1893 | inline _LIBCPP_INLINE_VISIBILITY |
| 1894 | bool |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 1895 | operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT |
Howard Hinnant | b17caf9 | 2012-02-21 21:02:58 +0000 | [diff] [blame] | 1896 | { |
| 1897 | return !__x; |
| 1898 | } |
| 1899 | |
| 1900 | template <class _T1, class _D1> |
| 1901 | inline _LIBCPP_INLINE_VISIBILITY |
| 1902 | bool |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 1903 | operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT |
Howard Hinnant | b17caf9 | 2012-02-21 21:02:58 +0000 | [diff] [blame] | 1904 | { |
| 1905 | return static_cast<bool>(__x); |
| 1906 | } |
| 1907 | |
| 1908 | template <class _T1, class _D1> |
| 1909 | inline _LIBCPP_INLINE_VISIBILITY |
| 1910 | bool |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 1911 | operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT |
Howard Hinnant | b17caf9 | 2012-02-21 21:02:58 +0000 | [diff] [blame] | 1912 | { |
| 1913 | return static_cast<bool>(__x); |
| 1914 | } |
| 1915 | |
| 1916 | template <class _T1, class _D1> |
| 1917 | inline _LIBCPP_INLINE_VISIBILITY |
| 1918 | bool |
| 1919 | operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t) |
| 1920 | { |
| 1921 | typedef typename unique_ptr<_T1, _D1>::pointer _P1; |
| 1922 | return less<_P1>()(__x.get(), nullptr); |
| 1923 | } |
| 1924 | |
| 1925 | template <class _T1, class _D1> |
| 1926 | inline _LIBCPP_INLINE_VISIBILITY |
| 1927 | bool |
| 1928 | operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x) |
| 1929 | { |
| 1930 | typedef typename unique_ptr<_T1, _D1>::pointer _P1; |
| 1931 | return less<_P1>()(nullptr, __x.get()); |
| 1932 | } |
| 1933 | |
| 1934 | template <class _T1, class _D1> |
| 1935 | inline _LIBCPP_INLINE_VISIBILITY |
| 1936 | bool |
| 1937 | operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t) |
| 1938 | { |
| 1939 | return nullptr < __x; |
| 1940 | } |
| 1941 | |
| 1942 | template <class _T1, class _D1> |
| 1943 | inline _LIBCPP_INLINE_VISIBILITY |
| 1944 | bool |
| 1945 | operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x) |
| 1946 | { |
| 1947 | return __x < nullptr; |
| 1948 | } |
| 1949 | |
| 1950 | template <class _T1, class _D1> |
| 1951 | inline _LIBCPP_INLINE_VISIBILITY |
| 1952 | bool |
| 1953 | operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t) |
| 1954 | { |
| 1955 | return !(nullptr < __x); |
| 1956 | } |
| 1957 | |
| 1958 | template <class _T1, class _D1> |
| 1959 | inline _LIBCPP_INLINE_VISIBILITY |
| 1960 | bool |
| 1961 | operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x) |
| 1962 | { |
| 1963 | return !(__x < nullptr); |
| 1964 | } |
| 1965 | |
| 1966 | template <class _T1, class _D1> |
| 1967 | inline _LIBCPP_INLINE_VISIBILITY |
| 1968 | bool |
| 1969 | operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t) |
| 1970 | { |
| 1971 | return !(__x < nullptr); |
| 1972 | } |
| 1973 | |
| 1974 | template <class _T1, class _D1> |
| 1975 | inline _LIBCPP_INLINE_VISIBILITY |
| 1976 | bool |
| 1977 | operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x) |
| 1978 | { |
| 1979 | return !(nullptr < __x); |
| 1980 | } |
| 1981 | |
Marshall Clow | 9e8f4a9 | 2013-07-01 18:16:03 +0000 | [diff] [blame] | 1982 | #if _LIBCPP_STD_VER > 11 |
| 1983 | |
| 1984 | template<class _Tp> |
| 1985 | struct __unique_if |
| 1986 | { |
| 1987 | typedef unique_ptr<_Tp> __unique_single; |
| 1988 | }; |
| 1989 | |
| 1990 | template<class _Tp> |
| 1991 | struct __unique_if<_Tp[]> |
| 1992 | { |
| 1993 | typedef unique_ptr<_Tp[]> __unique_array_unknown_bound; |
| 1994 | }; |
| 1995 | |
| 1996 | template<class _Tp, size_t _Np> |
| 1997 | struct __unique_if<_Tp[_Np]> |
| 1998 | { |
| 1999 | typedef void __unique_array_known_bound; |
| 2000 | }; |
| 2001 | |
| 2002 | template<class _Tp, class... _Args> |
Marshall Clow | 724e3df | 2013-07-02 20:06:09 +0000 | [diff] [blame] | 2003 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 9e8f4a9 | 2013-07-01 18:16:03 +0000 | [diff] [blame] | 2004 | typename __unique_if<_Tp>::__unique_single |
| 2005 | make_unique(_Args&&... __args) |
| 2006 | { |
| 2007 | return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...)); |
| 2008 | } |
| 2009 | |
| 2010 | template<class _Tp> |
Marshall Clow | 724e3df | 2013-07-02 20:06:09 +0000 | [diff] [blame] | 2011 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 9e8f4a9 | 2013-07-01 18:16:03 +0000 | [diff] [blame] | 2012 | typename __unique_if<_Tp>::__unique_array_unknown_bound |
| 2013 | make_unique(size_t __n) |
| 2014 | { |
| 2015 | typedef typename remove_extent<_Tp>::type _Up; |
| 2016 | return unique_ptr<_Tp>(new _Up[__n]()); |
| 2017 | } |
| 2018 | |
| 2019 | template<class _Tp, class... _Args> |
| 2020 | typename __unique_if<_Tp>::__unique_array_known_bound |
| 2021 | make_unique(_Args&&...) = delete; |
| 2022 | |
| 2023 | #endif // _LIBCPP_STD_VER > 11 |
| 2024 | |
Howard Hinnant | 36b31ae | 2010-06-03 16:42:57 +0000 | [diff] [blame] | 2025 | template <class _Tp, class _Dp> |
Eric Fiselier | 698a97b | 2017-01-21 00:02:12 +0000 | [diff] [blame] | 2026 | #ifdef _LIBCPP_CXX03_LANG |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2027 | struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> > |
Eric Fiselier | 698a97b | 2017-01-21 00:02:12 +0000 | [diff] [blame] | 2028 | #else |
| 2029 | struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper< |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 2030 | unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> > |
Eric Fiselier | 698a97b | 2017-01-21 00:02:12 +0000 | [diff] [blame] | 2031 | #endif |
Howard Hinnant | 36b31ae | 2010-06-03 16:42:57 +0000 | [diff] [blame] | 2032 | { |
| 2033 | typedef unique_ptr<_Tp, _Dp> argument_type; |
| 2034 | typedef size_t result_type; |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2035 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 4903689 | 2017-03-23 02:40:28 +0000 | [diff] [blame] | 2036 | result_type operator()(const argument_type& __ptr) const |
Howard Hinnant | 36b31ae | 2010-06-03 16:42:57 +0000 | [diff] [blame] | 2037 | { |
| 2038 | typedef typename argument_type::pointer pointer; |
| 2039 | return hash<pointer>()(__ptr.get()); |
| 2040 | } |
| 2041 | }; |
| 2042 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2043 | struct __destruct_n |
| 2044 | { |
| 2045 | private: |
Eric Fiselier | c5ea1ae | 2017-06-01 02:29:37 +0000 | [diff] [blame] | 2046 | size_t __size_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2047 | |
| 2048 | template <class _Tp> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2049 | _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT |
Eric Fiselier | c5ea1ae | 2017-06-01 02:29:37 +0000 | [diff] [blame] | 2050 | {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2051 | |
| 2052 | template <class _Tp> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2053 | _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2054 | {} |
| 2055 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2056 | _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT |
Eric Fiselier | c5ea1ae | 2017-06-01 02:29:37 +0000 | [diff] [blame] | 2057 | {++__size_;} |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2058 | _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2059 | {} |
| 2060 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2061 | _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT |
Eric Fiselier | c5ea1ae | 2017-06-01 02:29:37 +0000 | [diff] [blame] | 2062 | {__size_ = __s;} |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2063 | _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2064 | {} |
| 2065 | public: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2066 | _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT |
Eric Fiselier | c5ea1ae | 2017-06-01 02:29:37 +0000 | [diff] [blame] | 2067 | : __size_(__s) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2068 | |
| 2069 | template <class _Tp> |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 2070 | _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT |
Howard Hinnant | a9a897e | 2010-11-19 22:17:28 +0000 | [diff] [blame] | 2071 | {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2072 | |
| 2073 | template <class _Tp> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2074 | _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT |
Howard Hinnant | a9a897e | 2010-11-19 22:17:28 +0000 | [diff] [blame] | 2075 | {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2076 | |
| 2077 | template <class _Tp> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2078 | _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT |
Howard Hinnant | a9a897e | 2010-11-19 22:17:28 +0000 | [diff] [blame] | 2079 | {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2080 | }; |
| 2081 | |
| 2082 | template <class _Alloc> |
| 2083 | class __allocator_destructor |
| 2084 | { |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 2085 | typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2086 | public: |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 2087 | typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer; |
| 2088 | typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2089 | private: |
| 2090 | _Alloc& __alloc_; |
| 2091 | size_type __s_; |
| 2092 | public: |
| 2093 | _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s) |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2094 | _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2095 | : __alloc_(__a), __s_(__s) {} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2096 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2097 | void operator()(pointer __p) _NOEXCEPT |
| 2098 | {__alloc_traits::deallocate(__alloc_, __p, __s_);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2099 | }; |
| 2100 | |
| 2101 | template <class _InputIterator, class _ForwardIterator> |
| 2102 | _ForwardIterator |
| 2103 | uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r) |
| 2104 | { |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2105 | typedef typename iterator_traits<_ForwardIterator>::value_type value_type; |
Howard Hinnant | 2fa038c | 2011-12-29 17:45:35 +0000 | [diff] [blame] | 2106 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2107 | _ForwardIterator __s = __r; |
| 2108 | try |
| 2109 | { |
| 2110 | #endif |
Marshall Clow | 5e9cb8f | 2015-05-19 15:01:48 +0000 | [diff] [blame] | 2111 | for (; __f != __l; ++__f, (void) ++__r) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 2112 | ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f); |
Howard Hinnant | 2fa038c | 2011-12-29 17:45:35 +0000 | [diff] [blame] | 2113 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2114 | } |
| 2115 | catch (...) |
| 2116 | { |
| 2117 | for (; __s != __r; ++__s) |
| 2118 | __s->~value_type(); |
| 2119 | throw; |
| 2120 | } |
| 2121 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2122 | return __r; |
| 2123 | } |
| 2124 | |
| 2125 | template <class _InputIterator, class _Size, class _ForwardIterator> |
| 2126 | _ForwardIterator |
| 2127 | uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r) |
| 2128 | { |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2129 | typedef typename iterator_traits<_ForwardIterator>::value_type value_type; |
Howard Hinnant | 2fa038c | 2011-12-29 17:45:35 +0000 | [diff] [blame] | 2130 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2131 | _ForwardIterator __s = __r; |
| 2132 | try |
| 2133 | { |
| 2134 | #endif |
Marshall Clow | 5e9cb8f | 2015-05-19 15:01:48 +0000 | [diff] [blame] | 2135 | for (; __n > 0; ++__f, (void) ++__r, (void) --__n) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 2136 | ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f); |
Howard Hinnant | 2fa038c | 2011-12-29 17:45:35 +0000 | [diff] [blame] | 2137 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2138 | } |
| 2139 | catch (...) |
| 2140 | { |
| 2141 | for (; __s != __r; ++__s) |
| 2142 | __s->~value_type(); |
| 2143 | throw; |
| 2144 | } |
| 2145 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2146 | return __r; |
| 2147 | } |
| 2148 | |
| 2149 | template <class _ForwardIterator, class _Tp> |
| 2150 | void |
| 2151 | uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x) |
| 2152 | { |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2153 | typedef typename iterator_traits<_ForwardIterator>::value_type value_type; |
Howard Hinnant | 2fa038c | 2011-12-29 17:45:35 +0000 | [diff] [blame] | 2154 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2155 | _ForwardIterator __s = __f; |
| 2156 | try |
| 2157 | { |
| 2158 | #endif |
| 2159 | for (; __f != __l; ++__f) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 2160 | ::new ((void*)_VSTD::addressof(*__f)) value_type(__x); |
Howard Hinnant | 2fa038c | 2011-12-29 17:45:35 +0000 | [diff] [blame] | 2161 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2162 | } |
| 2163 | catch (...) |
| 2164 | { |
| 2165 | for (; __s != __f; ++__s) |
| 2166 | __s->~value_type(); |
| 2167 | throw; |
| 2168 | } |
| 2169 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | template <class _ForwardIterator, class _Size, class _Tp> |
Howard Hinnant | 3c81109 | 2010-11-18 16:13:03 +0000 | [diff] [blame] | 2173 | _ForwardIterator |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2174 | uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x) |
| 2175 | { |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2176 | typedef typename iterator_traits<_ForwardIterator>::value_type value_type; |
Howard Hinnant | 2fa038c | 2011-12-29 17:45:35 +0000 | [diff] [blame] | 2177 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2178 | _ForwardIterator __s = __f; |
| 2179 | try |
| 2180 | { |
| 2181 | #endif |
Marshall Clow | 5e9cb8f | 2015-05-19 15:01:48 +0000 | [diff] [blame] | 2182 | for (; __n > 0; ++__f, (void) --__n) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 2183 | ::new ((void*)_VSTD::addressof(*__f)) value_type(__x); |
Howard Hinnant | 2fa038c | 2011-12-29 17:45:35 +0000 | [diff] [blame] | 2184 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2185 | } |
| 2186 | catch (...) |
| 2187 | { |
| 2188 | for (; __s != __f; ++__s) |
| 2189 | __s->~value_type(); |
| 2190 | throw; |
| 2191 | } |
| 2192 | #endif |
Howard Hinnant | 3c81109 | 2010-11-18 16:13:03 +0000 | [diff] [blame] | 2193 | return __f; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
Eric Fiselier | 383f6cf | 2016-07-24 03:51:39 +0000 | [diff] [blame] | 2196 | #if _LIBCPP_STD_VER > 14 |
| 2197 | |
Eric Fiselier | 383f6cf | 2016-07-24 03:51:39 +0000 | [diff] [blame] | 2198 | template <class _ForwardIterator> |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 2199 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Eric Fiselier | 383f6cf | 2016-07-24 03:51:39 +0000 | [diff] [blame] | 2200 | void destroy(_ForwardIterator __first, _ForwardIterator __last) { |
| 2201 | for (; __first != __last; ++__first) |
| 2202 | _VSTD::destroy_at(_VSTD::addressof(*__first)); |
| 2203 | } |
| 2204 | |
| 2205 | template <class _ForwardIterator, class _Size> |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 2206 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Eric Fiselier | 383f6cf | 2016-07-24 03:51:39 +0000 | [diff] [blame] | 2207 | _ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) { |
| 2208 | for (; __n > 0; (void)++__first, --__n) |
| 2209 | _VSTD::destroy_at(_VSTD::addressof(*__first)); |
| 2210 | return __first; |
| 2211 | } |
| 2212 | |
Eric Fiselier | 290c07c | 2016-10-11 21:13:44 +0000 | [diff] [blame] | 2213 | template <class _ForwardIterator> |
| 2214 | inline _LIBCPP_INLINE_VISIBILITY |
| 2215 | void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) { |
| 2216 | using _Vt = typename iterator_traits<_ForwardIterator>::value_type; |
| 2217 | auto __idx = __first; |
| 2218 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2219 | try { |
| 2220 | #endif |
| 2221 | for (; __idx != __last; ++__idx) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 2222 | ::new ((void*)_VSTD::addressof(*__idx)) _Vt; |
Eric Fiselier | 290c07c | 2016-10-11 21:13:44 +0000 | [diff] [blame] | 2223 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2224 | } catch (...) { |
| 2225 | _VSTD::destroy(__first, __idx); |
| 2226 | throw; |
| 2227 | } |
| 2228 | #endif |
| 2229 | } |
| 2230 | |
| 2231 | template <class _ForwardIterator, class _Size> |
| 2232 | inline _LIBCPP_INLINE_VISIBILITY |
| 2233 | _ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) { |
| 2234 | using _Vt = typename iterator_traits<_ForwardIterator>::value_type; |
| 2235 | auto __idx = __first; |
| 2236 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2237 | try { |
| 2238 | #endif |
| 2239 | for (; __n > 0; (void)++__idx, --__n) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 2240 | ::new ((void*)_VSTD::addressof(*__idx)) _Vt; |
Eric Fiselier | 290c07c | 2016-10-11 21:13:44 +0000 | [diff] [blame] | 2241 | return __idx; |
| 2242 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2243 | } catch (...) { |
| 2244 | _VSTD::destroy(__first, __idx); |
| 2245 | throw; |
| 2246 | } |
| 2247 | #endif |
| 2248 | } |
| 2249 | |
| 2250 | |
| 2251 | template <class _ForwardIterator> |
| 2252 | inline _LIBCPP_INLINE_VISIBILITY |
| 2253 | void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) { |
| 2254 | using _Vt = typename iterator_traits<_ForwardIterator>::value_type; |
| 2255 | auto __idx = __first; |
| 2256 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2257 | try { |
| 2258 | #endif |
| 2259 | for (; __idx != __last; ++__idx) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 2260 | ::new ((void*)_VSTD::addressof(*__idx)) _Vt(); |
Eric Fiselier | 290c07c | 2016-10-11 21:13:44 +0000 | [diff] [blame] | 2261 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2262 | } catch (...) { |
| 2263 | _VSTD::destroy(__first, __idx); |
| 2264 | throw; |
| 2265 | } |
| 2266 | #endif |
| 2267 | } |
| 2268 | |
| 2269 | template <class _ForwardIterator, class _Size> |
| 2270 | inline _LIBCPP_INLINE_VISIBILITY |
| 2271 | _ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) { |
| 2272 | using _Vt = typename iterator_traits<_ForwardIterator>::value_type; |
| 2273 | auto __idx = __first; |
| 2274 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2275 | try { |
| 2276 | #endif |
| 2277 | for (; __n > 0; (void)++__idx, --__n) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 2278 | ::new ((void*)_VSTD::addressof(*__idx)) _Vt(); |
Eric Fiselier | 290c07c | 2016-10-11 21:13:44 +0000 | [diff] [blame] | 2279 | return __idx; |
| 2280 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2281 | } catch (...) { |
| 2282 | _VSTD::destroy(__first, __idx); |
| 2283 | throw; |
| 2284 | } |
| 2285 | #endif |
| 2286 | } |
| 2287 | |
| 2288 | |
| 2289 | template <class _InputIt, class _ForwardIt> |
| 2290 | inline _LIBCPP_INLINE_VISIBILITY |
| 2291 | _ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) { |
| 2292 | using _Vt = typename iterator_traits<_ForwardIt>::value_type; |
| 2293 | auto __idx = __first_res; |
| 2294 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2295 | try { |
| 2296 | #endif |
| 2297 | for (; __first != __last; (void)++__idx, ++__first) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 2298 | ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first)); |
Eric Fiselier | 290c07c | 2016-10-11 21:13:44 +0000 | [diff] [blame] | 2299 | return __idx; |
| 2300 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2301 | } catch (...) { |
| 2302 | _VSTD::destroy(__first_res, __idx); |
| 2303 | throw; |
| 2304 | } |
| 2305 | #endif |
| 2306 | } |
| 2307 | |
| 2308 | template <class _InputIt, class _Size, class _ForwardIt> |
| 2309 | inline _LIBCPP_INLINE_VISIBILITY |
| 2310 | pair<_InputIt, _ForwardIt> |
| 2311 | uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) { |
| 2312 | using _Vt = typename iterator_traits<_ForwardIt>::value_type; |
| 2313 | auto __idx = __first_res; |
| 2314 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2315 | try { |
| 2316 | #endif |
| 2317 | for (; __n > 0; ++__idx, (void)++__first, --__n) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 2318 | ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first)); |
Eric Fiselier | 290c07c | 2016-10-11 21:13:44 +0000 | [diff] [blame] | 2319 | return {__first, __idx}; |
| 2320 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2321 | } catch (...) { |
| 2322 | _VSTD::destroy(__first_res, __idx); |
| 2323 | throw; |
| 2324 | } |
| 2325 | #endif |
| 2326 | } |
| 2327 | |
| 2328 | |
Eric Fiselier | 383f6cf | 2016-07-24 03:51:39 +0000 | [diff] [blame] | 2329 | #endif // _LIBCPP_STD_VER > 14 |
| 2330 | |
Kevin Hu | 4bdc8a0 | 2017-01-17 02:46:33 +0000 | [diff] [blame] | 2331 | // NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively) |
| 2332 | // should be sufficient for thread safety. |
Louis Dionne | 3843740 | 2021-03-03 13:45:06 -0500 | [diff] [blame] | 2333 | // See https://llvm.org/PR22803 |
Kevin Hu | 4bdc8a0 | 2017-01-17 02:46:33 +0000 | [diff] [blame] | 2334 | #if defined(__clang__) && __has_builtin(__atomic_add_fetch) \ |
| 2335 | && defined(__ATOMIC_RELAXED) \ |
| 2336 | && defined(__ATOMIC_ACQ_REL) |
| 2337 | # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT |
Richard Smith | ca47d0f | 2019-04-25 20:02:10 +0000 | [diff] [blame] | 2338 | #elif defined(_LIBCPP_COMPILER_GCC) |
Kevin Hu | 4bdc8a0 | 2017-01-17 02:46:33 +0000 | [diff] [blame] | 2339 | # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT |
| 2340 | #endif |
| 2341 | |
| 2342 | template <class _Tp> |
| 2343 | inline _LIBCPP_INLINE_VISIBILITY _Tp |
| 2344 | __libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT |
| 2345 | { |
| 2346 | #if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS) |
| 2347 | return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED); |
| 2348 | #else |
| 2349 | return __t += 1; |
| 2350 | #endif |
| 2351 | } |
| 2352 | |
| 2353 | template <class _Tp> |
| 2354 | inline _LIBCPP_INLINE_VISIBILITY _Tp |
| 2355 | __libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT |
| 2356 | { |
| 2357 | #if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS) |
| 2358 | return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL); |
| 2359 | #else |
| 2360 | return __t -= 1; |
| 2361 | #endif |
| 2362 | } |
| 2363 | |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2364 | class _LIBCPP_EXCEPTION_ABI bad_weak_ptr |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2365 | : public std::exception |
| 2366 | { |
| 2367 | public: |
Dimitry Andric | 47269ce | 2020-03-13 19:36:26 +0100 | [diff] [blame] | 2368 | bad_weak_ptr() _NOEXCEPT = default; |
| 2369 | bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default; |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2370 | virtual ~bad_weak_ptr() _NOEXCEPT; |
| 2371 | virtual const char* what() const _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2372 | }; |
| 2373 | |
Louis Dionne | 16fe295 | 2018-07-11 23:14:33 +0000 | [diff] [blame] | 2374 | _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 8fea161 | 2016-08-25 15:09:01 +0000 | [diff] [blame] | 2375 | void __throw_bad_weak_ptr() |
| 2376 | { |
| 2377 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2378 | throw bad_weak_ptr(); |
| 2379 | #else |
| 2380 | _VSTD::abort(); |
| 2381 | #endif |
| 2382 | } |
| 2383 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2384 | template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2385 | |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 2386 | class _LIBCPP_TYPE_VIS __shared_count |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2387 | { |
| 2388 | __shared_count(const __shared_count&); |
| 2389 | __shared_count& operator=(const __shared_count&); |
| 2390 | |
| 2391 | protected: |
| 2392 | long __shared_owners_; |
| 2393 | virtual ~__shared_count(); |
| 2394 | private: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2395 | virtual void __on_zero_shared() _NOEXCEPT = 0; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2396 | |
| 2397 | public: |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2398 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2399 | explicit __shared_count(long __refs = 0) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2400 | : __shared_owners_(__refs) {} |
| 2401 | |
Louis Dionne | 5e0eadd | 2018-08-01 02:08:59 +0000 | [diff] [blame] | 2402 | #if defined(_LIBCPP_BUILDING_LIBRARY) && \ |
Eric Fiselier | 9884857 | 2017-01-17 03:16:26 +0000 | [diff] [blame] | 2403 | defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS) |
Eric Fiselier | e0700ff | 2017-01-17 03:05:31 +0000 | [diff] [blame] | 2404 | void __add_shared() _NOEXCEPT; |
| 2405 | bool __release_shared() _NOEXCEPT; |
Kevin Hu | 4bdc8a0 | 2017-01-17 02:46:33 +0000 | [diff] [blame] | 2406 | #else |
| 2407 | _LIBCPP_INLINE_VISIBILITY |
| 2408 | void __add_shared() _NOEXCEPT { |
| 2409 | __libcpp_atomic_refcount_increment(__shared_owners_); |
| 2410 | } |
| 2411 | _LIBCPP_INLINE_VISIBILITY |
| 2412 | bool __release_shared() _NOEXCEPT { |
| 2413 | if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) { |
| 2414 | __on_zero_shared(); |
| 2415 | return true; |
| 2416 | } |
| 2417 | return false; |
| 2418 | } |
| 2419 | #endif |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2420 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 89659d1 | 2015-07-07 00:27:16 +0000 | [diff] [blame] | 2421 | long use_count() const _NOEXCEPT { |
| 2422 | return __libcpp_relaxed_load(&__shared_owners_) + 1; |
| 2423 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2424 | }; |
| 2425 | |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 2426 | class _LIBCPP_TYPE_VIS __shared_weak_count |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2427 | : private __shared_count |
| 2428 | { |
| 2429 | long __shared_weak_owners_; |
| 2430 | |
| 2431 | public: |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2432 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2433 | explicit __shared_weak_count(long __refs = 0) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2434 | : __shared_count(__refs), |
| 2435 | __shared_weak_owners_(__refs) {} |
| 2436 | protected: |
| 2437 | virtual ~__shared_weak_count(); |
| 2438 | |
| 2439 | public: |
Louis Dionne | 5e0eadd | 2018-08-01 02:08:59 +0000 | [diff] [blame] | 2440 | #if defined(_LIBCPP_BUILDING_LIBRARY) && \ |
Eric Fiselier | 9884857 | 2017-01-17 03:16:26 +0000 | [diff] [blame] | 2441 | defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS) |
Eric Fiselier | e0700ff | 2017-01-17 03:05:31 +0000 | [diff] [blame] | 2442 | void __add_shared() _NOEXCEPT; |
| 2443 | void __add_weak() _NOEXCEPT; |
| 2444 | void __release_shared() _NOEXCEPT; |
Kevin Hu | 4bdc8a0 | 2017-01-17 02:46:33 +0000 | [diff] [blame] | 2445 | #else |
| 2446 | _LIBCPP_INLINE_VISIBILITY |
| 2447 | void __add_shared() _NOEXCEPT { |
| 2448 | __shared_count::__add_shared(); |
| 2449 | } |
| 2450 | _LIBCPP_INLINE_VISIBILITY |
| 2451 | void __add_weak() _NOEXCEPT { |
| 2452 | __libcpp_atomic_refcount_increment(__shared_weak_owners_); |
| 2453 | } |
| 2454 | _LIBCPP_INLINE_VISIBILITY |
| 2455 | void __release_shared() _NOEXCEPT { |
| 2456 | if (__shared_count::__release_shared()) |
| 2457 | __release_weak(); |
| 2458 | } |
| 2459 | #endif |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2460 | void __release_weak() _NOEXCEPT; |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2461 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2462 | long use_count() const _NOEXCEPT {return __shared_count::use_count();} |
| 2463 | __shared_weak_count* lock() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2464 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2465 | virtual const void* __get_deleter(const type_info&) const _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2466 | private: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2467 | virtual void __on_zero_shared_weak() _NOEXCEPT = 0; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2468 | }; |
| 2469 | |
| 2470 | template <class _Tp, class _Dp, class _Alloc> |
| 2471 | class __shared_ptr_pointer |
| 2472 | : public __shared_weak_count |
| 2473 | { |
| 2474 | __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_; |
| 2475 | public: |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2476 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2477 | __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2478 | : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2479 | |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 2480 | #ifndef _LIBCPP_NO_RTTI |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2481 | virtual const void* __get_deleter(const type_info&) const _NOEXCEPT; |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 2482 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2483 | |
| 2484 | private: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2485 | virtual void __on_zero_shared() _NOEXCEPT; |
| 2486 | virtual void __on_zero_shared_weak() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2487 | }; |
| 2488 | |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 2489 | #ifndef _LIBCPP_NO_RTTI |
| 2490 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2491 | template <class _Tp, class _Dp, class _Alloc> |
| 2492 | const void* |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2493 | __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2494 | { |
Eric Fiselier | c7490d0 | 2017-05-04 01:06:56 +0000 | [diff] [blame] | 2495 | return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2496 | } |
| 2497 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2498 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 2499 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2500 | template <class _Tp, class _Dp, class _Alloc> |
| 2501 | void |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2502 | __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2503 | { |
| 2504 | __data_.first().second()(__data_.first().first()); |
| 2505 | __data_.first().second().~_Dp(); |
| 2506 | } |
| 2507 | |
| 2508 | template <class _Tp, class _Dp, class _Alloc> |
| 2509 | void |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2510 | __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2511 | { |
Eric Fiselier | f8898c8 | 2015-02-05 23:01:40 +0000 | [diff] [blame] | 2512 | typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al; |
| 2513 | typedef allocator_traits<_Al> _ATraits; |
Eric Fiselier | 6bd814f | 2014-10-23 04:12:28 +0000 | [diff] [blame] | 2514 | typedef pointer_traits<typename _ATraits::pointer> _PTraits; |
| 2515 | |
Eric Fiselier | f8898c8 | 2015-02-05 23:01:40 +0000 | [diff] [blame] | 2516 | _Al __a(__data_.second()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2517 | __data_.second().~_Alloc(); |
Eric Fiselier | 6bd814f | 2014-10-23 04:12:28 +0000 | [diff] [blame] | 2518 | __a.deallocate(_PTraits::pointer_to(*this), 1); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2519 | } |
| 2520 | |
| 2521 | template <class _Tp, class _Alloc> |
Louis Dionne | 86549d7 | 2020-12-09 16:22:17 -0500 | [diff] [blame] | 2522 | struct __shared_ptr_emplace |
| 2523 | : __shared_weak_count |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2524 | { |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 2525 | template<class ..._Args> |
Louis Dionne | 86549d7 | 2020-12-09 16:22:17 -0500 | [diff] [blame] | 2526 | _LIBCPP_HIDE_FROM_ABI |
| 2527 | explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args) |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 2528 | : __storage_(_VSTD::move(__a)) |
| 2529 | { |
| 2530 | #if _LIBCPP_STD_VER > 17 |
| 2531 | using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type; |
| 2532 | _TpAlloc __tmp(*__get_alloc()); |
| 2533 | allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...); |
Louis Dionne | 1a1fc9d | 2020-07-30 10:00:53 -0400 | [diff] [blame] | 2534 | #else |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 2535 | ::new ((void*)__get_elem()) _Tp(_VSTD::forward<_Args>(__args)...); |
Louis Dionne | 1a1fc9d | 2020-07-30 10:00:53 -0400 | [diff] [blame] | 2536 | #endif |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 2537 | } |
Louis Dionne | 86549d7 | 2020-12-09 16:22:17 -0500 | [diff] [blame] | 2538 | |
| 2539 | _LIBCPP_HIDE_FROM_ABI |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 2540 | _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); } |
Louis Dionne | 86549d7 | 2020-12-09 16:22:17 -0500 | [diff] [blame] | 2541 | |
| 2542 | _LIBCPP_HIDE_FROM_ABI |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 2543 | _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2544 | |
| 2545 | private: |
Louis Dionne | 86549d7 | 2020-12-09 16:22:17 -0500 | [diff] [blame] | 2546 | virtual void __on_zero_shared() _NOEXCEPT { |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 2547 | #if _LIBCPP_STD_VER > 17 |
| 2548 | using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type; |
| 2549 | _TpAlloc __tmp(*__get_alloc()); |
| 2550 | allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem()); |
| 2551 | #else |
Louis Dionne | 86549d7 | 2020-12-09 16:22:17 -0500 | [diff] [blame] | 2552 | __get_elem()->~_Tp(); |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 2553 | #endif |
Louis Dionne | 86549d7 | 2020-12-09 16:22:17 -0500 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | virtual void __on_zero_shared_weak() _NOEXCEPT { |
| 2557 | using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type; |
| 2558 | using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer; |
Louis Dionne | ca117a2 | 2020-12-14 17:40:56 -0500 | [diff] [blame] | 2559 | _ControlBlockAlloc __tmp(*__get_alloc()); |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 2560 | __storage_.~_Storage(); |
Louis Dionne | 86549d7 | 2020-12-09 16:22:17 -0500 | [diff] [blame] | 2561 | allocator_traits<_ControlBlockAlloc>::deallocate(__tmp, |
| 2562 | pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1); |
| 2563 | } |
| 2564 | |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 2565 | // This class implements the control block for non-array shared pointers created |
| 2566 | // through `std::allocate_shared` and `std::make_shared`. |
| 2567 | // |
| 2568 | // In previous versions of the library, we used a compressed pair to store |
| 2569 | // both the _Alloc and the _Tp. This implies using EBO, which is incompatible |
| 2570 | // with Allocator construction for _Tp. To allow implementing P0674 in C++20, |
| 2571 | // we now use a properly aligned char buffer while making sure that we maintain |
| 2572 | // the same layout that we had when we used a compressed pair. |
| 2573 | using _CompressedPair = __compressed_pair<_Alloc, _Tp>; |
| 2574 | struct _ALIGNAS_TYPE(_CompressedPair) _Storage { |
| 2575 | char __blob_[sizeof(_CompressedPair)]; |
| 2576 | |
| 2577 | _LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) { |
| 2578 | ::new ((void*)__get_alloc()) _Alloc(_VSTD::move(__a)); |
| 2579 | } |
| 2580 | _LIBCPP_HIDE_FROM_ABI ~_Storage() { |
| 2581 | __get_alloc()->~_Alloc(); |
| 2582 | } |
| 2583 | _Alloc* __get_alloc() _NOEXCEPT { |
| 2584 | _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_); |
| 2585 | typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair); |
| 2586 | _Alloc *__alloc = reinterpret_cast<_Alloc*>(__first); |
| 2587 | return __alloc; |
| 2588 | } |
Reid Kleckner | a43e87e | 2021-02-01 15:18:42 -0800 | [diff] [blame] | 2589 | _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT { |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 2590 | _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_); |
| 2591 | typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair); |
| 2592 | _Tp *__elem = reinterpret_cast<_Tp*>(__second); |
| 2593 | return __elem; |
| 2594 | } |
| 2595 | }; |
| 2596 | |
| 2597 | static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), ""); |
| 2598 | static_assert(sizeof(_Storage) == sizeof(_CompressedPair), ""); |
| 2599 | _Storage __storage_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2600 | }; |
| 2601 | |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 2602 | struct __shared_ptr_dummy_rebind_allocator_type; |
| 2603 | template <> |
| 2604 | class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type> |
| 2605 | { |
| 2606 | public: |
| 2607 | template <class _Other> |
| 2608 | struct rebind |
| 2609 | { |
| 2610 | typedef allocator<_Other> other; |
| 2611 | }; |
| 2612 | }; |
| 2613 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2614 | template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2615 | |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2616 | template<class _Tp, class _Up> |
| 2617 | struct __compatible_with |
| 2618 | #if _LIBCPP_STD_VER > 14 |
| 2619 | : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {}; |
| 2620 | #else |
| 2621 | : is_convertible<_Tp*, _Up*> {}; |
| 2622 | #endif // _LIBCPP_STD_VER > 14 |
| 2623 | |
zoecarver | 9bc3910 | 2021-02-19 11:10:36 -0800 | [diff] [blame] | 2624 | template <class _Dp, class _Pt, |
| 2625 | class = decltype(_VSTD::declval<_Dp>()(_VSTD::declval<_Pt>()))> |
| 2626 | static true_type __well_formed_deleter_test(int); |
| 2627 | |
| 2628 | template <class, class> |
| 2629 | static false_type __well_formed_deleter_test(...); |
| 2630 | |
| 2631 | template <class _Dp, class _Pt> |
| 2632 | struct __well_formed_deleter : decltype(__well_formed_deleter_test<_Dp, _Pt>(0)) {}; |
| 2633 | |
| 2634 | template<class _Dp, class _Tp, class _Yp> |
| 2635 | struct __shared_ptr_deleter_ctor_reqs |
| 2636 | { |
| 2637 | static const bool value = __compatible_with<_Tp, _Yp>::value && |
| 2638 | is_move_constructible<_Dp>::value && |
| 2639 | __well_formed_deleter<_Dp, _Tp*>::value; |
| 2640 | }; |
| 2641 | |
Vy Nguyen | e369bd9 | 2020-07-13 12:34:37 -0400 | [diff] [blame] | 2642 | #if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI) |
| 2643 | # define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi)) |
| 2644 | #else |
| 2645 | # define _LIBCPP_SHARED_PTR_TRIVIAL_ABI |
| 2646 | #endif |
| 2647 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2648 | template<class _Tp> |
Vy Nguyen | e369bd9 | 2020-07-13 12:34:37 -0400 | [diff] [blame] | 2649 | class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2650 | { |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2651 | public: |
Eric Fiselier | ae5b667 | 2016-06-27 01:02:43 +0000 | [diff] [blame] | 2652 | #if _LIBCPP_STD_VER > 14 |
| 2653 | typedef weak_ptr<_Tp> weak_type; |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2654 | typedef remove_extent_t<_Tp> element_type; |
| 2655 | #else |
| 2656 | typedef _Tp element_type; |
Eric Fiselier | ae5b667 | 2016-06-27 01:02:43 +0000 | [diff] [blame] | 2657 | #endif |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2658 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2659 | private: |
| 2660 | element_type* __ptr_; |
| 2661 | __shared_weak_count* __cntrl_; |
| 2662 | |
| 2663 | struct __nat {int __for_bool_;}; |
| 2664 | public: |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2665 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 2666 | _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2667 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 2668 | _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT; |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2669 | template<class _Yp> |
| 2670 | explicit shared_ptr(_Yp* __p, |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2671 | typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat()); |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2672 | template<class _Yp, class _Dp> |
| 2673 | shared_ptr(_Yp* __p, _Dp __d, |
zoecarver | 9bc3910 | 2021-02-19 11:10:36 -0800 | [diff] [blame] | 2674 | typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat()); |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2675 | template<class _Yp, class _Dp, class _Alloc> |
| 2676 | shared_ptr(_Yp* __p, _Dp __d, _Alloc __a, |
zoecarver | 9bc3910 | 2021-02-19 11:10:36 -0800 | [diff] [blame] | 2677 | typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type = __nat()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2678 | template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d); |
| 2679 | template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a); |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2680 | template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT; |
| 2681 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2682 | shared_ptr(const shared_ptr& __r) _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2683 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2684 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2685 | shared_ptr(const shared_ptr<_Yp>& __r, |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2686 | typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat()) |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2687 | _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2688 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2689 | shared_ptr(shared_ptr&& __r) _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2690 | template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r, |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2691 | typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat()) |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2692 | _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2693 | template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r, |
Marshall Clow | 7e384b7 | 2017-01-10 16:59:33 +0000 | [diff] [blame] | 2694 | typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat()); |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 2695 | #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2696 | template<class _Yp> |
| 2697 | shared_ptr(auto_ptr<_Yp>&& __r, |
| 2698 | typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat()); |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 2699 | #endif |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2700 | template <class _Yp, class _Dp> |
| 2701 | shared_ptr(unique_ptr<_Yp, _Dp>&&, |
| 2702 | typename enable_if |
| 2703 | < |
| 2704 | !is_lvalue_reference<_Dp>::value && |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2705 | is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value, |
| 2706 | __nat |
| 2707 | >::type = __nat()); |
| 2708 | template <class _Yp, class _Dp> |
| 2709 | shared_ptr(unique_ptr<_Yp, _Dp>&&, |
| 2710 | typename enable_if |
| 2711 | < |
| 2712 | is_lvalue_reference<_Dp>::value && |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2713 | is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value, |
| 2714 | __nat |
| 2715 | >::type = __nat()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2716 | |
| 2717 | ~shared_ptr(); |
| 2718 | |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2719 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2720 | shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT; |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2721 | template<class _Yp> |
| 2722 | typename enable_if |
| 2723 | < |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2724 | __compatible_with<_Yp, element_type>::value, |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2725 | shared_ptr& |
| 2726 | >::type |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2727 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2728 | operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2729 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2730 | shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT; |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2731 | template<class _Yp> |
| 2732 | typename enable_if |
| 2733 | < |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2734 | __compatible_with<_Yp, element_type>::value, |
| 2735 | shared_ptr& |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2736 | >::type |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2737 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2738 | operator=(shared_ptr<_Yp>&& __r); |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 2739 | #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2740 | template<class _Yp> |
Eric Fiselier | 6585c75 | 2016-04-21 22:54:21 +0000 | [diff] [blame] | 2741 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2742 | typename enable_if |
| 2743 | < |
| 2744 | !is_array<_Yp>::value && |
| 2745 | is_convertible<_Yp*, element_type*>::value, |
Howard Hinnant | 25bcaf6 | 2013-09-13 23:56:00 +0000 | [diff] [blame] | 2746 | shared_ptr |
| 2747 | >::type& |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2748 | operator=(auto_ptr<_Yp>&& __r); |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 2749 | #endif |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2750 | template <class _Yp, class _Dp> |
| 2751 | typename enable_if |
| 2752 | < |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2753 | is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value, |
| 2754 | shared_ptr& |
| 2755 | >::type |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2756 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2757 | operator=(unique_ptr<_Yp, _Dp>&& __r); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2758 | |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2759 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2760 | void swap(shared_ptr& __r) _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2761 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2762 | void reset() _NOEXCEPT; |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2763 | template<class _Yp> |
| 2764 | typename enable_if |
| 2765 | < |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2766 | __compatible_with<_Yp, element_type>::value, |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2767 | void |
| 2768 | >::type |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2769 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2770 | reset(_Yp* __p); |
| 2771 | template<class _Yp, class _Dp> |
| 2772 | typename enable_if |
| 2773 | < |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2774 | __compatible_with<_Yp, element_type>::value, |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2775 | void |
| 2776 | >::type |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2777 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2778 | reset(_Yp* __p, _Dp __d); |
| 2779 | template<class _Yp, class _Dp, class _Alloc> |
| 2780 | typename enable_if |
| 2781 | < |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2782 | __compatible_with<_Yp, element_type>::value, |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2783 | void |
| 2784 | >::type |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2785 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2786 | reset(_Yp* __p, _Dp __d, _Alloc __a); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2787 | |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2788 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2789 | element_type* get() const _NOEXCEPT {return __ptr_;} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2790 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2791 | typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT |
| 2792 | {return *__ptr_;} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2793 | _LIBCPP_INLINE_VISIBILITY |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2794 | element_type* operator->() const _NOEXCEPT |
| 2795 | { |
| 2796 | static_assert(!_VSTD::is_array<_Tp>::value, |
| 2797 | "std::shared_ptr<T>::operator-> is only valid when T is not an array type."); |
| 2798 | return __ptr_; |
| 2799 | } |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2800 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2801 | long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2802 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2803 | bool unique() const _NOEXCEPT {return use_count() == 1;} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2804 | _LIBCPP_INLINE_VISIBILITY |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 2805 | _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;} |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2806 | template <class _Up> |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2807 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 2808 | bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2809 | {return __cntrl_ < __p.__cntrl_;} |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2810 | template <class _Up> |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2811 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 2812 | bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2813 | {return __cntrl_ < __p.__cntrl_;} |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 2814 | _LIBCPP_INLINE_VISIBILITY |
| 2815 | bool |
| 2816 | __owner_equivalent(const shared_ptr& __p) const |
| 2817 | {return __cntrl_ == __p.__cntrl_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2818 | |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2819 | #if _LIBCPP_STD_VER > 14 |
| 2820 | typename add_lvalue_reference<element_type>::type |
| 2821 | _LIBCPP_INLINE_VISIBILITY |
| 2822 | operator[](ptrdiff_t __i) const |
| 2823 | { |
| 2824 | static_assert(_VSTD::is_array<_Tp>::value, |
| 2825 | "std::shared_ptr<T>::operator[] is only valid when T is an array type."); |
| 2826 | return __ptr_[__i]; |
| 2827 | } |
| 2828 | #endif |
| 2829 | |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 2830 | #ifndef _LIBCPP_NO_RTTI |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2831 | template <class _Dp> |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2832 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2833 | _Dp* __get_deleter() const _NOEXCEPT |
Marshall Clow | 31350ab | 2017-06-14 16:54:43 +0000 | [diff] [blame] | 2834 | {return static_cast<_Dp*>(__cntrl_ |
Aditya Kumar | 7c5db69 | 2017-08-20 10:38:55 +0000 | [diff] [blame] | 2835 | ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp))) |
Marshall Clow | 31350ab | 2017-06-14 16:54:43 +0000 | [diff] [blame] | 2836 | : nullptr);} |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2837 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2838 | |
Zoe Carver | d9040c7 | 2019-10-22 15:16:49 +0000 | [diff] [blame] | 2839 | template<class _Yp, class _CntrlBlk> |
| 2840 | static shared_ptr<_Tp> |
zoecarver | 867d311 | 2020-05-19 17:17:16 -0700 | [diff] [blame] | 2841 | __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT |
Zoe Carver | d9040c7 | 2019-10-22 15:16:49 +0000 | [diff] [blame] | 2842 | { |
| 2843 | shared_ptr<_Tp> __r; |
| 2844 | __r.__ptr_ = __p; |
| 2845 | __r.__cntrl_ = __cntrl; |
| 2846 | __r.__enable_weak_this(__r.__ptr_, __r.__ptr_); |
| 2847 | return __r; |
| 2848 | } |
Zoe Carver | 6cd05c3 | 2019-08-19 15:47:16 +0000 | [diff] [blame] | 2849 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2850 | private: |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 2851 | template <class _Yp, bool = is_function<_Yp>::value> |
| 2852 | struct __shared_ptr_default_allocator |
| 2853 | { |
| 2854 | typedef allocator<_Yp> type; |
| 2855 | }; |
| 2856 | |
| 2857 | template <class _Yp> |
| 2858 | struct __shared_ptr_default_allocator<_Yp, true> |
| 2859 | { |
| 2860 | typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type; |
| 2861 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2862 | |
Eric Fiselier | f16c93f | 2016-06-26 23:56:32 +0000 | [diff] [blame] | 2863 | template <class _Yp, class _OrigPtr> |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2864 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 30d5ac6 | 2017-05-10 19:35:49 +0000 | [diff] [blame] | 2865 | typename enable_if<is_convertible<_OrigPtr*, |
| 2866 | const enable_shared_from_this<_Yp>* |
| 2867 | >::value, |
| 2868 | void>::type |
Eric Fiselier | f16c93f | 2016-06-26 23:56:32 +0000 | [diff] [blame] | 2869 | __enable_weak_this(const enable_shared_from_this<_Yp>* __e, |
| 2870 | _OrigPtr* __ptr) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2871 | { |
Eric Fiselier | f16c93f | 2016-06-26 23:56:32 +0000 | [diff] [blame] | 2872 | typedef typename remove_cv<_Yp>::type _RawYp; |
Eric Fiselier | 8400686 | 2016-06-02 00:15:35 +0000 | [diff] [blame] | 2873 | if (__e && __e->__weak_this_.expired()) |
Marshall Clow | 99442fc | 2015-06-19 15:54:13 +0000 | [diff] [blame] | 2874 | { |
Eric Fiselier | f16c93f | 2016-06-26 23:56:32 +0000 | [diff] [blame] | 2875 | __e->__weak_this_ = shared_ptr<_RawYp>(*this, |
| 2876 | const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr))); |
Marshall Clow | 99442fc | 2015-06-19 15:54:13 +0000 | [diff] [blame] | 2877 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2878 | } |
| 2879 | |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 2880 | _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2881 | |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2882 | template <class, class _Yp> |
| 2883 | struct __shared_ptr_default_delete |
| 2884 | : default_delete<_Yp> {}; |
| 2885 | |
| 2886 | template <class _Yp, class _Un, size_t _Sz> |
| 2887 | struct __shared_ptr_default_delete<_Yp[_Sz], _Un> |
| 2888 | : default_delete<_Yp[]> {}; |
| 2889 | |
| 2890 | template <class _Yp, class _Un> |
| 2891 | struct __shared_ptr_default_delete<_Yp[], _Un> |
| 2892 | : default_delete<_Yp[]> {}; |
| 2893 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2894 | template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr; |
| 2895 | template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2896 | }; |
| 2897 | |
Logan Smith | a5e4d7e | 2020-05-07 12:07:01 -0400 | [diff] [blame] | 2898 | #ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES |
| 2899 | template<class _Tp> |
| 2900 | shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>; |
| 2901 | template<class _Tp, class _Dp> |
| 2902 | shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>; |
| 2903 | #endif |
Eric Fiselier | 30d5ac6 | 2017-05-10 19:35:49 +0000 | [diff] [blame] | 2904 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2905 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2906 | inline |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 2907 | _LIBCPP_CONSTEXPR |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2908 | shared_ptr<_Tp>::shared_ptr() _NOEXCEPT |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 2909 | : __ptr_(nullptr), |
| 2910 | __cntrl_(nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2911 | { |
| 2912 | } |
| 2913 | |
| 2914 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2915 | inline |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 2916 | _LIBCPP_CONSTEXPR |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2917 | shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 2918 | : __ptr_(nullptr), |
| 2919 | __cntrl_(nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2920 | { |
| 2921 | } |
| 2922 | |
| 2923 | template<class _Tp> |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2924 | template<class _Yp> |
| 2925 | shared_ptr<_Tp>::shared_ptr(_Yp* __p, |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2926 | typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2927 | : __ptr_(__p) |
| 2928 | { |
| 2929 | unique_ptr<_Yp> __hold(__p); |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 2930 | typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2931 | typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk; |
| 2932 | __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2933 | __hold.release(); |
Eric Fiselier | f16c93f | 2016-06-26 23:56:32 +0000 | [diff] [blame] | 2934 | __enable_weak_this(__p, __p); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
| 2937 | template<class _Tp> |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2938 | template<class _Yp, class _Dp> |
| 2939 | shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, |
zoecarver | 9bc3910 | 2021-02-19 11:10:36 -0800 | [diff] [blame] | 2940 | typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2941 | : __ptr_(__p) |
| 2942 | { |
| 2943 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2944 | try |
| 2945 | { |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2946 | #endif // _LIBCPP_NO_EXCEPTIONS |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 2947 | typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; |
| 2948 | typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk; |
zoecarver | bec93cf | 2021-02-18 21:31:07 -0800 | [diff] [blame] | 2949 | #ifndef _LIBCPP_CXX03_LANG |
| 2950 | __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT()); |
| 2951 | #else |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 2952 | __cntrl_ = new _CntrlBlk(__p, __d, _AllocT()); |
zoecarver | bec93cf | 2021-02-18 21:31:07 -0800 | [diff] [blame] | 2953 | #endif // not _LIBCPP_CXX03_LANG |
Eric Fiselier | f16c93f | 2016-06-26 23:56:32 +0000 | [diff] [blame] | 2954 | __enable_weak_this(__p, __p); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2955 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2956 | } |
| 2957 | catch (...) |
| 2958 | { |
| 2959 | __d(__p); |
| 2960 | throw; |
| 2961 | } |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2962 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
| 2965 | template<class _Tp> |
| 2966 | template<class _Dp> |
| 2967 | shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d) |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 2968 | : __ptr_(nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2969 | { |
| 2970 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2971 | try |
| 2972 | { |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2973 | #endif // _LIBCPP_NO_EXCEPTIONS |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 2974 | typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT; |
| 2975 | typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk; |
zoecarver | bec93cf | 2021-02-18 21:31:07 -0800 | [diff] [blame] | 2976 | #ifndef _LIBCPP_CXX03_LANG |
| 2977 | __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT()); |
| 2978 | #else |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 2979 | __cntrl_ = new _CntrlBlk(__p, __d, _AllocT()); |
zoecarver | bec93cf | 2021-02-18 21:31:07 -0800 | [diff] [blame] | 2980 | #endif // not _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2981 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2982 | } |
| 2983 | catch (...) |
| 2984 | { |
| 2985 | __d(__p); |
| 2986 | throw; |
| 2987 | } |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2988 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2989 | } |
| 2990 | |
| 2991 | template<class _Tp> |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2992 | template<class _Yp, class _Dp, class _Alloc> |
| 2993 | shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a, |
zoecarver | 9bc3910 | 2021-02-19 11:10:36 -0800 | [diff] [blame] | 2994 | typename enable_if<__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, element_type>::value, __nat>::type) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2995 | : __ptr_(__p) |
| 2996 | { |
| 2997 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2998 | try |
| 2999 | { |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3000 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3001 | typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk; |
Eric Fiselier | 6bd814f | 2014-10-23 04:12:28 +0000 | [diff] [blame] | 3002 | typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3003 | typedef __allocator_destructor<_A2> _D2; |
| 3004 | _A2 __a2(__a); |
| 3005 | unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1)); |
zoecarver | bec93cf | 2021-02-18 21:31:07 -0800 | [diff] [blame] | 3006 | ::new ((void*)_VSTD::addressof(*__hold2.get())) |
| 3007 | #ifndef _LIBCPP_CXX03_LANG |
| 3008 | _CntrlBlk(__p, _VSTD::move(__d), __a); |
| 3009 | #else |
| 3010 | _CntrlBlk(__p, __d, __a); |
| 3011 | #endif // not _LIBCPP_CXX03_LANG |
Eric Fiselier | 6bd814f | 2014-10-23 04:12:28 +0000 | [diff] [blame] | 3012 | __cntrl_ = _VSTD::addressof(*__hold2.release()); |
Eric Fiselier | f16c93f | 2016-06-26 23:56:32 +0000 | [diff] [blame] | 3013 | __enable_weak_this(__p, __p); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3014 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 3015 | } |
| 3016 | catch (...) |
| 3017 | { |
| 3018 | __d(__p); |
| 3019 | throw; |
| 3020 | } |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3021 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
| 3024 | template<class _Tp> |
| 3025 | template<class _Dp, class _Alloc> |
| 3026 | shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a) |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 3027 | : __ptr_(nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3028 | { |
| 3029 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 3030 | try |
| 3031 | { |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3032 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3033 | typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk; |
Eric Fiselier | 6bd814f | 2014-10-23 04:12:28 +0000 | [diff] [blame] | 3034 | typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3035 | typedef __allocator_destructor<_A2> _D2; |
| 3036 | _A2 __a2(__a); |
| 3037 | unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1)); |
zoecarver | bec93cf | 2021-02-18 21:31:07 -0800 | [diff] [blame] | 3038 | ::new ((void*)_VSTD::addressof(*__hold2.get())) |
| 3039 | #ifndef _LIBCPP_CXX03_LANG |
| 3040 | _CntrlBlk(__p, _VSTD::move(__d), __a); |
| 3041 | #else |
| 3042 | _CntrlBlk(__p, __d, __a); |
| 3043 | #endif // not _LIBCPP_CXX03_LANG |
Eric Fiselier | 6bd814f | 2014-10-23 04:12:28 +0000 | [diff] [blame] | 3044 | __cntrl_ = _VSTD::addressof(*__hold2.release()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3045 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 3046 | } |
| 3047 | catch (...) |
| 3048 | { |
| 3049 | __d(__p); |
| 3050 | throw; |
| 3051 | } |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3052 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3053 | } |
| 3054 | |
| 3055 | template<class _Tp> |
| 3056 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3057 | inline |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3058 | shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3059 | : __ptr_(__p), |
| 3060 | __cntrl_(__r.__cntrl_) |
| 3061 | { |
| 3062 | if (__cntrl_) |
| 3063 | __cntrl_->__add_shared(); |
| 3064 | } |
| 3065 | |
| 3066 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3067 | inline |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3068 | shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3069 | : __ptr_(__r.__ptr_), |
| 3070 | __cntrl_(__r.__cntrl_) |
| 3071 | { |
| 3072 | if (__cntrl_) |
| 3073 | __cntrl_->__add_shared(); |
| 3074 | } |
| 3075 | |
| 3076 | template<class _Tp> |
| 3077 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3078 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3079 | shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3080 | typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type) |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3081 | _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3082 | : __ptr_(__r.__ptr_), |
| 3083 | __cntrl_(__r.__cntrl_) |
| 3084 | { |
| 3085 | if (__cntrl_) |
| 3086 | __cntrl_->__add_shared(); |
| 3087 | } |
| 3088 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3089 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3090 | inline |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3091 | shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3092 | : __ptr_(__r.__ptr_), |
| 3093 | __cntrl_(__r.__cntrl_) |
| 3094 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 3095 | __r.__ptr_ = nullptr; |
| 3096 | __r.__cntrl_ = nullptr; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
| 3099 | template<class _Tp> |
| 3100 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3101 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3102 | shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r, |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3103 | typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type) |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3104 | _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3105 | : __ptr_(__r.__ptr_), |
| 3106 | __cntrl_(__r.__cntrl_) |
| 3107 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 3108 | __r.__ptr_ = nullptr; |
| 3109 | __r.__cntrl_ = nullptr; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3110 | } |
| 3111 | |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 3112 | #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3113 | template<class _Tp> |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 3114 | template<class _Yp> |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 3115 | shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r, |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 3116 | typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3117 | : __ptr_(__r.get()) |
| 3118 | { |
| 3119 | typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk; |
| 3120 | __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>()); |
Eric Fiselier | f16c93f | 2016-06-26 23:56:32 +0000 | [diff] [blame] | 3121 | __enable_weak_this(__r.get(), __r.get()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3122 | __r.release(); |
| 3123 | } |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 3124 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3125 | |
| 3126 | template<class _Tp> |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 3127 | template <class _Yp, class _Dp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3128 | shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r, |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 3129 | typename enable_if |
| 3130 | < |
| 3131 | !is_lvalue_reference<_Dp>::value && |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 3132 | is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value, |
| 3133 | __nat |
| 3134 | >::type) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3135 | : __ptr_(__r.get()) |
| 3136 | { |
Marshall Clow | 35cde74 | 2015-05-10 13:59:45 +0000 | [diff] [blame] | 3137 | #if _LIBCPP_STD_VER > 11 |
| 3138 | if (__ptr_ == nullptr) |
| 3139 | __cntrl_ = nullptr; |
| 3140 | else |
| 3141 | #endif |
| 3142 | { |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 3143 | typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; |
zoecarver | caf89ec | 2021-04-08 22:01:35 -0700 | [diff] [blame^] | 3144 | typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Dp, _AllocT > _CntrlBlk; |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 3145 | __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT()); |
Eric Fiselier | f16c93f | 2016-06-26 23:56:32 +0000 | [diff] [blame] | 3146 | __enable_weak_this(__r.get(), __r.get()); |
Marshall Clow | 35cde74 | 2015-05-10 13:59:45 +0000 | [diff] [blame] | 3147 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3148 | __r.release(); |
| 3149 | } |
| 3150 | |
| 3151 | template<class _Tp> |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 3152 | template <class _Yp, class _Dp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3153 | shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r, |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 3154 | typename enable_if |
| 3155 | < |
| 3156 | is_lvalue_reference<_Dp>::value && |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 3157 | is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value, |
| 3158 | __nat |
| 3159 | >::type) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3160 | : __ptr_(__r.get()) |
| 3161 | { |
Marshall Clow | 35cde74 | 2015-05-10 13:59:45 +0000 | [diff] [blame] | 3162 | #if _LIBCPP_STD_VER > 11 |
| 3163 | if (__ptr_ == nullptr) |
| 3164 | __cntrl_ = nullptr; |
| 3165 | else |
| 3166 | #endif |
| 3167 | { |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 3168 | typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; |
zoecarver | caf89ec | 2021-04-08 22:01:35 -0700 | [diff] [blame^] | 3169 | typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, |
Marshall Clow | 35cde74 | 2015-05-10 13:59:45 +0000 | [diff] [blame] | 3170 | reference_wrapper<typename remove_reference<_Dp>::type>, |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 3171 | _AllocT > _CntrlBlk; |
Logan Smith | 85cb081 | 2020-02-20 12:23:36 -0500 | [diff] [blame] | 3172 | __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT()); |
Eric Fiselier | f16c93f | 2016-06-26 23:56:32 +0000 | [diff] [blame] | 3173 | __enable_weak_this(__r.get(), __r.get()); |
Marshall Clow | 35cde74 | 2015-05-10 13:59:45 +0000 | [diff] [blame] | 3174 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3175 | __r.release(); |
| 3176 | } |
| 3177 | |
Zoe Carver | 6cd05c3 | 2019-08-19 15:47:16 +0000 | [diff] [blame] | 3178 | template<class _Tp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3179 | shared_ptr<_Tp>::~shared_ptr() |
| 3180 | { |
| 3181 | if (__cntrl_) |
| 3182 | __cntrl_->__release_shared(); |
| 3183 | } |
| 3184 | |
| 3185 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3186 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3187 | shared_ptr<_Tp>& |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3188 | shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3189 | { |
| 3190 | shared_ptr(__r).swap(*this); |
| 3191 | return *this; |
| 3192 | } |
| 3193 | |
| 3194 | template<class _Tp> |
| 3195 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3196 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3197 | typename enable_if |
| 3198 | < |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3199 | __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value, |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3200 | shared_ptr<_Tp>& |
| 3201 | >::type |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3202 | shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3203 | { |
| 3204 | shared_ptr(__r).swap(*this); |
| 3205 | return *this; |
| 3206 | } |
| 3207 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3208 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3209 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3210 | shared_ptr<_Tp>& |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3211 | shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3212 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3213 | shared_ptr(_VSTD::move(__r)).swap(*this); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3214 | return *this; |
| 3215 | } |
| 3216 | |
| 3217 | template<class _Tp> |
| 3218 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3219 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3220 | typename enable_if |
| 3221 | < |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3222 | __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value, |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3223 | shared_ptr<_Tp>& |
| 3224 | >::type |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3225 | shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r) |
| 3226 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3227 | shared_ptr(_VSTD::move(__r)).swap(*this); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3228 | return *this; |
| 3229 | } |
| 3230 | |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 3231 | #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3232 | template<class _Tp> |
| 3233 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3234 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3235 | typename enable_if |
| 3236 | < |
| 3237 | !is_array<_Yp>::value && |
Marshall Clow | 7e384b7 | 2017-01-10 16:59:33 +0000 | [diff] [blame] | 3238 | is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value, |
Howard Hinnant | 25bcaf6 | 2013-09-13 23:56:00 +0000 | [diff] [blame] | 3239 | shared_ptr<_Tp> |
| 3240 | >::type& |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3241 | shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r) |
| 3242 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3243 | shared_ptr(_VSTD::move(__r)).swap(*this); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3244 | return *this; |
| 3245 | } |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 3246 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3247 | |
| 3248 | template<class _Tp> |
| 3249 | template <class _Yp, class _Dp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3250 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3251 | typename enable_if |
| 3252 | < |
Aditya Kumar | 7c5db69 | 2017-08-20 10:38:55 +0000 | [diff] [blame] | 3253 | is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, |
Marshall Clow | 7e384b7 | 2017-01-10 16:59:33 +0000 | [diff] [blame] | 3254 | typename shared_ptr<_Tp>::element_type*>::value, |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3255 | shared_ptr<_Tp>& |
| 3256 | >::type |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3257 | shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r) |
| 3258 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3259 | shared_ptr(_VSTD::move(__r)).swap(*this); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3260 | return *this; |
| 3261 | } |
| 3262 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3263 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3264 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3265 | void |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3266 | shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3267 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3268 | _VSTD::swap(__ptr_, __r.__ptr_); |
| 3269 | _VSTD::swap(__cntrl_, __r.__cntrl_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3270 | } |
| 3271 | |
| 3272 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3273 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3274 | void |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3275 | shared_ptr<_Tp>::reset() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3276 | { |
| 3277 | shared_ptr().swap(*this); |
| 3278 | } |
| 3279 | |
| 3280 | template<class _Tp> |
| 3281 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3282 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3283 | typename enable_if |
| 3284 | < |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3285 | __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value, |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3286 | void |
| 3287 | >::type |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3288 | shared_ptr<_Tp>::reset(_Yp* __p) |
| 3289 | { |
| 3290 | shared_ptr(__p).swap(*this); |
| 3291 | } |
| 3292 | |
| 3293 | template<class _Tp> |
| 3294 | template<class _Yp, class _Dp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3295 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3296 | typename enable_if |
| 3297 | < |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3298 | __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value, |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3299 | void |
| 3300 | >::type |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3301 | shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d) |
| 3302 | { |
| 3303 | shared_ptr(__p, __d).swap(*this); |
| 3304 | } |
| 3305 | |
| 3306 | template<class _Tp> |
| 3307 | template<class _Yp, class _Dp, class _Alloc> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3308 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3309 | typename enable_if |
| 3310 | < |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3311 | __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value, |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3312 | void |
| 3313 | >::type |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3314 | shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a) |
| 3315 | { |
| 3316 | shared_ptr(__p, __d, __a).swap(*this); |
| 3317 | } |
| 3318 | |
Louis Dionne | 74aef9f | 2020-12-11 12:20:06 -0500 | [diff] [blame] | 3319 | // |
| 3320 | // std::allocate_shared and std::make_shared |
| 3321 | // |
| 3322 | template<class _Tp, class _Alloc, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> > |
| 3323 | _LIBCPP_HIDE_FROM_ABI |
| 3324 | shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&& ...__args) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3325 | { |
Louis Dionne | 74aef9f | 2020-12-11 12:20:06 -0500 | [diff] [blame] | 3326 | using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>; |
| 3327 | using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type; |
| 3328 | __allocation_guard<_ControlBlockAllocator> __guard(__a, 1); |
| 3329 | ::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...); |
| 3330 | auto __control_block = __guard.__release_ptr(); |
| 3331 | return shared_ptr<_Tp>::__create_with_control_block((*__control_block).__get_elem(), _VSTD::addressof(*__control_block)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3332 | } |
| 3333 | |
Louis Dionne | 43c9f8f | 2020-12-09 16:57:28 -0500 | [diff] [blame] | 3334 | template<class _Tp, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> > |
| 3335 | _LIBCPP_HIDE_FROM_ABI |
| 3336 | shared_ptr<_Tp> make_shared(_Args&& ...__args) |
| 3337 | { |
| 3338 | return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...); |
| 3339 | } |
| 3340 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3341 | template<class _Tp, class _Up> |
| 3342 | inline _LIBCPP_INLINE_VISIBILITY |
| 3343 | bool |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3344 | operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3345 | { |
| 3346 | return __x.get() == __y.get(); |
| 3347 | } |
| 3348 | |
| 3349 | template<class _Tp, class _Up> |
| 3350 | inline _LIBCPP_INLINE_VISIBILITY |
| 3351 | bool |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3352 | operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3353 | { |
| 3354 | return !(__x == __y); |
| 3355 | } |
| 3356 | |
| 3357 | template<class _Tp, class _Up> |
| 3358 | inline _LIBCPP_INLINE_VISIBILITY |
| 3359 | bool |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3360 | operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3361 | { |
Marshall Clow | 8afdf7a | 2018-02-12 17:26:40 +0000 | [diff] [blame] | 3362 | #if _LIBCPP_STD_VER <= 11 |
Eric Fiselier | f8898c8 | 2015-02-05 23:01:40 +0000 | [diff] [blame] | 3363 | typedef typename common_type<_Tp*, _Up*>::type _Vp; |
| 3364 | return less<_Vp>()(__x.get(), __y.get()); |
Marshall Clow | 8afdf7a | 2018-02-12 17:26:40 +0000 | [diff] [blame] | 3365 | #else |
| 3366 | return less<>()(__x.get(), __y.get()); |
| 3367 | #endif |
| 3368 | |
Howard Hinnant | b17caf9 | 2012-02-21 21:02:58 +0000 | [diff] [blame] | 3369 | } |
| 3370 | |
| 3371 | template<class _Tp, class _Up> |
| 3372 | inline _LIBCPP_INLINE_VISIBILITY |
| 3373 | bool |
| 3374 | operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT |
| 3375 | { |
| 3376 | return __y < __x; |
| 3377 | } |
| 3378 | |
| 3379 | template<class _Tp, class _Up> |
| 3380 | inline _LIBCPP_INLINE_VISIBILITY |
| 3381 | bool |
| 3382 | operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT |
| 3383 | { |
| 3384 | return !(__y < __x); |
| 3385 | } |
| 3386 | |
| 3387 | template<class _Tp, class _Up> |
| 3388 | inline _LIBCPP_INLINE_VISIBILITY |
| 3389 | bool |
| 3390 | operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT |
| 3391 | { |
| 3392 | return !(__x < __y); |
| 3393 | } |
| 3394 | |
| 3395 | template<class _Tp> |
| 3396 | inline _LIBCPP_INLINE_VISIBILITY |
| 3397 | bool |
| 3398 | operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT |
| 3399 | { |
| 3400 | return !__x; |
| 3401 | } |
| 3402 | |
| 3403 | template<class _Tp> |
| 3404 | inline _LIBCPP_INLINE_VISIBILITY |
| 3405 | bool |
| 3406 | operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT |
| 3407 | { |
| 3408 | return !__x; |
| 3409 | } |
| 3410 | |
| 3411 | template<class _Tp> |
| 3412 | inline _LIBCPP_INLINE_VISIBILITY |
| 3413 | bool |
| 3414 | operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT |
| 3415 | { |
| 3416 | return static_cast<bool>(__x); |
| 3417 | } |
| 3418 | |
| 3419 | template<class _Tp> |
| 3420 | inline _LIBCPP_INLINE_VISIBILITY |
| 3421 | bool |
| 3422 | operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT |
| 3423 | { |
| 3424 | return static_cast<bool>(__x); |
| 3425 | } |
| 3426 | |
| 3427 | template<class _Tp> |
| 3428 | inline _LIBCPP_INLINE_VISIBILITY |
| 3429 | bool |
| 3430 | operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT |
| 3431 | { |
| 3432 | return less<_Tp*>()(__x.get(), nullptr); |
| 3433 | } |
| 3434 | |
| 3435 | template<class _Tp> |
| 3436 | inline _LIBCPP_INLINE_VISIBILITY |
| 3437 | bool |
| 3438 | operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT |
| 3439 | { |
| 3440 | return less<_Tp*>()(nullptr, __x.get()); |
| 3441 | } |
| 3442 | |
| 3443 | template<class _Tp> |
| 3444 | inline _LIBCPP_INLINE_VISIBILITY |
| 3445 | bool |
| 3446 | operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT |
| 3447 | { |
| 3448 | return nullptr < __x; |
| 3449 | } |
| 3450 | |
| 3451 | template<class _Tp> |
| 3452 | inline _LIBCPP_INLINE_VISIBILITY |
| 3453 | bool |
| 3454 | operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT |
| 3455 | { |
| 3456 | return __x < nullptr; |
| 3457 | } |
| 3458 | |
| 3459 | template<class _Tp> |
| 3460 | inline _LIBCPP_INLINE_VISIBILITY |
| 3461 | bool |
| 3462 | operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT |
| 3463 | { |
| 3464 | return !(nullptr < __x); |
| 3465 | } |
| 3466 | |
| 3467 | template<class _Tp> |
| 3468 | inline _LIBCPP_INLINE_VISIBILITY |
| 3469 | bool |
| 3470 | operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT |
| 3471 | { |
| 3472 | return !(__x < nullptr); |
| 3473 | } |
| 3474 | |
| 3475 | template<class _Tp> |
| 3476 | inline _LIBCPP_INLINE_VISIBILITY |
| 3477 | bool |
| 3478 | operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT |
| 3479 | { |
| 3480 | return !(__x < nullptr); |
| 3481 | } |
| 3482 | |
| 3483 | template<class _Tp> |
| 3484 | inline _LIBCPP_INLINE_VISIBILITY |
| 3485 | bool |
| 3486 | operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT |
| 3487 | { |
| 3488 | return !(nullptr < __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3489 | } |
| 3490 | |
| 3491 | template<class _Tp> |
| 3492 | inline _LIBCPP_INLINE_VISIBILITY |
| 3493 | void |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3494 | swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3495 | { |
| 3496 | __x.swap(__y); |
| 3497 | } |
| 3498 | |
| 3499 | template<class _Tp, class _Up> |
| 3500 | inline _LIBCPP_INLINE_VISIBILITY |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3501 | shared_ptr<_Tp> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3502 | static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3503 | { |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3504 | return shared_ptr<_Tp>(__r, |
| 3505 | static_cast< |
| 3506 | typename shared_ptr<_Tp>::element_type*>(__r.get())); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3507 | } |
| 3508 | |
| 3509 | template<class _Tp, class _Up> |
| 3510 | inline _LIBCPP_INLINE_VISIBILITY |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3511 | shared_ptr<_Tp> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3512 | dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3513 | { |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3514 | typedef typename shared_ptr<_Tp>::element_type _ET; |
| 3515 | _ET* __p = dynamic_cast<_ET*>(__r.get()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3516 | return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>(); |
| 3517 | } |
| 3518 | |
| 3519 | template<class _Tp, class _Up> |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3520 | shared_ptr<_Tp> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3521 | const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3522 | { |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3523 | typedef typename shared_ptr<_Tp>::element_type _RTp; |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3524 | return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get())); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3525 | } |
| 3526 | |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3527 | template<class _Tp, class _Up> |
| 3528 | shared_ptr<_Tp> |
| 3529 | reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT |
| 3530 | { |
| 3531 | return shared_ptr<_Tp>(__r, |
| 3532 | reinterpret_cast< |
| 3533 | typename shared_ptr<_Tp>::element_type*>(__r.get())); |
| 3534 | } |
| 3535 | |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 3536 | #ifndef _LIBCPP_NO_RTTI |
| 3537 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3538 | template<class _Dp, class _Tp> |
| 3539 | inline _LIBCPP_INLINE_VISIBILITY |
| 3540 | _Dp* |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3541 | get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3542 | { |
| 3543 | return __p.template __get_deleter<_Dp>(); |
| 3544 | } |
| 3545 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3546 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 3547 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3548 | template<class _Tp> |
Vy Nguyen | e369bd9 | 2020-07-13 12:34:37 -0400 | [diff] [blame] | 3549 | class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3550 | { |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3551 | public: |
| 3552 | typedef _Tp element_type; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3553 | private: |
| 3554 | element_type* __ptr_; |
| 3555 | __shared_weak_count* __cntrl_; |
| 3556 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3557 | public: |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3558 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 3559 | _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3560 | template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r, |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3561 | typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0) |
| 3562 | _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3563 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3564 | weak_ptr(weak_ptr const& __r) _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3565 | template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r, |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3566 | typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0) |
| 3567 | _NOEXCEPT; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3568 | |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3569 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3570 | weak_ptr(weak_ptr&& __r) _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3571 | template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r, |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3572 | typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0) |
| 3573 | _NOEXCEPT; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3574 | ~weak_ptr(); |
| 3575 | |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3576 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3577 | weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT; |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3578 | template<class _Yp> |
| 3579 | typename enable_if |
| 3580 | < |
| 3581 | is_convertible<_Yp*, element_type*>::value, |
| 3582 | weak_ptr& |
| 3583 | >::type |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3584 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3585 | operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT; |
| 3586 | |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3587 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3588 | weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT; |
| 3589 | template<class _Yp> |
| 3590 | typename enable_if |
| 3591 | < |
| 3592 | is_convertible<_Yp*, element_type*>::value, |
| 3593 | weak_ptr& |
| 3594 | >::type |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3595 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3596 | operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT; |
| 3597 | |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3598 | template<class _Yp> |
| 3599 | typename enable_if |
| 3600 | < |
| 3601 | is_convertible<_Yp*, element_type*>::value, |
| 3602 | weak_ptr& |
| 3603 | >::type |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3604 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3605 | operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3606 | |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3607 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3608 | void swap(weak_ptr& __r) _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3609 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3610 | void reset() _NOEXCEPT; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3611 | |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3612 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3613 | long use_count() const _NOEXCEPT |
| 3614 | {return __cntrl_ ? __cntrl_->use_count() : 0;} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3615 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3616 | bool expired() const _NOEXCEPT |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 3617 | {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;} |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3618 | shared_ptr<_Tp> lock() const _NOEXCEPT; |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3619 | template<class _Up> |
| 3620 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3621 | bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3622 | {return __cntrl_ < __r.__cntrl_;} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3623 | template<class _Up> |
| 3624 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3625 | bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3626 | {return __cntrl_ < __r.__cntrl_;} |
| 3627 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3628 | template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr; |
| 3629 | template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3630 | }; |
| 3631 | |
Logan Smith | a5e4d7e | 2020-05-07 12:07:01 -0400 | [diff] [blame] | 3632 | #ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES |
| 3633 | template<class _Tp> |
| 3634 | weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>; |
| 3635 | #endif |
| 3636 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3637 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3638 | inline |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 3639 | _LIBCPP_CONSTEXPR |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3640 | weak_ptr<_Tp>::weak_ptr() _NOEXCEPT |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 3641 | : __ptr_(nullptr), |
| 3642 | __cntrl_(nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3643 | { |
| 3644 | } |
| 3645 | |
| 3646 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3647 | inline |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3648 | weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3649 | : __ptr_(__r.__ptr_), |
| 3650 | __cntrl_(__r.__cntrl_) |
| 3651 | { |
| 3652 | if (__cntrl_) |
| 3653 | __cntrl_->__add_weak(); |
| 3654 | } |
| 3655 | |
| 3656 | template<class _Tp> |
| 3657 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3658 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3659 | weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r, |
Howard Hinnant | 3a085e8 | 2011-05-22 00:09:02 +0000 | [diff] [blame] | 3660 | typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type) |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3661 | _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3662 | : __ptr_(__r.__ptr_), |
| 3663 | __cntrl_(__r.__cntrl_) |
| 3664 | { |
| 3665 | if (__cntrl_) |
| 3666 | __cntrl_->__add_weak(); |
| 3667 | } |
| 3668 | |
| 3669 | template<class _Tp> |
| 3670 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3671 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3672 | weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r, |
Howard Hinnant | 3a085e8 | 2011-05-22 00:09:02 +0000 | [diff] [blame] | 3673 | typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type) |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3674 | _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3675 | : __ptr_(__r.__ptr_), |
| 3676 | __cntrl_(__r.__cntrl_) |
| 3677 | { |
| 3678 | if (__cntrl_) |
| 3679 | __cntrl_->__add_weak(); |
| 3680 | } |
| 3681 | |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3682 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3683 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3684 | weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT |
| 3685 | : __ptr_(__r.__ptr_), |
| 3686 | __cntrl_(__r.__cntrl_) |
| 3687 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 3688 | __r.__ptr_ = nullptr; |
| 3689 | __r.__cntrl_ = nullptr; |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3690 | } |
| 3691 | |
| 3692 | template<class _Tp> |
| 3693 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3694 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3695 | weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r, |
| 3696 | typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type) |
| 3697 | _NOEXCEPT |
| 3698 | : __ptr_(__r.__ptr_), |
| 3699 | __cntrl_(__r.__cntrl_) |
| 3700 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 3701 | __r.__ptr_ = nullptr; |
| 3702 | __r.__cntrl_ = nullptr; |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3703 | } |
| 3704 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3705 | template<class _Tp> |
| 3706 | weak_ptr<_Tp>::~weak_ptr() |
| 3707 | { |
| 3708 | if (__cntrl_) |
| 3709 | __cntrl_->__release_weak(); |
| 3710 | } |
| 3711 | |
| 3712 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3713 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3714 | weak_ptr<_Tp>& |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3715 | weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3716 | { |
| 3717 | weak_ptr(__r).swap(*this); |
| 3718 | return *this; |
| 3719 | } |
| 3720 | |
| 3721 | template<class _Tp> |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3722 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3723 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3724 | typename enable_if |
| 3725 | < |
| 3726 | is_convertible<_Yp*, _Tp*>::value, |
| 3727 | weak_ptr<_Tp>& |
| 3728 | >::type |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3729 | weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3730 | { |
| 3731 | weak_ptr(__r).swap(*this); |
| 3732 | return *this; |
| 3733 | } |
| 3734 | |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3735 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3736 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3737 | weak_ptr<_Tp>& |
| 3738 | weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT |
| 3739 | { |
| 3740 | weak_ptr(_VSTD::move(__r)).swap(*this); |
| 3741 | return *this; |
| 3742 | } |
| 3743 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3744 | template<class _Tp> |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3745 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3746 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3747 | typename enable_if |
| 3748 | < |
| 3749 | is_convertible<_Yp*, _Tp*>::value, |
| 3750 | weak_ptr<_Tp>& |
| 3751 | >::type |
| 3752 | weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT |
| 3753 | { |
| 3754 | weak_ptr(_VSTD::move(__r)).swap(*this); |
| 3755 | return *this; |
| 3756 | } |
| 3757 | |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3758 | template<class _Tp> |
| 3759 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3760 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3761 | typename enable_if |
| 3762 | < |
| 3763 | is_convertible<_Yp*, _Tp*>::value, |
| 3764 | weak_ptr<_Tp>& |
| 3765 | >::type |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3766 | weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3767 | { |
| 3768 | weak_ptr(__r).swap(*this); |
| 3769 | return *this; |
| 3770 | } |
| 3771 | |
| 3772 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3773 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3774 | void |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3775 | weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3776 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3777 | _VSTD::swap(__ptr_, __r.__ptr_); |
| 3778 | _VSTD::swap(__cntrl_, __r.__cntrl_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3779 | } |
| 3780 | |
| 3781 | template<class _Tp> |
| 3782 | inline _LIBCPP_INLINE_VISIBILITY |
| 3783 | void |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3784 | swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3785 | { |
| 3786 | __x.swap(__y); |
| 3787 | } |
| 3788 | |
| 3789 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3790 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3791 | void |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3792 | weak_ptr<_Tp>::reset() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3793 | { |
| 3794 | weak_ptr().swap(*this); |
| 3795 | } |
| 3796 | |
| 3797 | template<class _Tp> |
| 3798 | template<class _Yp> |
| 3799 | shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r, |
Marshall Clow | 7e384b7 | 2017-01-10 16:59:33 +0000 | [diff] [blame] | 3800 | typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3801 | : __ptr_(__r.__ptr_), |
| 3802 | __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_) |
| 3803 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 3804 | if (__cntrl_ == nullptr) |
Marshall Clow | 8fea161 | 2016-08-25 15:09:01 +0000 | [diff] [blame] | 3805 | __throw_bad_weak_ptr(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3806 | } |
| 3807 | |
| 3808 | template<class _Tp> |
| 3809 | shared_ptr<_Tp> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3810 | weak_ptr<_Tp>::lock() const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3811 | { |
| 3812 | shared_ptr<_Tp> __r; |
| 3813 | __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_; |
| 3814 | if (__r.__cntrl_) |
| 3815 | __r.__ptr_ = __ptr_; |
| 3816 | return __r; |
| 3817 | } |
| 3818 | |
Marshall Clow | 3c2d8a4 | 2015-11-12 15:56:44 +0000 | [diff] [blame] | 3819 | #if _LIBCPP_STD_VER > 14 |
| 3820 | template <class _Tp = void> struct owner_less; |
| 3821 | #else |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3822 | template <class _Tp> struct owner_less; |
Marshall Clow | 3c2d8a4 | 2015-11-12 15:56:44 +0000 | [diff] [blame] | 3823 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3824 | |
| 3825 | template <class _Tp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3826 | struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> > |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3827 | : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool> |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3828 | { |
| 3829 | typedef bool result_type; |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3830 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3831 | bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3832 | {return __x.owner_before(__y);} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3833 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3834 | bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3835 | {return __x.owner_before(__y);} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3836 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3837 | bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3838 | {return __x.owner_before(__y);} |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3839 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3840 | |
| 3841 | template <class _Tp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3842 | struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> > |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3843 | : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool> |
| 3844 | { |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3845 | typedef bool result_type; |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3846 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3847 | bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3848 | {return __x.owner_before(__y);} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3849 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3850 | bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3851 | {return __x.owner_before(__y);} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3852 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3853 | bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3854 | {return __x.owner_before(__y);} |
| 3855 | }; |
| 3856 | |
Marshall Clow | 3c2d8a4 | 2015-11-12 15:56:44 +0000 | [diff] [blame] | 3857 | #if _LIBCPP_STD_VER > 14 |
| 3858 | template <> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3859 | struct _LIBCPP_TEMPLATE_VIS owner_less<void> |
Marshall Clow | 3c2d8a4 | 2015-11-12 15:56:44 +0000 | [diff] [blame] | 3860 | { |
| 3861 | template <class _Tp, class _Up> |
| 3862 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3863 | bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT |
Marshall Clow | 3c2d8a4 | 2015-11-12 15:56:44 +0000 | [diff] [blame] | 3864 | {return __x.owner_before(__y);} |
| 3865 | template <class _Tp, class _Up> |
| 3866 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3867 | bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT |
Marshall Clow | 3c2d8a4 | 2015-11-12 15:56:44 +0000 | [diff] [blame] | 3868 | {return __x.owner_before(__y);} |
| 3869 | template <class _Tp, class _Up> |
| 3870 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3871 | bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT |
Marshall Clow | 3c2d8a4 | 2015-11-12 15:56:44 +0000 | [diff] [blame] | 3872 | {return __x.owner_before(__y);} |
| 3873 | template <class _Tp, class _Up> |
| 3874 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3875 | bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT |
Marshall Clow | 3c2d8a4 | 2015-11-12 15:56:44 +0000 | [diff] [blame] | 3876 | {return __x.owner_before(__y);} |
| 3877 | typedef void is_transparent; |
| 3878 | }; |
| 3879 | #endif |
| 3880 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3881 | template<class _Tp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3882 | class _LIBCPP_TEMPLATE_VIS enable_shared_from_this |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3883 | { |
| 3884 | mutable weak_ptr<_Tp> __weak_this_; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3885 | protected: |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 3886 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3887 | enable_shared_from_this() _NOEXCEPT {} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3888 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3889 | enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3890 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3891 | enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT |
| 3892 | {return *this;} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3893 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3894 | ~enable_shared_from_this() {} |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3895 | public: |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3896 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3897 | shared_ptr<_Tp> shared_from_this() |
| 3898 | {return shared_ptr<_Tp>(__weak_this_);} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3899 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3900 | shared_ptr<_Tp const> shared_from_this() const |
| 3901 | {return shared_ptr<const _Tp>(__weak_this_);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3902 | |
Eric Fiselier | 8400686 | 2016-06-02 00:15:35 +0000 | [diff] [blame] | 3903 | #if _LIBCPP_STD_VER > 14 |
| 3904 | _LIBCPP_INLINE_VISIBILITY |
| 3905 | weak_ptr<_Tp> weak_from_this() _NOEXCEPT |
| 3906 | { return __weak_this_; } |
| 3907 | |
| 3908 | _LIBCPP_INLINE_VISIBILITY |
| 3909 | weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT |
| 3910 | { return __weak_this_; } |
| 3911 | #endif // _LIBCPP_STD_VER > 14 |
| 3912 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3913 | template <class _Up> friend class shared_ptr; |
| 3914 | }; |
| 3915 | |
Howard Hinnant | 36b31ae | 2010-06-03 16:42:57 +0000 | [diff] [blame] | 3916 | template <class _Tp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3917 | struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> > |
Howard Hinnant | 36b31ae | 2010-06-03 16:42:57 +0000 | [diff] [blame] | 3918 | { |
| 3919 | typedef shared_ptr<_Tp> argument_type; |
| 3920 | typedef size_t result_type; |
Eric Fiselier | 698a97b | 2017-01-21 00:02:12 +0000 | [diff] [blame] | 3921 | |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3922 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3923 | result_type operator()(const argument_type& __ptr) const _NOEXCEPT |
Howard Hinnant | 36b31ae | 2010-06-03 16:42:57 +0000 | [diff] [blame] | 3924 | { |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3925 | return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get()); |
Howard Hinnant | 36b31ae | 2010-06-03 16:42:57 +0000 | [diff] [blame] | 3926 | } |
| 3927 | }; |
| 3928 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3929 | template<class _CharT, class _Traits, class _Yp> |
Howard Hinnant | dc09597 | 2011-07-18 15:51:59 +0000 | [diff] [blame] | 3930 | inline _LIBCPP_INLINE_VISIBILITY |
| 3931 | basic_ostream<_CharT, _Traits>& |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3932 | operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p); |
Howard Hinnant | dc09597 | 2011-07-18 15:51:59 +0000 | [diff] [blame] | 3933 | |
Eric Fiselier | 9b49267 | 2016-06-18 02:12:53 +0000 | [diff] [blame] | 3934 | |
| 3935 | #if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER) |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3936 | |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 3937 | class _LIBCPP_TYPE_VIS __sp_mut |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3938 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3939 | void* __lx; |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3940 | public: |
| 3941 | void lock() _NOEXCEPT; |
| 3942 | void unlock() _NOEXCEPT; |
| 3943 | |
| 3944 | private: |
| 3945 | _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT; |
| 3946 | __sp_mut(const __sp_mut&); |
| 3947 | __sp_mut& operator=(const __sp_mut&); |
| 3948 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 3949 | friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*); |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3950 | }; |
| 3951 | |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 3952 | _LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR |
| 3953 | __sp_mut& __get_sp_mut(const void*); |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3954 | |
| 3955 | template <class _Tp> |
| 3956 | inline _LIBCPP_INLINE_VISIBILITY |
| 3957 | bool |
| 3958 | atomic_is_lock_free(const shared_ptr<_Tp>*) |
| 3959 | { |
| 3960 | return false; |
| 3961 | } |
| 3962 | |
| 3963 | template <class _Tp> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 3964 | _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3965 | shared_ptr<_Tp> |
| 3966 | atomic_load(const shared_ptr<_Tp>* __p) |
| 3967 | { |
| 3968 | __sp_mut& __m = __get_sp_mut(__p); |
| 3969 | __m.lock(); |
| 3970 | shared_ptr<_Tp> __q = *__p; |
| 3971 | __m.unlock(); |
| 3972 | return __q; |
| 3973 | } |
Aditya Kumar | 7c5db69 | 2017-08-20 10:38:55 +0000 | [diff] [blame] | 3974 | |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3975 | template <class _Tp> |
| 3976 | inline _LIBCPP_INLINE_VISIBILITY |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 3977 | _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3978 | shared_ptr<_Tp> |
| 3979 | atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order) |
| 3980 | { |
| 3981 | return atomic_load(__p); |
| 3982 | } |
| 3983 | |
| 3984 | template <class _Tp> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 3985 | _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3986 | void |
| 3987 | atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r) |
| 3988 | { |
| 3989 | __sp_mut& __m = __get_sp_mut(__p); |
| 3990 | __m.lock(); |
| 3991 | __p->swap(__r); |
| 3992 | __m.unlock(); |
| 3993 | } |
| 3994 | |
| 3995 | template <class _Tp> |
| 3996 | inline _LIBCPP_INLINE_VISIBILITY |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 3997 | _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3998 | void |
| 3999 | atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order) |
| 4000 | { |
| 4001 | atomic_store(__p, __r); |
| 4002 | } |
| 4003 | |
| 4004 | template <class _Tp> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 4005 | _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 4006 | shared_ptr<_Tp> |
| 4007 | atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r) |
| 4008 | { |
| 4009 | __sp_mut& __m = __get_sp_mut(__p); |
| 4010 | __m.lock(); |
| 4011 | __p->swap(__r); |
| 4012 | __m.unlock(); |
| 4013 | return __r; |
| 4014 | } |
Aditya Kumar | 7c5db69 | 2017-08-20 10:38:55 +0000 | [diff] [blame] | 4015 | |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 4016 | template <class _Tp> |
| 4017 | inline _LIBCPP_INLINE_VISIBILITY |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 4018 | _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 4019 | shared_ptr<_Tp> |
| 4020 | atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order) |
| 4021 | { |
| 4022 | return atomic_exchange(__p, __r); |
| 4023 | } |
| 4024 | |
| 4025 | template <class _Tp> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 4026 | _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 4027 | bool |
| 4028 | atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w) |
| 4029 | { |
Marshall Clow | 4201ee8 | 2016-05-18 17:50:13 +0000 | [diff] [blame] | 4030 | shared_ptr<_Tp> __temp; |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 4031 | __sp_mut& __m = __get_sp_mut(__p); |
| 4032 | __m.lock(); |
| 4033 | if (__p->__owner_equivalent(*__v)) |
| 4034 | { |
Marshall Clow | 4201ee8 | 2016-05-18 17:50:13 +0000 | [diff] [blame] | 4035 | _VSTD::swap(__temp, *__p); |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 4036 | *__p = __w; |
| 4037 | __m.unlock(); |
| 4038 | return true; |
| 4039 | } |
Marshall Clow | 4201ee8 | 2016-05-18 17:50:13 +0000 | [diff] [blame] | 4040 | _VSTD::swap(__temp, *__v); |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 4041 | *__v = *__p; |
| 4042 | __m.unlock(); |
| 4043 | return false; |
| 4044 | } |
| 4045 | |
| 4046 | template <class _Tp> |
| 4047 | inline _LIBCPP_INLINE_VISIBILITY |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 4048 | _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 4049 | bool |
| 4050 | atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w) |
| 4051 | { |
| 4052 | return atomic_compare_exchange_strong(__p, __v, __w); |
| 4053 | } |
| 4054 | |
| 4055 | template <class _Tp> |
| 4056 | inline _LIBCPP_INLINE_VISIBILITY |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 4057 | _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 4058 | bool |
| 4059 | atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, |
| 4060 | shared_ptr<_Tp> __w, memory_order, memory_order) |
| 4061 | { |
| 4062 | return atomic_compare_exchange_strong(__p, __v, __w); |
| 4063 | } |
| 4064 | |
| 4065 | template <class _Tp> |
| 4066 | inline _LIBCPP_INLINE_VISIBILITY |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 4067 | _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 4068 | bool |
| 4069 | atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, |
| 4070 | shared_ptr<_Tp> __w, memory_order, memory_order) |
| 4071 | { |
| 4072 | return atomic_compare_exchange_weak(__p, __v, __w); |
| 4073 | } |
| 4074 | |
Eric Fiselier | 9b49267 | 2016-06-18 02:12:53 +0000 | [diff] [blame] | 4075 | #endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER) |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 4076 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 4077 | //enum class |
Eric Fiselier | ab6bb30 | 2017-01-05 01:15:42 +0000 | [diff] [blame] | 4078 | #if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) |
| 4079 | # ifndef _LIBCPP_CXX03_LANG |
| 4080 | enum class pointer_safety : unsigned char { |
| 4081 | relaxed, |
| 4082 | preferred, |
| 4083 | strict |
| 4084 | }; |
| 4085 | # endif |
| 4086 | #else |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 4087 | struct _LIBCPP_TYPE_VIS pointer_safety |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4088 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4089 | enum __lx |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4090 | { |
| 4091 | relaxed, |
| 4092 | preferred, |
| 4093 | strict |
| 4094 | }; |
| 4095 | |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4096 | __lx __v_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4097 | |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 4098 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 2fdd22b | 2017-01-05 01:28:40 +0000 | [diff] [blame] | 4099 | pointer_safety() : __v_() {} |
| 4100 | |
| 4101 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 4102 | pointer_safety(__lx __v) : __v_(__v) {} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 4103 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4104 | operator int() const {return __v_;} |
| 4105 | }; |
Eric Fiselier | ab6bb30 | 2017-01-05 01:15:42 +0000 | [diff] [blame] | 4106 | #endif |
| 4107 | |
| 4108 | #if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \ |
Louis Dionne | 5e0eadd | 2018-08-01 02:08:59 +0000 | [diff] [blame] | 4109 | defined(_LIBCPP_BUILDING_LIBRARY) |
Eric Fiselier | ab6bb30 | 2017-01-05 01:15:42 +0000 | [diff] [blame] | 4110 | _LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT; |
| 4111 | #else |
| 4112 | // This function is only offered in C++03 under ABI v1. |
| 4113 | # if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG) |
| 4114 | inline _LIBCPP_INLINE_VISIBILITY |
| 4115 | pointer_safety get_pointer_safety() _NOEXCEPT { |
| 4116 | return pointer_safety::relaxed; |
| 4117 | } |
| 4118 | # endif |
| 4119 | #endif |
| 4120 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4121 | |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 4122 | _LIBCPP_FUNC_VIS void declare_reachable(void* __p); |
| 4123 | _LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n); |
| 4124 | _LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n); |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 4125 | _LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4126 | |
| 4127 | template <class _Tp> |
| 4128 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 4129 | _Tp* |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4130 | undeclare_reachable(_Tp* __p) |
| 4131 | { |
| 4132 | return static_cast<_Tp*>(__undeclare_reachable(__p)); |
| 4133 | } |
| 4134 | |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 4135 | _LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4136 | |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 4137 | // --- Helper for container swap -- |
| 4138 | template <typename _Alloc> |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 4139 | _LIBCPP_INLINE_VISIBILITY |
| 4140 | void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type) |
| 4141 | #if _LIBCPP_STD_VER >= 14 |
| 4142 | _NOEXCEPT |
| 4143 | #else |
| 4144 | _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value) |
| 4145 | #endif |
| 4146 | { |
| 4147 | using _VSTD::swap; |
| 4148 | swap(__a1, __a2); |
| 4149 | } |
| 4150 | |
| 4151 | template <typename _Alloc> |
Eric Fiselier | 6585c75 | 2016-04-21 22:54:21 +0000 | [diff] [blame] | 4152 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 4153 | void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {} |
| 4154 | |
Arthur O'Dwyer | 4e0de31 | 2020-11-18 18:54:38 -0500 | [diff] [blame] | 4155 | template <typename _Alloc> |
| 4156 | inline _LIBCPP_INLINE_VISIBILITY |
| 4157 | void __swap_allocator(_Alloc & __a1, _Alloc & __a2) |
| 4158 | #if _LIBCPP_STD_VER >= 14 |
| 4159 | _NOEXCEPT |
| 4160 | #else |
| 4161 | _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value) |
| 4162 | #endif |
| 4163 | { |
| 4164 | _VSTD::__swap_allocator(__a1, __a2, |
| 4165 | integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>()); |
| 4166 | } |
| 4167 | |
Marshall Clow | ff91de8 | 2015-08-18 19:51:37 +0000 | [diff] [blame] | 4168 | template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> > |
Aditya Kumar | 7c5db69 | 2017-08-20 10:38:55 +0000 | [diff] [blame] | 4169 | struct __noexcept_move_assign_container : public integral_constant<bool, |
Marshall Clow | 2fe8a8d | 2015-08-18 18:57:00 +0000 | [diff] [blame] | 4170 | _Traits::propagate_on_container_move_assignment::value |
| 4171 | #if _LIBCPP_STD_VER > 14 |
| 4172 | || _Traits::is_always_equal::value |
| 4173 | #else |
| 4174 | && is_nothrow_move_assignable<_Alloc>::value |
| 4175 | #endif |
| 4176 | > {}; |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 4177 | |
Marshall Clow | a591b9a | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 4178 | |
Marshall Clow | a591b9a | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 4179 | template <class _Tp, class _Alloc> |
| 4180 | struct __temp_value { |
| 4181 | typedef allocator_traits<_Alloc> _Traits; |
Aditya Kumar | 7c5db69 | 2017-08-20 10:38:55 +0000 | [diff] [blame] | 4182 | |
Eric Fiselier | 0f0ed9d | 2019-01-16 01:51:12 +0000 | [diff] [blame] | 4183 | typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v; |
Marshall Clow | a591b9a | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 4184 | _Alloc &__a; |
| 4185 | |
| 4186 | _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); } |
| 4187 | _Tp & get() { return *__addr(); } |
Aditya Kumar | 7c5db69 | 2017-08-20 10:38:55 +0000 | [diff] [blame] | 4188 | |
Marshall Clow | a591b9a | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 4189 | template<class... _Args> |
Peter Collingbourne | cb12615 | 2018-08-15 17:49:30 +0000 | [diff] [blame] | 4190 | _LIBCPP_NO_CFI |
| 4191 | __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) { |
| 4192 | _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)), |
| 4193 | _VSTD::forward<_Args>(__args)...); |
| 4194 | } |
Aditya Kumar | 7c5db69 | 2017-08-20 10:38:55 +0000 | [diff] [blame] | 4195 | |
Marshall Clow | a591b9a | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 4196 | ~__temp_value() { _Traits::destroy(__a, __addr()); } |
| 4197 | }; |
Marshall Clow | a591b9a | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 4198 | |
Marshall Clow | e46031a | 2018-07-02 18:41:15 +0000 | [diff] [blame] | 4199 | template<typename _Alloc, typename = void, typename = void> |
Marshall Clow | 82c90aa | 2018-01-11 19:36:22 +0000 | [diff] [blame] | 4200 | struct __is_allocator : false_type {}; |
| 4201 | |
| 4202 | template<typename _Alloc> |
| 4203 | struct __is_allocator<_Alloc, |
Marshall Clow | e46031a | 2018-07-02 18:41:15 +0000 | [diff] [blame] | 4204 | typename __void_t<typename _Alloc::value_type>::type, |
| 4205 | typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type |
| 4206 | > |
Marshall Clow | 82c90aa | 2018-01-11 19:36:22 +0000 | [diff] [blame] | 4207 | : true_type {}; |
Marshall Clow | 82c90aa | 2018-01-11 19:36:22 +0000 | [diff] [blame] | 4208 | |
Eric Fiselier | 74ebee6 | 2019-06-08 01:31:19 +0000 | [diff] [blame] | 4209 | // __builtin_new_allocator -- A non-templated helper for allocating and |
| 4210 | // deallocating memory using __builtin_operator_new and |
| 4211 | // __builtin_operator_delete. It should be used in preference to |
| 4212 | // `std::allocator<T>` to avoid additional instantiations. |
| 4213 | struct __builtin_new_allocator { |
| 4214 | struct __builtin_new_deleter { |
| 4215 | typedef void* pointer_type; |
| 4216 | |
| 4217 | _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align) |
| 4218 | : __size_(__size), __align_(__align) {} |
| 4219 | |
| 4220 | void operator()(void* p) const _NOEXCEPT { |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 4221 | _VSTD::__libcpp_deallocate(p, __size_, __align_); |
Eric Fiselier | 74ebee6 | 2019-06-08 01:31:19 +0000 | [diff] [blame] | 4222 | } |
| 4223 | |
| 4224 | private: |
| 4225 | size_t __size_; |
| 4226 | size_t __align_; |
| 4227 | }; |
| 4228 | |
| 4229 | typedef unique_ptr<void, __builtin_new_deleter> __holder_t; |
| 4230 | |
| 4231 | static __holder_t __allocate_bytes(size_t __s, size_t __align) { |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 4232 | return __holder_t(_VSTD::__libcpp_allocate(__s, __align), |
Eric Fiselier | 74ebee6 | 2019-06-08 01:31:19 +0000 | [diff] [blame] | 4233 | __builtin_new_deleter(__s, __align)); |
| 4234 | } |
| 4235 | |
| 4236 | static void __deallocate_bytes(void* __p, size_t __s, |
| 4237 | size_t __align) _NOEXCEPT { |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 4238 | _VSTD::__libcpp_deallocate(__p, __s, __align); |
Eric Fiselier | 74ebee6 | 2019-06-08 01:31:19 +0000 | [diff] [blame] | 4239 | } |
| 4240 | |
| 4241 | template <class _Tp> |
| 4242 | _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE |
| 4243 | static __holder_t __allocate_type(size_t __n) { |
| 4244 | return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)); |
| 4245 | } |
| 4246 | |
| 4247 | template <class _Tp> |
| 4248 | _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE |
| 4249 | static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT { |
| 4250 | __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)); |
| 4251 | } |
| 4252 | }; |
| 4253 | |
| 4254 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4255 | _LIBCPP_END_NAMESPACE_STD |
| 4256 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 4257 | _LIBCPP_POP_MACROS |
| 4258 | |
Louis Dionne | 59d0b3c | 2019-08-05 18:29:14 +0000 | [diff] [blame] | 4259 | #if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17 |
Louis Dionne | d53d8c0 | 2019-08-06 21:11:24 +0000 | [diff] [blame] | 4260 | # include <__pstl_memory> |
Louis Dionne | 59d0b3c | 2019-08-05 18:29:14 +0000 | [diff] [blame] | 4261 | #endif |
| 4262 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4263 | #endif // _LIBCPP_MEMORY |