|
libstdc++
|
00001 // unique_ptr implementation -*- C++ -*- 00002 00003 // Copyright (C) 2008-2018 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file bits/unique_ptr.h 00026 * This is an internal header file, included by other library headers. 00027 * Do not attempt to use it directly. @headername{memory} 00028 */ 00029 00030 #ifndef _UNIQUE_PTR_H 00031 #define _UNIQUE_PTR_H 1 00032 00033 #include <bits/c++config.h> 00034 #include <debug/assertions.h> 00035 #include <type_traits> 00036 #include <utility> 00037 #include <tuple> 00038 #include <bits/stl_function.h> 00039 #include <bits/functional_hash.h> 00040 00041 namespace std _GLIBCXX_VISIBILITY(default) 00042 { 00043 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00044 00045 /** 00046 * @addtogroup pointer_abstractions 00047 * @{ 00048 */ 00049 00050 #if _GLIBCXX_USE_DEPRECATED 00051 #pragma GCC diagnostic push 00052 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 00053 template<typename> class auto_ptr; 00054 #pragma GCC diagnostic pop 00055 #endif 00056 00057 /// Primary template of default_delete, used by unique_ptr 00058 template<typename _Tp> 00059 struct default_delete 00060 { 00061 /// Default constructor 00062 constexpr default_delete() noexcept = default; 00063 00064 /** @brief Converting constructor. 00065 * 00066 * Allows conversion from a deleter for arrays of another type, @p _Up, 00067 * only if @p _Up* is convertible to @p _Tp*. 00068 */ 00069 template<typename _Up, typename = typename 00070 enable_if<is_convertible<_Up*, _Tp*>::value>::type> 00071 default_delete(const default_delete<_Up>&) noexcept { } 00072 00073 /// Calls @c delete @p __ptr 00074 void 00075 operator()(_Tp* __ptr) const 00076 { 00077 static_assert(!is_void<_Tp>::value, 00078 "can't delete pointer to incomplete type"); 00079 static_assert(sizeof(_Tp)>0, 00080 "can't delete pointer to incomplete type"); 00081 delete __ptr; 00082 } 00083 }; 00084 00085 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00086 // DR 740 - omit specialization for array objects with a compile time length 00087 /// Specialization for arrays, default_delete. 00088 template<typename _Tp> 00089 struct default_delete<_Tp[]> 00090 { 00091 public: 00092 /// Default constructor 00093 constexpr default_delete() noexcept = default; 00094 00095 /** @brief Converting constructor. 00096 * 00097 * Allows conversion from a deleter for arrays of another type, such as 00098 * a const-qualified version of @p _Tp. 00099 * 00100 * Conversions from types derived from @c _Tp are not allowed because 00101 * it is unsafe to @c delete[] an array of derived types through a 00102 * pointer to the base type. 00103 */ 00104 template<typename _Up, typename = typename 00105 enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type> 00106 default_delete(const default_delete<_Up[]>&) noexcept { } 00107 00108 /// Calls @c delete[] @p __ptr 00109 template<typename _Up> 00110 typename enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type 00111 operator()(_Up* __ptr) const 00112 { 00113 static_assert(sizeof(_Tp)>0, 00114 "can't delete pointer to incomplete type"); 00115 delete [] __ptr; 00116 } 00117 }; 00118 00119 template <typename _Tp, typename _Dp> 00120 class __uniq_ptr_impl 00121 { 00122 template <typename _Up, typename _Ep, typename = void> 00123 struct _Ptr 00124 { 00125 using type = _Up*; 00126 }; 00127 00128 template <typename _Up, typename _Ep> 00129 struct 00130 _Ptr<_Up, _Ep, __void_t<typename remove_reference<_Ep>::type::pointer>> 00131 { 00132 using type = typename remove_reference<_Ep>::type::pointer; 00133 }; 00134 00135 public: 00136 using _DeleterConstraint = enable_if< 00137 __and_<__not_<is_pointer<_Dp>>, 00138 is_default_constructible<_Dp>>::value>; 00139 00140 using pointer = typename _Ptr<_Tp, _Dp>::type; 00141 00142 __uniq_ptr_impl() = default; 00143 __uniq_ptr_impl(pointer __p) : _M_t() { _M_ptr() = __p; } 00144 00145 template<typename _Del> 00146 __uniq_ptr_impl(pointer __p, _Del&& __d) 00147 : _M_t(__p, std::forward<_Del>(__d)) { } 00148 00149 pointer& _M_ptr() { return std::get<0>(_M_t); } 00150 pointer _M_ptr() const { return std::get<0>(_M_t); } 00151 _Dp& _M_deleter() { return std::get<1>(_M_t); } 00152 const _Dp& _M_deleter() const { return std::get<1>(_M_t); } 00153 00154 void 00155 swap(__uniq_ptr_impl& __rhs) noexcept 00156 { 00157 using std::swap; 00158 swap(this->_M_ptr(), __rhs._M_ptr()); 00159 swap(this->_M_deleter(), __rhs._M_deleter()); 00160 } 00161 00162 private: 00163 tuple<pointer, _Dp> _M_t; 00164 }; 00165 00166 /// 20.7.1.2 unique_ptr for single objects. 00167 template <typename _Tp, typename _Dp = default_delete<_Tp>> 00168 class unique_ptr 00169 { 00170 template <class _Up> 00171 using _DeleterConstraint = 00172 typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type; 00173 00174 __uniq_ptr_impl<_Tp, _Dp> _M_t; 00175 00176 public: 00177 using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer; 00178 using element_type = _Tp; 00179 using deleter_type = _Dp; 00180 00181 // helper template for detecting a safe conversion from another 00182 // unique_ptr 00183 template<typename _Up, typename _Ep> 00184 using __safe_conversion_up = __and_< 00185 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>, 00186 __not_<is_array<_Up>> 00187 >; 00188 00189 // Constructors. 00190 00191 /// Default constructor, creates a unique_ptr that owns nothing. 00192 template <typename _Up = _Dp, 00193 typename = _DeleterConstraint<_Up>> 00194 constexpr unique_ptr() noexcept 00195 : _M_t() 00196 { } 00197 00198 /** Takes ownership of a pointer. 00199 * 00200 * @param __p A pointer to an object of @c element_type 00201 * 00202 * The deleter will be value-initialized. 00203 */ 00204 template <typename _Up = _Dp, 00205 typename = _DeleterConstraint<_Up>> 00206 explicit 00207 unique_ptr(pointer __p) noexcept 00208 : _M_t(__p) 00209 { } 00210 00211 /** Takes ownership of a pointer. 00212 * 00213 * @param __p A pointer to an object of @c element_type 00214 * @param __d A reference to a deleter. 00215 * 00216 * The deleter will be initialized with @p __d 00217 */ 00218 unique_ptr(pointer __p, 00219 typename conditional<is_reference<deleter_type>::value, 00220 deleter_type, const deleter_type&>::type __d) noexcept 00221 : _M_t(__p, __d) { } 00222 00223 /** Takes ownership of a pointer. 00224 * 00225 * @param __p A pointer to an object of @c element_type 00226 * @param __d An rvalue reference to a deleter. 00227 * 00228 * The deleter will be initialized with @p std::move(__d) 00229 */ 00230 unique_ptr(pointer __p, 00231 typename remove_reference<deleter_type>::type&& __d) noexcept 00232 : _M_t(std::move(__p), std::move(__d)) 00233 { static_assert(!std::is_reference<deleter_type>::value, 00234 "rvalue deleter bound to reference"); } 00235 00236 /// Creates a unique_ptr that owns nothing. 00237 template <typename _Up = _Dp, 00238 typename = _DeleterConstraint<_Up>> 00239 constexpr unique_ptr(nullptr_t) noexcept : _M_t() { } 00240 00241 // Move constructors. 00242 00243 /// Move constructor. 00244 unique_ptr(unique_ptr&& __u) noexcept 00245 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { } 00246 00247 /** @brief Converting constructor from another type 00248 * 00249 * Requires that the pointer owned by @p __u is convertible to the 00250 * type of pointer owned by this object, @p __u does not own an array, 00251 * and @p __u has a compatible deleter type. 00252 */ 00253 template<typename _Up, typename _Ep, typename = _Require< 00254 __safe_conversion_up<_Up, _Ep>, 00255 typename conditional<is_reference<_Dp>::value, 00256 is_same<_Ep, _Dp>, 00257 is_convertible<_Ep, _Dp>>::type>> 00258 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept 00259 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter())) 00260 { } 00261 00262 #if _GLIBCXX_USE_DEPRECATED 00263 #pragma GCC diagnostic push 00264 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 00265 /// Converting constructor from @c auto_ptr 00266 template<typename _Up, typename = _Require< 00267 is_convertible<_Up*, _Tp*>, is_same<_Dp, default_delete<_Tp>>>> 00268 unique_ptr(auto_ptr<_Up>&& __u) noexcept; 00269 #pragma GCC diagnostic pop 00270 #endif 00271 00272 /// Destructor, invokes the deleter if the stored pointer is not null. 00273 ~unique_ptr() noexcept 00274 { 00275 auto& __ptr = _M_t._M_ptr(); 00276 if (__ptr != nullptr) 00277 get_deleter()(__ptr); 00278 __ptr = pointer(); 00279 } 00280 00281 // Assignment. 00282 00283 /** @brief Move assignment operator. 00284 * 00285 * @param __u The object to transfer ownership from. 00286 * 00287 * Invokes the deleter first if this object owns a pointer. 00288 */ 00289 unique_ptr& 00290 operator=(unique_ptr&& __u) noexcept 00291 { 00292 reset(__u.release()); 00293 get_deleter() = std::forward<deleter_type>(__u.get_deleter()); 00294 return *this; 00295 } 00296 00297 /** @brief Assignment from another type. 00298 * 00299 * @param __u The object to transfer ownership from, which owns a 00300 * convertible pointer to a non-array object. 00301 * 00302 * Invokes the deleter first if this object owns a pointer. 00303 */ 00304 template<typename _Up, typename _Ep> 00305 typename enable_if< __and_< 00306 __safe_conversion_up<_Up, _Ep>, 00307 is_assignable<deleter_type&, _Ep&&> 00308 >::value, 00309 unique_ptr&>::type 00310 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept 00311 { 00312 reset(__u.release()); 00313 get_deleter() = std::forward<_Ep>(__u.get_deleter()); 00314 return *this; 00315 } 00316 00317 /// Reset the %unique_ptr to empty, invoking the deleter if necessary. 00318 unique_ptr& 00319 operator=(nullptr_t) noexcept 00320 { 00321 reset(); 00322 return *this; 00323 } 00324 00325 // Observers. 00326 00327 /// Dereference the stored pointer. 00328 typename add_lvalue_reference<element_type>::type 00329 operator*() const 00330 { 00331 __glibcxx_assert(get() != pointer()); 00332 return *get(); 00333 } 00334 00335 /// Return the stored pointer. 00336 pointer 00337 operator->() const noexcept 00338 { 00339 _GLIBCXX_DEBUG_PEDASSERT(get() != pointer()); 00340 return get(); 00341 } 00342 00343 /// Return the stored pointer. 00344 pointer 00345 get() const noexcept 00346 { return _M_t._M_ptr(); } 00347 00348 /// Return a reference to the stored deleter. 00349 deleter_type& 00350 get_deleter() noexcept 00351 { return _M_t._M_deleter(); } 00352 00353 /// Return a reference to the stored deleter. 00354 const deleter_type& 00355 get_deleter() const noexcept 00356 { return _M_t._M_deleter(); } 00357 00358 /// Return @c true if the stored pointer is not null. 00359 explicit operator bool() const noexcept 00360 { return get() == pointer() ? false : true; } 00361 00362 // Modifiers. 00363 00364 /// Release ownership of any stored pointer. 00365 pointer 00366 release() noexcept 00367 { 00368 pointer __p = get(); 00369 _M_t._M_ptr() = pointer(); 00370 return __p; 00371 } 00372 00373 /** @brief Replace the stored pointer. 00374 * 00375 * @param __p The new pointer to store. 00376 * 00377 * The deleter will be invoked if a pointer is already owned. 00378 */ 00379 void 00380 reset(pointer __p = pointer()) noexcept 00381 { 00382 using std::swap; 00383 swap(_M_t._M_ptr(), __p); 00384 if (__p != pointer()) 00385 get_deleter()(__p); 00386 } 00387 00388 /// Exchange the pointer and deleter with another object. 00389 void 00390 swap(unique_ptr& __u) noexcept 00391 { 00392 static_assert(__is_swappable<_Dp>::value, "deleter must be swappable"); 00393 _M_t.swap(__u._M_t); 00394 } 00395 00396 // Disable copy from lvalue. 00397 unique_ptr(const unique_ptr&) = delete; 00398 unique_ptr& operator=(const unique_ptr&) = delete; 00399 }; 00400 00401 /// 20.7.1.3 unique_ptr for array objects with a runtime length 00402 // [unique.ptr.runtime] 00403 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00404 // DR 740 - omit specialization for array objects with a compile time length 00405 template<typename _Tp, typename _Dp> 00406 class unique_ptr<_Tp[], _Dp> 00407 { 00408 template <typename _Up> 00409 using _DeleterConstraint = 00410 typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type; 00411 00412 __uniq_ptr_impl<_Tp, _Dp> _M_t; 00413 00414 template<typename _Up> 00415 using __remove_cv = typename remove_cv<_Up>::type; 00416 00417 // like is_base_of<_Tp, _Up> but false if unqualified types are the same 00418 template<typename _Up> 00419 using __is_derived_Tp 00420 = __and_< is_base_of<_Tp, _Up>, 00421 __not_<is_same<__remove_cv<_Tp>, __remove_cv<_Up>>> >; 00422 00423 public: 00424 using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer; 00425 using element_type = _Tp; 00426 using deleter_type = _Dp; 00427 00428 // helper template for detecting a safe conversion from another 00429 // unique_ptr 00430 template<typename _Up, typename _Ep, 00431 typename _UPtr = unique_ptr<_Up, _Ep>, 00432 typename _UP_pointer = typename _UPtr::pointer, 00433 typename _UP_element_type = typename _UPtr::element_type> 00434 using __safe_conversion_up = __and_< 00435 is_array<_Up>, 00436 is_same<pointer, element_type*>, 00437 is_same<_UP_pointer, _UP_element_type*>, 00438 is_convertible<_UP_element_type(*)[], element_type(*)[]> 00439 >; 00440 00441 // helper template for detecting a safe conversion from a raw pointer 00442 template<typename _Up> 00443 using __safe_conversion_raw = __and_< 00444 __or_<__or_<is_same<_Up, pointer>, 00445 is_same<_Up, nullptr_t>>, 00446 __and_<is_pointer<_Up>, 00447 is_same<pointer, element_type*>, 00448 is_convertible< 00449 typename remove_pointer<_Up>::type(*)[], 00450 element_type(*)[]> 00451 > 00452 > 00453 >; 00454 00455 // Constructors. 00456 00457 /// Default constructor, creates a unique_ptr that owns nothing. 00458 template <typename _Up = _Dp, 00459 typename = _DeleterConstraint<_Up>> 00460 constexpr unique_ptr() noexcept 00461 : _M_t() 00462 { } 00463 00464 /** Takes ownership of a pointer. 00465 * 00466 * @param __p A pointer to an array of a type safely convertible 00467 * to an array of @c element_type 00468 * 00469 * The deleter will be value-initialized. 00470 */ 00471 template<typename _Up, 00472 typename _Vp = _Dp, 00473 typename = _DeleterConstraint<_Vp>, 00474 typename = typename enable_if< 00475 __safe_conversion_raw<_Up>::value, bool>::type> 00476 explicit 00477 unique_ptr(_Up __p) noexcept 00478 : _M_t(__p) 00479 { } 00480 00481 /** Takes ownership of a pointer. 00482 * 00483 * @param __p A pointer to an array of a type safely convertible 00484 * to an array of @c element_type 00485 * @param __d A reference to a deleter. 00486 * 00487 * The deleter will be initialized with @p __d 00488 */ 00489 template<typename _Up, 00490 typename = typename enable_if< 00491 __safe_conversion_raw<_Up>::value, bool>::type> 00492 unique_ptr(_Up __p, 00493 typename conditional<is_reference<deleter_type>::value, 00494 deleter_type, const deleter_type&>::type __d) noexcept 00495 : _M_t(__p, __d) { } 00496 00497 /** Takes ownership of a pointer. 00498 * 00499 * @param __p A pointer to an array of a type safely convertible 00500 * to an array of @c element_type 00501 * @param __d A reference to a deleter. 00502 * 00503 * The deleter will be initialized with @p std::move(__d) 00504 */ 00505 template<typename _Up, 00506 typename = typename enable_if< 00507 __safe_conversion_raw<_Up>::value, bool>::type> 00508 unique_ptr(_Up __p, typename 00509 remove_reference<deleter_type>::type&& __d) noexcept 00510 : _M_t(std::move(__p), std::move(__d)) 00511 { static_assert(!is_reference<deleter_type>::value, 00512 "rvalue deleter bound to reference"); } 00513 00514 /// Move constructor. 00515 unique_ptr(unique_ptr&& __u) noexcept 00516 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { } 00517 00518 /// Creates a unique_ptr that owns nothing. 00519 template <typename _Up = _Dp, 00520 typename = _DeleterConstraint<_Up>> 00521 constexpr unique_ptr(nullptr_t) noexcept : _M_t() { } 00522 00523 template<typename _Up, typename _Ep, typename = _Require< 00524 __safe_conversion_up<_Up, _Ep>, 00525 typename conditional<is_reference<_Dp>::value, 00526 is_same<_Ep, _Dp>, 00527 is_convertible<_Ep, _Dp>>::type>> 00528 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept 00529 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter())) 00530 { } 00531 00532 /// Destructor, invokes the deleter if the stored pointer is not null. 00533 ~unique_ptr() 00534 { 00535 auto& __ptr = _M_t._M_ptr(); 00536 if (__ptr != nullptr) 00537 get_deleter()(__ptr); 00538 __ptr = pointer(); 00539 } 00540 00541 // Assignment. 00542 00543 /** @brief Move assignment operator. 00544 * 00545 * @param __u The object to transfer ownership from. 00546 * 00547 * Invokes the deleter first if this object owns a pointer. 00548 */ 00549 unique_ptr& 00550 operator=(unique_ptr&& __u) noexcept 00551 { 00552 reset(__u.release()); 00553 get_deleter() = std::forward<deleter_type>(__u.get_deleter()); 00554 return *this; 00555 } 00556 00557 /** @brief Assignment from another type. 00558 * 00559 * @param __u The object to transfer ownership from, which owns a 00560 * convertible pointer to an array object. 00561 * 00562 * Invokes the deleter first if this object owns a pointer. 00563 */ 00564 template<typename _Up, typename _Ep> 00565 typename 00566 enable_if<__and_<__safe_conversion_up<_Up, _Ep>, 00567 is_assignable<deleter_type&, _Ep&&> 00568 >::value, 00569 unique_ptr&>::type 00570 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept 00571 { 00572 reset(__u.release()); 00573 get_deleter() = std::forward<_Ep>(__u.get_deleter()); 00574 return *this; 00575 } 00576 00577 /// Reset the %unique_ptr to empty, invoking the deleter if necessary. 00578 unique_ptr& 00579 operator=(nullptr_t) noexcept 00580 { 00581 reset(); 00582 return *this; 00583 } 00584 00585 // Observers. 00586 00587 /// Access an element of owned array. 00588 typename std::add_lvalue_reference<element_type>::type 00589 operator[](size_t __i) const 00590 { 00591 __glibcxx_assert(get() != pointer()); 00592 return get()[__i]; 00593 } 00594 00595 /// Return the stored pointer. 00596 pointer 00597 get() const noexcept 00598 { return _M_t._M_ptr(); } 00599 00600 /// Return a reference to the stored deleter. 00601 deleter_type& 00602 get_deleter() noexcept 00603 { return _M_t._M_deleter(); } 00604 00605 /// Return a reference to the stored deleter. 00606 const deleter_type& 00607 get_deleter() const noexcept 00608 { return _M_t._M_deleter(); } 00609 00610 /// Return @c true if the stored pointer is not null. 00611 explicit operator bool() const noexcept 00612 { return get() == pointer() ? false : true; } 00613 00614 // Modifiers. 00615 00616 /// Release ownership of any stored pointer. 00617 pointer 00618 release() noexcept 00619 { 00620 pointer __p = get(); 00621 _M_t._M_ptr() = pointer(); 00622 return __p; 00623 } 00624 00625 /** @brief Replace the stored pointer. 00626 * 00627 * @param __p The new pointer to store. 00628 * 00629 * The deleter will be invoked if a pointer is already owned. 00630 */ 00631 template <typename _Up, 00632 typename = _Require< 00633 __or_<is_same<_Up, pointer>, 00634 __and_<is_same<pointer, element_type*>, 00635 is_pointer<_Up>, 00636 is_convertible< 00637 typename remove_pointer<_Up>::type(*)[], 00638 element_type(*)[] 00639 > 00640 > 00641 > 00642 >> 00643 void 00644 reset(_Up __p) noexcept 00645 { 00646 pointer __ptr = __p; 00647 using std::swap; 00648 swap(_M_t._M_ptr(), __ptr); 00649 if (__ptr != nullptr) 00650 get_deleter()(__ptr); 00651 } 00652 00653 void reset(nullptr_t = nullptr) noexcept 00654 { 00655 reset(pointer()); 00656 } 00657 00658 /// Exchange the pointer and deleter with another object. 00659 void 00660 swap(unique_ptr& __u) noexcept 00661 { 00662 static_assert(__is_swappable<_Dp>::value, "deleter must be swappable"); 00663 _M_t.swap(__u._M_t); 00664 } 00665 00666 // Disable copy from lvalue. 00667 unique_ptr(const unique_ptr&) = delete; 00668 unique_ptr& operator=(const unique_ptr&) = delete; 00669 }; 00670 00671 template<typename _Tp, typename _Dp> 00672 inline 00673 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 00674 // Constrained free swap overload, see p0185r1 00675 typename enable_if<__is_swappable<_Dp>::value>::type 00676 #else 00677 void 00678 #endif 00679 swap(unique_ptr<_Tp, _Dp>& __x, 00680 unique_ptr<_Tp, _Dp>& __y) noexcept 00681 { __x.swap(__y); } 00682 00683 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 00684 template<typename _Tp, typename _Dp> 00685 typename enable_if<!__is_swappable<_Dp>::value>::type 00686 swap(unique_ptr<_Tp, _Dp>&, 00687 unique_ptr<_Tp, _Dp>&) = delete; 00688 #endif 00689 00690 template<typename _Tp, typename _Dp, 00691 typename _Up, typename _Ep> 00692 inline bool 00693 operator==(const unique_ptr<_Tp, _Dp>& __x, 00694 const unique_ptr<_Up, _Ep>& __y) 00695 { return __x.get() == __y.get(); } 00696 00697 template<typename _Tp, typename _Dp> 00698 inline bool 00699 operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept 00700 { return !__x; } 00701 00702 template<typename _Tp, typename _Dp> 00703 inline bool 00704 operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept 00705 { return !__x; } 00706 00707 template<typename _Tp, typename _Dp, 00708 typename _Up, typename _Ep> 00709 inline bool 00710 operator!=(const unique_ptr<_Tp, _Dp>& __x, 00711 const unique_ptr<_Up, _Ep>& __y) 00712 { return __x.get() != __y.get(); } 00713 00714 template<typename _Tp, typename _Dp> 00715 inline bool 00716 operator!=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept 00717 { return (bool)__x; } 00718 00719 template<typename _Tp, typename _Dp> 00720 inline bool 00721 operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept 00722 { return (bool)__x; } 00723 00724 template<typename _Tp, typename _Dp, 00725 typename _Up, typename _Ep> 00726 inline bool 00727 operator<(const unique_ptr<_Tp, _Dp>& __x, 00728 const unique_ptr<_Up, _Ep>& __y) 00729 { 00730 typedef typename 00731 std::common_type<typename unique_ptr<_Tp, _Dp>::pointer, 00732 typename unique_ptr<_Up, _Ep>::pointer>::type _CT; 00733 return std::less<_CT>()(__x.get(), __y.get()); 00734 } 00735 00736 template<typename _Tp, typename _Dp> 00737 inline bool 00738 operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) 00739 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(), 00740 nullptr); } 00741 00742 template<typename _Tp, typename _Dp> 00743 inline bool 00744 operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) 00745 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr, 00746 __x.get()); } 00747 00748 template<typename _Tp, typename _Dp, 00749 typename _Up, typename _Ep> 00750 inline bool 00751 operator<=(const unique_ptr<_Tp, _Dp>& __x, 00752 const unique_ptr<_Up, _Ep>& __y) 00753 { return !(__y < __x); } 00754 00755 template<typename _Tp, typename _Dp> 00756 inline bool 00757 operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) 00758 { return !(nullptr < __x); } 00759 00760 template<typename _Tp, typename _Dp> 00761 inline bool 00762 operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) 00763 { return !(__x < nullptr); } 00764 00765 template<typename _Tp, typename _Dp, 00766 typename _Up, typename _Ep> 00767 inline bool 00768 operator>(const unique_ptr<_Tp, _Dp>& __x, 00769 const unique_ptr<_Up, _Ep>& __y) 00770 { return (__y < __x); } 00771 00772 template<typename _Tp, typename _Dp> 00773 inline bool 00774 operator>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) 00775 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr, 00776 __x.get()); } 00777 00778 template<typename _Tp, typename _Dp> 00779 inline bool 00780 operator>(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) 00781 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(), 00782 nullptr); } 00783 00784 template<typename _Tp, typename _Dp, 00785 typename _Up, typename _Ep> 00786 inline bool 00787 operator>=(const unique_ptr<_Tp, _Dp>& __x, 00788 const unique_ptr<_Up, _Ep>& __y) 00789 { return !(__x < __y); } 00790 00791 template<typename _Tp, typename _Dp> 00792 inline bool 00793 operator>=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) 00794 { return !(__x < nullptr); } 00795 00796 template<typename _Tp, typename _Dp> 00797 inline bool 00798 operator>=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) 00799 { return !(nullptr < __x); } 00800 00801 /// std::hash specialization for unique_ptr. 00802 template<typename _Tp, typename _Dp> 00803 struct hash<unique_ptr<_Tp, _Dp>> 00804 : public __hash_base<size_t, unique_ptr<_Tp, _Dp>>, 00805 private __poison_hash<typename unique_ptr<_Tp, _Dp>::pointer> 00806 { 00807 size_t 00808 operator()(const unique_ptr<_Tp, _Dp>& __u) const noexcept 00809 { 00810 typedef unique_ptr<_Tp, _Dp> _UP; 00811 return std::hash<typename _UP::pointer>()(__u.get()); 00812 } 00813 }; 00814 00815 #if __cplusplus > 201103L 00816 00817 #define __cpp_lib_make_unique 201304 00818 00819 template<typename _Tp> 00820 struct _MakeUniq 00821 { typedef unique_ptr<_Tp> __single_object; }; 00822 00823 template<typename _Tp> 00824 struct _MakeUniq<_Tp[]> 00825 { typedef unique_ptr<_Tp[]> __array; }; 00826 00827 template<typename _Tp, size_t _Bound> 00828 struct _MakeUniq<_Tp[_Bound]> 00829 { struct __invalid_type { }; }; 00830 00831 /// std::make_unique for single objects 00832 template<typename _Tp, typename... _Args> 00833 inline typename _MakeUniq<_Tp>::__single_object 00834 make_unique(_Args&&... __args) 00835 { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); } 00836 00837 /// std::make_unique for arrays of unknown bound 00838 template<typename _Tp> 00839 inline typename _MakeUniq<_Tp>::__array 00840 make_unique(size_t __num) 00841 { return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]()); } 00842 00843 /// Disable std::make_unique for arrays of known bound 00844 template<typename _Tp, typename... _Args> 00845 inline typename _MakeUniq<_Tp>::__invalid_type 00846 make_unique(_Args&&...) = delete; 00847 #endif 00848 00849 // @} group pointer_abstractions 00850 00851 _GLIBCXX_END_NAMESPACE_VERSION 00852 } // namespace 00853 00854 #endif /* _UNIQUE_PTR_H */