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 std::get<0>(_M_t) = __p;
175 "constructed with null function pointer deleter");
187 deleter_type,
const deleter_type&>::type __d) noexcept
198 typename remove_reference<deleter_type>::type&& __d) noexcept
199 : _M_t(
std::move(__p),
std::move(__d))
201 "rvalue deleter bound to reference"); }
218 template<
typename _Up,
typename _Ep,
typename = _Require<
219 __safe_conversion_up<_Up, _Ep>,
220 typename conditional<is_reference<_Dp>::value,
222 is_convertible<_Ep, _Dp>>::type>>
227 #if _GLIBCXX_USE_DEPRECATED 229 template<
typename _Up,
typename = _Require<
230 is_convertible<_Up*, _Tp*>, is_same<_Dp, default_delete<_Tp>>>>
237 auto& __ptr = std::get<0>(_M_t);
238 if (__ptr !=
nullptr)
254 reset(__u.release());
255 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
266 template<
typename _Up,
typename _Ep>
267 typename enable_if< __and_<
268 __safe_conversion_up<_Up, _Ep>,
269 is_assignable<deleter_type&, _Ep&&>
274 reset(__u.release());
275 get_deleter() = std::forward<_Ep>(__u.get_deleter());
290 typename add_lvalue_reference<element_type>::type
293 __glibcxx_assert(
get() != pointer());
301 _GLIBCXX_DEBUG_PEDASSERT(
get() != pointer());
308 {
return std::get<0>(_M_t); }
313 {
return std::get<1>(_M_t); }
318 {
return std::get<1>(_M_t); }
321 explicit operator bool() const noexcept
322 {
return get() == pointer() ?
false :
true; }
331 std::get<0>(_M_t) = pointer();
342 reset(pointer __p = pointer()) noexcept
345 swap(std::get<0>(_M_t), __p);
346 if (__p != pointer())
355 swap(_M_t, __u._M_t);
367 template<
typename _Tp,
typename _Dp>
373 template<
typename _Up>
374 static typename _Up::pointer __test(
typename _Up::pointer*);
376 template<
typename _Up>
377 static _Tp* __test(...);
379 typedef typename remove_reference<_Dp>::type _Del;
382 typedef decltype(__test<_Del>(0)) type;
388 template<
typename _Up>
389 using __remove_cv =
typename remove_cv<_Up>::type;
392 template<
typename _Up>
393 using __is_derived_Tp
394 = __and_< is_base_of<_Tp, _Up>,
395 __not_<is_same<__remove_cv<_Tp>, __remove_cv<_Up>>> >;
399 typedef typename _Pointer::type pointer;
400 typedef _Tp element_type;
401 typedef _Dp deleter_type;
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>
452 : _M_t(__p, deleter_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 = std::get<0>(_M_t);
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 std::get<0>(_M_t); }
573 {
return std::get<1>(_M_t); }
578 {
return std::get<1>(_M_t); }
581 explicit operator bool() const noexcept
582 {
return get() == pointer() ?
false :
true; }
591 std::get<0>(_M_t) = 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(std::get<0>(_M_t), __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>
647 template<
typename _Tp,
typename _Dp,
648 typename _Up,
typename _Ep>
652 {
return __x.
get() == __y.
get(); }
654 template<
typename _Tp,
typename _Dp>
659 template<
typename _Tp,
typename _Dp>
664 template<
typename _Tp,
typename _Dp,
665 typename _Up,
typename _Ep>
669 {
return __x.
get() != __y.
get(); }
671 template<
typename _Tp,
typename _Dp>
674 {
return (
bool)__x; }
676 template<
typename _Tp,
typename _Dp>
679 {
return (
bool)__x; }
681 template<
typename _Tp,
typename _Dp,
682 typename _Up,
typename _Ep>
684 operator<(const unique_ptr<_Tp, _Dp>& __x,
688 std::common_type<typename unique_ptr<_Tp, _Dp>::pointer,
689 typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
693 template<
typename _Tp,
typename _Dp>
695 operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
699 template<
typename _Tp,
typename _Dp>
701 operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
705 template<
typename _Tp,
typename _Dp,
706 typename _Up,
typename _Ep>
708 operator<=(const unique_ptr<_Tp, _Dp>& __x,
710 {
return !(__y < __x); }
712 template<
typename _Tp,
typename _Dp>
714 operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
715 {
return !(
nullptr < __x); }
717 template<
typename _Tp,
typename _Dp>
719 operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
720 {
return !(__x <
nullptr); }
722 template<
typename _Tp,
typename _Dp,
723 typename _Up,
typename _Ep>
727 {
return (__y < __x); }
729 template<
typename _Tp,
typename _Dp>
735 template<
typename _Tp,
typename _Dp>
741 template<
typename _Tp,
typename _Dp,
742 typename _Up,
typename _Ep>
746 {
return !(__x < __y); }
748 template<
typename _Tp,
typename _Dp>
751 {
return !(__x <
nullptr); }
753 template<
typename _Tp,
typename _Dp>
756 {
return !(
nullptr < __x); }
759 template<
typename _Tp,
typename _Dp>
761 :
public __hash_base<size_t, unique_ptr<_Tp, _Dp>>
771 #if __cplusplus > 201103L 773 #define __cpp_lib_make_unique 201304 775 template<
typename _Tp>
779 template<
typename _Tp>
780 struct _MakeUniq<_Tp[]>
783 template<
typename _Tp,
size_t _Bound>
784 struct _MakeUniq<_Tp[_Bound]>
785 {
struct __invalid_type { }; };
788 template<
typename _Tp,
typename... _Args>
794 template<
typename _Tp>
795 inline typename _MakeUniq<_Tp>::__array
800 template<
typename _Tp,
typename... _Args>
801 inline typename _MakeUniq<_Tp>::__invalid_type
807 _GLIBCXX_END_NAMESPACE_VERSION
void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
pointer release() noexcept
Release ownership of any stored pointer.
constexpr unique_ptr() noexcept
Default constructor, 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
~unique_ptr()
Destructor, invokes the deleter if the stored pointer is not null.
default_delete(const default_delete< _Up[]> &) noexcept
Converting constructor.
void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
enable_if< is_convertible< _Up(*)[], _Tp(*)[]>::value >::type operator()(_Up *__ptr) const
Calls delete[] __ptr.
unique_ptr(_Up __p, typename remove_reference< deleter_type >::type &&__d) noexcept
deleter_type & get_deleter() noexcept
Return a reference to the stored deleter.
default_delete(const default_delete< _Up > &) noexcept
Converting constructor.
unique_ptr & operator=(unique_ptr &&__u) noexcept
Move assignment operator.
Primary template of default_delete, used by unique_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.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
unique_ptr(pointer __p) noexcept
unique_ptr(_Up __p) noexcept
pointer get() const noexcept
Return the stored pointer.
constexpr unique_ptr(nullptr_t) noexcept
Creates a unique_ptr that owns nothing.
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.
A simple smart pointer providing strict ownership semantics.
void operator()(_Tp *__ptr) const
Calls delete __ptr.
const deleter_type & get_deleter() const noexcept
Return a reference to the stored deleter.
constexpr unique_ptr(nullptr_t) noexcept
Creates a unique_ptr that owns nothing.
pointer operator->() const noexcept
Return the 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.
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.
unique_ptr(pointer __p, typename remove_reference< deleter_type >::type &&__d) noexcept
unique_ptr & operator=(nullptr_t) noexcept
Reset the unique_ptr to empty, invoking the deleter if necessary.
pointer release() noexcept
Release ownership of any stored pointer.
const deleter_type & get_deleter() const noexcept
Return a reference to the stored deleter.
unique_ptr(unique_ptr &&__u) noexcept
Move constructor.
~unique_ptr() noexcept
Destructor, invokes the deleter if the stored pointer is not null.
One of the comparison functors.
unique_ptr(unique_ptr< _Up, _Ep > &&__u) noexcept
Converting constructor from another type.
unique_ptr(unique_ptr &&__u) noexcept
Move constructor.
20.7.1.2 unique_ptr for single objects.
add_lvalue_reference< element_type >::type operator*() const
Dereference the stored pointer.
_MakeUniq< _Tp >::__single_object make_unique(_Args &&...__args)
std::make_unique for single objects
unique_ptr(_Up __p, typename conditional< is_reference< deleter_type >::value, deleter_type, const deleter_type & >::type __d) noexcept
constexpr unique_ptr() noexcept
Default constructor, creates a unique_ptr that owns nothing.
_Del * get_deleter(const __shared_ptr< _Tp, _Lp > &__p) noexcept
20.7.2.2.10 shared_ptr get_deleter
ISO C++ entities toplevel namespace is std.
void reset(_Up __p) noexcept
Replace the stored pointer.
Primary class template hash.
void reset(pointer __p=pointer()) noexcept
Replace the stored pointer.
deleter_type & get_deleter() noexcept
Return a reference to the stored deleter.