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