31 #define _UNIQUE_PTR_H 1 41 namespace std _GLIBCXX_VISIBILITY(default)
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 #if _GLIBCXX_USE_DEPRECATED 51 #pragma GCC diagnostic push 52 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 53 template<
typename>
class auto_ptr;
54 #pragma GCC diagnostic pop 58 template<
typename _Tp>
69 template<
typename _Up,
typename =
typename 78 "can't delete pointer to incomplete type");
79 static_assert(
sizeof(_Tp)>0,
80 "can't delete pointer to incomplete type");
88 template<
typename _Tp>
104 template<
typename _Up,
typename =
typename 109 template<
typename _Up>
113 static_assert(
sizeof(_Tp)>0,
114 "can't delete pointer to incomplete type");
119 template <
typename _Tp,
typename _Dp>
120 class __uniq_ptr_impl
122 template <
typename _Up,
typename _Ep,
typename =
void>
128 template <
typename _Up,
typename _Ep>
130 _Ptr<_Up, _Ep, __void_t<typename remove_reference<_Ep>::type::pointer>>
132 using type =
typename remove_reference<_Ep>::type::pointer;
136 using _DeleterConstraint = enable_if<
137 __and_<__not_<is_pointer<_Dp>>,
138 is_default_constructible<_Dp>>::value>;
140 using pointer =
typename _Ptr<_Tp, _Dp>::type;
142 __uniq_ptr_impl() =
default;
143 __uniq_ptr_impl(pointer __p) : _M_t() { _M_ptr() = __p; }
145 template<
typename _Del>
146 __uniq_ptr_impl(pointer __p, _Del&& __d)
149 pointer& _M_ptr() {
return std::get<0>(_M_t); }
150 pointer _M_ptr()
const {
return std::get<0>(_M_t); }
151 _Dp& _M_deleter() {
return std::get<1>(_M_t); }
152 const _Dp& _M_deleter()
const {
return std::get<1>(_M_t); }
155 swap(__uniq_ptr_impl& __rhs) noexcept
158 swap(this->_M_ptr(), __rhs._M_ptr());
159 swap(this->_M_deleter(), __rhs._M_deleter());
163 tuple<pointer, _Dp> _M_t;
167 template <
typename _Tp,
typename _Dp = default_delete<_Tp>>
171 using _DeleterConstraint =
172 typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
174 __uniq_ptr_impl<_Tp, _Dp> _M_t;
177 using pointer =
typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
178 using element_type = _Tp;
179 using deleter_type = _Dp;
183 template<
typename _Up,
typename _Ep>
184 using __safe_conversion_up = __and_<
186 __not_<is_array<_Up>>
192 template <
typename _Up = _Dp,
193 typename = _DeleterConstraint<_Up>>
204 template <
typename _Up = _Dp,
205 typename = _DeleterConstraint<_Up>>
231 typename remove_reference<deleter_type>::type&& __d) noexcept
232 : _M_t(std::move(__p), std::move(__d))
234 "rvalue deleter bound to reference"); }
237 template <
typename _Up = _Dp,
238 typename = _DeleterConstraint<_Up>>
245 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
253 template<
typename _Up,
typename _Ep,
typename = _Require<
254 __safe_conversion_up<_Up, _Ep>,
259 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
262 #if _GLIBCXX_USE_DEPRECATED 263 #pragma GCC diagnostic push 264 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 266 template<
typename _Up,
typename = _Require<
269 #pragma GCC diagnostic pop 275 auto& __ptr = _M_t._M_ptr();
276 if (__ptr !=
nullptr)
292 reset(__u.release());
293 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
304 template<
typename _Up,
typename _Ep>
306 __safe_conversion_up<_Up, _Ep>,
312 reset(__u.release());
313 get_deleter() = std::forward<_Ep>(__u.get_deleter());
328 typename add_lvalue_reference<element_type>::type
331 __glibcxx_assert(
get() != pointer());
339 _GLIBCXX_DEBUG_PEDASSERT(
get() != pointer());
346 {
return _M_t._M_ptr(); }
351 {
return _M_t._M_deleter(); }
356 {
return _M_t._M_deleter(); }
359 explicit operator bool() const noexcept
360 {
return get() == pointer() ? false :
true; }
369 _M_t._M_ptr() = pointer();
380 reset(pointer __p = pointer()) noexcept
383 swap(_M_t._M_ptr(), __p);
384 if (__p != pointer())
392 static_assert(__is_swappable<_Dp>::value,
"deleter must be swappable");
405 template<
typename _Tp,
typename _Dp>
408 template <
typename _Up>
409 using _DeleterConstraint =
410 typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
412 __uniq_ptr_impl<_Tp, _Dp> _M_t;
414 template<
typename _Up>
415 using __remove_cv =
typename remove_cv<_Up>::type;
418 template<
typename _Up>
419 using __is_derived_Tp
420 = __and_< is_base_of<_Tp, _Up>,
421 __not_<is_same<__remove_cv<_Tp>, __remove_cv<_Up>>> >;
424 using pointer =
typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
425 using element_type = _Tp;
426 using deleter_type = _Dp;
430 template<
typename _Up,
typename _Ep,
432 typename _UP_pointer =
typename _UPtr::pointer,
433 typename _UP_element_type =
typename _UPtr::element_type>
434 using __safe_conversion_up = __and_<
442 template<
typename _Up>
443 using __safe_conversion_raw = __and_<
444 __or_<__or_<is_same<_Up, pointer>,
446 __and_<is_pointer<_Up>,
449 typename remove_pointer<_Up>::type(*)[],
458 template <
typename _Up = _Dp,
459 typename = _DeleterConstraint<_Up>>
471 template<
typename _Up,
473 typename = _DeleterConstraint<_Vp>,
475 __safe_conversion_raw<_Up>::value,
bool>::type>
489 template<
typename _Up,
491 __safe_conversion_raw<_Up>::value,
bool>::type>
494 deleter_type,
const deleter_type&>::type __d) noexcept
505 template<
typename _Up,
507 __safe_conversion_raw<_Up>::value,
bool>::type>
509 remove_reference<deleter_type>::type&& __d) noexcept
510 : _M_t(std::move(__p), std::move(__d))
512 "rvalue deleter bound to reference"); }
516 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
519 template <
typename _Up = _Dp,
520 typename = _DeleterConstraint<_Up>>
523 template<
typename _Up,
typename _Ep,
typename = _Require<
524 __safe_conversion_up<_Up, _Ep>,
529 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
535 auto& __ptr = _M_t._M_ptr();
536 if (__ptr !=
nullptr)
552 reset(__u.release());
553 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
564 template<
typename _Up,
typename _Ep>
572 reset(__u.release());
573 get_deleter() = std::forward<_Ep>(__u.get_deleter());
588 typename std::add_lvalue_reference<element_type>::type
591 __glibcxx_assert(
get() != pointer());
598 {
return _M_t._M_ptr(); }
603 {
return _M_t._M_deleter(); }
608 {
return _M_t._M_deleter(); }
611 explicit operator bool() const noexcept
612 {
return get() == pointer() ? false :
true; }
621 _M_t._M_ptr() = pointer();
631 template <
typename _Up,
633 __or_<is_same<_Up, pointer>,
634 __and_<is_same<pointer, element_type*>,
637 typename remove_pointer<_Up>::type(*)[],
648 swap(_M_t._M_ptr(), __ptr);
649 if (__ptr !=
nullptr)
653 void reset(nullptr_t =
nullptr) noexcept
662 static_assert(__is_swappable<_Dp>::value,
"deleter must be swappable");
671 template<
typename _Tp,
typename _Dp>
673 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 675 typename enable_if<__is_swappable<_Dp>::value>::type
679 swap(unique_ptr<_Tp, _Dp>& __x,
680 unique_ptr<_Tp, _Dp>& __y) noexcept
683 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 684 template<
typename _Tp,
typename _Dp>
685 typename enable_if<!__is_swappable<_Dp>::value>::type
686 swap(unique_ptr<_Tp, _Dp>&,
687 unique_ptr<_Tp, _Dp>&) =
delete;
690 template<
typename _Tp,
typename _Dp,
691 typename _Up,
typename _Ep>
693 operator==(
const unique_ptr<_Tp, _Dp>& __x,
694 const unique_ptr<_Up, _Ep>& __y)
695 {
return __x.get() == __y.get(); }
697 template<
typename _Tp,
typename _Dp>
699 operator==(
const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
702 template<
typename _Tp,
typename _Dp>
704 operator==(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x) noexcept
707 template<
typename _Tp,
typename _Dp,
708 typename _Up,
typename _Ep>
710 operator!=(
const unique_ptr<_Tp, _Dp>& __x,
711 const unique_ptr<_Up, _Ep>& __y)
712 {
return __x.get() != __y.get(); }
714 template<
typename _Tp,
typename _Dp>
716 operator!=(
const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
717 {
return (
bool)__x; }
719 template<
typename _Tp,
typename _Dp>
721 operator!=(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x) noexcept
722 {
return (
bool)__x; }
724 template<
typename _Tp,
typename _Dp,
725 typename _Up,
typename _Ep>
727 operator<(
const unique_ptr<_Tp, _Dp>& __x,
728 const unique_ptr<_Up, _Ep>& __y)
732 typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
736 template<
typename _Tp,
typename _Dp>
738 operator<(
const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
742 template<
typename _Tp,
typename _Dp>
744 operator<(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x)
748 template<
typename _Tp,
typename _Dp,
749 typename _Up,
typename _Ep>
751 operator<=(
const unique_ptr<_Tp, _Dp>& __x,
752 const unique_ptr<_Up, _Ep>& __y)
753 {
return !(__y < __x); }
755 template<
typename _Tp,
typename _Dp>
757 operator<=(
const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
758 {
return !(
nullptr < __x); }
760 template<
typename _Tp,
typename _Dp>
762 operator<=(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x)
763 {
return !(__x <
nullptr); }
765 template<
typename _Tp,
typename _Dp,
766 typename _Up,
typename _Ep>
768 operator>(
const unique_ptr<_Tp, _Dp>& __x,
769 const unique_ptr<_Up, _Ep>& __y)
770 {
return (__y < __x); }
772 template<
typename _Tp,
typename _Dp>
774 operator>(
const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
778 template<
typename _Tp,
typename _Dp>
780 operator>(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x)
784 template<
typename _Tp,
typename _Dp,
785 typename _Up,
typename _Ep>
787 operator>=(
const unique_ptr<_Tp, _Dp>& __x,
788 const unique_ptr<_Up, _Ep>& __y)
789 {
return !(__x < __y); }
791 template<
typename _Tp,
typename _Dp>
793 operator>=(
const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
794 {
return !(__x <
nullptr); }
796 template<
typename _Tp,
typename _Dp>
798 operator>=(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x)
799 {
return !(
nullptr < __x); }
802 template<
typename _Tp,
typename _Dp>
804 :
public __hash_base<size_t, unique_ptr<_Tp, _Dp>>,
805 private __poison_hash<typename unique_ptr<_Tp, _Dp>::pointer>
815 #if __cplusplus > 201103L 817 #define __cpp_lib_make_unique 201304 819 template<
typename _Tp>
823 template<
typename _Tp>
824 struct _MakeUniq<_Tp[]>
825 {
typedef unique_ptr<_Tp[]> __array; };
827 template<
typename _Tp,
size_t _Bound>
828 struct _MakeUniq<_Tp[_Bound]>
829 {
struct __invalid_type { }; };
832 template<
typename _Tp,
typename... _Args>
833 inline typename _MakeUniq<_Tp>::__single_object
838 template<
typename _Tp>
839 inline typename _MakeUniq<_Tp>::__array
844 template<
typename _Tp,
typename... _Args>
845 inline typename _MakeUniq<_Tp>::__invalid_type
851 _GLIBCXX_END_NAMESPACE_VERSION
void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
One of the comparison functors.
20.7.1.2 unique_ptr for single objects.
default_delete(const default_delete< _Up > &) noexcept
Converting constructor.
constexpr unique_ptr(nullptr_t) noexcept
Creates a unique_ptr that owns nothing.
void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
A simple smart pointer providing strict ownership semantics.
unique_ptr(unique_ptr &&__u) noexcept
Move constructor.
unique_ptr(unique_ptr &&__u) noexcept
Move constructor.
~unique_ptr()
Destructor, invokes the deleter if the stored pointer is not null.
enable_if< is_convertible< _Up(*)[], _Tp(*)[]>::value >::type operator()(_Up *__ptr) const
Calls delete[] __ptr.
Define a member typedef type only if a boolean constant is true.
constexpr unique_ptr(nullptr_t) noexcept
Creates a unique_ptr that owns nothing.
void reset(_Up __p) noexcept
Replace the stored pointer.
Primary class template hash.
Define a member typedef type to one of two argument types.
pointer get() const noexcept
Return the stored pointer.
std::add_lvalue_reference< element_type >::type operator[](size_t __i) const
Access an element of owned array.
pointer release() noexcept
Release ownership of any stored pointer.
typename remove_extent< _Tp >::type remove_extent_t
Alias template for remove_extent.
ISO C++ entities toplevel namespace is std.
add_lvalue_reference< element_type >::type operator*() const
Dereference the stored pointer.
const deleter_type & get_deleter() const noexcept
Return a reference to the stored deleter.
deleter_type & get_deleter() noexcept
Return a reference to the stored deleter.
unique_ptr & operator=(unique_ptr &&__u) noexcept
Move assignment operator.
pointer release() noexcept
Release ownership of any stored pointer.
constexpr unique_ptr() noexcept
Default constructor, creates a unique_ptr that owns nothing.
unique_ptr(_Up __p, typename remove_reference< deleter_type >::type &&__d) noexcept
~unique_ptr() noexcept
Destructor, invokes the deleter if the stored pointer is not null.
_MakeUniq< _Tp >::__single_object make_unique(_Args &&... __args)
std::make_unique for single objects
unique_ptr(_Up __p) noexcept
default_delete(const default_delete< _Up[]> &) noexcept
Converting constructor.
void operator()(_Tp *__ptr) const
Calls delete __ptr.
void reset(pointer __p=pointer()) noexcept
Replace the stored pointer.
unique_ptr & operator=(nullptr_t) noexcept
Reset the unique_ptr to empty, invoking the deleter if necessary.
pointer operator->() const noexcept
Return the stored pointer.
unique_ptr(pointer __p, typename remove_reference< deleter_type >::type &&__d) noexcept
unique_ptr & operator=(unique_ptr &&__u) noexcept
Move assignment operator.
enable_if< __and_< __safe_conversion_up< _Up, _Ep >, is_assignable< deleter_type &, _Ep && > >::value, unique_ptr & >::type operator=(unique_ptr< _Up, _Ep > &&__u) noexcept
Assignment from another type.
unique_ptr(_Up __p, typename conditional< is_reference< deleter_type >::value, deleter_type, const deleter_type & >::type __d) noexcept
unique_ptr(pointer __p, typename conditional< is_reference< deleter_type >::value, deleter_type, const deleter_type & >::type __d) noexcept
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
enable_if< __and_< __safe_conversion_up< _Up, _Ep >, is_assignable< deleter_type &, _Ep && > >::value, unique_ptr & >::type operator=(unique_ptr< _Up, _Ep > &&__u) noexcept
Assignment from another type.
deleter_type & get_deleter() noexcept
Return a reference to the stored deleter.
unique_ptr(unique_ptr< _Up, _Ep > &&__u) noexcept
Converting constructor from another type.
pointer get() const noexcept
Return the stored pointer.
const deleter_type & get_deleter() const noexcept
Return a reference to the stored deleter.
unique_ptr & operator=(nullptr_t) noexcept
Reset the unique_ptr to empty, invoking the deleter if necessary.
constexpr default_delete() noexcept=default
Default constructor.
Primary template of default_delete, used by unique_ptr.
unique_ptr(pointer __p) noexcept
constexpr unique_ptr() noexcept
Default constructor, creates a unique_ptr that owns nothing.