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 pointer =
typename _Ptr<_Tp, _Dp>::type;
135 __uniq_ptr_impl() =
default;
136 __uniq_ptr_impl(pointer __p) : _M_t() { _M_ptr() = __p; }
138 template<
typename _Del>
139 __uniq_ptr_impl(pointer __p, _Del&& __d)
140 : _M_t(__p, std::forward<_Del>(__d)) { }
142 pointer& _M_ptr() {
return std::get<0>(_M_t); }
143 pointer _M_ptr()
const {
return std::get<0>(_M_t); }
144 _Dp& _M_deleter() {
return std::get<1>(_M_t); }
145 const _Dp& _M_deleter()
const {
return std::get<1>(_M_t); }
152 template <
typename _Tp,
typename _Dp = default_delete<_Tp>>
155 __uniq_ptr_impl<_Tp, _Dp> _M_t;
158 using pointer =
typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
159 using element_type = _Tp;
160 using deleter_type = _Dp;
164 template<
typename _Up,
typename _Ep>
165 using __safe_conversion_up = __and_<
166 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>,
167 __not_<is_array<_Up>>,
168 __or_<__and_<is_reference<deleter_type>,
169 is_same<deleter_type, _Ep>>,
170 __and_<__not_<is_reference<deleter_type>>,
171 is_convertible<_Ep, deleter_type>>
181 "constructed with null function pointer deleter"); }
193 "constructed with null function pointer deleter"); }
215 typename remove_reference<deleter_type>::type&& __d) noexcept
216 : _M_t(
std::move(__p),
std::move(__d))
218 "rvalue deleter bound to reference"); }
235 template<
typename _Up,
typename _Ep,
typename = _Require<
236 __safe_conversion_up<_Up, _Ep>,
237 typename conditional<is_reference<_Dp>::value,
239 is_convertible<_Ep, _Dp>>::type>>
244 #if _GLIBCXX_USE_DEPRECATED 246 template<
typename _Up,
typename = _Require<
247 is_convertible<_Up*, _Tp*>, is_same<_Dp, default_delete<_Tp>>>>
254 auto& __ptr = _M_t._M_ptr();
255 if (__ptr !=
nullptr)
271 reset(__u.release());
272 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
283 template<
typename _Up,
typename _Ep>
284 typename enable_if< __and_<
285 __safe_conversion_up<_Up, _Ep>,
286 is_assignable<deleter_type&, _Ep&&>
291 reset(__u.release());
292 get_deleter() = std::forward<_Ep>(__u.get_deleter());
307 typename add_lvalue_reference<element_type>::type
310 __glibcxx_assert(
get() != pointer());
318 _GLIBCXX_DEBUG_PEDASSERT(
get() != pointer());
325 {
return _M_t._M_ptr(); }
330 {
return _M_t._M_deleter(); }
335 {
return _M_t._M_deleter(); }
338 explicit operator bool() const noexcept
339 {
return get() == pointer() ?
false :
true; }
348 _M_t._M_ptr() = pointer();
359 reset(pointer __p = pointer()) noexcept
362 swap(_M_t._M_ptr(), __p);
363 if (__p != pointer())
372 swap(_M_t, __u._M_t);
384 template<
typename _Tp,
typename _Dp>
387 __uniq_ptr_impl<_Tp, _Dp> _M_t;
389 template<
typename _Up>
390 using __remove_cv =
typename remove_cv<_Up>::type;
393 template<
typename _Up>
394 using __is_derived_Tp
395 = __and_< is_base_of<_Tp, _Up>,
396 __not_<is_same<__remove_cv<_Tp>, __remove_cv<_Up>>> >;
399 using pointer =
typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
400 using element_type = _Tp;
401 using deleter_type = _Dp;
405 template<
typename _Up,
typename _Ep,
407 typename _Up_element_type =
typename _Up_up::element_type>
408 using __safe_conversion_up = __and_<
410 is_same<pointer, element_type*>,
411 is_same<typename _Up_up::pointer, _Up_element_type*>,
412 is_convertible<_Up_element_type(*)[], element_type(*)[]>,
413 __or_<__and_<is_reference<deleter_type>, is_same<deleter_type, _Ep>>,
414 __and_<__not_<is_reference<deleter_type>>,
415 is_convertible<_Ep, deleter_type>>>
419 template<
typename _Up>
420 using __safe_conversion_raw = __and_<
421 __or_<__or_<is_same<_Up, pointer>,
422 is_same<_Up, nullptr_t>>,
423 __and_<is_pointer<_Up>,
424 is_same<pointer, element_type*>,
426 typename remove_pointer<_Up>::type(*)[],
438 "constructed with null function pointer deleter"); }
447 template<
typename _Up,
448 typename =
typename enable_if<
449 __safe_conversion_raw<_Up>::value,
bool>::type>
454 "constructed with null function pointer deleter"); }
464 template<
typename _Up,
465 typename =
typename enable_if<
466 __safe_conversion_raw<_Up>::value,
bool>::type>
469 deleter_type,
const deleter_type&>::type __d) noexcept
480 template<
typename _Up,
481 typename =
typename enable_if<
482 __safe_conversion_raw<_Up>::value,
bool>::type>
484 remove_reference<deleter_type>::type&& __d) noexcept
485 : _M_t(
std::move(__p),
std::move(__d))
487 "rvalue deleter bound to reference"); }
496 template<
typename _Up,
typename _Ep,
497 typename = _Require<__safe_conversion_up<_Up, _Ep>>>
499 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
505 auto& __ptr = _M_t._M_ptr();
506 if (__ptr !=
nullptr)
522 reset(__u.release());
523 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
534 template<
typename _Up,
typename _Ep>
536 enable_if<__and_<__safe_conversion_up<_Up, _Ep>,
537 is_assignable<deleter_type&, _Ep&&>
542 reset(__u.release());
543 get_deleter() = std::forward<_Ep>(__u.get_deleter());
558 typename std::add_lvalue_reference<element_type>::type
561 __glibcxx_assert(
get() != pointer());
568 {
return _M_t._M_ptr(); }
573 {
return _M_t._M_deleter(); }
578 {
return _M_t._M_deleter(); }
581 explicit operator bool() const noexcept
582 {
return get() == pointer() ?
false :
true; }
591 _M_t._M_ptr() = pointer();
601 template <
typename _Up,
603 __or_<is_same<_Up, pointer>,
604 __and_<is_same<pointer, element_type*>,
607 typename remove_pointer<_Up>::type(*)[],
618 swap(_M_t._M_ptr(), __ptr);
619 if (__ptr !=
nullptr)
623 void reset(nullptr_t =
nullptr) noexcept
633 swap(_M_t, __u._M_t);
641 template<
typename _Tp,
typename _Dp>
643 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 645 typename enable_if<__is_swappable<_Dp>::value>::type
653 template<
typename _Tp,
typename _Dp,
654 typename _Up,
typename _Ep>
658 {
return __x.
get() == __y.
get(); }
660 template<
typename _Tp,
typename _Dp>
665 template<
typename _Tp,
typename _Dp>
670 template<
typename _Tp,
typename _Dp,
671 typename _Up,
typename _Ep>
675 {
return __x.
get() != __y.
get(); }
677 template<
typename _Tp,
typename _Dp>
680 {
return (
bool)__x; }
682 template<
typename _Tp,
typename _Dp>
685 {
return (
bool)__x; }
687 template<
typename _Tp,
typename _Dp,
688 typename _Up,
typename _Ep>
690 operator<(const unique_ptr<_Tp, _Dp>& __x,
694 std::common_type<typename unique_ptr<_Tp, _Dp>::pointer,
695 typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
699 template<
typename _Tp,
typename _Dp>
701 operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
705 template<
typename _Tp,
typename _Dp>
707 operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
711 template<
typename _Tp,
typename _Dp,
712 typename _Up,
typename _Ep>
714 operator<=(const unique_ptr<_Tp, _Dp>& __x,
716 {
return !(__y < __x); }
718 template<
typename _Tp,
typename _Dp>
720 operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
721 {
return !(
nullptr < __x); }
723 template<
typename _Tp,
typename _Dp>
725 operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
726 {
return !(__x <
nullptr); }
728 template<
typename _Tp,
typename _Dp,
729 typename _Up,
typename _Ep>
733 {
return (__y < __x); }
735 template<
typename _Tp,
typename _Dp>
741 template<
typename _Tp,
typename _Dp>
747 template<
typename _Tp,
typename _Dp,
748 typename _Up,
typename _Ep>
752 {
return !(__x < __y); }
754 template<
typename _Tp,
typename _Dp>
757 {
return !(__x <
nullptr); }
759 template<
typename _Tp,
typename _Dp>
762 {
return !(
nullptr < __x); }
765 template<
typename _Tp,
typename _Dp>
767 :
public __hash_base<size_t, unique_ptr<_Tp, _Dp>>,
768 private __poison_hash<typename unique_ptr<_Tp, _Dp>::pointer>
778 #if __cplusplus > 201103L 780 #define __cpp_lib_make_unique 201304 782 template<
typename _Tp>
786 template<
typename _Tp>
787 struct _MakeUniq<_Tp[]>
790 template<
typename _Tp,
size_t _Bound>
791 struct _MakeUniq<_Tp[_Bound]>
792 {
struct __invalid_type { }; };
795 template<
typename _Tp,
typename... _Args>
801 template<
typename _Tp>
802 inline typename _MakeUniq<_Tp>::__array
807 template<
typename _Tp,
typename... _Args>
808 inline typename _MakeUniq<_Tp>::__invalid_type
814 _GLIBCXX_END_NAMESPACE_VERSION
void reset(pointer __p=pointer()) noexcept
Replace the stored pointer.
Primary class template hash.
const deleter_type & get_deleter() const noexcept
Return a reference to the stored deleter.
unique_ptr(pointer __p, typename conditional< is_reference< deleter_type >::value, deleter_type, const deleter_type &>::type __d) noexcept
unique_ptr(_Up __p, typename remove_reference< deleter_type >::type &&__d) noexcept
constexpr unique_ptr(nullptr_t) noexcept
Creates a unique_ptr that owns nothing.
unique_ptr & operator=(nullptr_t) noexcept
Reset the unique_ptr to empty, invoking the deleter if necessary.
~unique_ptr() noexcept
Destructor, invokes the deleter if the stored pointer is not null.
add_lvalue_reference< element_type >::type operator*() const
Dereference the stored pointer.
unique_ptr(_Up __p, typename conditional< is_reference< deleter_type >::value, deleter_type, const deleter_type &>::type __d) noexcept
_MakeUniq< _Tp >::__single_object make_unique(_Args &&... __args)
std::make_unique for single objects
default_delete(const default_delete< _Up[]> &) noexcept
Converting constructor.
constexpr unique_ptr() noexcept
Default constructor, creates a unique_ptr that owns nothing.
ISO C++ entities toplevel namespace is std.
const deleter_type & get_deleter() const noexcept
Return a reference to the stored deleter.
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.
20.7.1.2 unique_ptr for single objects.
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 & operator=(unique_ptr &&__u) noexcept
Move assignment operator.
unique_ptr(unique_ptr< _Up, _Ep > &&__u) noexcept
Converting constructor from another type.
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 swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
void operator()(_Tp *__ptr) const
Calls delete __ptr.
constexpr unique_ptr(nullptr_t) noexcept
Creates a unique_ptr that owns nothing.
unique_ptr(pointer __p, typename remove_reference< deleter_type >::type &&__d) noexcept
pointer release() noexcept
Release ownership of any stored pointer.
default_delete(const default_delete< _Up > &) noexcept
Converting constructor.
pointer get() const noexcept
Return the stored pointer.
unique_ptr & operator=(unique_ptr &&__u) noexcept
Move assignment operator.
enable_if< is_convertible< _Up(*)[], _Tp(*)[]>::value >::type operator()(_Up *__ptr) const
Calls delete[] __ptr.
deleter_type & get_deleter() noexcept
Return a reference to the stored deleter.
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.
constexpr default_delete() noexcept=default
Default constructor.
pointer operator->() const noexcept
Return the stored pointer.
unique_ptr(unique_ptr &&__u) noexcept
Move constructor.
unique_ptr(_Up __p) noexcept
One of the comparison functors.
Primary template of default_delete, used by unique_ptr.
constexpr unique_ptr() noexcept
Default constructor, creates a unique_ptr that owns nothing.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
void reset(_Up __p) noexcept
Replace the stored pointer.
unique_ptr(unique_ptr &&__u) noexcept
Move constructor.
pointer release() noexcept
Release ownership of any stored pointer.
~unique_ptr()
Destructor, invokes the deleter if the stored pointer is not null.
A simple smart pointer providing strict ownership semantics.
_Del * get_deleter(const __shared_ptr< _Tp, _Lp > &__p) noexcept
20.7.2.2.10 shared_ptr get_deleter
unique_ptr(pointer __p) noexcept