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>
87 template<
typename _Up>
88 using __remove_cv =
typename remove_cv<_Up>::type;
91 template<
typename _Up>
93 = __and_< is_base_of<_Tp, _Up>,
94 __not_<is_same<__remove_cv<_Tp>, __remove_cv<_Up>>> >;
109 template<
typename _Up,
typename =
typename 110 enable_if<!__is_derived_Tp<_Up>::value>::type>
117 static_assert(
sizeof(_Tp)>0,
118 "can't delete pointer to incomplete type");
122 template<
typename _Up>
123 typename enable_if<__is_derived_Tp<_Up>::value>::type
128 template <
typename _Tp,
typename _Dp = default_delete<_Tp> >
134 template<
typename _Up>
135 static typename _Up::pointer __test(
typename _Up::pointer*);
137 template<
typename _Up>
138 static _Tp* __test(...);
140 typedef typename remove_reference<_Dp>::type _Del;
143 typedef decltype(__test<_Del>(0)) type;
150 typedef typename _Pointer::type pointer;
151 typedef _Tp element_type;
152 typedef _Dp deleter_type;
160 "constructed with null function pointer deleter"); }
172 std::get<0>(_M_t) = __p;
174 "constructed with null function pointer deleter");
186 deleter_type,
const deleter_type&>::type __d) noexcept
197 typename remove_reference<deleter_type>::type&& __d) noexcept
198 : _M_t(std::move(__p), std::move(__d))
200 "rvalue deleter bound to reference"); }
209 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
217 template<
typename _Up,
typename _Ep,
typename = _Require<
218 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>,
219 __not_<is_array<_Up>>,
220 typename conditional<is_reference<_Dp>::value,
222 is_convertible<_Ep, _Dp>>::type>>
224 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
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 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>,
269 __not_<is_array<_Up>>
274 reset(__u.release());
275 get_deleter() = std::forward<_Ep>(__u.get_deleter());
290 typename add_lvalue_reference<element_type>::type
293 _GLIBCXX_DEBUG_ASSERT(
get() != pointer());
301 _GLIBCXX_DEBUG_ASSERT(
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>>> >;
397 template<
typename _Up,
typename _Ep,
398 typename _Tp_pointer =
typename _Pointer::type,
399 typename _Up_pointer =
typename unique_ptr<_Up, _Ep>::pointer>
400 using __safe_conversion = __and_<
401 is_convertible<_Up_pointer, _Tp_pointer>,
403 __or_<__not_<is_pointer<_Up_pointer>>,
404 __not_<is_pointer<_Tp_pointer>>,
405 __not_<__is_derived_Tp<typename remove_extent<_Up>::type>>
410 typedef typename _Pointer::type pointer;
411 typedef _Tp element_type;
412 typedef _Dp deleter_type;
420 "constructed with null function pointer deleter"); }
430 : _M_t(__p, deleter_type())
432 "constructed with null function pointer deleter"); }
435 template<
typename _Up,
typename = _Require<is_po
inter<po
inter>,
436 is_convertible<_Up*, po
inter>, __is_derived_Tp<_Up>>>
449 deleter_type,
const deleter_type&>::type __d) noexcept
460 remove_reference<deleter_type>::type&& __d) noexcept
461 : _M_t(std::move(__p), std::move(__d))
463 "rvalue deleter bound to reference"); }
467 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
472 template<
typename _Up,
typename _Ep,
473 typename = _Require<__safe_conversion<_Up, _Ep>,
474 typename conditional<is_reference<_Dp>::value,
476 is_convertible<_Ep, _Dp>>::type
479 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
485 auto& __ptr = std::get<0>(_M_t);
486 if (__ptr !=
nullptr)
502 reset(__u.release());
503 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
514 template<
typename _Up,
typename _Ep>
516 enable_if<__safe_conversion<_Up, _Ep>::value,
unique_ptr&>::type
519 reset(__u.release());
520 get_deleter() = std::forward<_Ep>(__u.get_deleter());
535 typename std::add_lvalue_reference<element_type>::type
538 _GLIBCXX_DEBUG_ASSERT(
get() != pointer());
545 {
return std::get<0>(_M_t); }
550 {
return std::get<1>(_M_t); }
555 {
return std::get<1>(_M_t); }
558 explicit operator bool() const noexcept
559 {
return get() == pointer() ? false :
true; }
568 std::get<0>(_M_t) = pointer();
579 reset(pointer __p = pointer()) noexcept
582 swap(std::get<0>(_M_t), __p);
588 template<
typename _Up,
typename = _Require<is_po
inter<po
inter>,
589 is_convertible<_Up*, po
inter>, __is_derived_Tp<_Up>>>
590 void reset(_Up*) =
delete;
597 swap(_M_t, __u._M_t);
605 template<
typename _Up,
typename = _Require<is_po
inter<po
inter>,
606 is_convertible<_Up*, po
inter>, __is_derived_Tp<_Up>>>
609 deleter_type,
const deleter_type&>::type) =
delete;
612 template<
typename _Up,
typename = _Require<is_po
inter<po
inter>,
613 is_convertible<_Up*, po
inter>, __is_derived_Tp<_Up>>>
615 remove_reference<deleter_type>::type&&) =
delete;
618 template<
typename _Tp,
typename _Dp>
624 template<
typename _Tp,
typename _Dp,
625 typename _Up,
typename _Ep>
629 {
return __x.
get() == __y.
get(); }
631 template<
typename _Tp,
typename _Dp>
636 template<
typename _Tp,
typename _Dp>
641 template<
typename _Tp,
typename _Dp,
642 typename _Up,
typename _Ep>
646 {
return __x.
get() != __y.
get(); }
648 template<
typename _Tp,
typename _Dp>
651 {
return (
bool)__x; }
653 template<
typename _Tp,
typename _Dp>
656 {
return (
bool)__x; }
658 template<
typename _Tp,
typename _Dp,
659 typename _Up,
typename _Ep>
661 operator<(const unique_ptr<_Tp, _Dp>& __x,
665 std::common_type<typename unique_ptr<_Tp, _Dp>::pointer,
666 typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
670 template<
typename _Tp,
typename _Dp>
672 operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
676 template<
typename _Tp,
typename _Dp>
678 operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
682 template<
typename _Tp,
typename _Dp,
683 typename _Up,
typename _Ep>
685 operator<=(const unique_ptr<_Tp, _Dp>& __x,
687 {
return !(__y < __x); }
689 template<
typename _Tp,
typename _Dp>
691 operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
692 {
return !(
nullptr < __x); }
694 template<
typename _Tp,
typename _Dp>
696 operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
697 {
return !(__x <
nullptr); }
699 template<
typename _Tp,
typename _Dp,
700 typename _Up,
typename _Ep>
704 {
return (__y < __x); }
706 template<
typename _Tp,
typename _Dp>
712 template<
typename _Tp,
typename _Dp>
718 template<
typename _Tp,
typename _Dp,
719 typename _Up,
typename _Ep>
723 {
return !(__x < __y); }
725 template<
typename _Tp,
typename _Dp>
728 {
return !(__x <
nullptr); }
730 template<
typename _Tp,
typename _Dp>
733 {
return !(
nullptr < __x); }
736 template<
typename _Tp,
typename _Dp>
738 :
public __hash_base<size_t, unique_ptr<_Tp, _Dp>>
748 #if __cplusplus > 201103L 750 #define __cpp_lib_make_unique 201304 752 template<
typename _Tp>
756 template<
typename _Tp>
757 struct _MakeUniq<_Tp[]>
760 template<
typename _Tp,
size_t _Bound>
761 struct _MakeUniq<_Tp[_Bound]>
762 {
struct __invalid_type { }; };
765 template<
typename _Tp,
typename... _Args>
771 template<
typename _Tp>
772 inline typename _MakeUniq<_Tp>::__array
777 template<
typename _Tp,
typename... _Args>
778 inline typename _MakeUniq<_Tp>::__invalid_type
784 _GLIBCXX_END_NAMESPACE_VERSION
unique_ptr & operator=(unique_ptr &&__u) noexcept
Move assignment operator.
unique_ptr(pointer __p, typename remove_reference< deleter_type >::type &&__d) noexcept
deleter_type & get_deleter() noexcept
Return a reference to the stored deleter.
pointer operator->() const noexcept
Return the stored pointer.
pointer release() noexcept
Release ownership of any stored pointer.
add_lvalue_reference< element_type >::type operator*() const
Dereference the stored pointer.
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.
One of the comparison functors.
unique_ptr(unique_ptr &&__u) noexcept
Move constructor.
void reset(pointer __p=pointer()) noexcept
Replace the stored pointer.
~unique_ptr()
Destructor, invokes the deleter if the stored pointer is not null.
pointer release() noexcept
Release ownership of any stored pointer.
A simple smart pointer providing strict ownership semantics.
unique_ptr(pointer __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.
unique_ptr(unique_ptr< _Up, _Ep > &&__u) noexcept
Converting constructor from another type.
unique_ptr(pointer __p, typename remove_reference< deleter_type >::type &&__d) noexcept
default_delete(const default_delete< _Up[]> &) noexcept
Converting constructor.
_Del * get_deleter(const __shared_ptr< _Tp, _Lp > &__p) noexcept
20.7.2.2.10 shared_ptr get_deleter
void operator()(_Tp *__ptr) const
Calls delete[] __ptr.
ISO C++ entities toplevel namespace is std.
unique_ptr(pointer __p) noexcept
void swap(unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
constexpr default_delete() noexcept=default
Default constructor.
_MakeUniq< _Tp >::__single_object make_unique(_Args &&... __args)
std::make_unique for single objects
const deleter_type & get_deleter() const noexcept
Return a reference to the stored deleter.
constexpr unique_ptr() noexcept
Default constructor, creates a unique_ptr that owns nothing.
constexpr unique_ptr() noexcept
Default constructor, creates a unique_ptr that owns nothing.
void operator()(_Tp *__ptr) const
Calls delete __ptr.
Primary template of default_delete, used by unique_ptr.
enable_if< __and_< is_convertible< typename unique_ptr< _Up, _Ep >::pointer, pointer >, __not_< is_array< _Up > > >::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.
enable_if< __safe_conversion< _Up, _Ep >::value, unique_ptr & >::type operator=(unique_ptr< _Up, _Ep > &&__u) noexcept
Assignment from another type.
~unique_ptr() noexcept
Destructor, invokes the deleter if the stored pointer is not null.
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.
deleter_type & get_deleter() noexcept
Return a reference to the stored deleter.
Primary class template hash.
std::add_lvalue_reference< element_type >::type operator[](size_t __i) const
Access an element of owned array.
unique_ptr(unique_ptr &&__u) noexcept
Move constructor.
void reset(pointer __p=pointer()) noexcept
Replace the stored pointer.
unique_ptr(pointer __p, typename conditional< is_reference< deleter_type >::value, deleter_type, const deleter_type &>::type __d) noexcept
unique_ptr(pointer __p) noexcept
constexpr unique_ptr(nullptr_t) noexcept
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.