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 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 654 template<
typename _Tp,
typename _Dp>
655 typename enable_if<!__is_swappable<_Dp>::value>::type
660 template<
typename _Tp,
typename _Dp,
661 typename _Up,
typename _Ep>
665 {
return __x.
get() == __y.
get(); }
667 template<
typename _Tp,
typename _Dp>
672 template<
typename _Tp,
typename _Dp>
677 template<
typename _Tp,
typename _Dp,
678 typename _Up,
typename _Ep>
682 {
return __x.
get() != __y.
get(); }
684 template<
typename _Tp,
typename _Dp>
687 {
return (
bool)__x; }
689 template<
typename _Tp,
typename _Dp>
692 {
return (
bool)__x; }
694 template<
typename _Tp,
typename _Dp,
695 typename _Up,
typename _Ep>
697 operator<(const unique_ptr<_Tp, _Dp>& __x,
701 std::common_type<typename unique_ptr<_Tp, _Dp>::pointer,
702 typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
706 template<
typename _Tp,
typename _Dp>
708 operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
712 template<
typename _Tp,
typename _Dp>
714 operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
718 template<
typename _Tp,
typename _Dp,
719 typename _Up,
typename _Ep>
721 operator<=(const unique_ptr<_Tp, _Dp>& __x,
723 {
return !(__y < __x); }
725 template<
typename _Tp,
typename _Dp>
727 operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
728 {
return !(
nullptr < __x); }
730 template<
typename _Tp,
typename _Dp>
732 operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
733 {
return !(__x <
nullptr); }
735 template<
typename _Tp,
typename _Dp,
736 typename _Up,
typename _Ep>
740 {
return (__y < __x); }
742 template<
typename _Tp,
typename _Dp>
748 template<
typename _Tp,
typename _Dp>
754 template<
typename _Tp,
typename _Dp,
755 typename _Up,
typename _Ep>
759 {
return !(__x < __y); }
761 template<
typename _Tp,
typename _Dp>
764 {
return !(__x <
nullptr); }
766 template<
typename _Tp,
typename _Dp>
769 {
return !(
nullptr < __x); }
772 template<
typename _Tp,
typename _Dp>
774 :
public __hash_base<size_t, unique_ptr<_Tp, _Dp>>,
775 private __poison_hash<typename unique_ptr<_Tp, _Dp>::pointer>
785 #if __cplusplus > 201103L 787 #define __cpp_lib_make_unique 201304 789 template<
typename _Tp>
793 template<
typename _Tp>
794 struct _MakeUniq<_Tp[]>
797 template<
typename _Tp,
size_t _Bound>
798 struct _MakeUniq<_Tp[_Bound]>
799 {
struct __invalid_type { }; };
802 template<
typename _Tp,
typename... _Args>
808 template<
typename _Tp>
809 inline typename _MakeUniq<_Tp>::__array
814 template<
typename _Tp,
typename... _Args>
815 inline typename _MakeUniq<_Tp>::__invalid_type
821 _GLIBCXX_END_NAMESPACE_VERSION
constexpr unique_ptr(nullptr_t) noexcept
Creates a unique_ptr that owns nothing.
add_lvalue_reference< element_type >::type operator*() const
Dereference the stored pointer.
constexpr default_delete() noexcept=default
Default constructor.
deleter_type & get_deleter() 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.
Primary template of default_delete, used by unique_ptr.
Primary class template hash.
unique_ptr(unique_ptr &&__u) noexcept
Move constructor.
A simple smart pointer providing strict ownership semantics.
void operator()(_Tp *__ptr) const
Calls delete __ptr.
unique_ptr & operator=(nullptr_t) noexcept
Reset the unique_ptr to empty, invoking the deleter if necessary.
20.7.1.2 unique_ptr for single objects.
_MakeUniq< _Tp >::__single_object make_unique(_Args &&... __args)
std::make_unique for single objects
unique_ptr(unique_ptr &&__u) noexcept
Move constructor.
constexpr unique_ptr() noexcept
Default constructor, creates a unique_ptr that owns nothing.
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.
default_delete(const default_delete< _Up > &) noexcept
Converting constructor.
ISO C++ entities toplevel namespace is std.
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.
const deleter_type & get_deleter() const noexcept
Return a reference to the stored deleter.
~unique_ptr() noexcept
Destructor, invokes the deleter if the stored pointer is not null.
unique_ptr(pointer __p) noexcept
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.
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 & operator=(unique_ptr &&__u) noexcept
Move assignment operator.
unique_ptr(_Up __p) noexcept
pointer release() noexcept
Release ownership of any stored pointer.
default_delete(const default_delete< _Up[]> &) noexcept
Converting constructor.
void reset(pointer __p=pointer()) noexcept
Replace the stored pointer.
enable_if< is_convertible< _Up(*)[], _Tp(*)[]>::value >::type operator()(_Up *__ptr) const
Calls delete[] __ptr.
void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
unique_ptr(pointer __p, typename conditional< is_reference< deleter_type >::value, deleter_type, const deleter_type &>::type __d) noexcept
pointer get() const noexcept
Return the stored pointer.
void reset(_Up __p) noexcept
Replace the stored pointer.
One of the comparison functors.
unique_ptr(_Up __p, typename conditional< is_reference< deleter_type >::value, deleter_type, const deleter_type &>::type __d) noexcept
unique_ptr & operator=(unique_ptr &&__u) noexcept
Move assignment operator.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
unique_ptr(pointer __p, typename remove_reference< deleter_type >::type &&__d) noexcept
unique_ptr(unique_ptr< _Up, _Ep > &&__u) noexcept
Converting constructor from another type.
const deleter_type & get_deleter() const noexcept
Return a reference to the stored deleter.
~unique_ptr()
Destructor, invokes the deleter if the stored pointer is not null.
constexpr unique_ptr() noexcept
Default constructor, creates a unique_ptr that owns nothing.
pointer operator->() const noexcept
Return the stored pointer.
_Del * get_deleter(const __shared_ptr< _Tp, _Lp > &__p) noexcept
20.7.2.2.10 shared_ptr get_deleter
deleter_type & get_deleter() noexcept
Return a reference to the stored deleter.
unique_ptr(_Up __p, typename remove_reference< deleter_type >::type &&__d) noexcept