31 #define _UNIQUE_PTR_H 1 41 namespace std _GLIBCXX_VISIBILITY(default)
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 #if _GLIBCXX_USE_DEPRECATED 51 template<
typename>
class auto_ptr;
55 template<
typename _Tp>
66 template<
typename _Up,
typename =
typename 67 enable_if<is_convertible<_Up*, _Tp*>::value>::type>
75 "can't delete pointer to incomplete type");
76 static_assert(
sizeof(_Tp)>0,
77 "can't delete pointer to incomplete type");
85 template<
typename _Tp>
101 template<
typename _Up,
typename =
typename 102 enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type>
106 template<
typename _Up>
107 typename enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type
110 static_assert(
sizeof(_Tp)>0,
111 "can't delete pointer to incomplete type");
116 template <
typename _Tp,
typename _Dp>
117 class __uniq_ptr_impl
119 template <
typename _Up,
typename _Ep,
typename =
void>
125 template <
typename _Up,
typename _Ep>
127 _Ptr<_Up, _Ep, __void_t<typename remove_reference<_Ep>::type::pointer>>
129 using type =
typename remove_reference<_Ep>::type::pointer;
133 using _DeleterConstraint = enable_if<
134 __and_<__not_<is_pointer<_Dp>>,
135 is_default_constructible<_Dp>>::value>;
137 using pointer =
typename _Ptr<_Tp, _Dp>::type;
139 __uniq_ptr_impl() =
default;
140 __uniq_ptr_impl(pointer __p) : _M_t() { _M_ptr() = __p; }
142 template<
typename _Del>
143 __uniq_ptr_impl(pointer __p, _Del&& __d)
144 : _M_t(__p, std::forward<_Del>(__d)) { }
146 pointer& _M_ptr() {
return std::get<0>(_M_t); }
147 pointer _M_ptr()
const {
return std::get<0>(_M_t); }
148 _Dp& _M_deleter() {
return std::get<1>(_M_t); }
149 const _Dp& _M_deleter()
const {
return std::get<1>(_M_t); }
156 template <
typename _Tp,
typename _Dp = default_delete<_Tp>>
160 using _DeleterConstraint =
161 typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
163 __uniq_ptr_impl<_Tp, _Dp> _M_t;
166 using pointer =
typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
167 using element_type = _Tp;
168 using deleter_type = _Dp;
172 template<
typename _Up,
typename _Ep>
173 using __safe_conversion_up = __and_<
174 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>,
175 __not_<is_array<_Up>>
181 template <
typename _Up = _Dp,
182 typename = _DeleterConstraint<_Up>>
193 template <
typename _Up = _Dp,
194 typename = _DeleterConstraint<_Up>>
220 typename remove_reference<deleter_type>::type&& __d) noexcept
221 : _M_t(
std::move(__p),
std::move(__d))
223 "rvalue deleter bound to reference"); }
226 template <
typename _Up = _Dp,
227 typename = _DeleterConstraint<_Up>>
242 template<
typename _Up,
typename _Ep,
typename = _Require<
243 __safe_conversion_up<_Up, _Ep>,
244 typename conditional<is_reference<_Dp>::value,
246 is_convertible<_Ep, _Dp>>::type>>
251 #if _GLIBCXX_USE_DEPRECATED 253 template<
typename _Up,
typename = _Require<
254 is_convertible<_Up*, _Tp*>, is_same<_Dp, default_delete<_Tp>>>>
261 auto& __ptr = _M_t._M_ptr();
262 if (__ptr !=
nullptr)
278 reset(__u.release());
279 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
290 template<
typename _Up,
typename _Ep>
291 typename enable_if< __and_<
292 __safe_conversion_up<_Up, _Ep>,
293 is_assignable<deleter_type&, _Ep&&>
298 reset(__u.release());
299 get_deleter() = std::forward<_Ep>(__u.get_deleter());
314 typename add_lvalue_reference<element_type>::type
317 __glibcxx_assert(
get() != pointer());
325 _GLIBCXX_DEBUG_PEDASSERT(
get() != pointer());
332 {
return _M_t._M_ptr(); }
337 {
return _M_t._M_deleter(); }
342 {
return _M_t._M_deleter(); }
345 explicit operator bool() const noexcept
346 {
return get() == pointer() ?
false :
true; }
355 _M_t._M_ptr() = pointer();
366 reset(pointer __p = pointer()) noexcept
369 swap(_M_t._M_ptr(), __p);
370 if (__p != pointer())
379 swap(_M_t, __u._M_t);
391 template<
typename _Tp,
typename _Dp>
394 template <
typename _Up>
395 using _DeleterConstraint =
396 typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
398 __uniq_ptr_impl<_Tp, _Dp> _M_t;
400 template<
typename _Up>
401 using __remove_cv =
typename remove_cv<_Up>::type;
404 template<
typename _Up>
405 using __is_derived_Tp
406 = __and_< is_base_of<_Tp, _Up>,
407 __not_<is_same<__remove_cv<_Tp>, __remove_cv<_Up>>> >;
410 using pointer =
typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
411 using element_type = _Tp;
412 using deleter_type = _Dp;
416 template<
typename _Up,
typename _Ep,
418 typename _UP_pointer =
typename _UPtr::pointer,
419 typename _UP_element_type =
typename _UPtr::element_type>
420 using __safe_conversion_up = __and_<
422 is_same<pointer, element_type*>,
423 is_same<_UP_pointer, _UP_element_type*>,
424 is_convertible<_UP_element_type(*)[], element_type(*)[]>
428 template<
typename _Up>
429 using __safe_conversion_raw = __and_<
430 __or_<__or_<is_same<_Up, pointer>,
431 is_same<_Up, nullptr_t>>,
432 __and_<is_pointer<_Up>,
433 is_same<pointer, element_type*>,
435 typename remove_pointer<_Up>::type(*)[],
444 template <
typename _Up = _Dp,
445 typename = _DeleterConstraint<_Up>>
457 template<
typename _Up,
459 typename = _DeleterConstraint<_Vp>,
460 typename =
typename enable_if<
461 __safe_conversion_raw<_Up>::value,
bool>::type>
475 template<
typename _Up,
476 typename =
typename enable_if<
477 __safe_conversion_raw<_Up>::value,
bool>::type>
480 deleter_type,
const deleter_type&>::type __d) noexcept
491 template<
typename _Up,
492 typename =
typename enable_if<
493 __safe_conversion_raw<_Up>::value,
bool>::type>
495 remove_reference<deleter_type>::type&& __d) noexcept
496 : _M_t(
std::move(__p),
std::move(__d))
498 "rvalue deleter bound to reference"); }
505 template <
typename _Up = _Dp,
506 typename = _DeleterConstraint<_Up>>
509 template<
typename _Up,
typename _Ep,
typename = _Require<
510 __safe_conversion_up<_Up, _Ep>,
511 typename conditional<is_reference<_Dp>::value,
513 is_convertible<_Ep, _Dp>>::type>>
515 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
521 auto& __ptr = _M_t._M_ptr();
522 if (__ptr !=
nullptr)
538 reset(__u.release());
539 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
550 template<
typename _Up,
typename _Ep>
552 enable_if<__and_<__safe_conversion_up<_Up, _Ep>,
553 is_assignable<deleter_type&, _Ep&&>
558 reset(__u.release());
559 get_deleter() = std::forward<_Ep>(__u.get_deleter());
574 typename std::add_lvalue_reference<element_type>::type
577 __glibcxx_assert(
get() != pointer());
584 {
return _M_t._M_ptr(); }
589 {
return _M_t._M_deleter(); }
594 {
return _M_t._M_deleter(); }
597 explicit operator bool() const noexcept
598 {
return get() == pointer() ?
false :
true; }
607 _M_t._M_ptr() = pointer();
617 template <
typename _Up,
619 __or_<is_same<_Up, pointer>,
620 __and_<is_same<pointer, element_type*>,
623 typename remove_pointer<_Up>::type(*)[],
634 swap(_M_t._M_ptr(), __ptr);
635 if (__ptr !=
nullptr)
639 void reset(nullptr_t =
nullptr) noexcept
649 swap(_M_t, __u._M_t);
657 template<
typename _Tp,
typename _Dp>
659 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 661 typename enable_if<__is_swappable<_Dp>::value>::type
669 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 670 template<
typename _Tp,
typename _Dp>
671 typename enable_if<!__is_swappable<_Dp>::value>::type
676 template<
typename _Tp,
typename _Dp,
677 typename _Up,
typename _Ep>
681 {
return __x.
get() == __y.
get(); }
683 template<
typename _Tp,
typename _Dp>
688 template<
typename _Tp,
typename _Dp>
693 template<
typename _Tp,
typename _Dp,
694 typename _Up,
typename _Ep>
698 {
return __x.
get() != __y.
get(); }
700 template<
typename _Tp,
typename _Dp>
703 {
return (
bool)__x; }
705 template<
typename _Tp,
typename _Dp>
708 {
return (
bool)__x; }
710 template<
typename _Tp,
typename _Dp,
711 typename _Up,
typename _Ep>
713 operator<(const unique_ptr<_Tp, _Dp>& __x,
717 std::common_type<typename unique_ptr<_Tp, _Dp>::pointer,
718 typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
722 template<
typename _Tp,
typename _Dp>
724 operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
728 template<
typename _Tp,
typename _Dp>
730 operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
734 template<
typename _Tp,
typename _Dp,
735 typename _Up,
typename _Ep>
737 operator<=(const unique_ptr<_Tp, _Dp>& __x,
739 {
return !(__y < __x); }
741 template<
typename _Tp,
typename _Dp>
743 operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
744 {
return !(
nullptr < __x); }
746 template<
typename _Tp,
typename _Dp>
748 operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
749 {
return !(__x <
nullptr); }
751 template<
typename _Tp,
typename _Dp,
752 typename _Up,
typename _Ep>
756 {
return (__y < __x); }
758 template<
typename _Tp,
typename _Dp>
764 template<
typename _Tp,
typename _Dp>
770 template<
typename _Tp,
typename _Dp,
771 typename _Up,
typename _Ep>
775 {
return !(__x < __y); }
777 template<
typename _Tp,
typename _Dp>
780 {
return !(__x <
nullptr); }
782 template<
typename _Tp,
typename _Dp>
785 {
return !(
nullptr < __x); }
788 template<
typename _Tp,
typename _Dp>
790 :
public __hash_base<size_t, unique_ptr<_Tp, _Dp>>,
791 private __poison_hash<typename unique_ptr<_Tp, _Dp>::pointer>
801 #if __cplusplus > 201103L 803 #define __cpp_lib_make_unique 201304 805 template<
typename _Tp>
809 template<
typename _Tp>
810 struct _MakeUniq<_Tp[]>
813 template<
typename _Tp,
size_t _Bound>
814 struct _MakeUniq<_Tp[_Bound]>
815 {
struct __invalid_type { }; };
818 template<
typename _Tp,
typename... _Args>
824 template<
typename _Tp>
825 inline typename _MakeUniq<_Tp>::__array
830 template<
typename _Tp,
typename... _Args>
831 inline typename _MakeUniq<_Tp>::__invalid_type
837 _GLIBCXX_END_NAMESPACE_VERSION
deleter_type & get_deleter() noexcept
Return a reference to the stored deleter.
unique_ptr(unique_ptr &&__u) noexcept
Move constructor.
ISO C++ entities toplevel namespace is std.
unique_ptr & operator=(unique_ptr &&__u) noexcept
Move assignment operator.
A simple smart pointer providing strict ownership semantics.
~unique_ptr()
Destructor, invokes the deleter if the stored pointer is not null.
unique_ptr(unique_ptr< _Up, _Ep > &&__u) noexcept
Converting constructor from another type.
void reset(pointer __p=pointer()) noexcept
Replace the stored pointer.
const deleter_type & get_deleter() const noexcept
Return a reference to the stored deleter.
add_lvalue_reference< element_type >::type operator*() const
Dereference the stored pointer.
unique_ptr & operator=(unique_ptr &&__u) noexcept
Move assignment operator.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
pointer release() noexcept
Release ownership of any stored pointer.
_Del * get_deleter(const __shared_ptr< _Tp, _Lp > &__p) noexcept
20.7.2.2.10 shared_ptr get_deleter
constexpr unique_ptr() noexcept
Default constructor, creates a unique_ptr that owns nothing.
constexpr default_delete() noexcept=default
Default constructor.
unique_ptr(pointer __p, typename remove_reference< deleter_type >::type &&__d) noexcept
constexpr unique_ptr() noexcept
Default constructor, creates a unique_ptr that owns nothing.
unique_ptr & operator=(nullptr_t) noexcept
Reset the unique_ptr to empty, invoking the deleter if necessary.
deleter_type & get_deleter() noexcept
Return a reference to the stored deleter.
unique_ptr(unique_ptr &&__u) noexcept
Move constructor.
pointer get() const noexcept
Return the stored pointer.
default_delete(const default_delete< _Up > &) noexcept
Converting constructor.
unique_ptr & operator=(nullptr_t) noexcept
Reset the unique_ptr to empty, invoking the deleter if necessary.
default_delete(const default_delete< _Up[]> &) noexcept
Converting constructor.
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.
void operator()(_Tp *__ptr) const
Calls delete __ptr.
20.7.1.2 unique_ptr for single objects.
void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
std::add_lvalue_reference< element_type >::type operator[](size_t __i) const
Access an element of owned array.
unique_ptr(_Up __p, typename conditional< is_reference< deleter_type >::value, deleter_type, const deleter_type & >::type __d) noexcept
One of the comparison functors.
enable_if< is_convertible< _Up(*)[], _Tp(*)[]>::value >::type operator()(_Up *__ptr) const
Calls delete[] __ptr.
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.
pointer operator->() const noexcept
Return the stored pointer.
_MakeUniq< _Tp >::__single_object make_unique(_Args &&...__args)
std::make_unique for single objects
constexpr unique_ptr(nullptr_t) noexcept
Creates a unique_ptr that owns nothing.
unique_ptr(pointer __p, typename conditional< is_reference< deleter_type >::value, deleter_type, const deleter_type & >::type __d) noexcept
void reset(_Up __p) noexcept
Replace the stored pointer.
pointer release() noexcept
Release ownership of any stored pointer.
const deleter_type & get_deleter() const noexcept
Return a reference to the stored deleter.
void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
unique_ptr(pointer __p) noexcept
~unique_ptr() noexcept
Destructor, invokes the deleter if the stored pointer is not null.
Primary class template hash.
constexpr unique_ptr(nullptr_t) noexcept
Creates a unique_ptr that owns nothing.
unique_ptr(_Up __p, typename remove_reference< deleter_type >::type &&__d) noexcept
Primary template of default_delete, used by unique_ptr.
unique_ptr(_Up __p) noexcept