31 #define _UNIQUE_PTR_H 1
35 #include <type_traits>
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>
58 template<
typename _Up,
typename =
typename
59 enable_if<is_convertible<_Up*, _Tp*>::value>::type>
63 operator()(_Tp* __ptr)
const
65 static_assert(
sizeof(_Tp)>0,
66 "can't delete pointer to incomplete type");
74 template<
typename _Tp>
78 template<
typename _Up>
79 using __remove_cv =
typename remove_cv<_Up>::type;
82 template<
typename _Up>
84 = __and_< is_base_of<_Tp, _Up>,
85 __not_<is_same<__remove_cv<_Tp>, __remove_cv<_Up>>> >;
90 template<
typename _Up,
typename =
typename
91 enable_if<!__is_derived_Tp<_Up>::value>::type>
95 operator()(_Tp* __ptr)
const
97 static_assert(
sizeof(_Tp)>0,
98 "can't delete pointer to incomplete type");
102 template<
typename _Up>
103 typename enable_if<__is_derived_Tp<_Up>::value>::type
104 operator()(_Up*)
const =
delete;
108 template <
typename _Tp,
typename _Dp = default_delete<_Tp> >
114 template<
typename _Up>
115 static typename _Up::pointer __test(
typename _Up::pointer*);
117 template<
typename _Up>
118 static _Tp* __test(...);
120 typedef typename remove_reference<_Dp>::type _Del;
123 typedef decltype(__test<_Del>(0)) type;
126 typedef std::tuple<typename _Pointer::type, _Dp> __tuple_type;
130 typedef typename _Pointer::type pointer;
131 typedef _Tp element_type;
132 typedef _Dp deleter_type;
137 { static_assert(!is_pointer<deleter_type>::value,
138 "constructed with null function pointer deleter"); }
142 : _M_t(__p, deleter_type())
143 { static_assert(!is_pointer<deleter_type>::value,
144 "constructed with null function pointer deleter"); }
147 typename conditional<is_reference<deleter_type>::value,
148 deleter_type,
const deleter_type&>::type __d) noexcept
152 typename remove_reference<deleter_type>::type&& __d) noexcept
154 { static_assert(!std::is_reference<deleter_type>::value,
155 "rvalue deleter bound to reference"); }
161 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
163 template<
typename _Up,
typename _Ep,
typename = _Require<
164 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>,
165 __not_<is_array<_Up>>,
166 typename conditional<is_reference<_Dp>::value,
168 is_convertible<_Ep, _Dp>>::type>>
170 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
173 #if _GLIBCXX_USE_DEPRECATED
174 template<
typename _Up,
typename = _Require<
175 is_convertible<_Up*, _Tp*>, is_same<_Dp, default_delete<_Tp>>>>
182 auto& __ptr = std::get<0>(_M_t);
183 if (__ptr !=
nullptr)
184 get_deleter()(__ptr);
192 reset(__u.release());
193 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
197 template<
typename _Up,
typename _Ep>
198 typename enable_if< __and_<
199 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>,
200 __not_<is_array<_Up>>
205 reset(__u.release());
206 get_deleter() = std::forward<_Ep>(__u.get_deleter());
211 operator=(nullptr_t) noexcept
218 typename add_lvalue_reference<element_type>::type
221 _GLIBCXX_DEBUG_ASSERT(
get() != pointer());
226 operator->()
const noexcept
228 _GLIBCXX_DEBUG_ASSERT(
get() != pointer());
234 {
return std::get<0>(_M_t); }
237 get_deleter() noexcept
238 {
return std::get<1>(_M_t); }
241 get_deleter()
const noexcept
242 {
return std::get<1>(_M_t); }
244 explicit operator bool()
const noexcept
245 {
return get() == pointer() ?
false :
true; }
252 std::get<0>(_M_t) = pointer();
257 reset(pointer __p = pointer()) noexcept
260 swap(std::get<0>(_M_t), __p);
261 if (__p != pointer())
269 swap(_M_t, __u._M_t);
281 template<
typename _Tp,
typename _Dp>
287 template<
typename _Up>
288 static typename _Up::pointer __test(
typename _Up::pointer*);
290 template<
typename _Up>
291 static _Tp* __test(...);
293 typedef typename remove_reference<_Dp>::type _Del;
296 typedef decltype(__test<_Del>(0)) type;
299 typedef std::tuple<typename _Pointer::type, _Dp> __tuple_type;
302 template<
typename _Up>
303 using __remove_cv =
typename remove_cv<_Up>::type;
306 template<
typename _Up>
307 using __is_derived_Tp
308 = __and_< is_base_of<_Tp, _Up>,
309 __not_<is_same<__remove_cv<_Tp>, __remove_cv<_Up>>> >;
311 template<
typename _Up,
typename _Ep,
312 typename _Tp_pointer =
typename _Pointer::type,
313 typename _Up_pointer =
typename unique_ptr<_Up, _Ep>::pointer>
314 using __safe_conversion = __and_<
315 is_convertible<_Up_pointer, _Tp_pointer>,
317 __or_<__not_<is_pointer<_Up_pointer>>,
318 __not_<is_pointer<_Tp_pointer>>,
319 __not_<__is_derived_Tp<typename remove_extent<_Up>::type>>
324 typedef typename _Pointer::type pointer;
325 typedef _Tp element_type;
326 typedef _Dp deleter_type;
331 { static_assert(!std::is_pointer<deleter_type>::value,
332 "constructed with null function pointer deleter"); }
336 : _M_t(__p, deleter_type())
337 { static_assert(!is_pointer<deleter_type>::value,
338 "constructed with null function pointer deleter"); }
340 template<
typename _Up,
typename = _Require<is_po
inter<po
inter>,
341 is_convertible<_Up*, po
inter>, __is_derived_Tp<_Up>>>
346 typename conditional<is_reference<deleter_type>::value,
347 deleter_type,
const deleter_type&>::type __d) noexcept
351 remove_reference<deleter_type>::type&& __d) noexcept
353 { static_assert(!is_reference<deleter_type>::value,
354 "rvalue deleter bound to reference"); }
358 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
362 template<
typename _Up,
typename _Ep,
363 typename = _Require<__safe_conversion<_Up, _Ep>,
364 typename conditional<is_reference<_Dp>::value,
366 is_convertible<_Ep, _Dp>>::type
369 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
375 auto& __ptr = std::get<0>(_M_t);
376 if (__ptr !=
nullptr)
377 get_deleter()(__ptr);
385 reset(__u.release());
386 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
390 template<
typename _Up,
typename _Ep>
392 enable_if<__safe_conversion<_Up, _Ep>::value,
unique_ptr&>::type
395 reset(__u.release());
396 get_deleter() = std::forward<_Ep>(__u.get_deleter());
401 operator=(nullptr_t) noexcept
408 typename std::add_lvalue_reference<element_type>::type
409 operator[](
size_t __i)
const
411 _GLIBCXX_DEBUG_ASSERT(
get() != pointer());
417 {
return std::get<0>(_M_t); }
420 get_deleter() noexcept
421 {
return std::get<1>(_M_t); }
424 get_deleter()
const noexcept
425 {
return std::get<1>(_M_t); }
427 explicit operator bool()
const noexcept
428 {
return get() == pointer() ?
false :
true; }
435 std::get<0>(_M_t) = pointer();
441 { reset(pointer()); }
444 reset(pointer __p) noexcept
447 swap(std::get<0>(_M_t), __p);
452 template<
typename _Up,
typename = _Require<is_po
inter<po
inter>,
453 is_convertible<_Up*, po
inter>, __is_derived_Tp<_Up>>>
454 void reset(_Up*) =
delete;
460 swap(_M_t, __u._M_t);
468 template<
typename _Up,
typename = _Require<is_po
inter<po
inter>,
469 is_convertible<_Up*, po
inter>, __is_derived_Tp<_Up>>>
471 conditional<is_reference<deleter_type>::value,
472 deleter_type,
const deleter_type&>::type) =
delete;
474 template<
typename _Up,
typename = _Require<is_po
inter<po
inter>,
475 is_convertible<_Up*, po
inter>, __is_derived_Tp<_Up>>>
477 remove_reference<deleter_type>::type&&) =
delete;
480 template<
typename _Tp,
typename _Dp>
486 template<
typename _Tp,
typename _Dp,
487 typename _Up,
typename _Ep>
489 operator==(
const unique_ptr<_Tp, _Dp>& __x,
490 const unique_ptr<_Up, _Ep>& __y)
491 {
return __x.get() == __y.get(); }
493 template<
typename _Tp,
typename _Dp>
495 operator==(
const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
498 template<
typename _Tp,
typename _Dp>
500 operator==(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x) noexcept
503 template<
typename _Tp,
typename _Dp,
504 typename _Up,
typename _Ep>
506 operator!=(
const unique_ptr<_Tp, _Dp>& __x,
507 const unique_ptr<_Up, _Ep>& __y)
508 {
return __x.get() != __y.get(); }
510 template<
typename _Tp,
typename _Dp>
512 operator!=(
const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
513 {
return (
bool)__x; }
515 template<
typename _Tp,
typename _Dp>
517 operator!=(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x) noexcept
518 {
return (
bool)__x; }
520 template<
typename _Tp,
typename _Dp,
521 typename _Up,
typename _Ep>
523 operator<(const unique_ptr<_Tp, _Dp>& __x,
524 const unique_ptr<_Up, _Ep>& __y)
527 std::common_type<typename unique_ptr<_Tp, _Dp>::pointer,
528 typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
532 template<
typename _Tp,
typename _Dp>
534 operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
538 template<
typename _Tp,
typename _Dp>
540 operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
544 template<
typename _Tp,
typename _Dp,
545 typename _Up,
typename _Ep>
547 operator<=(const unique_ptr<_Tp, _Dp>& __x,
548 const unique_ptr<_Up, _Ep>& __y)
549 {
return !(__y < __x); }
551 template<
typename _Tp,
typename _Dp>
553 operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
554 {
return !(
nullptr < __x); }
556 template<
typename _Tp,
typename _Dp>
558 operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
559 {
return !(__x <
nullptr); }
561 template<
typename _Tp,
typename _Dp,
562 typename _Up,
typename _Ep>
564 operator>(
const unique_ptr<_Tp, _Dp>& __x,
565 const unique_ptr<_Up, _Ep>& __y)
566 {
return (__y < __x); }
568 template<
typename _Tp,
typename _Dp>
570 operator>(
const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
574 template<
typename _Tp,
typename _Dp>
576 operator>(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x)
580 template<
typename _Tp,
typename _Dp,
581 typename _Up,
typename _Ep>
584 const unique_ptr<_Up, _Ep>& __y)
585 {
return !(__x < __y); }
587 template<
typename _Tp,
typename _Dp>
589 operator>=(
const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
590 {
return !(__x <
nullptr); }
592 template<
typename _Tp,
typename _Dp>
594 operator>=(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x)
595 {
return !(
nullptr < __x); }
598 template<
typename _Tp,
typename _Dp>
600 :
public __hash_base<size_t, unique_ptr<_Tp, _Dp>>
612 _GLIBCXX_END_NAMESPACE_VERSION
One of the comparison functors.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
bool operator>=(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Test if string doesn't precede string.
ISO C++ entities toplevel namespace is std.
A simple smart pointer providing strict ownership semantics.
void swap(_Tp &, _Tp &) noexcept(__and_< is_nothrow_move_constructible< _Tp >, is_nothrow_move_assignable< _Tp >>::value)
Swaps two values.
20.7.1.2 unique_ptr for single objects.
Primary class template hash.
Primary template, default_delete.
bool operator>(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Test if string follows string.