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>>,
176 __or_<__and_<is_reference<deleter_type>,
177 is_same<deleter_type, _Ep>>,
178 __and_<__not_<is_reference<deleter_type>>,
179 is_convertible<_Ep, deleter_type>>
186 template <
typename _Up = _Dp,
187 typename = _DeleterConstraint<_Up>>
198 template <
typename _Up = _Dp,
199 typename = _DeleterConstraint<_Up>>
225 typename remove_reference<deleter_type>::type&& __d) noexcept
226 : _M_t(
std::move(__p),
std::move(__d))
228 "rvalue deleter bound to reference"); }
231 template <
typename _Up = _Dp,
232 typename = _DeleterConstraint<_Up>>
247 template<
typename _Up,
typename _Ep,
typename = _Require<
248 __safe_conversion_up<_Up, _Ep>,
249 typename conditional<is_reference<_Dp>::value,
251 is_convertible<_Ep, _Dp>>::type>>
256 #if _GLIBCXX_USE_DEPRECATED 258 template<
typename _Up,
typename = _Require<
259 is_convertible<_Up*, _Tp*>, is_same<_Dp, default_delete<_Tp>>>>
266 auto& __ptr = _M_t._M_ptr();
267 if (__ptr !=
nullptr)
283 reset(__u.release());
284 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
295 template<
typename _Up,
typename _Ep>
296 typename enable_if< __and_<
297 __safe_conversion_up<_Up, _Ep>,
298 is_assignable<deleter_type&, _Ep&&>
303 reset(__u.release());
304 get_deleter() = std::forward<_Ep>(__u.get_deleter());
319 typename add_lvalue_reference<element_type>::type
322 __glibcxx_assert(
get() != pointer());
330 _GLIBCXX_DEBUG_PEDASSERT(
get() != pointer());
337 {
return _M_t._M_ptr(); }
342 {
return _M_t._M_deleter(); }
347 {
return _M_t._M_deleter(); }
350 explicit operator bool() const noexcept
351 {
return get() == pointer() ?
false :
true; }
360 _M_t._M_ptr() = pointer();
371 reset(pointer __p = pointer()) noexcept
374 swap(_M_t._M_ptr(), __p);
375 if (__p != pointer())
384 swap(_M_t, __u._M_t);
396 template<
typename _Tp,
typename _Dp>
399 template <
typename _Up>
400 using _DeleterConstraint =
401 typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
403 __uniq_ptr_impl<_Tp, _Dp> _M_t;
405 template<
typename _Up>
406 using __remove_cv =
typename remove_cv<_Up>::type;
409 template<
typename _Up>
410 using __is_derived_Tp
411 = __and_< is_base_of<_Tp, _Up>,
412 __not_<is_same<__remove_cv<_Tp>, __remove_cv<_Up>>> >;
415 using pointer =
typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
416 using element_type = _Tp;
417 using deleter_type = _Dp;
421 template<
typename _Up,
typename _Ep,
423 typename _Up_element_type =
typename _Up_up::element_type>
424 using __safe_conversion_up = __and_<
426 is_same<pointer, element_type*>,
427 is_same<typename _Up_up::pointer, _Up_element_type*>,
428 is_convertible<_Up_element_type(*)[], element_type(*)[]>,
429 __or_<__and_<is_reference<deleter_type>, is_same<deleter_type, _Ep>>,
430 __and_<__not_<is_reference<deleter_type>>,
431 is_convertible<_Ep, deleter_type>>>
435 template<
typename _Up>
436 using __safe_conversion_raw = __and_<
437 __or_<__or_<is_same<_Up, pointer>,
438 is_same<_Up, nullptr_t>>,
439 __and_<is_pointer<_Up>,
440 is_same<pointer, element_type*>,
442 typename remove_pointer<_Up>::type(*)[],
451 template <
typename _Up = _Dp,
452 typename = _DeleterConstraint<_Up>>
464 template<
typename _Up,
466 typename = _DeleterConstraint<_Vp>,
467 typename =
typename enable_if<
468 __safe_conversion_raw<_Up>::value,
bool>::type>
482 template<
typename _Up,
483 typename =
typename enable_if<
484 __safe_conversion_raw<_Up>::value,
bool>::type>
487 deleter_type,
const deleter_type&>::type __d) noexcept
498 template<
typename _Up,
499 typename =
typename enable_if<
500 __safe_conversion_raw<_Up>::value,
bool>::type>
502 remove_reference<deleter_type>::type&& __d) noexcept
503 : _M_t(
std::move(__p),
std::move(__d))
505 "rvalue deleter bound to reference"); }
512 template <
typename _Up = _Dp,
513 typename = _DeleterConstraint<_Up>>
516 template<
typename _Up,
typename _Ep,
517 typename = _Require<__safe_conversion_up<_Up, _Ep>>>
519 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
525 auto& __ptr = _M_t._M_ptr();
526 if (__ptr !=
nullptr)
542 reset(__u.release());
543 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
554 template<
typename _Up,
typename _Ep>
556 enable_if<__and_<__safe_conversion_up<_Up, _Ep>,
557 is_assignable<deleter_type&, _Ep&&>
562 reset(__u.release());
563 get_deleter() = std::forward<_Ep>(__u.get_deleter());
578 typename std::add_lvalue_reference<element_type>::type
581 __glibcxx_assert(
get() != pointer());
588 {
return _M_t._M_ptr(); }
593 {
return _M_t._M_deleter(); }
598 {
return _M_t._M_deleter(); }
601 explicit operator bool() const noexcept
602 {
return get() == pointer() ?
false :
true; }
611 _M_t._M_ptr() = pointer();
621 template <
typename _Up,
623 __or_<is_same<_Up, pointer>,
624 __and_<is_same<pointer, element_type*>,
627 typename remove_pointer<_Up>::type(*)[],
638 swap(_M_t._M_ptr(), __ptr);
639 if (__ptr !=
nullptr)
643 void reset(nullptr_t =
nullptr) noexcept
653 swap(_M_t, __u._M_t);
661 template<
typename _Tp,
typename _Dp>
663 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 665 typename enable_if<__is_swappable<_Dp>::value>::type
673 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 674 template<
typename _Tp,
typename _Dp>
675 typename enable_if<!__is_swappable<_Dp>::value>::type
680 template<
typename _Tp,
typename _Dp,
681 typename _Up,
typename _Ep>
685 {
return __x.
get() == __y.
get(); }
687 template<
typename _Tp,
typename _Dp>
692 template<
typename _Tp,
typename _Dp>
697 template<
typename _Tp,
typename _Dp,
698 typename _Up,
typename _Ep>
702 {
return __x.
get() != __y.
get(); }
704 template<
typename _Tp,
typename _Dp>
707 {
return (
bool)__x; }
709 template<
typename _Tp,
typename _Dp>
712 {
return (
bool)__x; }
714 template<
typename _Tp,
typename _Dp,
715 typename _Up,
typename _Ep>
717 operator<(const unique_ptr<_Tp, _Dp>& __x,
721 std::common_type<typename unique_ptr<_Tp, _Dp>::pointer,
722 typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
726 template<
typename _Tp,
typename _Dp>
728 operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
732 template<
typename _Tp,
typename _Dp>
734 operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
738 template<
typename _Tp,
typename _Dp,
739 typename _Up,
typename _Ep>
741 operator<=(const unique_ptr<_Tp, _Dp>& __x,
743 {
return !(__y < __x); }
745 template<
typename _Tp,
typename _Dp>
747 operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
748 {
return !(
nullptr < __x); }
750 template<
typename _Tp,
typename _Dp>
752 operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
753 {
return !(__x <
nullptr); }
755 template<
typename _Tp,
typename _Dp,
756 typename _Up,
typename _Ep>
760 {
return (__y < __x); }
762 template<
typename _Tp,
typename _Dp>
768 template<
typename _Tp,
typename _Dp>
774 template<
typename _Tp,
typename _Dp,
775 typename _Up,
typename _Ep>
779 {
return !(__x < __y); }
781 template<
typename _Tp,
typename _Dp>
784 {
return !(__x <
nullptr); }
786 template<
typename _Tp,
typename _Dp>
789 {
return !(
nullptr < __x); }
792 template<
typename _Tp,
typename _Dp>
794 :
public __hash_base<size_t, unique_ptr<_Tp, _Dp>>,
795 private __poison_hash<typename unique_ptr<_Tp, _Dp>::pointer>
805 #if __cplusplus > 201103L 807 #define __cpp_lib_make_unique 201304 809 template<
typename _Tp>
813 template<
typename _Tp>
814 struct _MakeUniq<_Tp[]>
817 template<
typename _Tp,
size_t _Bound>
818 struct _MakeUniq<_Tp[_Bound]>
819 {
struct __invalid_type { }; };
822 template<
typename _Tp,
typename... _Args>
828 template<
typename _Tp>
829 inline typename _MakeUniq<_Tp>::__array
834 template<
typename _Tp,
typename... _Args>
835 inline typename _MakeUniq<_Tp>::__invalid_type
841 _GLIBCXX_END_NAMESPACE_VERSION
deleter_type & get_deleter() noexcept
Return a reference to the stored deleter.
unique_ptr(unique_ptr &&__u) noexcept
Move constructor.
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.
add_lvalue_reference< element_type >::type operator*() const
Dereference the stored pointer.
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.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
constexpr default_delete() noexcept=default
Default constructor.
_MakeUniq< _Tp >::__single_object make_unique(_Args &&...__args)
std::make_unique for single objects
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.
ISO C++ entities toplevel namespace is std.
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.
const deleter_type & get_deleter() const noexcept
Return a reference to the stored deleter.
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
pointer get() const noexcept
Return the stored pointer.
One of the comparison functors.
enable_if< is_convertible< _Up(*)[], _Tp(*)[]>::value >::type operator()(_Up *__ptr) const
Calls delete[] __ptr.
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.
_Del * get_deleter(const __shared_ptr< _Tp, _Lp > &__p) noexcept
20.7.2.2.10 shared_ptr get_deleter
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.
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.
pointer operator->() const noexcept
Return the stored pointer.
unique_ptr(_Up __p) noexcept