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