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> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 676 | #include <iterator> |
| 677 | #include <__functional_base> |
Howard Hinnant | dc09597 | 2011-07-18 15:51:59 +0000 | [diff] [blame] | 678 | #include <iosfwd> |
Howard Hinnant | 83b1c05 | 2011-12-19 17:58:44 +0000 | [diff] [blame] | 679 | #include <tuple> |
Eric Fiselier | 2db2bd5 | 2016-05-07 03:12:24 +0000 | [diff] [blame] | 680 | #include <stdexcept> |
Howard Hinnant | d2cab2f | 2012-02-15 00:41:34 +0000 | [diff] [blame] | 681 | #include <cstring> |
Louis Dionne | 1e38faa | 2021-04-09 12:48:34 -0400 | [diff] [blame] | 682 | #include <__memory/allocator.h> |
Louis Dionne | fa621d7 | 2020-12-10 18:28:13 -0500 | [diff] [blame] | 683 | #include <__memory/allocator_traits.h> |
Louis Dionne | 26bc98e | 2021-04-09 12:44:26 -0400 | [diff] [blame] | 684 | #include <__memory/auto_ptr.h> |
Louis Dionne | fa621d7 | 2020-12-10 18:28:13 -0500 | [diff] [blame] | 685 | #include <__memory/base.h> |
Louis Dionne | 556fcd0 | 2021-04-09 14:43:01 -0400 | [diff] [blame^] | 686 | #include <__memory/compressed_pair.h> |
Louis Dionne | fa621d7 | 2020-12-10 18:28:13 -0500 | [diff] [blame] | 687 | #include <__memory/pointer_traits.h> |
Louis Dionne | 0018087 | 2021-04-09 12:58:00 -0400 | [diff] [blame] | 688 | #include <__memory/temporary_buffer.h> |
Louis Dionne | 74aef9f | 2020-12-11 12:20:06 -0500 | [diff] [blame] | 689 | #include <__memory/utilities.h> |
Eric Fiselier | 8020b6c | 2015-08-19 17:21:46 +0000 | [diff] [blame] | 690 | #if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER) |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 691 | # include <atomic> |
| 692 | #endif |
Marshall Clow | 0a1e750 | 2018-09-12 19:41:40 +0000 | [diff] [blame] | 693 | #include <version> |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 694 | |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 695 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 696 | #pragma GCC system_header |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 697 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 698 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 699 | _LIBCPP_PUSH_MACROS |
| 700 | #include <__undef_macros> |
| 701 | |
| 702 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 703 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 704 | |
Eric Fiselier | 89659d1 | 2015-07-07 00:27:16 +0000 | [diff] [blame] | 705 | template <class _ValueType> |
Louis Dionne | 16fe295 | 2018-07-11 23:14:33 +0000 | [diff] [blame] | 706 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 89659d1 | 2015-07-07 00:27:16 +0000 | [diff] [blame] | 707 | _ValueType __libcpp_relaxed_load(_ValueType const* __value) { |
| 708 | #if !defined(_LIBCPP_HAS_NO_THREADS) && \ |
| 709 | defined(__ATOMIC_RELAXED) && \ |
Richard Smith | ca47d0f | 2019-04-25 20:02:10 +0000 | [diff] [blame] | 710 | (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC)) |
Eric Fiselier | 89659d1 | 2015-07-07 00:27:16 +0000 | [diff] [blame] | 711 | return __atomic_load_n(__value, __ATOMIC_RELAXED); |
| 712 | #else |
| 713 | return *__value; |
| 714 | #endif |
| 715 | } |
| 716 | |
Kuba Brecka | de9d679 | 2016-09-04 09:55:12 +0000 | [diff] [blame] | 717 | template <class _ValueType> |
Louis Dionne | 16fe295 | 2018-07-11 23:14:33 +0000 | [diff] [blame] | 718 | inline _LIBCPP_INLINE_VISIBILITY |
Kuba Brecka | de9d679 | 2016-09-04 09:55:12 +0000 | [diff] [blame] | 719 | _ValueType __libcpp_acquire_load(_ValueType const* __value) { |
| 720 | #if !defined(_LIBCPP_HAS_NO_THREADS) && \ |
| 721 | defined(__ATOMIC_ACQUIRE) && \ |
Richard Smith | ca47d0f | 2019-04-25 20:02:10 +0000 | [diff] [blame] | 722 | (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC)) |
Kuba Brecka | de9d679 | 2016-09-04 09:55:12 +0000 | [diff] [blame] | 723 | return __atomic_load_n(__value, __ATOMIC_ACQUIRE); |
| 724 | #else |
| 725 | return *__value; |
| 726 | #endif |
| 727 | } |
| 728 | |
Louis Dionne | d665154 | 2020-11-03 12:05:55 -0500 | [diff] [blame] | 729 | template <class _Alloc, class _Ptr> |
| 730 | _LIBCPP_INLINE_VISIBILITY |
| 731 | void __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) { |
| 732 | static_assert(__is_cpp17_move_insertable<_Alloc>::value, |
Arthur O'Dwyer | f2016bb | 2020-12-12 11:43:15 -0500 | [diff] [blame] | 733 | "The specified type does not meet the requirements of Cpp17MoveInsertable"); |
Louis Dionne | d665154 | 2020-11-03 12:05:55 -0500 | [diff] [blame] | 734 | typedef allocator_traits<_Alloc> _Traits; |
| 735 | for (; __begin1 != __end1; ++__begin1, (void)++__begin2) { |
| 736 | _Traits::construct(__a, _VSTD::__to_address(__begin2), |
| 737 | #ifdef _LIBCPP_NO_EXCEPTIONS |
| 738 | _VSTD::move(*__begin1) |
| 739 | #else |
| 740 | _VSTD::move_if_noexcept(*__begin1) |
| 741 | #endif |
| 742 | ); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | template <class _Alloc, class _Tp, typename enable_if< |
| 747 | (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) && |
| 748 | is_trivially_move_constructible<_Tp>::value |
| 749 | >::type> |
| 750 | _LIBCPP_INLINE_VISIBILITY |
| 751 | void __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) { |
| 752 | ptrdiff_t _Np = __end1 - __begin1; |
| 753 | if (_Np > 0) { |
| 754 | _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp)); |
| 755 | __begin2 += _Np; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | template <class _Alloc, class _Iter, class _Ptr> |
| 760 | _LIBCPP_INLINE_VISIBILITY |
| 761 | void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) { |
| 762 | typedef allocator_traits<_Alloc> _Traits; |
| 763 | for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) { |
| 764 | _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1); |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | template <class _Alloc, class _Source, class _Dest, |
| 769 | class _RawSource = typename remove_const<_Source>::type, |
| 770 | class _RawDest = typename remove_const<_Dest>::type, |
| 771 | class = |
| 772 | typename enable_if< |
| 773 | is_trivially_copy_constructible<_Dest>::value && |
| 774 | is_same<_RawSource, _RawDest>::value && |
| 775 | (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value) |
| 776 | >::type> |
| 777 | _LIBCPP_INLINE_VISIBILITY |
| 778 | void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) { |
| 779 | ptrdiff_t _Np = __end1 - __begin1; |
| 780 | if (_Np > 0) { |
| 781 | _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest)); |
| 782 | __begin2 += _Np; |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | template <class _Alloc, class _Ptr> |
| 787 | _LIBCPP_INLINE_VISIBILITY |
| 788 | void __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) { |
| 789 | static_assert(__is_cpp17_move_insertable<_Alloc>::value, |
| 790 | "The specified type does not meet the requirements of Cpp17MoveInsertable"); |
| 791 | typedef allocator_traits<_Alloc> _Traits; |
| 792 | while (__end1 != __begin1) { |
| 793 | _Traits::construct(__a, _VSTD::__to_address(__end2 - 1), |
| 794 | #ifdef _LIBCPP_NO_EXCEPTIONS |
| 795 | _VSTD::move(*--__end1) |
| 796 | #else |
| 797 | _VSTD::move_if_noexcept(*--__end1) |
| 798 | #endif |
| 799 | ); |
| 800 | --__end2; |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | template <class _Alloc, class _Tp, class = typename enable_if< |
| 805 | (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) && |
| 806 | is_trivially_move_constructible<_Tp>::value |
| 807 | >::type> |
| 808 | _LIBCPP_INLINE_VISIBILITY |
| 809 | void __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) { |
| 810 | ptrdiff_t _Np = __end1 - __begin1; |
| 811 | __end2 -= _Np; |
| 812 | if (_Np > 0) |
| 813 | _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp)); |
| 814 | } |
| 815 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 816 | template <class _OutputIterator, class _Tp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 817 | class _LIBCPP_TEMPLATE_VIS raw_storage_iterator |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 818 | : public iterator<output_iterator_tag, |
| 819 | _Tp, // purposefully not C++03 |
| 820 | ptrdiff_t, // purposefully not C++03 |
| 821 | _Tp*, // purposefully not C++03 |
| 822 | raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03 |
| 823 | { |
| 824 | private: |
| 825 | _OutputIterator __x_; |
| 826 | public: |
| 827 | _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {} |
| 828 | _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;} |
| 829 | _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 830 | {::new ((void*)_VSTD::addressof(*__x_)) _Tp(__element); return *this;} |
Marshall Clow | 5c3f09e | 2015-10-25 18:58:07 +0000 | [diff] [blame] | 831 | #if _LIBCPP_STD_VER >= 14 |
| 832 | _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 833 | {::new ((void*)_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;} |
Marshall Clow | 5c3f09e | 2015-10-25 18:58:07 +0000 | [diff] [blame] | 834 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 835 | _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;} |
| 836 | _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int) |
| 837 | {raw_storage_iterator __t(*this); ++__x_; return __t;} |
Marshall Clow | b949f1b | 2015-05-10 13:14:08 +0000 | [diff] [blame] | 838 | #if _LIBCPP_STD_VER >= 14 |
Aditya Kumar | 7c5db69 | 2017-08-20 10:38:55 +0000 | [diff] [blame] | 839 | _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; } |
Marshall Clow | b949f1b | 2015-05-10 13:14:08 +0000 | [diff] [blame] | 840 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 841 | }; |
| 842 | |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 843 | // default_delete |
| 844 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 845 | template <class _Tp> |
Eric Fiselier | 6779b06 | 2017-04-16 02:06:25 +0000 | [diff] [blame] | 846 | struct _LIBCPP_TEMPLATE_VIS default_delete { |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 847 | static_assert(!is_function<_Tp>::value, |
| 848 | "default_delete cannot be instantiated for function types"); |
Eric Fiselier | 6779b06 | 2017-04-16 02:06:25 +0000 | [diff] [blame] | 849 | #ifndef _LIBCPP_CXX03_LANG |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 850 | _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default; |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 851 | #else |
Eric Fiselier | 6779b06 | 2017-04-16 02:06:25 +0000 | [diff] [blame] | 852 | _LIBCPP_INLINE_VISIBILITY default_delete() {} |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 853 | #endif |
Eric Fiselier | 6779b06 | 2017-04-16 02:06:25 +0000 | [diff] [blame] | 854 | template <class _Up> |
| 855 | _LIBCPP_INLINE_VISIBILITY |
| 856 | default_delete(const default_delete<_Up>&, |
| 857 | typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* = |
| 858 | 0) _NOEXCEPT {} |
| 859 | |
| 860 | _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT { |
| 861 | static_assert(sizeof(_Tp) > 0, |
| 862 | "default_delete can not delete incomplete type"); |
| 863 | static_assert(!is_void<_Tp>::value, |
| 864 | "default_delete can not delete incomplete type"); |
| 865 | delete __ptr; |
| 866 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 867 | }; |
| 868 | |
| 869 | template <class _Tp> |
Eric Fiselier | 6779b06 | 2017-04-16 02:06:25 +0000 | [diff] [blame] | 870 | struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> { |
| 871 | private: |
| 872 | template <class _Up> |
| 873 | struct _EnableIfConvertible |
| 874 | : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {}; |
| 875 | |
Howard Hinnant | 4500ca5 | 2011-12-18 21:19:44 +0000 | [diff] [blame] | 876 | public: |
Eric Fiselier | 6779b06 | 2017-04-16 02:06:25 +0000 | [diff] [blame] | 877 | #ifndef _LIBCPP_CXX03_LANG |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 878 | _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default; |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 879 | #else |
Eric Fiselier | 6779b06 | 2017-04-16 02:06:25 +0000 | [diff] [blame] | 880 | _LIBCPP_INLINE_VISIBILITY default_delete() {} |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 881 | #endif |
Eric Fiselier | 6779b06 | 2017-04-16 02:06:25 +0000 | [diff] [blame] | 882 | |
| 883 | template <class _Up> |
| 884 | _LIBCPP_INLINE_VISIBILITY |
| 885 | default_delete(const default_delete<_Up[]>&, |
| 886 | typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {} |
| 887 | |
| 888 | template <class _Up> |
| 889 | _LIBCPP_INLINE_VISIBILITY |
| 890 | typename _EnableIfConvertible<_Up>::type |
| 891 | operator()(_Up* __ptr) const _NOEXCEPT { |
| 892 | static_assert(sizeof(_Tp) > 0, |
| 893 | "default_delete can not delete incomplete type"); |
| 894 | static_assert(!is_void<_Tp>::value, |
| 895 | "default_delete can not delete void type"); |
| 896 | delete[] __ptr; |
| 897 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 898 | }; |
| 899 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 900 | template <class _Deleter> |
| 901 | struct __unique_ptr_deleter_sfinae { |
| 902 | static_assert(!is_reference<_Deleter>::value, "incorrect specialization"); |
| 903 | typedef const _Deleter& __lval_ref_type; |
| 904 | typedef _Deleter&& __good_rval_ref_type; |
| 905 | typedef true_type __enable_rval_overload; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 906 | }; |
| 907 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 908 | template <class _Deleter> |
| 909 | struct __unique_ptr_deleter_sfinae<_Deleter const&> { |
| 910 | typedef const _Deleter& __lval_ref_type; |
| 911 | typedef const _Deleter&& __bad_rval_ref_type; |
| 912 | typedef false_type __enable_rval_overload; |
| 913 | }; |
| 914 | |
| 915 | template <class _Deleter> |
| 916 | struct __unique_ptr_deleter_sfinae<_Deleter&> { |
| 917 | typedef _Deleter& __lval_ref_type; |
| 918 | typedef _Deleter&& __bad_rval_ref_type; |
| 919 | typedef false_type __enable_rval_overload; |
| 920 | }; |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 921 | |
Vy Nguyen | e369bd9 | 2020-07-13 12:34:37 -0400 | [diff] [blame] | 922 | #if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI) |
| 923 | # define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi)) |
| 924 | #else |
| 925 | # define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI |
| 926 | #endif |
| 927 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 928 | template <class _Tp, class _Dp = default_delete<_Tp> > |
Vy Nguyen | e369bd9 | 2020-07-13 12:34:37 -0400 | [diff] [blame] | 929 | class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr { |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 930 | public: |
| 931 | typedef _Tp element_type; |
| 932 | typedef _Dp deleter_type; |
Louis Dionne | 88978f6 | 2021-01-12 11:53:24 -0500 | [diff] [blame] | 933 | typedef _LIBCPP_NODEBUG_TYPE typename __pointer<_Tp, deleter_type>::type pointer; |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 934 | |
| 935 | static_assert(!is_rvalue_reference<deleter_type>::value, |
| 936 | "the specified deleter type cannot be an rvalue reference"); |
| 937 | |
| 938 | private: |
| 939 | __compressed_pair<pointer, deleter_type> __ptr_; |
| 940 | |
| 941 | struct __nat { int __for_bool_; }; |
| 942 | |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 943 | typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE; |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 944 | |
| 945 | template <bool _Dummy> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 946 | using _LValRefType _LIBCPP_NODEBUG_TYPE = |
Eric Fiselier | 31127cd | 2017-04-16 02:14:31 +0000 | [diff] [blame] | 947 | typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type; |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 948 | |
| 949 | template <bool _Dummy> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 950 | using _GoodRValRefType _LIBCPP_NODEBUG_TYPE = |
Eric Fiselier | 31127cd | 2017-04-16 02:14:31 +0000 | [diff] [blame] | 951 | typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type; |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 952 | |
| 953 | template <bool _Dummy> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 954 | using _BadRValRefType _LIBCPP_NODEBUG_TYPE = |
Eric Fiselier | 31127cd | 2017-04-16 02:14:31 +0000 | [diff] [blame] | 955 | typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type; |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 956 | |
| 957 | template <bool _Dummy, class _Deleter = typename __dependent_type< |
| 958 | __identity<deleter_type>, _Dummy>::type> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 959 | using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE = |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 960 | typename enable_if<is_default_constructible<_Deleter>::value && |
| 961 | !is_pointer<_Deleter>::value>::type; |
| 962 | |
| 963 | template <class _ArgType> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 964 | using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE = |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 965 | typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type; |
| 966 | |
| 967 | template <class _UPtr, class _Up> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 968 | using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if< |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 969 | is_convertible<typename _UPtr::pointer, pointer>::value && |
| 970 | !is_array<_Up>::value |
| 971 | >::type; |
| 972 | |
| 973 | template <class _UDel> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 974 | using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if< |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 975 | (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) || |
| 976 | (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value) |
| 977 | >::type; |
| 978 | |
| 979 | template <class _UDel> |
| 980 | using _EnableIfDeleterAssignable = typename enable_if< |
| 981 | is_assignable<_Dp&, _UDel&&>::value |
| 982 | >::type; |
| 983 | |
| 984 | public: |
| 985 | template <bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 986 | class = _EnableIfDeleterDefaultConstructible<_Dummy> > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 987 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 988 | _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {} |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 989 | |
| 990 | template <bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 991 | class = _EnableIfDeleterDefaultConstructible<_Dummy> > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 992 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 993 | _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {} |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 994 | |
| 995 | template <bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 996 | class = _EnableIfDeleterDefaultConstructible<_Dummy> > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 997 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 998 | explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __default_init_tag()) {} |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 999 | |
| 1000 | template <bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1001 | class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1002 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1003 | unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1004 | : __ptr_(__p, __d) {} |
| 1005 | |
| 1006 | template <bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1007 | class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1008 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1009 | unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1010 | : __ptr_(__p, _VSTD::move(__d)) { |
| 1011 | static_assert(!is_reference<deleter_type>::value, |
| 1012 | "rvalue deleter bound to reference"); |
| 1013 | } |
| 1014 | |
| 1015 | template <bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1016 | class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1017 | _LIBCPP_INLINE_VISIBILITY |
| 1018 | unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete; |
| 1019 | |
| 1020 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1021 | unique_ptr(unique_ptr&& __u) _NOEXCEPT |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1022 | : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) { |
| 1023 | } |
| 1024 | |
| 1025 | template <class _Up, class _Ep, |
| 1026 | class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>, |
| 1027 | class = _EnableIfDeleterConvertible<_Ep> |
| 1028 | > |
| 1029 | _LIBCPP_INLINE_VISIBILITY |
| 1030 | unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT |
| 1031 | : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {} |
| 1032 | |
| 1033 | #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) |
| 1034 | template <class _Up> |
| 1035 | _LIBCPP_INLINE_VISIBILITY |
| 1036 | unique_ptr(auto_ptr<_Up>&& __p, |
| 1037 | typename enable_if<is_convertible<_Up*, _Tp*>::value && |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1038 | is_same<_Dp, default_delete<_Tp> >::value, |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1039 | __nat>::type = __nat()) _NOEXCEPT |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1040 | : __ptr_(__p.release(), __default_init_tag()) {} |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1041 | #endif |
| 1042 | |
| 1043 | _LIBCPP_INLINE_VISIBILITY |
| 1044 | unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT { |
| 1045 | reset(__u.release()); |
| 1046 | __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter()); |
| 1047 | return *this; |
| 1048 | } |
| 1049 | |
| 1050 | template <class _Up, class _Ep, |
| 1051 | class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>, |
| 1052 | class = _EnableIfDeleterAssignable<_Ep> |
| 1053 | > |
| 1054 | _LIBCPP_INLINE_VISIBILITY |
| 1055 | unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT { |
| 1056 | reset(__u.release()); |
| 1057 | __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter()); |
| 1058 | return *this; |
| 1059 | } |
| 1060 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1061 | #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) |
| 1062 | template <class _Up> |
| 1063 | _LIBCPP_INLINE_VISIBILITY |
| 1064 | typename enable_if<is_convertible<_Up*, _Tp*>::value && |
| 1065 | is_same<_Dp, default_delete<_Tp> >::value, |
| 1066 | unique_ptr&>::type |
| 1067 | operator=(auto_ptr<_Up> __p) { |
| 1068 | reset(__p.release()); |
| 1069 | return *this; |
| 1070 | } |
| 1071 | #endif |
| 1072 | |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1073 | #ifdef _LIBCPP_CXX03_LANG |
| 1074 | unique_ptr(unique_ptr const&) = delete; |
| 1075 | unique_ptr& operator=(unique_ptr const&) = delete; |
| 1076 | #endif |
| 1077 | |
| 1078 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1079 | _LIBCPP_INLINE_VISIBILITY |
| 1080 | ~unique_ptr() { reset(); } |
| 1081 | |
| 1082 | _LIBCPP_INLINE_VISIBILITY |
| 1083 | unique_ptr& operator=(nullptr_t) _NOEXCEPT { |
| 1084 | reset(); |
| 1085 | return *this; |
| 1086 | } |
| 1087 | |
| 1088 | _LIBCPP_INLINE_VISIBILITY |
| 1089 | typename add_lvalue_reference<_Tp>::type |
| 1090 | operator*() const { |
| 1091 | return *__ptr_.first(); |
| 1092 | } |
| 1093 | _LIBCPP_INLINE_VISIBILITY |
| 1094 | pointer operator->() const _NOEXCEPT { |
| 1095 | return __ptr_.first(); |
| 1096 | } |
| 1097 | _LIBCPP_INLINE_VISIBILITY |
| 1098 | pointer get() const _NOEXCEPT { |
| 1099 | return __ptr_.first(); |
| 1100 | } |
| 1101 | _LIBCPP_INLINE_VISIBILITY |
| 1102 | deleter_type& get_deleter() _NOEXCEPT { |
| 1103 | return __ptr_.second(); |
| 1104 | } |
| 1105 | _LIBCPP_INLINE_VISIBILITY |
| 1106 | const deleter_type& get_deleter() const _NOEXCEPT { |
| 1107 | return __ptr_.second(); |
| 1108 | } |
| 1109 | _LIBCPP_INLINE_VISIBILITY |
| 1110 | _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT { |
| 1111 | return __ptr_.first() != nullptr; |
| 1112 | } |
| 1113 | |
| 1114 | _LIBCPP_INLINE_VISIBILITY |
| 1115 | pointer release() _NOEXCEPT { |
| 1116 | pointer __t = __ptr_.first(); |
| 1117 | __ptr_.first() = pointer(); |
| 1118 | return __t; |
| 1119 | } |
| 1120 | |
| 1121 | _LIBCPP_INLINE_VISIBILITY |
| 1122 | void reset(pointer __p = pointer()) _NOEXCEPT { |
| 1123 | pointer __tmp = __ptr_.first(); |
| 1124 | __ptr_.first() = __p; |
| 1125 | if (__tmp) |
| 1126 | __ptr_.second()(__tmp); |
| 1127 | } |
| 1128 | |
| 1129 | _LIBCPP_INLINE_VISIBILITY |
| 1130 | void swap(unique_ptr& __u) _NOEXCEPT { |
| 1131 | __ptr_.swap(__u.__ptr_); |
| 1132 | } |
| 1133 | }; |
| 1134 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1135 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1136 | template <class _Tp, class _Dp> |
Vy Nguyen | e369bd9 | 2020-07-13 12:34:37 -0400 | [diff] [blame] | 1137 | 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] | 1138 | public: |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1139 | typedef _Tp element_type; |
| 1140 | typedef _Dp deleter_type; |
Louis Dionne | 88978f6 | 2021-01-12 11:53:24 -0500 | [diff] [blame] | 1141 | typedef typename __pointer<_Tp, deleter_type>::type pointer; |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1142 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1143 | private: |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1144 | __compressed_pair<pointer, deleter_type> __ptr_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1145 | |
Eric Fiselier | 31127cd | 2017-04-16 02:14:31 +0000 | [diff] [blame] | 1146 | template <class _From> |
| 1147 | struct _CheckArrayPointerConversion : is_same<_From, pointer> {}; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1148 | |
Eric Fiselier | 31127cd | 2017-04-16 02:14:31 +0000 | [diff] [blame] | 1149 | template <class _FromElem> |
| 1150 | struct _CheckArrayPointerConversion<_FromElem*> |
| 1151 | : integral_constant<bool, |
| 1152 | is_same<_FromElem*, pointer>::value || |
| 1153 | (is_same<pointer, element_type*>::value && |
| 1154 | is_convertible<_FromElem(*)[], element_type(*)[]>::value) |
| 1155 | > |
| 1156 | {}; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1157 | |
Eric Fiselier | 31127cd | 2017-04-16 02:14:31 +0000 | [diff] [blame] | 1158 | typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE; |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1159 | |
| 1160 | template <bool _Dummy> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1161 | using _LValRefType _LIBCPP_NODEBUG_TYPE = |
Eric Fiselier | 31127cd | 2017-04-16 02:14:31 +0000 | [diff] [blame] | 1162 | typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type; |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1163 | |
| 1164 | template <bool _Dummy> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1165 | using _GoodRValRefType _LIBCPP_NODEBUG_TYPE = |
Eric Fiselier | 31127cd | 2017-04-16 02:14:31 +0000 | [diff] [blame] | 1166 | typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type; |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1167 | |
| 1168 | template <bool _Dummy> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1169 | using _BadRValRefType _LIBCPP_NODEBUG_TYPE = |
Eric Fiselier | 31127cd | 2017-04-16 02:14:31 +0000 | [diff] [blame] | 1170 | typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type; |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1171 | |
| 1172 | template <bool _Dummy, class _Deleter = typename __dependent_type< |
| 1173 | __identity<deleter_type>, _Dummy>::type> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1174 | using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE = |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1175 | typename enable_if<is_default_constructible<_Deleter>::value && |
| 1176 | !is_pointer<_Deleter>::value>::type; |
| 1177 | |
| 1178 | template <class _ArgType> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1179 | using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE = |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1180 | typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type; |
| 1181 | |
| 1182 | template <class _Pp> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1183 | using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if< |
Eric Fiselier | 31127cd | 2017-04-16 02:14:31 +0000 | [diff] [blame] | 1184 | _CheckArrayPointerConversion<_Pp>::value |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1185 | >::type; |
| 1186 | |
| 1187 | template <class _UPtr, class _Up, |
| 1188 | class _ElemT = typename _UPtr::element_type> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1189 | using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if< |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1190 | is_array<_Up>::value && |
| 1191 | is_same<pointer, element_type*>::value && |
| 1192 | is_same<typename _UPtr::pointer, _ElemT*>::value && |
| 1193 | is_convertible<_ElemT(*)[], element_type(*)[]>::value |
| 1194 | >::type; |
| 1195 | |
| 1196 | template <class _UDel> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1197 | using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if< |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1198 | (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) || |
| 1199 | (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value) |
| 1200 | >::type; |
| 1201 | |
| 1202 | template <class _UDel> |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1203 | using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if< |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1204 | is_assignable<_Dp&, _UDel&&>::value |
| 1205 | >::type; |
| 1206 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1207 | public: |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1208 | template <bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1209 | class = _EnableIfDeleterDefaultConstructible<_Dummy> > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1210 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1211 | _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1212 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1213 | template <bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1214 | class = _EnableIfDeleterDefaultConstructible<_Dummy> > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1215 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1216 | _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer(), __default_init_tag()) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1217 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1218 | template <class _Pp, bool _Dummy = true, |
| 1219 | class = _EnableIfDeleterDefaultConstructible<_Dummy>, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1220 | class = _EnableIfPointerConvertible<_Pp> > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1221 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1222 | explicit unique_ptr(_Pp __p) _NOEXCEPT |
Eric Fiselier | 33ebfb6 | 2019-12-16 18:23:39 -0500 | [diff] [blame] | 1223 | : __ptr_(__p, __default_init_tag()) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1224 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1225 | template <class _Pp, bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1226 | class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >, |
| 1227 | class = _EnableIfPointerConvertible<_Pp> > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1228 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1229 | unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1230 | : __ptr_(__p, __d) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1231 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1232 | template <bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1233 | class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1234 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1235 | unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1236 | : __ptr_(nullptr, __d) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1237 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1238 | template <class _Pp, bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1239 | class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >, |
| 1240 | class = _EnableIfPointerConvertible<_Pp> > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1241 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1242 | unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1243 | : __ptr_(__p, _VSTD::move(__d)) { |
| 1244 | static_assert(!is_reference<deleter_type>::value, |
| 1245 | "rvalue deleter bound to reference"); |
| 1246 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1247 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1248 | template <bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1249 | class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1250 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1251 | unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1252 | : __ptr_(nullptr, _VSTD::move(__d)) { |
| 1253 | static_assert(!is_reference<deleter_type>::value, |
| 1254 | "rvalue deleter bound to reference"); |
| 1255 | } |
Howard Hinnant | 4500ca5 | 2011-12-18 21:19:44 +0000 | [diff] [blame] | 1256 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1257 | template <class _Pp, bool _Dummy = true, |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1258 | class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >, |
| 1259 | class = _EnableIfPointerConvertible<_Pp> > |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1260 | _LIBCPP_INLINE_VISIBILITY |
| 1261 | unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete; |
Howard Hinnant | 4500ca5 | 2011-12-18 21:19:44 +0000 | [diff] [blame] | 1262 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1263 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1264 | unique_ptr(unique_ptr&& __u) _NOEXCEPT |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1265 | : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) { |
| 1266 | } |
Howard Hinnant | 4500ca5 | 2011-12-18 21:19:44 +0000 | [diff] [blame] | 1267 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1268 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1269 | unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT { |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1270 | reset(__u.release()); |
| 1271 | __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter()); |
| 1272 | return *this; |
| 1273 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1274 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1275 | template <class _Up, class _Ep, |
| 1276 | class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>, |
| 1277 | class = _EnableIfDeleterConvertible<_Ep> |
| 1278 | > |
| 1279 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1280 | unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT |
Eric Fiselier | 3827e6a | 2017-04-17 20:20:27 +0000 | [diff] [blame] | 1281 | : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) { |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1282 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1283 | |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1284 | template <class _Up, class _Ep, |
| 1285 | class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>, |
| 1286 | class = _EnableIfDeleterAssignable<_Ep> |
| 1287 | > |
| 1288 | _LIBCPP_INLINE_VISIBILITY |
| 1289 | unique_ptr& |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1290 | operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT { |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1291 | reset(__u.release()); |
| 1292 | __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter()); |
| 1293 | return *this; |
| 1294 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1295 | |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1296 | #ifdef _LIBCPP_CXX03_LANG |
| 1297 | unique_ptr(unique_ptr const&) = delete; |
| 1298 | unique_ptr& operator=(unique_ptr const&) = delete; |
| 1299 | #endif |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1300 | |
| 1301 | public: |
| 1302 | _LIBCPP_INLINE_VISIBILITY |
| 1303 | ~unique_ptr() { reset(); } |
| 1304 | |
| 1305 | _LIBCPP_INLINE_VISIBILITY |
| 1306 | unique_ptr& operator=(nullptr_t) _NOEXCEPT { |
| 1307 | reset(); |
| 1308 | return *this; |
| 1309 | } |
| 1310 | |
| 1311 | _LIBCPP_INLINE_VISIBILITY |
| 1312 | typename add_lvalue_reference<_Tp>::type |
| 1313 | operator[](size_t __i) const { |
| 1314 | return __ptr_.first()[__i]; |
| 1315 | } |
| 1316 | _LIBCPP_INLINE_VISIBILITY |
| 1317 | pointer get() const _NOEXCEPT { |
| 1318 | return __ptr_.first(); |
| 1319 | } |
| 1320 | |
| 1321 | _LIBCPP_INLINE_VISIBILITY |
| 1322 | deleter_type& get_deleter() _NOEXCEPT { |
| 1323 | return __ptr_.second(); |
| 1324 | } |
| 1325 | |
| 1326 | _LIBCPP_INLINE_VISIBILITY |
| 1327 | const deleter_type& get_deleter() const _NOEXCEPT { |
| 1328 | return __ptr_.second(); |
| 1329 | } |
| 1330 | _LIBCPP_INLINE_VISIBILITY |
| 1331 | _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT { |
| 1332 | return __ptr_.first() != nullptr; |
| 1333 | } |
| 1334 | |
| 1335 | _LIBCPP_INLINE_VISIBILITY |
| 1336 | pointer release() _NOEXCEPT { |
| 1337 | pointer __t = __ptr_.first(); |
| 1338 | __ptr_.first() = pointer(); |
| 1339 | return __t; |
| 1340 | } |
| 1341 | |
| 1342 | template <class _Pp> |
| 1343 | _LIBCPP_INLINE_VISIBILITY |
| 1344 | typename enable_if< |
Eric Fiselier | 31127cd | 2017-04-16 02:14:31 +0000 | [diff] [blame] | 1345 | _CheckArrayPointerConversion<_Pp>::value |
Eric Fiselier | fecf9ea | 2017-04-16 01:51:04 +0000 | [diff] [blame] | 1346 | >::type |
| 1347 | reset(_Pp __p) _NOEXCEPT { |
| 1348 | pointer __tmp = __ptr_.first(); |
| 1349 | __ptr_.first() = __p; |
| 1350 | if (__tmp) |
| 1351 | __ptr_.second()(__tmp); |
| 1352 | } |
| 1353 | |
| 1354 | _LIBCPP_INLINE_VISIBILITY |
| 1355 | void reset(nullptr_t = nullptr) _NOEXCEPT { |
| 1356 | pointer __tmp = __ptr_.first(); |
| 1357 | __ptr_.first() = nullptr; |
| 1358 | if (__tmp) |
| 1359 | __ptr_.second()(__tmp); |
| 1360 | } |
| 1361 | |
| 1362 | _LIBCPP_INLINE_VISIBILITY |
| 1363 | void swap(unique_ptr& __u) _NOEXCEPT { |
| 1364 | __ptr_.swap(__u.__ptr_); |
| 1365 | } |
| 1366 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1367 | }; |
| 1368 | |
| 1369 | template <class _Tp, class _Dp> |
| 1370 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 6bfed25 | 2016-04-21 23:38:59 +0000 | [diff] [blame] | 1371 | typename enable_if< |
| 1372 | __is_swappable<_Dp>::value, |
| 1373 | void |
| 1374 | >::type |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1375 | 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] | 1376 | |
| 1377 | template <class _T1, class _D1, class _T2, class _D2> |
| 1378 | inline _LIBCPP_INLINE_VISIBILITY |
| 1379 | bool |
| 1380 | operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();} |
| 1381 | |
| 1382 | template <class _T1, class _D1, class _T2, class _D2> |
| 1383 | inline _LIBCPP_INLINE_VISIBILITY |
| 1384 | bool |
| 1385 | operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);} |
| 1386 | |
| 1387 | template <class _T1, class _D1, class _T2, class _D2> |
| 1388 | inline _LIBCPP_INLINE_VISIBILITY |
| 1389 | bool |
Howard Hinnant | b17caf9 | 2012-02-21 21:02:58 +0000 | [diff] [blame] | 1390 | operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) |
| 1391 | { |
| 1392 | typedef typename unique_ptr<_T1, _D1>::pointer _P1; |
| 1393 | typedef typename unique_ptr<_T2, _D2>::pointer _P2; |
Eric Fiselier | f8898c8 | 2015-02-05 23:01:40 +0000 | [diff] [blame] | 1394 | typedef typename common_type<_P1, _P2>::type _Vp; |
| 1395 | return less<_Vp>()(__x.get(), __y.get()); |
Howard Hinnant | b17caf9 | 2012-02-21 21:02:58 +0000 | [diff] [blame] | 1396 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1397 | |
| 1398 | template <class _T1, class _D1, class _T2, class _D2> |
| 1399 | inline _LIBCPP_INLINE_VISIBILITY |
| 1400 | bool |
| 1401 | operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;} |
| 1402 | |
| 1403 | template <class _T1, class _D1, class _T2, class _D2> |
| 1404 | inline _LIBCPP_INLINE_VISIBILITY |
| 1405 | bool |
| 1406 | operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);} |
| 1407 | |
| 1408 | template <class _T1, class _D1, class _T2, class _D2> |
| 1409 | inline _LIBCPP_INLINE_VISIBILITY |
| 1410 | bool |
| 1411 | operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);} |
| 1412 | |
Howard Hinnant | b17caf9 | 2012-02-21 21:02:58 +0000 | [diff] [blame] | 1413 | template <class _T1, class _D1> |
| 1414 | inline _LIBCPP_INLINE_VISIBILITY |
| 1415 | bool |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 1416 | operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT |
Howard Hinnant | b17caf9 | 2012-02-21 21:02:58 +0000 | [diff] [blame] | 1417 | { |
| 1418 | return !__x; |
| 1419 | } |
| 1420 | |
| 1421 | template <class _T1, class _D1> |
| 1422 | inline _LIBCPP_INLINE_VISIBILITY |
| 1423 | bool |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 1424 | operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT |
Howard Hinnant | b17caf9 | 2012-02-21 21:02:58 +0000 | [diff] [blame] | 1425 | { |
| 1426 | return !__x; |
| 1427 | } |
| 1428 | |
| 1429 | template <class _T1, class _D1> |
| 1430 | inline _LIBCPP_INLINE_VISIBILITY |
| 1431 | bool |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 1432 | operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT |
Howard Hinnant | b17caf9 | 2012-02-21 21:02:58 +0000 | [diff] [blame] | 1433 | { |
| 1434 | return static_cast<bool>(__x); |
| 1435 | } |
| 1436 | |
| 1437 | template <class _T1, class _D1> |
| 1438 | inline _LIBCPP_INLINE_VISIBILITY |
| 1439 | bool |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 1440 | operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT |
Howard Hinnant | b17caf9 | 2012-02-21 21:02:58 +0000 | [diff] [blame] | 1441 | { |
| 1442 | return static_cast<bool>(__x); |
| 1443 | } |
| 1444 | |
| 1445 | template <class _T1, class _D1> |
| 1446 | inline _LIBCPP_INLINE_VISIBILITY |
| 1447 | bool |
| 1448 | operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t) |
| 1449 | { |
| 1450 | typedef typename unique_ptr<_T1, _D1>::pointer _P1; |
| 1451 | return less<_P1>()(__x.get(), nullptr); |
| 1452 | } |
| 1453 | |
| 1454 | template <class _T1, class _D1> |
| 1455 | inline _LIBCPP_INLINE_VISIBILITY |
| 1456 | bool |
| 1457 | operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x) |
| 1458 | { |
| 1459 | typedef typename unique_ptr<_T1, _D1>::pointer _P1; |
| 1460 | return less<_P1>()(nullptr, __x.get()); |
| 1461 | } |
| 1462 | |
| 1463 | template <class _T1, class _D1> |
| 1464 | inline _LIBCPP_INLINE_VISIBILITY |
| 1465 | bool |
| 1466 | operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t) |
| 1467 | { |
| 1468 | return nullptr < __x; |
| 1469 | } |
| 1470 | |
| 1471 | template <class _T1, class _D1> |
| 1472 | inline _LIBCPP_INLINE_VISIBILITY |
| 1473 | bool |
| 1474 | operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x) |
| 1475 | { |
| 1476 | return __x < nullptr; |
| 1477 | } |
| 1478 | |
| 1479 | template <class _T1, class _D1> |
| 1480 | inline _LIBCPP_INLINE_VISIBILITY |
| 1481 | bool |
| 1482 | operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t) |
| 1483 | { |
| 1484 | return !(nullptr < __x); |
| 1485 | } |
| 1486 | |
| 1487 | template <class _T1, class _D1> |
| 1488 | inline _LIBCPP_INLINE_VISIBILITY |
| 1489 | bool |
| 1490 | operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x) |
| 1491 | { |
| 1492 | return !(__x < nullptr); |
| 1493 | } |
| 1494 | |
| 1495 | template <class _T1, class _D1> |
| 1496 | inline _LIBCPP_INLINE_VISIBILITY |
| 1497 | bool |
| 1498 | operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t) |
| 1499 | { |
| 1500 | return !(__x < nullptr); |
| 1501 | } |
| 1502 | |
| 1503 | template <class _T1, class _D1> |
| 1504 | inline _LIBCPP_INLINE_VISIBILITY |
| 1505 | bool |
| 1506 | operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x) |
| 1507 | { |
| 1508 | return !(nullptr < __x); |
| 1509 | } |
| 1510 | |
Marshall Clow | 9e8f4a9 | 2013-07-01 18:16:03 +0000 | [diff] [blame] | 1511 | #if _LIBCPP_STD_VER > 11 |
| 1512 | |
| 1513 | template<class _Tp> |
| 1514 | struct __unique_if |
| 1515 | { |
| 1516 | typedef unique_ptr<_Tp> __unique_single; |
| 1517 | }; |
| 1518 | |
| 1519 | template<class _Tp> |
| 1520 | struct __unique_if<_Tp[]> |
| 1521 | { |
| 1522 | typedef unique_ptr<_Tp[]> __unique_array_unknown_bound; |
| 1523 | }; |
| 1524 | |
| 1525 | template<class _Tp, size_t _Np> |
| 1526 | struct __unique_if<_Tp[_Np]> |
| 1527 | { |
| 1528 | typedef void __unique_array_known_bound; |
| 1529 | }; |
| 1530 | |
| 1531 | template<class _Tp, class... _Args> |
Marshall Clow | 724e3df | 2013-07-02 20:06:09 +0000 | [diff] [blame] | 1532 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 9e8f4a9 | 2013-07-01 18:16:03 +0000 | [diff] [blame] | 1533 | typename __unique_if<_Tp>::__unique_single |
| 1534 | make_unique(_Args&&... __args) |
| 1535 | { |
| 1536 | return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...)); |
| 1537 | } |
| 1538 | |
| 1539 | template<class _Tp> |
Marshall Clow | 724e3df | 2013-07-02 20:06:09 +0000 | [diff] [blame] | 1540 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 9e8f4a9 | 2013-07-01 18:16:03 +0000 | [diff] [blame] | 1541 | typename __unique_if<_Tp>::__unique_array_unknown_bound |
| 1542 | make_unique(size_t __n) |
| 1543 | { |
| 1544 | typedef typename remove_extent<_Tp>::type _Up; |
| 1545 | return unique_ptr<_Tp>(new _Up[__n]()); |
| 1546 | } |
| 1547 | |
| 1548 | template<class _Tp, class... _Args> |
| 1549 | typename __unique_if<_Tp>::__unique_array_known_bound |
| 1550 | make_unique(_Args&&...) = delete; |
| 1551 | |
| 1552 | #endif // _LIBCPP_STD_VER > 11 |
| 1553 | |
Howard Hinnant | 36b31ae | 2010-06-03 16:42:57 +0000 | [diff] [blame] | 1554 | template <class _Tp, class _Dp> |
Eric Fiselier | 698a97b | 2017-01-21 00:02:12 +0000 | [diff] [blame] | 1555 | #ifdef _LIBCPP_CXX03_LANG |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1556 | struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> > |
Eric Fiselier | 698a97b | 2017-01-21 00:02:12 +0000 | [diff] [blame] | 1557 | #else |
| 1558 | struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper< |
Eric Fiselier | ea6fdf6 | 2019-06-23 20:47:21 +0000 | [diff] [blame] | 1559 | unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> > |
Eric Fiselier | 698a97b | 2017-01-21 00:02:12 +0000 | [diff] [blame] | 1560 | #endif |
Howard Hinnant | 36b31ae | 2010-06-03 16:42:57 +0000 | [diff] [blame] | 1561 | { |
| 1562 | typedef unique_ptr<_Tp, _Dp> argument_type; |
| 1563 | typedef size_t result_type; |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 1564 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 4903689 | 2017-03-23 02:40:28 +0000 | [diff] [blame] | 1565 | result_type operator()(const argument_type& __ptr) const |
Howard Hinnant | 36b31ae | 2010-06-03 16:42:57 +0000 | [diff] [blame] | 1566 | { |
| 1567 | typedef typename argument_type::pointer pointer; |
| 1568 | return hash<pointer>()(__ptr.get()); |
| 1569 | } |
| 1570 | }; |
| 1571 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1572 | struct __destruct_n |
| 1573 | { |
| 1574 | private: |
Eric Fiselier | c5ea1ae | 2017-06-01 02:29:37 +0000 | [diff] [blame] | 1575 | size_t __size_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1576 | |
| 1577 | template <class _Tp> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1578 | _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT |
Eric Fiselier | c5ea1ae | 2017-06-01 02:29:37 +0000 | [diff] [blame] | 1579 | {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1580 | |
| 1581 | template <class _Tp> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1582 | _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1583 | {} |
| 1584 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1585 | _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT |
Eric Fiselier | c5ea1ae | 2017-06-01 02:29:37 +0000 | [diff] [blame] | 1586 | {++__size_;} |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1587 | _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1588 | {} |
| 1589 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1590 | _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT |
Eric Fiselier | c5ea1ae | 2017-06-01 02:29:37 +0000 | [diff] [blame] | 1591 | {__size_ = __s;} |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1592 | _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1593 | {} |
| 1594 | public: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1595 | _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT |
Eric Fiselier | c5ea1ae | 2017-06-01 02:29:37 +0000 | [diff] [blame] | 1596 | : __size_(__s) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1597 | |
| 1598 | template <class _Tp> |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 1599 | _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT |
Howard Hinnant | a9a897e | 2010-11-19 22:17:28 +0000 | [diff] [blame] | 1600 | {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1601 | |
| 1602 | template <class _Tp> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1603 | _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT |
Howard Hinnant | a9a897e | 2010-11-19 22:17:28 +0000 | [diff] [blame] | 1604 | {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1605 | |
| 1606 | template <class _Tp> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1607 | _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT |
Howard Hinnant | a9a897e | 2010-11-19 22:17:28 +0000 | [diff] [blame] | 1608 | {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1609 | }; |
| 1610 | |
| 1611 | template <class _Alloc> |
| 1612 | class __allocator_destructor |
| 1613 | { |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1614 | typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1615 | public: |
Eric Fiselier | 4fc82a2 | 2019-06-12 02:03:31 +0000 | [diff] [blame] | 1616 | typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer; |
| 1617 | typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1618 | private: |
| 1619 | _Alloc& __alloc_; |
| 1620 | size_type __s_; |
| 1621 | public: |
| 1622 | _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s) |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1623 | _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1624 | : __alloc_(__a), __s_(__s) {} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 1625 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1626 | void operator()(pointer __p) _NOEXCEPT |
| 1627 | {__alloc_traits::deallocate(__alloc_, __p, __s_);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1628 | }; |
| 1629 | |
| 1630 | template <class _InputIterator, class _ForwardIterator> |
| 1631 | _ForwardIterator |
| 1632 | uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r) |
| 1633 | { |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1634 | typedef typename iterator_traits<_ForwardIterator>::value_type value_type; |
Howard Hinnant | 2fa038c | 2011-12-29 17:45:35 +0000 | [diff] [blame] | 1635 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1636 | _ForwardIterator __s = __r; |
| 1637 | try |
| 1638 | { |
| 1639 | #endif |
Marshall Clow | 5e9cb8f | 2015-05-19 15:01:48 +0000 | [diff] [blame] | 1640 | for (; __f != __l; ++__f, (void) ++__r) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 1641 | ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f); |
Howard Hinnant | 2fa038c | 2011-12-29 17:45:35 +0000 | [diff] [blame] | 1642 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1643 | } |
| 1644 | catch (...) |
| 1645 | { |
| 1646 | for (; __s != __r; ++__s) |
| 1647 | __s->~value_type(); |
| 1648 | throw; |
| 1649 | } |
| 1650 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1651 | return __r; |
| 1652 | } |
| 1653 | |
| 1654 | template <class _InputIterator, class _Size, class _ForwardIterator> |
| 1655 | _ForwardIterator |
| 1656 | uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r) |
| 1657 | { |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1658 | typedef typename iterator_traits<_ForwardIterator>::value_type value_type; |
Howard Hinnant | 2fa038c | 2011-12-29 17:45:35 +0000 | [diff] [blame] | 1659 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1660 | _ForwardIterator __s = __r; |
| 1661 | try |
| 1662 | { |
| 1663 | #endif |
Marshall Clow | 5e9cb8f | 2015-05-19 15:01:48 +0000 | [diff] [blame] | 1664 | for (; __n > 0; ++__f, (void) ++__r, (void) --__n) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 1665 | ::new ((void*)_VSTD::addressof(*__r)) value_type(*__f); |
Howard Hinnant | 2fa038c | 2011-12-29 17:45:35 +0000 | [diff] [blame] | 1666 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1667 | } |
| 1668 | catch (...) |
| 1669 | { |
| 1670 | for (; __s != __r; ++__s) |
| 1671 | __s->~value_type(); |
| 1672 | throw; |
| 1673 | } |
| 1674 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1675 | return __r; |
| 1676 | } |
| 1677 | |
| 1678 | template <class _ForwardIterator, class _Tp> |
| 1679 | void |
| 1680 | uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x) |
| 1681 | { |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1682 | typedef typename iterator_traits<_ForwardIterator>::value_type value_type; |
Howard Hinnant | 2fa038c | 2011-12-29 17:45:35 +0000 | [diff] [blame] | 1683 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1684 | _ForwardIterator __s = __f; |
| 1685 | try |
| 1686 | { |
| 1687 | #endif |
| 1688 | for (; __f != __l; ++__f) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 1689 | ::new ((void*)_VSTD::addressof(*__f)) value_type(__x); |
Howard Hinnant | 2fa038c | 2011-12-29 17:45:35 +0000 | [diff] [blame] | 1690 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1691 | } |
| 1692 | catch (...) |
| 1693 | { |
| 1694 | for (; __s != __f; ++__s) |
| 1695 | __s->~value_type(); |
| 1696 | throw; |
| 1697 | } |
| 1698 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
| 1701 | template <class _ForwardIterator, class _Size, class _Tp> |
Howard Hinnant | 3c81109 | 2010-11-18 16:13:03 +0000 | [diff] [blame] | 1702 | _ForwardIterator |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1703 | uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x) |
| 1704 | { |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1705 | typedef typename iterator_traits<_ForwardIterator>::value_type value_type; |
Howard Hinnant | 2fa038c | 2011-12-29 17:45:35 +0000 | [diff] [blame] | 1706 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1707 | _ForwardIterator __s = __f; |
| 1708 | try |
| 1709 | { |
| 1710 | #endif |
Marshall Clow | 5e9cb8f | 2015-05-19 15:01:48 +0000 | [diff] [blame] | 1711 | for (; __n > 0; ++__f, (void) --__n) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 1712 | ::new ((void*)_VSTD::addressof(*__f)) value_type(__x); |
Howard Hinnant | 2fa038c | 2011-12-29 17:45:35 +0000 | [diff] [blame] | 1713 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1714 | } |
| 1715 | catch (...) |
| 1716 | { |
| 1717 | for (; __s != __f; ++__s) |
| 1718 | __s->~value_type(); |
| 1719 | throw; |
| 1720 | } |
| 1721 | #endif |
Howard Hinnant | 3c81109 | 2010-11-18 16:13:03 +0000 | [diff] [blame] | 1722 | return __f; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
Eric Fiselier | 383f6cf | 2016-07-24 03:51:39 +0000 | [diff] [blame] | 1725 | #if _LIBCPP_STD_VER > 14 |
| 1726 | |
Eric Fiselier | 383f6cf | 2016-07-24 03:51:39 +0000 | [diff] [blame] | 1727 | template <class _ForwardIterator> |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 1728 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Eric Fiselier | 383f6cf | 2016-07-24 03:51:39 +0000 | [diff] [blame] | 1729 | void destroy(_ForwardIterator __first, _ForwardIterator __last) { |
| 1730 | for (; __first != __last; ++__first) |
| 1731 | _VSTD::destroy_at(_VSTD::addressof(*__first)); |
| 1732 | } |
| 1733 | |
| 1734 | template <class _ForwardIterator, class _Size> |
Louis Dionne | 99f5943 | 2020-09-17 12:06:13 -0400 | [diff] [blame] | 1735 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 |
Eric Fiselier | 383f6cf | 2016-07-24 03:51:39 +0000 | [diff] [blame] | 1736 | _ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) { |
| 1737 | for (; __n > 0; (void)++__first, --__n) |
| 1738 | _VSTD::destroy_at(_VSTD::addressof(*__first)); |
| 1739 | return __first; |
| 1740 | } |
| 1741 | |
Eric Fiselier | 290c07c | 2016-10-11 21:13:44 +0000 | [diff] [blame] | 1742 | template <class _ForwardIterator> |
| 1743 | inline _LIBCPP_INLINE_VISIBILITY |
| 1744 | void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) { |
| 1745 | using _Vt = typename iterator_traits<_ForwardIterator>::value_type; |
| 1746 | auto __idx = __first; |
| 1747 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1748 | try { |
| 1749 | #endif |
| 1750 | for (; __idx != __last; ++__idx) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 1751 | ::new ((void*)_VSTD::addressof(*__idx)) _Vt; |
Eric Fiselier | 290c07c | 2016-10-11 21:13:44 +0000 | [diff] [blame] | 1752 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1753 | } catch (...) { |
| 1754 | _VSTD::destroy(__first, __idx); |
| 1755 | throw; |
| 1756 | } |
| 1757 | #endif |
| 1758 | } |
| 1759 | |
| 1760 | template <class _ForwardIterator, class _Size> |
| 1761 | inline _LIBCPP_INLINE_VISIBILITY |
| 1762 | _ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) { |
| 1763 | using _Vt = typename iterator_traits<_ForwardIterator>::value_type; |
| 1764 | auto __idx = __first; |
| 1765 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1766 | try { |
| 1767 | #endif |
| 1768 | for (; __n > 0; (void)++__idx, --__n) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 1769 | ::new ((void*)_VSTD::addressof(*__idx)) _Vt; |
Eric Fiselier | 290c07c | 2016-10-11 21:13:44 +0000 | [diff] [blame] | 1770 | return __idx; |
| 1771 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1772 | } catch (...) { |
| 1773 | _VSTD::destroy(__first, __idx); |
| 1774 | throw; |
| 1775 | } |
| 1776 | #endif |
| 1777 | } |
| 1778 | |
| 1779 | |
| 1780 | template <class _ForwardIterator> |
| 1781 | inline _LIBCPP_INLINE_VISIBILITY |
| 1782 | void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) { |
| 1783 | using _Vt = typename iterator_traits<_ForwardIterator>::value_type; |
| 1784 | auto __idx = __first; |
| 1785 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1786 | try { |
| 1787 | #endif |
| 1788 | for (; __idx != __last; ++__idx) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 1789 | ::new ((void*)_VSTD::addressof(*__idx)) _Vt(); |
Eric Fiselier | 290c07c | 2016-10-11 21:13:44 +0000 | [diff] [blame] | 1790 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1791 | } catch (...) { |
| 1792 | _VSTD::destroy(__first, __idx); |
| 1793 | throw; |
| 1794 | } |
| 1795 | #endif |
| 1796 | } |
| 1797 | |
| 1798 | template <class _ForwardIterator, class _Size> |
| 1799 | inline _LIBCPP_INLINE_VISIBILITY |
| 1800 | _ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) { |
| 1801 | using _Vt = typename iterator_traits<_ForwardIterator>::value_type; |
| 1802 | auto __idx = __first; |
| 1803 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1804 | try { |
| 1805 | #endif |
| 1806 | for (; __n > 0; (void)++__idx, --__n) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 1807 | ::new ((void*)_VSTD::addressof(*__idx)) _Vt(); |
Eric Fiselier | 290c07c | 2016-10-11 21:13:44 +0000 | [diff] [blame] | 1808 | return __idx; |
| 1809 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1810 | } catch (...) { |
| 1811 | _VSTD::destroy(__first, __idx); |
| 1812 | throw; |
| 1813 | } |
| 1814 | #endif |
| 1815 | } |
| 1816 | |
| 1817 | |
| 1818 | template <class _InputIt, class _ForwardIt> |
| 1819 | inline _LIBCPP_INLINE_VISIBILITY |
| 1820 | _ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) { |
| 1821 | using _Vt = typename iterator_traits<_ForwardIt>::value_type; |
| 1822 | auto __idx = __first_res; |
| 1823 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1824 | try { |
| 1825 | #endif |
| 1826 | for (; __first != __last; (void)++__idx, ++__first) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 1827 | ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first)); |
Eric Fiselier | 290c07c | 2016-10-11 21:13:44 +0000 | [diff] [blame] | 1828 | return __idx; |
| 1829 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1830 | } catch (...) { |
| 1831 | _VSTD::destroy(__first_res, __idx); |
| 1832 | throw; |
| 1833 | } |
| 1834 | #endif |
| 1835 | } |
| 1836 | |
| 1837 | template <class _InputIt, class _Size, class _ForwardIt> |
| 1838 | inline _LIBCPP_INLINE_VISIBILITY |
| 1839 | pair<_InputIt, _ForwardIt> |
| 1840 | uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) { |
| 1841 | using _Vt = typename iterator_traits<_ForwardIt>::value_type; |
| 1842 | auto __idx = __first_res; |
| 1843 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1844 | try { |
| 1845 | #endif |
| 1846 | for (; __n > 0; ++__idx, (void)++__first, --__n) |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 1847 | ::new ((void*)_VSTD::addressof(*__idx)) _Vt(_VSTD::move(*__first)); |
Eric Fiselier | 290c07c | 2016-10-11 21:13:44 +0000 | [diff] [blame] | 1848 | return {__first, __idx}; |
| 1849 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1850 | } catch (...) { |
| 1851 | _VSTD::destroy(__first_res, __idx); |
| 1852 | throw; |
| 1853 | } |
| 1854 | #endif |
| 1855 | } |
| 1856 | |
| 1857 | |
Eric Fiselier | 383f6cf | 2016-07-24 03:51:39 +0000 | [diff] [blame] | 1858 | #endif // _LIBCPP_STD_VER > 14 |
| 1859 | |
Kevin Hu | 4bdc8a0 | 2017-01-17 02:46:33 +0000 | [diff] [blame] | 1860 | // NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively) |
| 1861 | // should be sufficient for thread safety. |
Louis Dionne | 3843740 | 2021-03-03 13:45:06 -0500 | [diff] [blame] | 1862 | // See https://llvm.org/PR22803 |
Kevin Hu | 4bdc8a0 | 2017-01-17 02:46:33 +0000 | [diff] [blame] | 1863 | #if defined(__clang__) && __has_builtin(__atomic_add_fetch) \ |
| 1864 | && defined(__ATOMIC_RELAXED) \ |
| 1865 | && defined(__ATOMIC_ACQ_REL) |
| 1866 | # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT |
Richard Smith | ca47d0f | 2019-04-25 20:02:10 +0000 | [diff] [blame] | 1867 | #elif defined(_LIBCPP_COMPILER_GCC) |
Kevin Hu | 4bdc8a0 | 2017-01-17 02:46:33 +0000 | [diff] [blame] | 1868 | # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT |
| 1869 | #endif |
| 1870 | |
| 1871 | template <class _Tp> |
| 1872 | inline _LIBCPP_INLINE_VISIBILITY _Tp |
| 1873 | __libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT |
| 1874 | { |
| 1875 | #if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS) |
| 1876 | return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED); |
| 1877 | #else |
| 1878 | return __t += 1; |
| 1879 | #endif |
| 1880 | } |
| 1881 | |
| 1882 | template <class _Tp> |
| 1883 | inline _LIBCPP_INLINE_VISIBILITY _Tp |
| 1884 | __libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT |
| 1885 | { |
| 1886 | #if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS) |
| 1887 | return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL); |
| 1888 | #else |
| 1889 | return __t -= 1; |
| 1890 | #endif |
| 1891 | } |
| 1892 | |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 1893 | class _LIBCPP_EXCEPTION_ABI bad_weak_ptr |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1894 | : public std::exception |
| 1895 | { |
| 1896 | public: |
Dimitry Andric | 47269ce | 2020-03-13 19:36:26 +0100 | [diff] [blame] | 1897 | bad_weak_ptr() _NOEXCEPT = default; |
| 1898 | bad_weak_ptr(const bad_weak_ptr&) _NOEXCEPT = default; |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1899 | virtual ~bad_weak_ptr() _NOEXCEPT; |
| 1900 | virtual const char* what() const _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1901 | }; |
| 1902 | |
Louis Dionne | 16fe295 | 2018-07-11 23:14:33 +0000 | [diff] [blame] | 1903 | _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 8fea161 | 2016-08-25 15:09:01 +0000 | [diff] [blame] | 1904 | void __throw_bad_weak_ptr() |
| 1905 | { |
| 1906 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1907 | throw bad_weak_ptr(); |
| 1908 | #else |
| 1909 | _VSTD::abort(); |
| 1910 | #endif |
| 1911 | } |
| 1912 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 1913 | template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1914 | |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 1915 | class _LIBCPP_TYPE_VIS __shared_count |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1916 | { |
| 1917 | __shared_count(const __shared_count&); |
| 1918 | __shared_count& operator=(const __shared_count&); |
| 1919 | |
| 1920 | protected: |
| 1921 | long __shared_owners_; |
| 1922 | virtual ~__shared_count(); |
| 1923 | private: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1924 | virtual void __on_zero_shared() _NOEXCEPT = 0; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1925 | |
| 1926 | public: |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 1927 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1928 | explicit __shared_count(long __refs = 0) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1929 | : __shared_owners_(__refs) {} |
| 1930 | |
Louis Dionne | 5e0eadd | 2018-08-01 02:08:59 +0000 | [diff] [blame] | 1931 | #if defined(_LIBCPP_BUILDING_LIBRARY) && \ |
Eric Fiselier | 9884857 | 2017-01-17 03:16:26 +0000 | [diff] [blame] | 1932 | defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS) |
Eric Fiselier | e0700ff | 2017-01-17 03:05:31 +0000 | [diff] [blame] | 1933 | void __add_shared() _NOEXCEPT; |
| 1934 | bool __release_shared() _NOEXCEPT; |
Kevin Hu | 4bdc8a0 | 2017-01-17 02:46:33 +0000 | [diff] [blame] | 1935 | #else |
| 1936 | _LIBCPP_INLINE_VISIBILITY |
| 1937 | void __add_shared() _NOEXCEPT { |
| 1938 | __libcpp_atomic_refcount_increment(__shared_owners_); |
| 1939 | } |
| 1940 | _LIBCPP_INLINE_VISIBILITY |
| 1941 | bool __release_shared() _NOEXCEPT { |
| 1942 | if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) { |
| 1943 | __on_zero_shared(); |
| 1944 | return true; |
| 1945 | } |
| 1946 | return false; |
| 1947 | } |
| 1948 | #endif |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 1949 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 89659d1 | 2015-07-07 00:27:16 +0000 | [diff] [blame] | 1950 | long use_count() const _NOEXCEPT { |
| 1951 | return __libcpp_relaxed_load(&__shared_owners_) + 1; |
| 1952 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1953 | }; |
| 1954 | |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 1955 | class _LIBCPP_TYPE_VIS __shared_weak_count |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1956 | : private __shared_count |
| 1957 | { |
| 1958 | long __shared_weak_owners_; |
| 1959 | |
| 1960 | public: |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 1961 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1962 | explicit __shared_weak_count(long __refs = 0) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1963 | : __shared_count(__refs), |
| 1964 | __shared_weak_owners_(__refs) {} |
| 1965 | protected: |
| 1966 | virtual ~__shared_weak_count(); |
| 1967 | |
| 1968 | public: |
Louis Dionne | 5e0eadd | 2018-08-01 02:08:59 +0000 | [diff] [blame] | 1969 | #if defined(_LIBCPP_BUILDING_LIBRARY) && \ |
Eric Fiselier | 9884857 | 2017-01-17 03:16:26 +0000 | [diff] [blame] | 1970 | defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS) |
Eric Fiselier | e0700ff | 2017-01-17 03:05:31 +0000 | [diff] [blame] | 1971 | void __add_shared() _NOEXCEPT; |
| 1972 | void __add_weak() _NOEXCEPT; |
| 1973 | void __release_shared() _NOEXCEPT; |
Kevin Hu | 4bdc8a0 | 2017-01-17 02:46:33 +0000 | [diff] [blame] | 1974 | #else |
| 1975 | _LIBCPP_INLINE_VISIBILITY |
| 1976 | void __add_shared() _NOEXCEPT { |
| 1977 | __shared_count::__add_shared(); |
| 1978 | } |
| 1979 | _LIBCPP_INLINE_VISIBILITY |
| 1980 | void __add_weak() _NOEXCEPT { |
| 1981 | __libcpp_atomic_refcount_increment(__shared_weak_owners_); |
| 1982 | } |
| 1983 | _LIBCPP_INLINE_VISIBILITY |
| 1984 | void __release_shared() _NOEXCEPT { |
| 1985 | if (__shared_count::__release_shared()) |
| 1986 | __release_weak(); |
| 1987 | } |
| 1988 | #endif |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1989 | void __release_weak() _NOEXCEPT; |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 1990 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1991 | long use_count() const _NOEXCEPT {return __shared_count::use_count();} |
| 1992 | __shared_weak_count* lock() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1993 | |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1994 | virtual const void* __get_deleter(const type_info&) const _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1995 | private: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 1996 | virtual void __on_zero_shared_weak() _NOEXCEPT = 0; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1997 | }; |
| 1998 | |
| 1999 | template <class _Tp, class _Dp, class _Alloc> |
| 2000 | class __shared_ptr_pointer |
| 2001 | : public __shared_weak_count |
| 2002 | { |
| 2003 | __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_; |
| 2004 | public: |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2005 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2006 | __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a) |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2007 | : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2008 | |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 2009 | #ifndef _LIBCPP_NO_RTTI |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2010 | virtual const void* __get_deleter(const type_info&) const _NOEXCEPT; |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 2011 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2012 | |
| 2013 | private: |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2014 | virtual void __on_zero_shared() _NOEXCEPT; |
| 2015 | virtual void __on_zero_shared_weak() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2016 | }; |
| 2017 | |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 2018 | #ifndef _LIBCPP_NO_RTTI |
| 2019 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2020 | template <class _Tp, class _Dp, class _Alloc> |
| 2021 | const void* |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2022 | __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] | 2023 | { |
Eric Fiselier | c7490d0 | 2017-05-04 01:06:56 +0000 | [diff] [blame] | 2024 | return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2027 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 2028 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2029 | template <class _Tp, class _Dp, class _Alloc> |
| 2030 | void |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2031 | __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2032 | { |
| 2033 | __data_.first().second()(__data_.first().first()); |
| 2034 | __data_.first().second().~_Dp(); |
| 2035 | } |
| 2036 | |
| 2037 | template <class _Tp, class _Dp, class _Alloc> |
| 2038 | void |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2039 | __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2040 | { |
Eric Fiselier | f8898c8 | 2015-02-05 23:01:40 +0000 | [diff] [blame] | 2041 | typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al; |
| 2042 | typedef allocator_traits<_Al> _ATraits; |
Eric Fiselier | 6bd814f | 2014-10-23 04:12:28 +0000 | [diff] [blame] | 2043 | typedef pointer_traits<typename _ATraits::pointer> _PTraits; |
| 2044 | |
Eric Fiselier | f8898c8 | 2015-02-05 23:01:40 +0000 | [diff] [blame] | 2045 | _Al __a(__data_.second()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2046 | __data_.second().~_Alloc(); |
Eric Fiselier | 6bd814f | 2014-10-23 04:12:28 +0000 | [diff] [blame] | 2047 | __a.deallocate(_PTraits::pointer_to(*this), 1); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | template <class _Tp, class _Alloc> |
Louis Dionne | 86549d7 | 2020-12-09 16:22:17 -0500 | [diff] [blame] | 2051 | struct __shared_ptr_emplace |
| 2052 | : __shared_weak_count |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2053 | { |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 2054 | template<class ..._Args> |
Louis Dionne | 86549d7 | 2020-12-09 16:22:17 -0500 | [diff] [blame] | 2055 | _LIBCPP_HIDE_FROM_ABI |
| 2056 | explicit __shared_ptr_emplace(_Alloc __a, _Args&& ...__args) |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 2057 | : __storage_(_VSTD::move(__a)) |
| 2058 | { |
| 2059 | #if _LIBCPP_STD_VER > 17 |
| 2060 | using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type; |
| 2061 | _TpAlloc __tmp(*__get_alloc()); |
| 2062 | allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...); |
Louis Dionne | 1a1fc9d | 2020-07-30 10:00:53 -0400 | [diff] [blame] | 2063 | #else |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 2064 | ::new ((void*)__get_elem()) _Tp(_VSTD::forward<_Args>(__args)...); |
Louis Dionne | 1a1fc9d | 2020-07-30 10:00:53 -0400 | [diff] [blame] | 2065 | #endif |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 2066 | } |
Louis Dionne | 86549d7 | 2020-12-09 16:22:17 -0500 | [diff] [blame] | 2067 | |
| 2068 | _LIBCPP_HIDE_FROM_ABI |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 2069 | _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); } |
Louis Dionne | 86549d7 | 2020-12-09 16:22:17 -0500 | [diff] [blame] | 2070 | |
| 2071 | _LIBCPP_HIDE_FROM_ABI |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 2072 | _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2073 | |
| 2074 | private: |
Louis Dionne | 86549d7 | 2020-12-09 16:22:17 -0500 | [diff] [blame] | 2075 | virtual void __on_zero_shared() _NOEXCEPT { |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 2076 | #if _LIBCPP_STD_VER > 17 |
| 2077 | using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type; |
| 2078 | _TpAlloc __tmp(*__get_alloc()); |
| 2079 | allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem()); |
| 2080 | #else |
Louis Dionne | 86549d7 | 2020-12-09 16:22:17 -0500 | [diff] [blame] | 2081 | __get_elem()->~_Tp(); |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 2082 | #endif |
Louis Dionne | 86549d7 | 2020-12-09 16:22:17 -0500 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | virtual void __on_zero_shared_weak() _NOEXCEPT { |
| 2086 | using _ControlBlockAlloc = typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type; |
| 2087 | using _ControlBlockPointer = typename allocator_traits<_ControlBlockAlloc>::pointer; |
Louis Dionne | ca117a2 | 2020-12-14 17:40:56 -0500 | [diff] [blame] | 2088 | _ControlBlockAlloc __tmp(*__get_alloc()); |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 2089 | __storage_.~_Storage(); |
Louis Dionne | 86549d7 | 2020-12-09 16:22:17 -0500 | [diff] [blame] | 2090 | allocator_traits<_ControlBlockAlloc>::deallocate(__tmp, |
| 2091 | pointer_traits<_ControlBlockPointer>::pointer_to(*this), 1); |
| 2092 | } |
| 2093 | |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 2094 | // This class implements the control block for non-array shared pointers created |
| 2095 | // through `std::allocate_shared` and `std::make_shared`. |
| 2096 | // |
| 2097 | // In previous versions of the library, we used a compressed pair to store |
| 2098 | // both the _Alloc and the _Tp. This implies using EBO, which is incompatible |
| 2099 | // with Allocator construction for _Tp. To allow implementing P0674 in C++20, |
| 2100 | // we now use a properly aligned char buffer while making sure that we maintain |
| 2101 | // the same layout that we had when we used a compressed pair. |
| 2102 | using _CompressedPair = __compressed_pair<_Alloc, _Tp>; |
| 2103 | struct _ALIGNAS_TYPE(_CompressedPair) _Storage { |
| 2104 | char __blob_[sizeof(_CompressedPair)]; |
| 2105 | |
| 2106 | _LIBCPP_HIDE_FROM_ABI explicit _Storage(_Alloc&& __a) { |
| 2107 | ::new ((void*)__get_alloc()) _Alloc(_VSTD::move(__a)); |
| 2108 | } |
| 2109 | _LIBCPP_HIDE_FROM_ABI ~_Storage() { |
| 2110 | __get_alloc()->~_Alloc(); |
| 2111 | } |
| 2112 | _Alloc* __get_alloc() _NOEXCEPT { |
| 2113 | _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_); |
| 2114 | typename _CompressedPair::_Base1* __first = _CompressedPair::__get_first_base(__as_pair); |
| 2115 | _Alloc *__alloc = reinterpret_cast<_Alloc*>(__first); |
| 2116 | return __alloc; |
| 2117 | } |
Reid Kleckner | a43e87e | 2021-02-01 15:18:42 -0800 | [diff] [blame] | 2118 | _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT { |
Louis Dionne | da463cb | 2020-12-11 12:22:16 -0500 | [diff] [blame] | 2119 | _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_); |
| 2120 | typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair); |
| 2121 | _Tp *__elem = reinterpret_cast<_Tp*>(__second); |
| 2122 | return __elem; |
| 2123 | } |
| 2124 | }; |
| 2125 | |
| 2126 | static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), ""); |
| 2127 | static_assert(sizeof(_Storage) == sizeof(_CompressedPair), ""); |
| 2128 | _Storage __storage_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2129 | }; |
| 2130 | |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 2131 | struct __shared_ptr_dummy_rebind_allocator_type; |
| 2132 | template <> |
| 2133 | class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type> |
| 2134 | { |
| 2135 | public: |
| 2136 | template <class _Other> |
| 2137 | struct rebind |
| 2138 | { |
| 2139 | typedef allocator<_Other> other; |
| 2140 | }; |
| 2141 | }; |
| 2142 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2143 | template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2144 | |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2145 | template<class _Tp, class _Up> |
| 2146 | struct __compatible_with |
| 2147 | #if _LIBCPP_STD_VER > 14 |
| 2148 | : is_convertible<remove_extent_t<_Tp>*, remove_extent_t<_Up>*> {}; |
| 2149 | #else |
| 2150 | : is_convertible<_Tp*, _Up*> {}; |
| 2151 | #endif // _LIBCPP_STD_VER > 14 |
| 2152 | |
zoecarver | 9bc3910 | 2021-02-19 11:10:36 -0800 | [diff] [blame] | 2153 | template <class _Dp, class _Pt, |
| 2154 | class = decltype(_VSTD::declval<_Dp>()(_VSTD::declval<_Pt>()))> |
| 2155 | static true_type __well_formed_deleter_test(int); |
| 2156 | |
| 2157 | template <class, class> |
| 2158 | static false_type __well_formed_deleter_test(...); |
| 2159 | |
| 2160 | template <class _Dp, class _Pt> |
| 2161 | struct __well_formed_deleter : decltype(__well_formed_deleter_test<_Dp, _Pt>(0)) {}; |
| 2162 | |
| 2163 | template<class _Dp, class _Tp, class _Yp> |
| 2164 | struct __shared_ptr_deleter_ctor_reqs |
| 2165 | { |
| 2166 | static const bool value = __compatible_with<_Tp, _Yp>::value && |
| 2167 | is_move_constructible<_Dp>::value && |
| 2168 | __well_formed_deleter<_Dp, _Tp*>::value; |
| 2169 | }; |
| 2170 | |
Vy Nguyen | e369bd9 | 2020-07-13 12:34:37 -0400 | [diff] [blame] | 2171 | #if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI) |
| 2172 | # define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((trivial_abi)) |
| 2173 | #else |
| 2174 | # define _LIBCPP_SHARED_PTR_TRIVIAL_ABI |
| 2175 | #endif |
| 2176 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2177 | template<class _Tp> |
Vy Nguyen | e369bd9 | 2020-07-13 12:34:37 -0400 | [diff] [blame] | 2178 | class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2179 | { |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2180 | public: |
Eric Fiselier | ae5b667 | 2016-06-27 01:02:43 +0000 | [diff] [blame] | 2181 | #if _LIBCPP_STD_VER > 14 |
| 2182 | typedef weak_ptr<_Tp> weak_type; |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2183 | typedef remove_extent_t<_Tp> element_type; |
| 2184 | #else |
| 2185 | typedef _Tp element_type; |
Eric Fiselier | ae5b667 | 2016-06-27 01:02:43 +0000 | [diff] [blame] | 2186 | #endif |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2187 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2188 | private: |
| 2189 | element_type* __ptr_; |
| 2190 | __shared_weak_count* __cntrl_; |
| 2191 | |
| 2192 | struct __nat {int __for_bool_;}; |
| 2193 | public: |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2194 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 2195 | _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2196 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 2197 | _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT; |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2198 | template<class _Yp> |
| 2199 | explicit shared_ptr(_Yp* __p, |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2200 | typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat()); |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2201 | template<class _Yp, class _Dp> |
| 2202 | shared_ptr(_Yp* __p, _Dp __d, |
zoecarver | 9bc3910 | 2021-02-19 11:10:36 -0800 | [diff] [blame] | 2203 | 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] | 2204 | template<class _Yp, class _Dp, class _Alloc> |
| 2205 | shared_ptr(_Yp* __p, _Dp __d, _Alloc __a, |
zoecarver | 9bc3910 | 2021-02-19 11:10:36 -0800 | [diff] [blame] | 2206 | 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] | 2207 | template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d); |
| 2208 | 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] | 2209 | template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT; |
| 2210 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2211 | shared_ptr(const shared_ptr& __r) _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2212 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2213 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2214 | shared_ptr(const shared_ptr<_Yp>& __r, |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2215 | typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat()) |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2216 | _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2217 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2218 | shared_ptr(shared_ptr&& __r) _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2219 | template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r, |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2220 | typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type = __nat()) |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2221 | _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2222 | template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r, |
Marshall Clow | 7e384b7 | 2017-01-10 16:59:33 +0000 | [diff] [blame] | 2223 | typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat()); |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 2224 | #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2225 | template<class _Yp> |
| 2226 | shared_ptr(auto_ptr<_Yp>&& __r, |
| 2227 | typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat()); |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 2228 | #endif |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2229 | template <class _Yp, class _Dp> |
| 2230 | shared_ptr(unique_ptr<_Yp, _Dp>&&, |
| 2231 | typename enable_if |
| 2232 | < |
| 2233 | !is_lvalue_reference<_Dp>::value && |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2234 | is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value, |
| 2235 | __nat |
| 2236 | >::type = __nat()); |
| 2237 | template <class _Yp, class _Dp> |
| 2238 | shared_ptr(unique_ptr<_Yp, _Dp>&&, |
| 2239 | typename enable_if |
| 2240 | < |
| 2241 | is_lvalue_reference<_Dp>::value && |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2242 | is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value, |
| 2243 | __nat |
| 2244 | >::type = __nat()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2245 | |
| 2246 | ~shared_ptr(); |
| 2247 | |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2248 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2249 | shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT; |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2250 | template<class _Yp> |
| 2251 | typename enable_if |
| 2252 | < |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2253 | __compatible_with<_Yp, element_type>::value, |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2254 | shared_ptr& |
| 2255 | >::type |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2256 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2257 | operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2258 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2259 | shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT; |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2260 | template<class _Yp> |
| 2261 | typename enable_if |
| 2262 | < |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2263 | __compatible_with<_Yp, element_type>::value, |
| 2264 | shared_ptr& |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2265 | >::type |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2266 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2267 | operator=(shared_ptr<_Yp>&& __r); |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 2268 | #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2269 | template<class _Yp> |
Eric Fiselier | 6585c75 | 2016-04-21 22:54:21 +0000 | [diff] [blame] | 2270 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2271 | typename enable_if |
| 2272 | < |
| 2273 | !is_array<_Yp>::value && |
| 2274 | is_convertible<_Yp*, element_type*>::value, |
Howard Hinnant | 25bcaf6 | 2013-09-13 23:56:00 +0000 | [diff] [blame] | 2275 | shared_ptr |
| 2276 | >::type& |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2277 | operator=(auto_ptr<_Yp>&& __r); |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 2278 | #endif |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2279 | template <class _Yp, class _Dp> |
| 2280 | typename enable_if |
| 2281 | < |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2282 | is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value, |
| 2283 | shared_ptr& |
| 2284 | >::type |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2285 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2286 | operator=(unique_ptr<_Yp, _Dp>&& __r); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2287 | |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2288 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2289 | void swap(shared_ptr& __r) _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2290 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2291 | void reset() _NOEXCEPT; |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2292 | template<class _Yp> |
| 2293 | typename enable_if |
| 2294 | < |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2295 | __compatible_with<_Yp, element_type>::value, |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2296 | void |
| 2297 | >::type |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2298 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2299 | reset(_Yp* __p); |
| 2300 | template<class _Yp, class _Dp> |
| 2301 | typename enable_if |
| 2302 | < |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2303 | __compatible_with<_Yp, element_type>::value, |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2304 | void |
| 2305 | >::type |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2306 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2307 | reset(_Yp* __p, _Dp __d); |
| 2308 | template<class _Yp, class _Dp, class _Alloc> |
| 2309 | typename enable_if |
| 2310 | < |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2311 | __compatible_with<_Yp, element_type>::value, |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2312 | void |
| 2313 | >::type |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2314 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2315 | reset(_Yp* __p, _Dp __d, _Alloc __a); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2316 | |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2317 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2318 | element_type* get() const _NOEXCEPT {return __ptr_;} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2319 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2320 | typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT |
| 2321 | {return *__ptr_;} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2322 | _LIBCPP_INLINE_VISIBILITY |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2323 | element_type* operator->() const _NOEXCEPT |
| 2324 | { |
| 2325 | static_assert(!_VSTD::is_array<_Tp>::value, |
| 2326 | "std::shared_ptr<T>::operator-> is only valid when T is not an array type."); |
| 2327 | return __ptr_; |
| 2328 | } |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2329 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2330 | long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2331 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2332 | bool unique() const _NOEXCEPT {return use_count() == 1;} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2333 | _LIBCPP_INLINE_VISIBILITY |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 2334 | _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;} |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2335 | template <class _Up> |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2336 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 2337 | bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2338 | {return __cntrl_ < __p.__cntrl_;} |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 2339 | template <class _Up> |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2340 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 2341 | bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2342 | {return __cntrl_ < __p.__cntrl_;} |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 2343 | _LIBCPP_INLINE_VISIBILITY |
| 2344 | bool |
| 2345 | __owner_equivalent(const shared_ptr& __p) const |
| 2346 | {return __cntrl_ == __p.__cntrl_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2347 | |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2348 | #if _LIBCPP_STD_VER > 14 |
| 2349 | typename add_lvalue_reference<element_type>::type |
| 2350 | _LIBCPP_INLINE_VISIBILITY |
| 2351 | operator[](ptrdiff_t __i) const |
| 2352 | { |
| 2353 | static_assert(_VSTD::is_array<_Tp>::value, |
| 2354 | "std::shared_ptr<T>::operator[] is only valid when T is an array type."); |
| 2355 | return __ptr_[__i]; |
| 2356 | } |
| 2357 | #endif |
| 2358 | |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 2359 | #ifndef _LIBCPP_NO_RTTI |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2360 | template <class _Dp> |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2361 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2362 | _Dp* __get_deleter() const _NOEXCEPT |
Marshall Clow | 31350ab | 2017-06-14 16:54:43 +0000 | [diff] [blame] | 2363 | {return static_cast<_Dp*>(__cntrl_ |
Aditya Kumar | 7c5db69 | 2017-08-20 10:38:55 +0000 | [diff] [blame] | 2364 | ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp))) |
Marshall Clow | 31350ab | 2017-06-14 16:54:43 +0000 | [diff] [blame] | 2365 | : nullptr);} |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2366 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2367 | |
Zoe Carver | d9040c7 | 2019-10-22 15:16:49 +0000 | [diff] [blame] | 2368 | template<class _Yp, class _CntrlBlk> |
| 2369 | static shared_ptr<_Tp> |
zoecarver | 867d311 | 2020-05-19 17:17:16 -0700 | [diff] [blame] | 2370 | __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl) _NOEXCEPT |
Zoe Carver | d9040c7 | 2019-10-22 15:16:49 +0000 | [diff] [blame] | 2371 | { |
| 2372 | shared_ptr<_Tp> __r; |
| 2373 | __r.__ptr_ = __p; |
| 2374 | __r.__cntrl_ = __cntrl; |
| 2375 | __r.__enable_weak_this(__r.__ptr_, __r.__ptr_); |
| 2376 | return __r; |
| 2377 | } |
Zoe Carver | 6cd05c3 | 2019-08-19 15:47:16 +0000 | [diff] [blame] | 2378 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2379 | private: |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 2380 | template <class _Yp, bool = is_function<_Yp>::value> |
| 2381 | struct __shared_ptr_default_allocator |
| 2382 | { |
| 2383 | typedef allocator<_Yp> type; |
| 2384 | }; |
| 2385 | |
| 2386 | template <class _Yp> |
| 2387 | struct __shared_ptr_default_allocator<_Yp, true> |
| 2388 | { |
| 2389 | typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type; |
| 2390 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2391 | |
Eric Fiselier | f16c93f | 2016-06-26 23:56:32 +0000 | [diff] [blame] | 2392 | template <class _Yp, class _OrigPtr> |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 2393 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 30d5ac6 | 2017-05-10 19:35:49 +0000 | [diff] [blame] | 2394 | typename enable_if<is_convertible<_OrigPtr*, |
| 2395 | const enable_shared_from_this<_Yp>* |
| 2396 | >::value, |
| 2397 | void>::type |
Eric Fiselier | f16c93f | 2016-06-26 23:56:32 +0000 | [diff] [blame] | 2398 | __enable_weak_this(const enable_shared_from_this<_Yp>* __e, |
| 2399 | _OrigPtr* __ptr) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2400 | { |
Eric Fiselier | f16c93f | 2016-06-26 23:56:32 +0000 | [diff] [blame] | 2401 | typedef typename remove_cv<_Yp>::type _RawYp; |
Eric Fiselier | 8400686 | 2016-06-02 00:15:35 +0000 | [diff] [blame] | 2402 | if (__e && __e->__weak_this_.expired()) |
Marshall Clow | 99442fc | 2015-06-19 15:54:13 +0000 | [diff] [blame] | 2403 | { |
Eric Fiselier | f16c93f | 2016-06-26 23:56:32 +0000 | [diff] [blame] | 2404 | __e->__weak_this_ = shared_ptr<_RawYp>(*this, |
| 2405 | const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr))); |
Marshall Clow | 99442fc | 2015-06-19 15:54:13 +0000 | [diff] [blame] | 2406 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2407 | } |
| 2408 | |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 2409 | _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2410 | |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2411 | template <class, class _Yp> |
| 2412 | struct __shared_ptr_default_delete |
| 2413 | : default_delete<_Yp> {}; |
| 2414 | |
| 2415 | template <class _Yp, class _Un, size_t _Sz> |
| 2416 | struct __shared_ptr_default_delete<_Yp[_Sz], _Un> |
| 2417 | : default_delete<_Yp[]> {}; |
| 2418 | |
| 2419 | template <class _Yp, class _Un> |
| 2420 | struct __shared_ptr_default_delete<_Yp[], _Un> |
| 2421 | : default_delete<_Yp[]> {}; |
| 2422 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 2423 | template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr; |
| 2424 | template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2425 | }; |
| 2426 | |
Logan Smith | a5e4d7e | 2020-05-07 12:07:01 -0400 | [diff] [blame] | 2427 | #ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES |
| 2428 | template<class _Tp> |
| 2429 | shared_ptr(weak_ptr<_Tp>) -> shared_ptr<_Tp>; |
| 2430 | template<class _Tp, class _Dp> |
| 2431 | shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>; |
| 2432 | #endif |
Eric Fiselier | 30d5ac6 | 2017-05-10 19:35:49 +0000 | [diff] [blame] | 2433 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2434 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2435 | inline |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 2436 | _LIBCPP_CONSTEXPR |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2437 | shared_ptr<_Tp>::shared_ptr() _NOEXCEPT |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 2438 | : __ptr_(nullptr), |
| 2439 | __cntrl_(nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2440 | { |
| 2441 | } |
| 2442 | |
| 2443 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2444 | inline |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 2445 | _LIBCPP_CONSTEXPR |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2446 | shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 2447 | : __ptr_(nullptr), |
| 2448 | __cntrl_(nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2449 | { |
| 2450 | } |
| 2451 | |
| 2452 | template<class _Tp> |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2453 | template<class _Yp> |
| 2454 | shared_ptr<_Tp>::shared_ptr(_Yp* __p, |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2455 | typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2456 | : __ptr_(__p) |
| 2457 | { |
| 2458 | unique_ptr<_Yp> __hold(__p); |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 2459 | typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2460 | typedef __shared_ptr_pointer<_Yp*, __shared_ptr_default_delete<_Tp, _Yp>, _AllocT > _CntrlBlk; |
| 2461 | __cntrl_ = new _CntrlBlk(__p, __shared_ptr_default_delete<_Tp, _Yp>(), _AllocT()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2462 | __hold.release(); |
Eric Fiselier | f16c93f | 2016-06-26 23:56:32 +0000 | [diff] [blame] | 2463 | __enable_weak_this(__p, __p); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
| 2466 | template<class _Tp> |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2467 | template<class _Yp, class _Dp> |
| 2468 | shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, |
zoecarver | 9bc3910 | 2021-02-19 11:10:36 -0800 | [diff] [blame] | 2469 | 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] | 2470 | : __ptr_(__p) |
| 2471 | { |
| 2472 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2473 | try |
| 2474 | { |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2475 | #endif // _LIBCPP_NO_EXCEPTIONS |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 2476 | typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; |
| 2477 | typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk; |
zoecarver | bec93cf | 2021-02-18 21:31:07 -0800 | [diff] [blame] | 2478 | #ifndef _LIBCPP_CXX03_LANG |
| 2479 | __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT()); |
| 2480 | #else |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 2481 | __cntrl_ = new _CntrlBlk(__p, __d, _AllocT()); |
zoecarver | bec93cf | 2021-02-18 21:31:07 -0800 | [diff] [blame] | 2482 | #endif // not _LIBCPP_CXX03_LANG |
Eric Fiselier | f16c93f | 2016-06-26 23:56:32 +0000 | [diff] [blame] | 2483 | __enable_weak_this(__p, __p); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2484 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2485 | } |
| 2486 | catch (...) |
| 2487 | { |
| 2488 | __d(__p); |
| 2489 | throw; |
| 2490 | } |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2491 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2492 | } |
| 2493 | |
| 2494 | template<class _Tp> |
| 2495 | template<class _Dp> |
| 2496 | shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d) |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 2497 | : __ptr_(nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2498 | { |
| 2499 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2500 | try |
| 2501 | { |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2502 | #endif // _LIBCPP_NO_EXCEPTIONS |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 2503 | typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT; |
| 2504 | typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk; |
zoecarver | bec93cf | 2021-02-18 21:31:07 -0800 | [diff] [blame] | 2505 | #ifndef _LIBCPP_CXX03_LANG |
| 2506 | __cntrl_ = new _CntrlBlk(__p, _VSTD::move(__d), _AllocT()); |
| 2507 | #else |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 2508 | __cntrl_ = new _CntrlBlk(__p, __d, _AllocT()); |
zoecarver | bec93cf | 2021-02-18 21:31:07 -0800 | [diff] [blame] | 2509 | #endif // not _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2510 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2511 | } |
| 2512 | catch (...) |
| 2513 | { |
| 2514 | __d(__p); |
| 2515 | throw; |
| 2516 | } |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2517 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
| 2520 | template<class _Tp> |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2521 | template<class _Yp, class _Dp, class _Alloc> |
| 2522 | shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a, |
zoecarver | 9bc3910 | 2021-02-19 11:10:36 -0800 | [diff] [blame] | 2523 | 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] | 2524 | : __ptr_(__p) |
| 2525 | { |
| 2526 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2527 | try |
| 2528 | { |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2529 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2530 | typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk; |
Eric Fiselier | 6bd814f | 2014-10-23 04:12:28 +0000 | [diff] [blame] | 2531 | typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2532 | typedef __allocator_destructor<_A2> _D2; |
| 2533 | _A2 __a2(__a); |
| 2534 | unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1)); |
zoecarver | bec93cf | 2021-02-18 21:31:07 -0800 | [diff] [blame] | 2535 | ::new ((void*)_VSTD::addressof(*__hold2.get())) |
| 2536 | #ifndef _LIBCPP_CXX03_LANG |
| 2537 | _CntrlBlk(__p, _VSTD::move(__d), __a); |
| 2538 | #else |
| 2539 | _CntrlBlk(__p, __d, __a); |
| 2540 | #endif // not _LIBCPP_CXX03_LANG |
Eric Fiselier | 6bd814f | 2014-10-23 04:12:28 +0000 | [diff] [blame] | 2541 | __cntrl_ = _VSTD::addressof(*__hold2.release()); |
Eric Fiselier | f16c93f | 2016-06-26 23:56:32 +0000 | [diff] [blame] | 2542 | __enable_weak_this(__p, __p); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2543 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2544 | } |
| 2545 | catch (...) |
| 2546 | { |
| 2547 | __d(__p); |
| 2548 | throw; |
| 2549 | } |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2550 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
| 2553 | template<class _Tp> |
| 2554 | template<class _Dp, class _Alloc> |
| 2555 | shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a) |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 2556 | : __ptr_(nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2557 | { |
| 2558 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2559 | try |
| 2560 | { |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2561 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2562 | typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk; |
Eric Fiselier | 6bd814f | 2014-10-23 04:12:28 +0000 | [diff] [blame] | 2563 | typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2564 | typedef __allocator_destructor<_A2> _D2; |
| 2565 | _A2 __a2(__a); |
| 2566 | unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1)); |
zoecarver | bec93cf | 2021-02-18 21:31:07 -0800 | [diff] [blame] | 2567 | ::new ((void*)_VSTD::addressof(*__hold2.get())) |
| 2568 | #ifndef _LIBCPP_CXX03_LANG |
| 2569 | _CntrlBlk(__p, _VSTD::move(__d), __a); |
| 2570 | #else |
| 2571 | _CntrlBlk(__p, __d, __a); |
| 2572 | #endif // not _LIBCPP_CXX03_LANG |
Eric Fiselier | 6bd814f | 2014-10-23 04:12:28 +0000 | [diff] [blame] | 2573 | __cntrl_ = _VSTD::addressof(*__hold2.release()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2574 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 2575 | } |
| 2576 | catch (...) |
| 2577 | { |
| 2578 | __d(__p); |
| 2579 | throw; |
| 2580 | } |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 2581 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2582 | } |
| 2583 | |
| 2584 | template<class _Tp> |
| 2585 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2586 | inline |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2587 | 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] | 2588 | : __ptr_(__p), |
| 2589 | __cntrl_(__r.__cntrl_) |
| 2590 | { |
| 2591 | if (__cntrl_) |
| 2592 | __cntrl_->__add_shared(); |
| 2593 | } |
| 2594 | |
| 2595 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2596 | inline |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2597 | shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2598 | : __ptr_(__r.__ptr_), |
| 2599 | __cntrl_(__r.__cntrl_) |
| 2600 | { |
| 2601 | if (__cntrl_) |
| 2602 | __cntrl_->__add_shared(); |
| 2603 | } |
| 2604 | |
| 2605 | template<class _Tp> |
| 2606 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2607 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2608 | shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2609 | typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type) |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2610 | _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2611 | : __ptr_(__r.__ptr_), |
| 2612 | __cntrl_(__r.__cntrl_) |
| 2613 | { |
| 2614 | if (__cntrl_) |
| 2615 | __cntrl_->__add_shared(); |
| 2616 | } |
| 2617 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2618 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2619 | inline |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2620 | shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2621 | : __ptr_(__r.__ptr_), |
| 2622 | __cntrl_(__r.__cntrl_) |
| 2623 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 2624 | __r.__ptr_ = nullptr; |
| 2625 | __r.__cntrl_ = nullptr; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | template<class _Tp> |
| 2629 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2630 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2631 | shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r, |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2632 | typename enable_if<__compatible_with<_Yp, element_type>::value, __nat>::type) |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2633 | _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2634 | : __ptr_(__r.__ptr_), |
| 2635 | __cntrl_(__r.__cntrl_) |
| 2636 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 2637 | __r.__ptr_ = nullptr; |
| 2638 | __r.__cntrl_ = nullptr; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2639 | } |
| 2640 | |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 2641 | #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2642 | template<class _Tp> |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2643 | template<class _Yp> |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2644 | shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r, |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2645 | typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2646 | : __ptr_(__r.get()) |
| 2647 | { |
| 2648 | typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk; |
| 2649 | __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>()); |
Eric Fiselier | f16c93f | 2016-06-26 23:56:32 +0000 | [diff] [blame] | 2650 | __enable_weak_this(__r.get(), __r.get()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2651 | __r.release(); |
| 2652 | } |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 2653 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2654 | |
| 2655 | template<class _Tp> |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2656 | template <class _Yp, class _Dp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2657 | shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r, |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2658 | typename enable_if |
| 2659 | < |
| 2660 | !is_lvalue_reference<_Dp>::value && |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2661 | is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value, |
| 2662 | __nat |
| 2663 | >::type) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2664 | : __ptr_(__r.get()) |
| 2665 | { |
Marshall Clow | 35cde74 | 2015-05-10 13:59:45 +0000 | [diff] [blame] | 2666 | #if _LIBCPP_STD_VER > 11 |
| 2667 | if (__ptr_ == nullptr) |
| 2668 | __cntrl_ = nullptr; |
| 2669 | else |
| 2670 | #endif |
| 2671 | { |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 2672 | typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; |
zoecarver | caf89ec | 2021-04-08 22:01:35 -0700 | [diff] [blame] | 2673 | 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] | 2674 | __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT()); |
Eric Fiselier | f16c93f | 2016-06-26 23:56:32 +0000 | [diff] [blame] | 2675 | __enable_weak_this(__r.get(), __r.get()); |
Marshall Clow | 35cde74 | 2015-05-10 13:59:45 +0000 | [diff] [blame] | 2676 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2677 | __r.release(); |
| 2678 | } |
| 2679 | |
| 2680 | template<class _Tp> |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2681 | template <class _Yp, class _Dp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2682 | shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r, |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2683 | typename enable_if |
| 2684 | < |
| 2685 | is_lvalue_reference<_Dp>::value && |
Logan Chien | d435f8b | 2014-01-31 09:30:46 +0000 | [diff] [blame] | 2686 | is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value, |
| 2687 | __nat |
| 2688 | >::type) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2689 | : __ptr_(__r.get()) |
| 2690 | { |
Marshall Clow | 35cde74 | 2015-05-10 13:59:45 +0000 | [diff] [blame] | 2691 | #if _LIBCPP_STD_VER > 11 |
| 2692 | if (__ptr_ == nullptr) |
| 2693 | __cntrl_ = nullptr; |
| 2694 | else |
| 2695 | #endif |
| 2696 | { |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 2697 | typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; |
zoecarver | caf89ec | 2021-04-08 22:01:35 -0700 | [diff] [blame] | 2698 | typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, |
Marshall Clow | 35cde74 | 2015-05-10 13:59:45 +0000 | [diff] [blame] | 2699 | reference_wrapper<typename remove_reference<_Dp>::type>, |
Erik Pilkington | 2a39876 | 2017-05-25 15:43:31 +0000 | [diff] [blame] | 2700 | _AllocT > _CntrlBlk; |
Logan Smith | 85cb081 | 2020-02-20 12:23:36 -0500 | [diff] [blame] | 2701 | __cntrl_ = new _CntrlBlk(__r.get(), _VSTD::ref(__r.get_deleter()), _AllocT()); |
Eric Fiselier | f16c93f | 2016-06-26 23:56:32 +0000 | [diff] [blame] | 2702 | __enable_weak_this(__r.get(), __r.get()); |
Marshall Clow | 35cde74 | 2015-05-10 13:59:45 +0000 | [diff] [blame] | 2703 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2704 | __r.release(); |
| 2705 | } |
| 2706 | |
Zoe Carver | 6cd05c3 | 2019-08-19 15:47:16 +0000 | [diff] [blame] | 2707 | template<class _Tp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2708 | shared_ptr<_Tp>::~shared_ptr() |
| 2709 | { |
| 2710 | if (__cntrl_) |
| 2711 | __cntrl_->__release_shared(); |
| 2712 | } |
| 2713 | |
| 2714 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2715 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2716 | shared_ptr<_Tp>& |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2717 | shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2718 | { |
| 2719 | shared_ptr(__r).swap(*this); |
| 2720 | return *this; |
| 2721 | } |
| 2722 | |
| 2723 | template<class _Tp> |
| 2724 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2725 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2726 | typename enable_if |
| 2727 | < |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2728 | __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value, |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2729 | shared_ptr<_Tp>& |
| 2730 | >::type |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2731 | shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2732 | { |
| 2733 | shared_ptr(__r).swap(*this); |
| 2734 | return *this; |
| 2735 | } |
| 2736 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2737 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2738 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2739 | shared_ptr<_Tp>& |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2740 | shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2741 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2742 | shared_ptr(_VSTD::move(__r)).swap(*this); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2743 | return *this; |
| 2744 | } |
| 2745 | |
| 2746 | template<class _Tp> |
| 2747 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2748 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2749 | typename enable_if |
| 2750 | < |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2751 | __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value, |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2752 | shared_ptr<_Tp>& |
| 2753 | >::type |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2754 | shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r) |
| 2755 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2756 | shared_ptr(_VSTD::move(__r)).swap(*this); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2757 | return *this; |
| 2758 | } |
| 2759 | |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 2760 | #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2761 | template<class _Tp> |
| 2762 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2763 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2764 | typename enable_if |
| 2765 | < |
| 2766 | !is_array<_Yp>::value && |
Marshall Clow | 7e384b7 | 2017-01-10 16:59:33 +0000 | [diff] [blame] | 2767 | is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value, |
Howard Hinnant | 25bcaf6 | 2013-09-13 23:56:00 +0000 | [diff] [blame] | 2768 | shared_ptr<_Tp> |
| 2769 | >::type& |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2770 | shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r) |
| 2771 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2772 | shared_ptr(_VSTD::move(__r)).swap(*this); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2773 | return *this; |
| 2774 | } |
Marshall Clow | b22274f | 2017-01-24 22:22:33 +0000 | [diff] [blame] | 2775 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2776 | |
| 2777 | template<class _Tp> |
| 2778 | template <class _Yp, class _Dp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2779 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2780 | typename enable_if |
| 2781 | < |
Aditya Kumar | 7c5db69 | 2017-08-20 10:38:55 +0000 | [diff] [blame] | 2782 | is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, |
Marshall Clow | 7e384b7 | 2017-01-10 16:59:33 +0000 | [diff] [blame] | 2783 | typename shared_ptr<_Tp>::element_type*>::value, |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2784 | shared_ptr<_Tp>& |
| 2785 | >::type |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2786 | shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r) |
| 2787 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2788 | shared_ptr(_VSTD::move(__r)).swap(*this); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2789 | return *this; |
| 2790 | } |
| 2791 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2792 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2793 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2794 | void |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2795 | shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2796 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 2797 | _VSTD::swap(__ptr_, __r.__ptr_); |
| 2798 | _VSTD::swap(__cntrl_, __r.__cntrl_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
| 2801 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2802 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2803 | void |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2804 | shared_ptr<_Tp>::reset() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2805 | { |
| 2806 | shared_ptr().swap(*this); |
| 2807 | } |
| 2808 | |
| 2809 | template<class _Tp> |
| 2810 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2811 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2812 | typename enable_if |
| 2813 | < |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2814 | __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value, |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2815 | void |
| 2816 | >::type |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2817 | shared_ptr<_Tp>::reset(_Yp* __p) |
| 2818 | { |
| 2819 | shared_ptr(__p).swap(*this); |
| 2820 | } |
| 2821 | |
| 2822 | template<class _Tp> |
| 2823 | template<class _Yp, class _Dp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2824 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2825 | typename enable_if |
| 2826 | < |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2827 | __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value, |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2828 | void |
| 2829 | >::type |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2830 | shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d) |
| 2831 | { |
| 2832 | shared_ptr(__p, __d).swap(*this); |
| 2833 | } |
| 2834 | |
| 2835 | template<class _Tp> |
| 2836 | template<class _Yp, class _Dp, class _Alloc> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 2837 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2838 | typename enable_if |
| 2839 | < |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 2840 | __compatible_with<_Yp, typename shared_ptr<_Tp>::element_type>::value, |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 2841 | void |
| 2842 | >::type |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2843 | shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a) |
| 2844 | { |
| 2845 | shared_ptr(__p, __d, __a).swap(*this); |
| 2846 | } |
| 2847 | |
Louis Dionne | 74aef9f | 2020-12-11 12:20:06 -0500 | [diff] [blame] | 2848 | // |
| 2849 | // std::allocate_shared and std::make_shared |
| 2850 | // |
| 2851 | template<class _Tp, class _Alloc, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> > |
| 2852 | _LIBCPP_HIDE_FROM_ABI |
| 2853 | shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&& ...__args) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2854 | { |
Louis Dionne | 74aef9f | 2020-12-11 12:20:06 -0500 | [diff] [blame] | 2855 | using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>; |
| 2856 | using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type; |
| 2857 | __allocation_guard<_ControlBlockAllocator> __guard(__a, 1); |
| 2858 | ::new ((void*)_VSTD::addressof(*__guard.__get())) _ControlBlock(__a, _VSTD::forward<_Args>(__args)...); |
| 2859 | auto __control_block = __guard.__release_ptr(); |
| 2860 | 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] | 2861 | } |
| 2862 | |
Louis Dionne | 43c9f8f | 2020-12-09 16:57:28 -0500 | [diff] [blame] | 2863 | template<class _Tp, class ..._Args, class = _EnableIf<!is_array<_Tp>::value> > |
| 2864 | _LIBCPP_HIDE_FROM_ABI |
| 2865 | shared_ptr<_Tp> make_shared(_Args&& ...__args) |
| 2866 | { |
| 2867 | return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...); |
| 2868 | } |
| 2869 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2870 | template<class _Tp, class _Up> |
| 2871 | inline _LIBCPP_INLINE_VISIBILITY |
| 2872 | bool |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2873 | operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2874 | { |
| 2875 | return __x.get() == __y.get(); |
| 2876 | } |
| 2877 | |
| 2878 | template<class _Tp, class _Up> |
| 2879 | inline _LIBCPP_INLINE_VISIBILITY |
| 2880 | bool |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2881 | operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2882 | { |
| 2883 | return !(__x == __y); |
| 2884 | } |
| 2885 | |
| 2886 | template<class _Tp, class _Up> |
| 2887 | inline _LIBCPP_INLINE_VISIBILITY |
| 2888 | bool |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 2889 | operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2890 | { |
Marshall Clow | 8afdf7a | 2018-02-12 17:26:40 +0000 | [diff] [blame] | 2891 | #if _LIBCPP_STD_VER <= 11 |
Eric Fiselier | f8898c8 | 2015-02-05 23:01:40 +0000 | [diff] [blame] | 2892 | typedef typename common_type<_Tp*, _Up*>::type _Vp; |
| 2893 | return less<_Vp>()(__x.get(), __y.get()); |
Marshall Clow | 8afdf7a | 2018-02-12 17:26:40 +0000 | [diff] [blame] | 2894 | #else |
| 2895 | return less<>()(__x.get(), __y.get()); |
| 2896 | #endif |
| 2897 | |
Howard Hinnant | b17caf9 | 2012-02-21 21:02:58 +0000 | [diff] [blame] | 2898 | } |
| 2899 | |
| 2900 | template<class _Tp, class _Up> |
| 2901 | inline _LIBCPP_INLINE_VISIBILITY |
| 2902 | bool |
| 2903 | operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT |
| 2904 | { |
| 2905 | return __y < __x; |
| 2906 | } |
| 2907 | |
| 2908 | template<class _Tp, class _Up> |
| 2909 | inline _LIBCPP_INLINE_VISIBILITY |
| 2910 | bool |
| 2911 | operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT |
| 2912 | { |
| 2913 | return !(__y < __x); |
| 2914 | } |
| 2915 | |
| 2916 | template<class _Tp, class _Up> |
| 2917 | inline _LIBCPP_INLINE_VISIBILITY |
| 2918 | bool |
| 2919 | operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT |
| 2920 | { |
| 2921 | return !(__x < __y); |
| 2922 | } |
| 2923 | |
| 2924 | template<class _Tp> |
| 2925 | inline _LIBCPP_INLINE_VISIBILITY |
| 2926 | bool |
| 2927 | operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT |
| 2928 | { |
| 2929 | return !__x; |
| 2930 | } |
| 2931 | |
| 2932 | template<class _Tp> |
| 2933 | inline _LIBCPP_INLINE_VISIBILITY |
| 2934 | bool |
| 2935 | operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT |
| 2936 | { |
| 2937 | return !__x; |
| 2938 | } |
| 2939 | |
| 2940 | template<class _Tp> |
| 2941 | inline _LIBCPP_INLINE_VISIBILITY |
| 2942 | bool |
| 2943 | operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT |
| 2944 | { |
| 2945 | return static_cast<bool>(__x); |
| 2946 | } |
| 2947 | |
| 2948 | template<class _Tp> |
| 2949 | inline _LIBCPP_INLINE_VISIBILITY |
| 2950 | bool |
| 2951 | operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT |
| 2952 | { |
| 2953 | return static_cast<bool>(__x); |
| 2954 | } |
| 2955 | |
| 2956 | template<class _Tp> |
| 2957 | inline _LIBCPP_INLINE_VISIBILITY |
| 2958 | bool |
| 2959 | operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT |
| 2960 | { |
| 2961 | return less<_Tp*>()(__x.get(), nullptr); |
| 2962 | } |
| 2963 | |
| 2964 | template<class _Tp> |
| 2965 | inline _LIBCPP_INLINE_VISIBILITY |
| 2966 | bool |
| 2967 | operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT |
| 2968 | { |
| 2969 | return less<_Tp*>()(nullptr, __x.get()); |
| 2970 | } |
| 2971 | |
| 2972 | template<class _Tp> |
| 2973 | inline _LIBCPP_INLINE_VISIBILITY |
| 2974 | bool |
| 2975 | operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT |
| 2976 | { |
| 2977 | return nullptr < __x; |
| 2978 | } |
| 2979 | |
| 2980 | template<class _Tp> |
| 2981 | inline _LIBCPP_INLINE_VISIBILITY |
| 2982 | bool |
| 2983 | operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT |
| 2984 | { |
| 2985 | return __x < nullptr; |
| 2986 | } |
| 2987 | |
| 2988 | template<class _Tp> |
| 2989 | inline _LIBCPP_INLINE_VISIBILITY |
| 2990 | bool |
| 2991 | operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT |
| 2992 | { |
| 2993 | return !(nullptr < __x); |
| 2994 | } |
| 2995 | |
| 2996 | template<class _Tp> |
| 2997 | inline _LIBCPP_INLINE_VISIBILITY |
| 2998 | bool |
| 2999 | operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT |
| 3000 | { |
| 3001 | return !(__x < nullptr); |
| 3002 | } |
| 3003 | |
| 3004 | template<class _Tp> |
| 3005 | inline _LIBCPP_INLINE_VISIBILITY |
| 3006 | bool |
| 3007 | operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT |
| 3008 | { |
| 3009 | return !(__x < nullptr); |
| 3010 | } |
| 3011 | |
| 3012 | template<class _Tp> |
| 3013 | inline _LIBCPP_INLINE_VISIBILITY |
| 3014 | bool |
| 3015 | operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT |
| 3016 | { |
| 3017 | return !(nullptr < __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3018 | } |
| 3019 | |
| 3020 | template<class _Tp> |
| 3021 | inline _LIBCPP_INLINE_VISIBILITY |
| 3022 | void |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3023 | swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3024 | { |
| 3025 | __x.swap(__y); |
| 3026 | } |
| 3027 | |
| 3028 | template<class _Tp, class _Up> |
| 3029 | inline _LIBCPP_INLINE_VISIBILITY |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3030 | shared_ptr<_Tp> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3031 | static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3032 | { |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3033 | return shared_ptr<_Tp>(__r, |
| 3034 | static_cast< |
| 3035 | typename shared_ptr<_Tp>::element_type*>(__r.get())); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
| 3038 | template<class _Tp, class _Up> |
| 3039 | inline _LIBCPP_INLINE_VISIBILITY |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3040 | shared_ptr<_Tp> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3041 | dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3042 | { |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3043 | typedef typename shared_ptr<_Tp>::element_type _ET; |
| 3044 | _ET* __p = dynamic_cast<_ET*>(__r.get()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3045 | return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>(); |
| 3046 | } |
| 3047 | |
| 3048 | template<class _Tp, class _Up> |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3049 | shared_ptr<_Tp> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3050 | const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3051 | { |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3052 | typedef typename shared_ptr<_Tp>::element_type _RTp; |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3053 | return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get())); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3054 | } |
| 3055 | |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3056 | template<class _Tp, class _Up> |
| 3057 | shared_ptr<_Tp> |
| 3058 | reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT |
| 3059 | { |
| 3060 | return shared_ptr<_Tp>(__r, |
| 3061 | reinterpret_cast< |
| 3062 | typename shared_ptr<_Tp>::element_type*>(__r.get())); |
| 3063 | } |
| 3064 | |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 3065 | #ifndef _LIBCPP_NO_RTTI |
| 3066 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3067 | template<class _Dp, class _Tp> |
| 3068 | inline _LIBCPP_INLINE_VISIBILITY |
| 3069 | _Dp* |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3070 | get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3071 | { |
| 3072 | return __p.template __get_deleter<_Dp>(); |
| 3073 | } |
| 3074 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3075 | #endif // _LIBCPP_NO_RTTI |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 3076 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3077 | template<class _Tp> |
Vy Nguyen | e369bd9 | 2020-07-13 12:34:37 -0400 | [diff] [blame] | 3078 | class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3079 | { |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3080 | public: |
| 3081 | typedef _Tp element_type; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3082 | private: |
| 3083 | element_type* __ptr_; |
| 3084 | __shared_weak_count* __cntrl_; |
| 3085 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3086 | public: |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3087 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 3088 | _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3089 | 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] | 3090 | typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0) |
| 3091 | _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3092 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3093 | weak_ptr(weak_ptr const& __r) _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3094 | 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] | 3095 | typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0) |
| 3096 | _NOEXCEPT; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3097 | |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3098 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3099 | weak_ptr(weak_ptr&& __r) _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3100 | template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r, |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3101 | typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0) |
| 3102 | _NOEXCEPT; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3103 | ~weak_ptr(); |
| 3104 | |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3105 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3106 | weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT; |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3107 | template<class _Yp> |
| 3108 | typename enable_if |
| 3109 | < |
| 3110 | is_convertible<_Yp*, element_type*>::value, |
| 3111 | weak_ptr& |
| 3112 | >::type |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3113 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3114 | operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT; |
| 3115 | |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3116 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3117 | weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT; |
| 3118 | template<class _Yp> |
| 3119 | typename enable_if |
| 3120 | < |
| 3121 | is_convertible<_Yp*, element_type*>::value, |
| 3122 | weak_ptr& |
| 3123 | >::type |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3124 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3125 | operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT; |
| 3126 | |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3127 | template<class _Yp> |
| 3128 | typename enable_if |
| 3129 | < |
| 3130 | is_convertible<_Yp*, element_type*>::value, |
| 3131 | weak_ptr& |
| 3132 | >::type |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3133 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3134 | operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3135 | |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3136 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3137 | void swap(weak_ptr& __r) _NOEXCEPT; |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3138 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3139 | void reset() _NOEXCEPT; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3140 | |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3141 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3142 | long use_count() const _NOEXCEPT |
| 3143 | {return __cntrl_ ? __cntrl_->use_count() : 0;} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3144 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3145 | bool expired() const _NOEXCEPT |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 3146 | {return __cntrl_ == nullptr || __cntrl_->use_count() == 0;} |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3147 | shared_ptr<_Tp> lock() const _NOEXCEPT; |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3148 | template<class _Up> |
| 3149 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3150 | bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3151 | {return __cntrl_ < __r.__cntrl_;} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3152 | template<class _Up> |
| 3153 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3154 | bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3155 | {return __cntrl_ < __r.__cntrl_;} |
| 3156 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3157 | template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr; |
| 3158 | template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3159 | }; |
| 3160 | |
Logan Smith | a5e4d7e | 2020-05-07 12:07:01 -0400 | [diff] [blame] | 3161 | #ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES |
| 3162 | template<class _Tp> |
| 3163 | weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>; |
| 3164 | #endif |
| 3165 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3166 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3167 | inline |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 3168 | _LIBCPP_CONSTEXPR |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3169 | weak_ptr<_Tp>::weak_ptr() _NOEXCEPT |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 3170 | : __ptr_(nullptr), |
| 3171 | __cntrl_(nullptr) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3172 | { |
| 3173 | } |
| 3174 | |
| 3175 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3176 | inline |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3177 | weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3178 | : __ptr_(__r.__ptr_), |
| 3179 | __cntrl_(__r.__cntrl_) |
| 3180 | { |
| 3181 | if (__cntrl_) |
| 3182 | __cntrl_->__add_weak(); |
| 3183 | } |
| 3184 | |
| 3185 | template<class _Tp> |
| 3186 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3187 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3188 | weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r, |
Howard Hinnant | 3a085e8 | 2011-05-22 00:09:02 +0000 | [diff] [blame] | 3189 | typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type) |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3190 | _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3191 | : __ptr_(__r.__ptr_), |
| 3192 | __cntrl_(__r.__cntrl_) |
| 3193 | { |
| 3194 | if (__cntrl_) |
| 3195 | __cntrl_->__add_weak(); |
| 3196 | } |
| 3197 | |
| 3198 | template<class _Tp> |
| 3199 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3200 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3201 | weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r, |
Howard Hinnant | 3a085e8 | 2011-05-22 00:09:02 +0000 | [diff] [blame] | 3202 | typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type) |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3203 | _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3204 | : __ptr_(__r.__ptr_), |
| 3205 | __cntrl_(__r.__cntrl_) |
| 3206 | { |
| 3207 | if (__cntrl_) |
| 3208 | __cntrl_->__add_weak(); |
| 3209 | } |
| 3210 | |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3211 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3212 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3213 | weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT |
| 3214 | : __ptr_(__r.__ptr_), |
| 3215 | __cntrl_(__r.__cntrl_) |
| 3216 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 3217 | __r.__ptr_ = nullptr; |
| 3218 | __r.__cntrl_ = nullptr; |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3219 | } |
| 3220 | |
| 3221 | template<class _Tp> |
| 3222 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3223 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3224 | weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r, |
| 3225 | typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type) |
| 3226 | _NOEXCEPT |
| 3227 | : __ptr_(__r.__ptr_), |
| 3228 | __cntrl_(__r.__cntrl_) |
| 3229 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 3230 | __r.__ptr_ = nullptr; |
| 3231 | __r.__cntrl_ = nullptr; |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3232 | } |
| 3233 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3234 | template<class _Tp> |
| 3235 | weak_ptr<_Tp>::~weak_ptr() |
| 3236 | { |
| 3237 | if (__cntrl_) |
| 3238 | __cntrl_->__release_weak(); |
| 3239 | } |
| 3240 | |
| 3241 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3242 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3243 | weak_ptr<_Tp>& |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3244 | weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3245 | { |
| 3246 | weak_ptr(__r).swap(*this); |
| 3247 | return *this; |
| 3248 | } |
| 3249 | |
| 3250 | template<class _Tp> |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3251 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3252 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3253 | typename enable_if |
| 3254 | < |
| 3255 | is_convertible<_Yp*, _Tp*>::value, |
| 3256 | weak_ptr<_Tp>& |
| 3257 | >::type |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3258 | weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3259 | { |
| 3260 | weak_ptr(__r).swap(*this); |
| 3261 | return *this; |
| 3262 | } |
| 3263 | |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3264 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3265 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3266 | weak_ptr<_Tp>& |
| 3267 | weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT |
| 3268 | { |
| 3269 | weak_ptr(_VSTD::move(__r)).swap(*this); |
| 3270 | return *this; |
| 3271 | } |
| 3272 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3273 | template<class _Tp> |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3274 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3275 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3276 | typename enable_if |
| 3277 | < |
| 3278 | is_convertible<_Yp*, _Tp*>::value, |
| 3279 | weak_ptr<_Tp>& |
| 3280 | >::type |
| 3281 | weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT |
| 3282 | { |
| 3283 | weak_ptr(_VSTD::move(__r)).swap(*this); |
| 3284 | return *this; |
| 3285 | } |
| 3286 | |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3287 | template<class _Tp> |
| 3288 | template<class _Yp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3289 | inline |
Howard Hinnant | 741c8fa | 2012-01-02 17:56:02 +0000 | [diff] [blame] | 3290 | typename enable_if |
| 3291 | < |
| 3292 | is_convertible<_Yp*, _Tp*>::value, |
| 3293 | weak_ptr<_Tp>& |
| 3294 | >::type |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3295 | weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3296 | { |
| 3297 | weak_ptr(__r).swap(*this); |
| 3298 | return *this; |
| 3299 | } |
| 3300 | |
| 3301 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3302 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3303 | void |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3304 | weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3305 | { |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 3306 | _VSTD::swap(__ptr_, __r.__ptr_); |
| 3307 | _VSTD::swap(__cntrl_, __r.__cntrl_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3308 | } |
| 3309 | |
| 3310 | template<class _Tp> |
| 3311 | inline _LIBCPP_INLINE_VISIBILITY |
| 3312 | void |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3313 | swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3314 | { |
| 3315 | __x.swap(__y); |
| 3316 | } |
| 3317 | |
| 3318 | template<class _Tp> |
Evgeniy Stepanov | 2fb1cb6 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 3319 | inline |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3320 | void |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3321 | weak_ptr<_Tp>::reset() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3322 | { |
| 3323 | weak_ptr().swap(*this); |
| 3324 | } |
| 3325 | |
| 3326 | template<class _Tp> |
| 3327 | template<class _Yp> |
| 3328 | shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r, |
Marshall Clow | 7e384b7 | 2017-01-10 16:59:33 +0000 | [diff] [blame] | 3329 | typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3330 | : __ptr_(__r.__ptr_), |
| 3331 | __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_) |
| 3332 | { |
Bruce Mitchener | 170d897 | 2020-11-24 12:53:53 -0500 | [diff] [blame] | 3333 | if (__cntrl_ == nullptr) |
Marshall Clow | 8fea161 | 2016-08-25 15:09:01 +0000 | [diff] [blame] | 3334 | __throw_bad_weak_ptr(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3335 | } |
| 3336 | |
| 3337 | template<class _Tp> |
| 3338 | shared_ptr<_Tp> |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3339 | weak_ptr<_Tp>::lock() const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3340 | { |
| 3341 | shared_ptr<_Tp> __r; |
| 3342 | __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_; |
| 3343 | if (__r.__cntrl_) |
| 3344 | __r.__ptr_ = __ptr_; |
| 3345 | return __r; |
| 3346 | } |
| 3347 | |
Marshall Clow | 3c2d8a4 | 2015-11-12 15:56:44 +0000 | [diff] [blame] | 3348 | #if _LIBCPP_STD_VER > 14 |
| 3349 | template <class _Tp = void> struct owner_less; |
| 3350 | #else |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3351 | template <class _Tp> struct owner_less; |
Marshall Clow | 3c2d8a4 | 2015-11-12 15:56:44 +0000 | [diff] [blame] | 3352 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3353 | |
| 3354 | template <class _Tp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3355 | struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> > |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3356 | : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool> |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3357 | { |
| 3358 | typedef bool result_type; |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3359 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3360 | 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] | 3361 | {return __x.owner_before(__y);} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3362 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3363 | 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] | 3364 | {return __x.owner_before(__y);} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3365 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3366 | 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] | 3367 | {return __x.owner_before(__y);} |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3368 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3369 | |
| 3370 | template <class _Tp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3371 | struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> > |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3372 | : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool> |
| 3373 | { |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3374 | typedef bool result_type; |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3375 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3376 | 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] | 3377 | {return __x.owner_before(__y);} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3378 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3379 | 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] | 3380 | {return __x.owner_before(__y);} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3381 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3382 | 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] | 3383 | {return __x.owner_before(__y);} |
| 3384 | }; |
| 3385 | |
Marshall Clow | 3c2d8a4 | 2015-11-12 15:56:44 +0000 | [diff] [blame] | 3386 | #if _LIBCPP_STD_VER > 14 |
| 3387 | template <> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3388 | struct _LIBCPP_TEMPLATE_VIS owner_less<void> |
Marshall Clow | 3c2d8a4 | 2015-11-12 15:56:44 +0000 | [diff] [blame] | 3389 | { |
| 3390 | template <class _Tp, class _Up> |
| 3391 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3392 | 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] | 3393 | {return __x.owner_before(__y);} |
| 3394 | template <class _Tp, class _Up> |
| 3395 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3396 | 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] | 3397 | {return __x.owner_before(__y);} |
| 3398 | template <class _Tp, class _Up> |
| 3399 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3400 | 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] | 3401 | {return __x.owner_before(__y);} |
| 3402 | template <class _Tp, class _Up> |
| 3403 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 18a7cd5 | 2017-04-11 17:08:53 +0000 | [diff] [blame] | 3404 | 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] | 3405 | {return __x.owner_before(__y);} |
| 3406 | typedef void is_transparent; |
| 3407 | }; |
| 3408 | #endif |
| 3409 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3410 | template<class _Tp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3411 | class _LIBCPP_TEMPLATE_VIS enable_shared_from_this |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3412 | { |
| 3413 | mutable weak_ptr<_Tp> __weak_this_; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3414 | protected: |
Howard Hinnant | b5fffe8 | 2012-07-07 20:56:04 +0000 | [diff] [blame] | 3415 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3416 | enable_shared_from_this() _NOEXCEPT {} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3417 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3418 | enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3419 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3420 | enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT |
| 3421 | {return *this;} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3422 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3423 | ~enable_shared_from_this() {} |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3424 | public: |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3425 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3426 | shared_ptr<_Tp> shared_from_this() |
| 3427 | {return shared_ptr<_Tp>(__weak_this_);} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3428 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3429 | shared_ptr<_Tp const> shared_from_this() const |
| 3430 | {return shared_ptr<const _Tp>(__weak_this_);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3431 | |
Eric Fiselier | 8400686 | 2016-06-02 00:15:35 +0000 | [diff] [blame] | 3432 | #if _LIBCPP_STD_VER > 14 |
| 3433 | _LIBCPP_INLINE_VISIBILITY |
| 3434 | weak_ptr<_Tp> weak_from_this() _NOEXCEPT |
| 3435 | { return __weak_this_; } |
| 3436 | |
| 3437 | _LIBCPP_INLINE_VISIBILITY |
| 3438 | weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT |
| 3439 | { return __weak_this_; } |
| 3440 | #endif // _LIBCPP_STD_VER > 14 |
| 3441 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3442 | template <class _Up> friend class shared_ptr; |
| 3443 | }; |
| 3444 | |
Howard Hinnant | 36b31ae | 2010-06-03 16:42:57 +0000 | [diff] [blame] | 3445 | template <class _Tp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 3446 | struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> > |
Howard Hinnant | 36b31ae | 2010-06-03 16:42:57 +0000 | [diff] [blame] | 3447 | { |
| 3448 | typedef shared_ptr<_Tp> argument_type; |
| 3449 | typedef size_t result_type; |
Eric Fiselier | 698a97b | 2017-01-21 00:02:12 +0000 | [diff] [blame] | 3450 | |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3451 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 719bda3 | 2011-05-28 14:41:13 +0000 | [diff] [blame] | 3452 | result_type operator()(const argument_type& __ptr) const _NOEXCEPT |
Howard Hinnant | 36b31ae | 2010-06-03 16:42:57 +0000 | [diff] [blame] | 3453 | { |
zoecarver | d73f42a | 2020-05-11 18:42:50 -0700 | [diff] [blame] | 3454 | return hash<typename shared_ptr<_Tp>::element_type*>()(__ptr.get()); |
Howard Hinnant | 36b31ae | 2010-06-03 16:42:57 +0000 | [diff] [blame] | 3455 | } |
| 3456 | }; |
| 3457 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3458 | template<class _CharT, class _Traits, class _Yp> |
Howard Hinnant | dc09597 | 2011-07-18 15:51:59 +0000 | [diff] [blame] | 3459 | inline _LIBCPP_INLINE_VISIBILITY |
| 3460 | basic_ostream<_CharT, _Traits>& |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 3461 | operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p); |
Howard Hinnant | dc09597 | 2011-07-18 15:51:59 +0000 | [diff] [blame] | 3462 | |
Eric Fiselier | 9b49267 | 2016-06-18 02:12:53 +0000 | [diff] [blame] | 3463 | |
| 3464 | #if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER) |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3465 | |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 3466 | class _LIBCPP_TYPE_VIS __sp_mut |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3467 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3468 | void* __lx; |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3469 | public: |
| 3470 | void lock() _NOEXCEPT; |
| 3471 | void unlock() _NOEXCEPT; |
| 3472 | |
| 3473 | private: |
| 3474 | _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT; |
| 3475 | __sp_mut(const __sp_mut&); |
| 3476 | __sp_mut& operator=(const __sp_mut&); |
| 3477 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 3478 | friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*); |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3479 | }; |
| 3480 | |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 3481 | _LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR |
| 3482 | __sp_mut& __get_sp_mut(const void*); |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3483 | |
| 3484 | template <class _Tp> |
| 3485 | inline _LIBCPP_INLINE_VISIBILITY |
| 3486 | bool |
| 3487 | atomic_is_lock_free(const shared_ptr<_Tp>*) |
| 3488 | { |
| 3489 | return false; |
| 3490 | } |
| 3491 | |
| 3492 | template <class _Tp> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 3493 | _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3494 | shared_ptr<_Tp> |
| 3495 | atomic_load(const shared_ptr<_Tp>* __p) |
| 3496 | { |
| 3497 | __sp_mut& __m = __get_sp_mut(__p); |
| 3498 | __m.lock(); |
| 3499 | shared_ptr<_Tp> __q = *__p; |
| 3500 | __m.unlock(); |
| 3501 | return __q; |
| 3502 | } |
Aditya Kumar | 7c5db69 | 2017-08-20 10:38:55 +0000 | [diff] [blame] | 3503 | |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3504 | template <class _Tp> |
| 3505 | inline _LIBCPP_INLINE_VISIBILITY |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 3506 | _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3507 | shared_ptr<_Tp> |
| 3508 | atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order) |
| 3509 | { |
| 3510 | return atomic_load(__p); |
| 3511 | } |
| 3512 | |
| 3513 | template <class _Tp> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 3514 | _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3515 | void |
| 3516 | atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r) |
| 3517 | { |
| 3518 | __sp_mut& __m = __get_sp_mut(__p); |
| 3519 | __m.lock(); |
| 3520 | __p->swap(__r); |
| 3521 | __m.unlock(); |
| 3522 | } |
| 3523 | |
| 3524 | template <class _Tp> |
| 3525 | inline _LIBCPP_INLINE_VISIBILITY |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 3526 | _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3527 | void |
| 3528 | atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order) |
| 3529 | { |
| 3530 | atomic_store(__p, __r); |
| 3531 | } |
| 3532 | |
| 3533 | template <class _Tp> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 3534 | _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3535 | shared_ptr<_Tp> |
| 3536 | atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r) |
| 3537 | { |
| 3538 | __sp_mut& __m = __get_sp_mut(__p); |
| 3539 | __m.lock(); |
| 3540 | __p->swap(__r); |
| 3541 | __m.unlock(); |
| 3542 | return __r; |
| 3543 | } |
Aditya Kumar | 7c5db69 | 2017-08-20 10:38:55 +0000 | [diff] [blame] | 3544 | |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3545 | template <class _Tp> |
| 3546 | inline _LIBCPP_INLINE_VISIBILITY |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 3547 | _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3548 | shared_ptr<_Tp> |
| 3549 | atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order) |
| 3550 | { |
| 3551 | return atomic_exchange(__p, __r); |
| 3552 | } |
| 3553 | |
| 3554 | template <class _Tp> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 3555 | _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3556 | bool |
| 3557 | atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w) |
| 3558 | { |
Marshall Clow | 4201ee8 | 2016-05-18 17:50:13 +0000 | [diff] [blame] | 3559 | shared_ptr<_Tp> __temp; |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3560 | __sp_mut& __m = __get_sp_mut(__p); |
| 3561 | __m.lock(); |
| 3562 | if (__p->__owner_equivalent(*__v)) |
| 3563 | { |
Marshall Clow | 4201ee8 | 2016-05-18 17:50:13 +0000 | [diff] [blame] | 3564 | _VSTD::swap(__temp, *__p); |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3565 | *__p = __w; |
| 3566 | __m.unlock(); |
| 3567 | return true; |
| 3568 | } |
Marshall Clow | 4201ee8 | 2016-05-18 17:50:13 +0000 | [diff] [blame] | 3569 | _VSTD::swap(__temp, *__v); |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3570 | *__v = *__p; |
| 3571 | __m.unlock(); |
| 3572 | return false; |
| 3573 | } |
| 3574 | |
| 3575 | template <class _Tp> |
| 3576 | inline _LIBCPP_INLINE_VISIBILITY |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 3577 | _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3578 | bool |
| 3579 | atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w) |
| 3580 | { |
| 3581 | return atomic_compare_exchange_strong(__p, __v, __w); |
| 3582 | } |
| 3583 | |
| 3584 | template <class _Tp> |
| 3585 | inline _LIBCPP_INLINE_VISIBILITY |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 3586 | _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3587 | bool |
| 3588 | atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, |
| 3589 | shared_ptr<_Tp> __w, memory_order, memory_order) |
| 3590 | { |
| 3591 | return atomic_compare_exchange_strong(__p, __v, __w); |
| 3592 | } |
| 3593 | |
| 3594 | template <class _Tp> |
| 3595 | inline _LIBCPP_INLINE_VISIBILITY |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 3596 | _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3597 | bool |
| 3598 | atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, |
| 3599 | shared_ptr<_Tp> __w, memory_order, memory_order) |
| 3600 | { |
| 3601 | return atomic_compare_exchange_weak(__p, __v, __w); |
| 3602 | } |
| 3603 | |
Eric Fiselier | 9b49267 | 2016-06-18 02:12:53 +0000 | [diff] [blame] | 3604 | #endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER) |
Howard Hinnant | 9fa3020 | 2012-07-30 01:40:57 +0000 | [diff] [blame] | 3605 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3606 | //enum class |
Eric Fiselier | ab6bb30 | 2017-01-05 01:15:42 +0000 | [diff] [blame] | 3607 | #if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) |
| 3608 | # ifndef _LIBCPP_CXX03_LANG |
| 3609 | enum class pointer_safety : unsigned char { |
| 3610 | relaxed, |
| 3611 | preferred, |
| 3612 | strict |
| 3613 | }; |
| 3614 | # endif |
| 3615 | #else |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 3616 | struct _LIBCPP_TYPE_VIS pointer_safety |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3617 | { |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3618 | enum __lx |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3619 | { |
| 3620 | relaxed, |
| 3621 | preferred, |
| 3622 | strict |
| 3623 | }; |
| 3624 | |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3625 | __lx __v_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3626 | |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3627 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 2fdd22b | 2017-01-05 01:28:40 +0000 | [diff] [blame] | 3628 | pointer_safety() : __v_() {} |
| 3629 | |
| 3630 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 49e145e | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 3631 | pointer_safety(__lx __v) : __v_(__v) {} |
Howard Hinnant | 756c69b | 2010-09-22 16:48:34 +0000 | [diff] [blame] | 3632 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3633 | operator int() const {return __v_;} |
| 3634 | }; |
Eric Fiselier | ab6bb30 | 2017-01-05 01:15:42 +0000 | [diff] [blame] | 3635 | #endif |
| 3636 | |
| 3637 | #if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \ |
Louis Dionne | 5e0eadd | 2018-08-01 02:08:59 +0000 | [diff] [blame] | 3638 | defined(_LIBCPP_BUILDING_LIBRARY) |
Eric Fiselier | ab6bb30 | 2017-01-05 01:15:42 +0000 | [diff] [blame] | 3639 | _LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT; |
| 3640 | #else |
| 3641 | // This function is only offered in C++03 under ABI v1. |
| 3642 | # if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG) |
| 3643 | inline _LIBCPP_INLINE_VISIBILITY |
| 3644 | pointer_safety get_pointer_safety() _NOEXCEPT { |
| 3645 | return pointer_safety::relaxed; |
| 3646 | } |
| 3647 | # endif |
| 3648 | #endif |
| 3649 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3650 | |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 3651 | _LIBCPP_FUNC_VIS void declare_reachable(void* __p); |
| 3652 | _LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n); |
| 3653 | _LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n); |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 3654 | _LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3655 | |
| 3656 | template <class _Tp> |
| 3657 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 3658 | _Tp* |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3659 | undeclare_reachable(_Tp* __p) |
| 3660 | { |
| 3661 | return static_cast<_Tp*>(__undeclare_reachable(__p)); |
| 3662 | } |
| 3663 | |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 3664 | _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] | 3665 | |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 3666 | // --- Helper for container swap -- |
| 3667 | template <typename _Alloc> |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 3668 | _LIBCPP_INLINE_VISIBILITY |
| 3669 | void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type) |
| 3670 | #if _LIBCPP_STD_VER >= 14 |
| 3671 | _NOEXCEPT |
| 3672 | #else |
| 3673 | _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value) |
| 3674 | #endif |
| 3675 | { |
| 3676 | using _VSTD::swap; |
| 3677 | swap(__a1, __a2); |
| 3678 | } |
| 3679 | |
| 3680 | template <typename _Alloc> |
Eric Fiselier | 6585c75 | 2016-04-21 22:54:21 +0000 | [diff] [blame] | 3681 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 3682 | void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {} |
| 3683 | |
Arthur O'Dwyer | 4e0de31 | 2020-11-18 18:54:38 -0500 | [diff] [blame] | 3684 | template <typename _Alloc> |
| 3685 | inline _LIBCPP_INLINE_VISIBILITY |
| 3686 | void __swap_allocator(_Alloc & __a1, _Alloc & __a2) |
| 3687 | #if _LIBCPP_STD_VER >= 14 |
| 3688 | _NOEXCEPT |
| 3689 | #else |
| 3690 | _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value) |
| 3691 | #endif |
| 3692 | { |
| 3693 | _VSTD::__swap_allocator(__a1, __a2, |
| 3694 | integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>()); |
| 3695 | } |
| 3696 | |
Marshall Clow | ff91de8 | 2015-08-18 19:51:37 +0000 | [diff] [blame] | 3697 | template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> > |
Aditya Kumar | 7c5db69 | 2017-08-20 10:38:55 +0000 | [diff] [blame] | 3698 | struct __noexcept_move_assign_container : public integral_constant<bool, |
Marshall Clow | 2fe8a8d | 2015-08-18 18:57:00 +0000 | [diff] [blame] | 3699 | _Traits::propagate_on_container_move_assignment::value |
| 3700 | #if _LIBCPP_STD_VER > 14 |
| 3701 | || _Traits::is_always_equal::value |
| 3702 | #else |
| 3703 | && is_nothrow_move_assignable<_Alloc>::value |
| 3704 | #endif |
| 3705 | > {}; |
Marshall Clow | 8982dcd | 2015-07-13 20:04:56 +0000 | [diff] [blame] | 3706 | |
Marshall Clow | a591b9a | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 3707 | |
Marshall Clow | a591b9a | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 3708 | template <class _Tp, class _Alloc> |
| 3709 | struct __temp_value { |
| 3710 | typedef allocator_traits<_Alloc> _Traits; |
Aditya Kumar | 7c5db69 | 2017-08-20 10:38:55 +0000 | [diff] [blame] | 3711 | |
Eric Fiselier | 0f0ed9d | 2019-01-16 01:51:12 +0000 | [diff] [blame] | 3712 | typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v; |
Marshall Clow | a591b9a | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 3713 | _Alloc &__a; |
| 3714 | |
| 3715 | _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); } |
| 3716 | _Tp & get() { return *__addr(); } |
Aditya Kumar | 7c5db69 | 2017-08-20 10:38:55 +0000 | [diff] [blame] | 3717 | |
Marshall Clow | a591b9a | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 3718 | template<class... _Args> |
Peter Collingbourne | cb12615 | 2018-08-15 17:49:30 +0000 | [diff] [blame] | 3719 | _LIBCPP_NO_CFI |
| 3720 | __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) { |
| 3721 | _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)), |
| 3722 | _VSTD::forward<_Args>(__args)...); |
| 3723 | } |
Aditya Kumar | 7c5db69 | 2017-08-20 10:38:55 +0000 | [diff] [blame] | 3724 | |
Marshall Clow | a591b9a | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 3725 | ~__temp_value() { _Traits::destroy(__a, __addr()); } |
| 3726 | }; |
Marshall Clow | a591b9a | 2016-07-11 21:38:08 +0000 | [diff] [blame] | 3727 | |
Marshall Clow | e46031a | 2018-07-02 18:41:15 +0000 | [diff] [blame] | 3728 | template<typename _Alloc, typename = void, typename = void> |
Marshall Clow | 82c90aa | 2018-01-11 19:36:22 +0000 | [diff] [blame] | 3729 | struct __is_allocator : false_type {}; |
| 3730 | |
| 3731 | template<typename _Alloc> |
| 3732 | struct __is_allocator<_Alloc, |
Marshall Clow | e46031a | 2018-07-02 18:41:15 +0000 | [diff] [blame] | 3733 | typename __void_t<typename _Alloc::value_type>::type, |
| 3734 | typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type |
| 3735 | > |
Marshall Clow | 82c90aa | 2018-01-11 19:36:22 +0000 | [diff] [blame] | 3736 | : true_type {}; |
Marshall Clow | 82c90aa | 2018-01-11 19:36:22 +0000 | [diff] [blame] | 3737 | |
Eric Fiselier | 74ebee6 | 2019-06-08 01:31:19 +0000 | [diff] [blame] | 3738 | // __builtin_new_allocator -- A non-templated helper for allocating and |
| 3739 | // deallocating memory using __builtin_operator_new and |
| 3740 | // __builtin_operator_delete. It should be used in preference to |
| 3741 | // `std::allocator<T>` to avoid additional instantiations. |
| 3742 | struct __builtin_new_allocator { |
| 3743 | struct __builtin_new_deleter { |
| 3744 | typedef void* pointer_type; |
| 3745 | |
| 3746 | _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align) |
| 3747 | : __size_(__size), __align_(__align) {} |
| 3748 | |
| 3749 | void operator()(void* p) const _NOEXCEPT { |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 3750 | _VSTD::__libcpp_deallocate(p, __size_, __align_); |
Eric Fiselier | 74ebee6 | 2019-06-08 01:31:19 +0000 | [diff] [blame] | 3751 | } |
| 3752 | |
| 3753 | private: |
| 3754 | size_t __size_; |
| 3755 | size_t __align_; |
| 3756 | }; |
| 3757 | |
| 3758 | typedef unique_ptr<void, __builtin_new_deleter> __holder_t; |
| 3759 | |
| 3760 | static __holder_t __allocate_bytes(size_t __s, size_t __align) { |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 3761 | return __holder_t(_VSTD::__libcpp_allocate(__s, __align), |
Eric Fiselier | 74ebee6 | 2019-06-08 01:31:19 +0000 | [diff] [blame] | 3762 | __builtin_new_deleter(__s, __align)); |
| 3763 | } |
| 3764 | |
| 3765 | static void __deallocate_bytes(void* __p, size_t __s, |
| 3766 | size_t __align) _NOEXCEPT { |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 3767 | _VSTD::__libcpp_deallocate(__p, __s, __align); |
Eric Fiselier | 74ebee6 | 2019-06-08 01:31:19 +0000 | [diff] [blame] | 3768 | } |
| 3769 | |
| 3770 | template <class _Tp> |
| 3771 | _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE |
| 3772 | static __holder_t __allocate_type(size_t __n) { |
| 3773 | return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)); |
| 3774 | } |
| 3775 | |
| 3776 | template <class _Tp> |
| 3777 | _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE |
| 3778 | static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT { |
| 3779 | __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)); |
| 3780 | } |
| 3781 | }; |
| 3782 | |
| 3783 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3784 | _LIBCPP_END_NAMESPACE_STD |
| 3785 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 3786 | _LIBCPP_POP_MACROS |
| 3787 | |
Louis Dionne | 59d0b3c | 2019-08-05 18:29:14 +0000 | [diff] [blame] | 3788 | #if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17 |
Louis Dionne | d53d8c0 | 2019-08-06 21:11:24 +0000 | [diff] [blame] | 3789 | # include <__pstl_memory> |
Louis Dionne | 59d0b3c | 2019-08-05 18:29:14 +0000 | [diff] [blame] | 3790 | #endif |
| 3791 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 3792 | #endif // _LIBCPP_MEMORY |