31 #define _UNIQUE_PTR_H 1
39 namespace std _GLIBCXX_VISIBILITY(default)
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
48 #if _GLIBCXX_USE_DEPRECATED
49 template<
typename>
class auto_ptr;
53 template<
typename _Tp>
64 template<
typename _Up,
typename =
typename
65 enable_if<is_convertible<_Up*, _Tp*>::value>::type>
73 "can't delete pointer to incomplete type");
74 static_assert(
sizeof(_Tp)>0,
75 "can't delete pointer to incomplete type");
83 template<
typename _Tp>
99 template<
typename _Up,
typename =
typename
100 enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type>
104 template<
typename _Up>
105 typename enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type
108 static_assert(
sizeof(_Tp)>0,
109 "can't delete pointer to incomplete type");
115 template <
typename _Tp,
typename _Dp = default_delete<_Tp> >
121 template<
typename _Up>
122 static typename _Up::pointer __test(
typename _Up::pointer*);
124 template<
typename _Up>
125 static _Tp* __test(...);
127 typedef typename remove_reference<_Dp>::type _Del;
130 typedef decltype(__test<_Del>(0)) type;
137 typedef typename _Pointer::type pointer;
138 typedef _Tp element_type;
139 typedef _Dp deleter_type;
144 template<
typename _Up,
typename _Ep>
145 using __safe_conversion_up = __and_<
146 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>,
147 __not_<is_array<_Up>>,
148 __or_<__and_<is_reference<deleter_type>,
149 is_same<deleter_type, _Ep>>,
150 __and_<__not_<is_reference<deleter_type>>,
151 is_convertible<_Ep, deleter_type>>
161 "constructed with null function pointer deleter"); }
173 "constructed with null function pointer deleter"); }
195 typename remove_reference<deleter_type>::type&& __d) noexcept
196 : _M_t(std::move(__p), std::move(__d))
198 "rvalue deleter bound to reference"); }
215 template<
typename _Up,
typename _Ep,
typename = _Require<
216 __safe_conversion_up<_Up, _Ep>,
217 typename conditional<is_reference<_Dp>::value,
219 is_convertible<_Ep, _Dp>>::type>>
224 #if _GLIBCXX_USE_DEPRECATED
226 template<
typename _Up,
typename = _Require<
227 is_convertible<_Up*, _Tp*>, is_same<_Dp, default_delete<_Tp>>>>
234 auto& __ptr = std::get<0>(_M_t);
235 if (__ptr !=
nullptr)
251 reset(__u.release());
252 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
263 template<
typename _Up,
typename _Ep>
264 typename enable_if< __and_<
265 __safe_conversion_up<_Up, _Ep>,
266 is_assignable<deleter_type&, _Ep&&>
271 reset(__u.release());
272 get_deleter() = std::forward<_Ep>(__u.get_deleter());
287 typename add_lvalue_reference<element_type>::type
290 __glibcxx_assert(
get() != pointer());
298 _GLIBCXX_DEBUG_PEDASSERT(
get() != pointer());
305 {
return std::get<0>(_M_t); }
310 {
return std::get<1>(_M_t); }
315 {
return std::get<1>(_M_t); }
318 explicit operator bool() const noexcept
319 {
return get() == pointer() ?
false :
true; }
328 std::get<0>(_M_t) = pointer();
339 reset(pointer __p = pointer()) noexcept
342 swap(std::get<0>(_M_t), __p);
343 if (__p != pointer())
352 swap(_M_t, __u._M_t);
364 template<
typename _Tp,
typename _Dp>
370 template<
typename _Up>
371 static typename _Up::pointer __test(
typename _Up::pointer*);
373 template<
typename _Up>
374 static _Tp* __test(...);
376 typedef typename remove_reference<_Dp>::type _Del;
379 typedef decltype(__test<_Del>(0)) type;
385 template<
typename _Up>
386 using __remove_cv =
typename remove_cv<_Up>::type;
389 template<
typename _Up>
390 using __is_derived_Tp
391 = __and_< is_base_of<_Tp, _Up>,
392 __not_<is_same<__remove_cv<_Tp>, __remove_cv<_Up>>> >;
396 typedef typename _Pointer::type pointer;
397 typedef _Tp element_type;
398 typedef _Dp deleter_type;
402 template<
typename _Up,
typename _Ep,
404 typename _Up_element_type =
typename _Up_up::element_type>
405 using __safe_conversion_up = __and_<
407 is_same<pointer, element_type*>,
408 is_same<typename _Up_up::pointer, _Up_element_type*>,
409 is_convertible<_Up_element_type(*)[], element_type(*)[]>,
410 __or_<__and_<is_reference<deleter_type>, is_same<deleter_type, _Ep>>,
411 __and_<__not_<is_reference<deleter_type>>,
412 is_convertible<_Ep, deleter_type>>>
416 template<
typename _Up>
417 using __safe_conversion_raw = __and_<
418 __or_<__or_<is_same<_Up, pointer>,
419 is_same<_Up, nullptr_t>>,
420 __and_<is_pointer<_Up>,
421 is_same<pointer, element_type*>,
423 typename remove_pointer<_Up>::type(*)[],
435 "constructed with null function pointer deleter"); }
444 template<
typename _Up,
445 typename =
typename enable_if<
446 __safe_conversion_raw<_Up>::value,
bool>::type>
449 : _M_t(__p, deleter_type())
451 "constructed with null function pointer deleter"); }
461 template<
typename _Up,
462 typename =
typename enable_if<
463 __safe_conversion_raw<_Up>::value,
bool>::type>
466 deleter_type,
const deleter_type&>::type __d) noexcept
477 template<
typename _Up,
478 typename =
typename enable_if<
479 __safe_conversion_raw<_Up>::value,
bool>::type>
481 remove_reference<deleter_type>::type&& __d) noexcept
482 : _M_t(std::move(__p), std::move(__d))
484 "rvalue deleter bound to reference"); }
493 template<
typename _Up,
typename _Ep,
494 typename = _Require<__safe_conversion_up<_Up, _Ep>>>
502 auto& __ptr = std::get<0>(_M_t);
503 if (__ptr !=
nullptr)
519 reset(__u.release());
520 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
531 template<
typename _Up,
typename _Ep>
533 enable_if<__and_<__safe_conversion_up<_Up, _Ep>,
534 is_assignable<deleter_type&, _Ep&&>
539 reset(__u.release());
540 get_deleter() = std::forward<_Ep>(__u.get_deleter());
555 typename std::add_lvalue_reference<element_type>::type
558 __glibcxx_assert(
get() != pointer());
565 {
return std::get<0>(_M_t); }
570 {
return std::get<1>(_M_t); }
575 {
return std::get<1>(_M_t); }
578 explicit operator bool() const noexcept
579 {
return get() == pointer() ?
false :
true; }
588 std::get<0>(_M_t) = pointer();
598 template <
typename _Up,
600 __or_<is_same<_Up, pointer>,
601 __and_<is_same<pointer, element_type*>,
604 typename remove_pointer<_Up>::type(*)[],
614 swap(std::get<0>(_M_t), __p);
619 void reset(nullptr_t =
nullptr) noexcept
629 swap(_M_t, __u._M_t);
637 template<
typename _Tp,
typename _Dp>
639 swap(unique_ptr<_Tp, _Dp>& __x,
640 unique_ptr<_Tp, _Dp>& __y) noexcept
643 template<
typename _Tp,
typename _Dp,
644 typename _Up,
typename _Ep>
646 operator==(
const unique_ptr<_Tp, _Dp>& __x,
647 const unique_ptr<_Up, _Ep>& __y)
648 {
return __x.get() == __y.get(); }
650 template<
typename _Tp,
typename _Dp>
652 operator==(
const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
655 template<
typename _Tp,
typename _Dp>
657 operator==(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x) noexcept
660 template<
typename _Tp,
typename _Dp,
661 typename _Up,
typename _Ep>
663 operator!=(
const unique_ptr<_Tp, _Dp>& __x,
664 const unique_ptr<_Up, _Ep>& __y)
665 {
return __x.get() != __y.get(); }
667 template<
typename _Tp,
typename _Dp>
669 operator!=(
const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
670 {
return (
bool)__x; }
672 template<
typename _Tp,
typename _Dp>
674 operator!=(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x) noexcept
675 {
return (
bool)__x; }
677 template<
typename _Tp,
typename _Dp,
678 typename _Up,
typename _Ep>
680 operator<(const unique_ptr<_Tp, _Dp>& __x,
681 const unique_ptr<_Up, _Ep>& __y)
684 std::common_type<typename unique_ptr<_Tp, _Dp>::pointer,
685 typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
689 template<
typename _Tp,
typename _Dp>
691 operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
695 template<
typename _Tp,
typename _Dp>
697 operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
701 template<
typename _Tp,
typename _Dp,
702 typename _Up,
typename _Ep>
704 operator<=(const unique_ptr<_Tp, _Dp>& __x,
705 const unique_ptr<_Up, _Ep>& __y)
706 {
return !(__y < __x); }
708 template<
typename _Tp,
typename _Dp>
710 operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
711 {
return !(
nullptr < __x); }
713 template<
typename _Tp,
typename _Dp>
715 operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
716 {
return !(__x <
nullptr); }
718 template<
typename _Tp,
typename _Dp,
719 typename _Up,
typename _Ep>
721 operator>(
const unique_ptr<_Tp, _Dp>& __x,
722 const unique_ptr<_Up, _Ep>& __y)
723 {
return (__y < __x); }
725 template<
typename _Tp,
typename _Dp>
727 operator>(
const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
731 template<
typename _Tp,
typename _Dp>
733 operator>(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x)
737 template<
typename _Tp,
typename _Dp,
738 typename _Up,
typename _Ep>
740 operator>=(
const unique_ptr<_Tp, _Dp>& __x,
741 const unique_ptr<_Up, _Ep>& __y)
742 {
return !(__x < __y); }
744 template<
typename _Tp,
typename _Dp>
746 operator>=(
const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
747 {
return !(__x <
nullptr); }
749 template<
typename _Tp,
typename _Dp>
751 operator>=(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x)
752 {
return !(
nullptr < __x); }
755 template<
typename _Tp,
typename _Dp>
757 :
public __hash_base<size_t, unique_ptr<_Tp, _Dp>>
767 #if __cplusplus > 201103L
769 #define __cpp_lib_make_unique 201304
771 template<
typename _Tp>
775 template<
typename _Tp>
776 struct _MakeUniq<_Tp[]>
777 {
typedef unique_ptr<_Tp[]> __array; };
779 template<
typename _Tp,
size_t _Bound>
780 struct _MakeUniq<_Tp[_Bound]>
781 {
struct __invalid_type { }; };
784 template<
typename _Tp,
typename... _Args>
785 inline typename _MakeUniq<_Tp>::__single_object
790 template<
typename _Tp>
791 inline typename _MakeUniq<_Tp>::__array
796 template<
typename _Tp,
typename... _Args>
797 inline typename _MakeUniq<_Tp>::__invalid_type
803 _GLIBCXX_END_NAMESPACE_VERSION
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.
pointer get() const noexcept
Return the stored pointer.
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.
void operator()(_Tp *__ptr) const
Calls delete __ptr.
unique_ptr(_Up __p) noexcept
20.7.1.2 unique_ptr for single objects.
unique_ptr(pointer __p, typename conditional< is_reference< deleter_type >::value, deleter_type, const deleter_type & >::type __d) noexcept
std::add_lvalue_reference< element_type >::type operator[](size_t __i) const
Access an element of owned array.
add_lvalue_reference< element_type >::type operator*() const
Dereference the stored pointer.
constexpr default_delete() noexcept=default
Default constructor.
unique_ptr(pointer __p) noexcept
void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
unique_ptr & operator=(unique_ptr &&__u) noexcept
Move assignment operator.
pointer operator->() const noexcept
Return the stored pointer.
Primary class template hash.
unique_ptr(unique_ptr &&__u) noexcept
Move constructor.
void reset(pointer __p=pointer()) noexcept
Replace the stored pointer.
default_delete(const default_delete< _Up[]> &) noexcept
Converting constructor.
constexpr unique_ptr() noexcept
Default constructor, creates a unique_ptr that owns nothing.
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 conditional< is_reference< deleter_type >::value, deleter_type, const deleter_type & >::type __d) noexcept
~unique_ptr() noexcept
Destructor, invokes the deleter if the stored pointer is not null.
Primary template of default_delete, used by unique_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
deleter_type & get_deleter() 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.
unique_ptr(unique_ptr &&__u) noexcept
Move constructor.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
deleter_type & get_deleter() noexcept
Return a reference to the stored deleter.
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.
unique_ptr & operator=(unique_ptr &&__u) noexcept
Move assignment operator.
unique_ptr & operator=(nullptr_t) noexcept
Reset the unique_ptr to empty, invoking the deleter if necessary.
void reset(_Up __p) noexcept
Replace the stored pointer.
A simple smart pointer providing strict ownership semantics.
void swap(basic_filebuf< _CharT, _Traits > &__x, basic_filebuf< _CharT, _Traits > &__y)
Swap specialization for filebufs.
void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
_MakeUniq< _Tp >::__single_object make_unique(_Args &&...__args)
std::make_unique for single objects
unique_ptr(_Up __p, typename remove_reference< deleter_type >::type &&__d) noexcept
pointer release() noexcept
Release ownership of any stored pointer.
enable_if< is_convertible< _Up(*)[], _Tp(*)[]>::value >::type operator()(_Up *__ptr) const
Calls delete[] __ptr.
One of the comparison functors.
constexpr unique_ptr(nullptr_t) noexcept
Creates a unique_ptr that owns nothing.
unique_ptr(unique_ptr< _Up, _Ep > &&__u) noexcept
Converting constructor from another type.