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