31 #define _UNIQUE_PTR_H 1
33 #include <bits/c++config.h>
35 #include <type_traits>
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
48 #if _GLIBCXX_USE_DEPRECATED
49 template<
typename>
class auto_ptr;
53 template<
typename _Tp>
56 constexpr default_delete() noexcept = default;
58 template<typename _Up, typename = typename
59 enable_if<is_convertible<_Up*, _Tp*>::value>::type>
60 default_delete(const default_delete<_Up>&) noexcept { }
63 operator()(_Tp* __ptr)
const
65 static_assert(
sizeof(_Tp)>0,
66 "can't delete pointer to incomplete type");
74 template<
typename _Tp>
75 struct default_delete<_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>>> >;
88 constexpr default_delete() noexcept = default;
90 template<typename _Up, typename = typename
91 enable_if<!__is_derived_Tp<_Up>::value>::type>
92 default_delete(const default_delete<_Up[]>&) noexcept { }
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;
135 constexpr unique_ptr() noexcept
137 { static_assert(!is_pointer<deleter_type>::value,
138 "constructed with null function pointer deleter"); }
141 unique_ptr(pointer __p) noexcept
142 : _M_t(__p, deleter_type())
143 { static_assert(!is_pointer<deleter_type>::value,
144 "constructed with null function pointer deleter"); }
146 unique_ptr(pointer __p,
147 typename conditional<is_reference<deleter_type>::value,
148 deleter_type,
const deleter_type&>::type __d) noexcept
151 unique_ptr(pointer __p,
152 typename remove_reference<deleter_type>::type&& __d) noexcept
153 : _M_t(std::move(__p), std::move(__d))
154 { static_assert(!std::is_reference<deleter_type>::value,
155 "rvalue deleter bound to reference"); }
157 constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { }
160 unique_ptr(unique_ptr&& __u) noexcept
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>>
169 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
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>>>>
176 unique_ptr(auto_ptr<_Up>&& __u) noexcept;
180 ~unique_ptr() noexcept
182 auto& __ptr = std::get<0>(_M_t);
183 if (__ptr !=
nullptr)
184 get_deleter()(__ptr);
190 operator=(unique_ptr&& __u) noexcept
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>>
203 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
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
226 operator->() const noexcept
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())
266 swap(unique_ptr& __u) noexcept
269 swap(_M_t, __u._M_t);
273 unique_ptr(
const unique_ptr&) =
delete;
274 unique_ptr& operator=(
const unique_ptr&) =
delete;
281 template<
typename _Tp,
typename _Dp>
282 class unique_ptr<_Tp[], _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;
329 constexpr unique_ptr() noexcept
331 { static_assert(!std::is_pointer<deleter_type>::value,
332 "constructed with null function pointer deleter"); }
335 unique_ptr(pointer __p) noexcept
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>>>
343 unique_ptr(_Up* __p) =
delete;
345 unique_ptr(pointer __p,
346 typename conditional<is_reference<deleter_type>::value,
347 deleter_type,
const deleter_type&>::type __d) noexcept
350 unique_ptr(pointer __p,
typename
351 remove_reference<deleter_type>::type&& __d) noexcept
352 : _M_t(std::move(__p), std::move(__d))
353 { static_assert(!is_reference<deleter_type>::value,
354 "rvalue deleter bound to reference"); }
357 unique_ptr(unique_ptr&& __u) noexcept
358 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
360 constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { }
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
368 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
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);
383 operator=(unique_ptr&& __u) noexcept
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
393 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
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
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;
457 swap(unique_ptr& __u) noexcept
460 swap(_M_t, __u._M_t);
464 unique_ptr(
const unique_ptr&) =
delete;
465 unique_ptr& operator=(
const unique_ptr&) =
delete;
468 template<
typename _Up,
typename = _Require<is_po
inter<po
inter>,
469 is_convertible<_Up*, po
inter>, __is_derived_Tp<_Up>>>
470 unique_ptr(_Up*,
typename
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>>>
476 unique_ptr(_Up*,
typename
477 remove_reference<deleter_type>::type&&) =
delete;
480 template<
typename _Tp,
typename _Dp>
482 swap(unique_ptr<_Tp, _Dp>& __x,
483 unique_ptr<_Tp, _Dp>& __y) noexcept
486 template<
typename _Tp,
typename _Dp,
487 typename _Up,
typename _Ep>
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>
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;
529 return std::less<_CT>()(__x.get(), __y.get());
532 template<
typename _Tp,
typename _Dp>
534 operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
535 {
return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(),
538 template<
typename _Tp,
typename _Dp>
540 operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
541 {
return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(
nullptr,
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)
571 {
return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(
nullptr,
574 template<
typename _Tp,
typename _Dp>
576 operator>(nullptr_t,
const unique_ptr<_Tp, _Dp>& __x)
577 {
return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(),
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>
599 struct hash<unique_ptr<_Tp, _Dp>>
600 :
public __hash_base<size_t, unique_ptr<_Tp, _Dp>>
603 operator()(
const unique_ptr<_Tp, _Dp>& __u)
const noexcept
605 typedef unique_ptr<_Tp, _Dp> _UP;
606 return std::hash<typename _UP::pointer>()(__u.get());
612 _GLIBCXX_END_NAMESPACE_VERSION
bool operator>=(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:644
bool operator==(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
#define _GLIBCXX_DEBUG_ASSERT(_Condition)
Definition: debug.h:61
#define bool
Definition: stdbool.h:33
namespace std _GLIBCXX_VISIBILITY(default)
Definition: auto_ptr.h:36
bool operator>(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:612
bool operator!=(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
void swap(exception_ptr &__lhs, exception_ptr &__rhs)
Definition: exception_ptr.h:160