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