1 // unique_ptr implementation -*- C++ -*-
3 // Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file unique_ptr.h
26 * This is an internal header file, included by other library headers.
27 * You should not attempt to use it directly.
31 #define _UNIQUE_PTR_H 1
33 #include <bits/c++config.h>
34 #include <debug/debug.h>
35 #include <type_traits>
39 _GLIBCXX_BEGIN_NAMESPACE(std
)
42 * @addtogroup pointer_abstractions
46 /// Primary template, default_delete.
47 template<typename _Tp
>
50 constexpr default_delete() { }
52 template<typename _Up
, typename
= typename
53 std::enable_if
<std::is_convertible
<_Up
*, _Tp
*>::value
>::type
>
54 default_delete(const default_delete
<_Up
>&) { }
57 operator()(_Tp
* __ptr
) const
59 static_assert(sizeof(_Tp
)>0,
60 "can't delete pointer to incomplete type");
65 // _GLIBCXX_RESOLVE_LIB_DEFECTS
66 // DR 740 - omit specialization for array objects with a compile time length
67 /// Specialization, default_delete.
68 template<typename _Tp
>
69 struct default_delete
<_Tp
[]>
71 constexpr default_delete() { }
74 operator()(_Tp
* __ptr
) const
76 static_assert(sizeof(_Tp
)>0,
77 "can't delete pointer to incomplete type");
82 /// 20.7.12.2 unique_ptr for single objects.
83 template <typename _Tp
, typename _Dp
= default_delete
<_Tp
> >
86 // use SFINAE to determine whether _Del::pointer exists
89 template<typename _Up
>
90 static typename
_Up::pointer
__test(typename
_Up::pointer
*);
92 template<typename _Up
>
93 static _Tp
* __test(...);
95 typedef typename remove_reference
<_Dp
>::type _Del
;
98 typedef decltype( __test
<_Del
>(0)) type
;
101 typedef std::tuple
<_Tp
*, _Dp
> __tuple_type
;
105 typedef typename
_Pointer::type pointer
;
106 typedef _Tp element_type
;
107 typedef _Dp deleter_type
;
110 constexpr unique_ptr()
112 { static_assert(!std::is_pointer
<deleter_type
>::value
,
113 "constructed with null function pointer deleter"); }
116 unique_ptr(pointer __p
)
117 : _M_t(__p
, deleter_type())
118 { static_assert(!std::is_pointer
<deleter_type
>::value
,
119 "constructed with null function pointer deleter"); }
121 unique_ptr(pointer __p
,
122 typename
std::conditional
<std::is_reference
<deleter_type
>::value
,
123 deleter_type
, const deleter_type
&>::type __d
)
126 unique_ptr(pointer __p
,
127 typename
std::remove_reference
<deleter_type
>::type
&& __d
)
128 : _M_t(std::move(__p
), std::move(__d
))
129 { static_assert(!std::is_reference
<deleter_type
>::value
,
130 "rvalue deleter bound to reference"); }
132 constexpr unique_ptr(nullptr_t
)
134 { static_assert(!std::is_pointer
<deleter_type
>::value
,
135 "constructed with null function pointer deleter"); }
137 // Move constructors.
138 unique_ptr(unique_ptr
&& __u
)
139 : _M_t(__u
.release(), std::forward
<deleter_type
>(__u
.get_deleter())) { }
141 template<typename _Up
, typename _Ep
, typename
= typename
143 <std::is_convertible
<typename unique_ptr
<_Up
, _Ep
>::pointer
,
145 && !std::is_array
<_Up
>::value
146 && ((std::is_reference
<_Dp
>::value
147 && std::is_same
<_Ep
, _Dp
>::value
)
148 || (!std::is_reference
<_Dp
>::value
149 && std::is_convertible
<_Ep
, _Dp
>::value
))>
151 unique_ptr(unique_ptr
<_Up
, _Ep
>&& __u
)
152 : _M_t(__u
.release(), std::forward
<deleter_type
>(__u
.get_deleter()))
155 #if _GLIBCXX_DEPRECATED
156 template<typename _Up
, typename
= typename
157 std::enable_if
<std::is_convertible
<_Up
*, _Tp
*>::value
159 default_delete
<_Tp
>>::value
>::type
>
160 unique_ptr(auto_ptr
<_Up
>&& __u
)
161 : _M_t(__u
.release(), deleter_type()) { }
165 ~unique_ptr() { reset(); }
169 operator=(unique_ptr
&& __u
)
171 reset(__u
.release());
172 get_deleter() = std::move(__u
.get_deleter());
176 template<typename _Up
, typename _Ep
, typename
= typename
178 <std::is_convertible
<typename unique_ptr
<_Up
, _Ep
>::pointer
,
180 && !std::is_array
<_Up
>::value
>::type
>
182 operator=(unique_ptr
<_Up
, _Ep
>&& __u
)
184 reset(__u
.release());
185 get_deleter() = std::move(__u
.get_deleter());
197 typename
std::add_lvalue_reference
<element_type
>::type
200 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
207 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
213 { return std::get
<0>(_M_t
); }
217 { return std::get
<1>(_M_t
); }
221 { return std::get
<1>(_M_t
); }
223 explicit operator bool() const
224 { return get() == pointer() ? false : true; }
231 std::get
<0>(_M_t
) = pointer();
236 reset(pointer __p
= pointer())
239 swap(std::get
<0>(_M_t
), __p
);
240 if (__p
!= pointer())
245 swap(unique_ptr
& __u
)
248 swap(_M_t
, __u
._M_t
);
251 // Disable copy from lvalue.
252 unique_ptr(const unique_ptr
&) = delete;
253 unique_ptr
& operator=(const unique_ptr
&) = delete;
256 /// 20.7.12.3 unique_ptr for array objects with a runtime length
257 // [unique.ptr.runtime]
258 // _GLIBCXX_RESOLVE_LIB_DEFECTS
259 // DR 740 - omit specialization for array objects with a compile time length
260 template<typename _Tp
, typename _Dp
>
261 class unique_ptr
<_Tp
[], _Dp
>
263 typedef std::tuple
<_Tp
*, _Dp
> __tuple_type
;
267 typedef _Tp
* pointer
;
268 typedef _Tp element_type
;
269 typedef _Dp deleter_type
;
272 constexpr unique_ptr()
274 { static_assert(!std::is_pointer
<deleter_type
>::value
,
275 "constructed with null function pointer deleter"); }
278 unique_ptr(pointer __p
)
279 : _M_t(__p
, deleter_type())
280 { static_assert(!std::is_pointer
<deleter_type
>::value
,
281 "constructed with null function pointer deleter"); }
283 unique_ptr(pointer __p
,
284 typename
std::conditional
<std::is_reference
<deleter_type
>::value
,
285 deleter_type
, const deleter_type
&>::type __d
)
288 unique_ptr(pointer __p
,
289 typename
std::remove_reference
<deleter_type
>::type
&& __d
)
290 : _M_t(std::move(__p
), std::move(__d
))
291 { static_assert(!std::is_reference
<deleter_type
>::value
,
292 "rvalue deleter bound to reference"); }
294 constexpr unique_ptr(nullptr_t
)
296 { static_assert(!std::is_pointer
<deleter_type
>::value
,
297 "constructed with null function pointer deleter"); }
299 // Move constructors.
300 unique_ptr(unique_ptr
&& __u
)
301 : _M_t(__u
.release(), std::forward
<deleter_type
>(__u
.get_deleter())) { }
303 template<typename _Up
, typename _Ep
>
304 unique_ptr(unique_ptr
<_Up
, _Ep
>&& __u
)
305 : _M_t(__u
.release(), std::forward
<deleter_type
>(__u
.get_deleter()))
309 ~unique_ptr() { reset(); }
313 operator=(unique_ptr
&& __u
)
315 reset(__u
.release());
316 get_deleter() = std::move(__u
.get_deleter());
320 template<typename _Up
, typename _Ep
>
322 operator=(unique_ptr
<_Up
, _Ep
>&& __u
)
324 reset(__u
.release());
325 get_deleter() = std::move(__u
.get_deleter());
337 typename
std::add_lvalue_reference
<element_type
>::type
338 operator[](size_t __i
) const
340 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
346 { return std::get
<0>(_M_t
); }
350 { return std::get
<1>(_M_t
); }
354 { return std::get
<1>(_M_t
); }
356 explicit operator bool() const
357 { return get() == pointer() ? false : true; }
364 std::get
<0>(_M_t
) = pointer();
369 reset(pointer __p
= pointer())
372 swap(std::get
<0>(_M_t
), __p
);
381 std::get
<0>(_M_t
) = pointer();
387 template<typename _Up
>
388 void reset(_Up
) = delete;
391 swap(unique_ptr
& __u
)
394 swap(_M_t
, __u
._M_t
);
397 // Disable copy from lvalue.
398 unique_ptr(const unique_ptr
&) = delete;
399 unique_ptr
& operator=(const unique_ptr
&) = delete;
401 // Disable construction from convertible pointer types.
402 // (N2315 - 20.6.5.3.1)
403 template<typename _Up
>
404 unique_ptr(_Up
*, typename
405 std::conditional
<std::is_reference
<deleter_type
>::value
,
406 deleter_type
, const deleter_type
&>::type
,
407 typename
std::enable_if
<std::is_convertible
<_Up
*,
408 pointer
>::value
>::type
* = 0) = delete;
410 template<typename _Up
>
411 unique_ptr(_Up
*, typename
std::remove_reference
<deleter_type
>::type
&&,
412 typename
std::enable_if
<std::is_convertible
<_Up
*,
413 pointer
>::value
>::type
* = 0) = delete;
415 template<typename _Up
>
417 unique_ptr(_Up
*, typename
std::enable_if
<std::is_convertible
<_Up
*,
418 pointer
>::value
>::type
* = 0) = delete;
421 template<typename _Tp
, typename _Dp
>
423 swap(unique_ptr
<_Tp
, _Dp
>& __x
,
424 unique_ptr
<_Tp
, _Dp
>& __y
)
427 template<typename _Tp
, typename _Dp
,
428 typename _Up
, typename _Ep
>
430 operator==(const unique_ptr
<_Tp
, _Dp
>& __x
,
431 const unique_ptr
<_Up
, _Ep
>& __y
)
432 { return __x
.get() == __y
.get(); }
434 template<typename _Tp
, typename _Dp
>
436 operator==(const unique_ptr
<_Tp
, _Dp
>& __x
, nullptr_t
)
437 { return __x
.get() == nullptr; }
439 template<typename _Tp
, typename _Dp
>
441 operator==(nullptr_t
, const unique_ptr
<_Tp
, _Dp
>& __y
)
442 { return nullptr == __y
.get(); }
444 template<typename _Tp
, typename _Dp
,
445 typename _Up
, typename _Ep
>
447 operator!=(const unique_ptr
<_Tp
, _Dp
>& __x
,
448 const unique_ptr
<_Up
, _Ep
>& __y
)
449 { return !(__x
.get() == __y
.get()); }
451 template<typename _Tp
, typename _Dp
>
453 operator!=(const unique_ptr
<_Tp
, _Dp
>& __x
, nullptr_t
)
454 { return __x
.get() != nullptr; }
456 template<typename _Tp
, typename _Dp
>
458 operator!=(nullptr_t
, const unique_ptr
<_Tp
, _Dp
>& __y
)
459 { return nullptr != __y
.get(); }
461 template<typename _Tp
, typename _Dp
,
462 typename _Up
, typename _Ep
>
464 operator<(const unique_ptr
<_Tp
, _Dp
>& __x
,
465 const unique_ptr
<_Up
, _Ep
>& __y
)
466 { return __x
.get() < __y
.get(); }
468 template<typename _Tp
, typename _Dp
,
469 typename _Up
, typename _Ep
>
471 operator<=(const unique_ptr
<_Tp
, _Dp
>& __x
,
472 const unique_ptr
<_Up
, _Ep
>& __y
)
473 { return !(__y
.get() < __x
.get()); }
475 template<typename _Tp
, typename _Dp
,
476 typename _Up
, typename _Ep
>
478 operator>(const unique_ptr
<_Tp
, _Dp
>& __x
,
479 const unique_ptr
<_Up
, _Ep
>& __y
)
480 { return __y
.get() < __x
.get(); }
482 template<typename _Tp
, typename _Dp
,
483 typename _Up
, typename _Ep
>
485 operator>=(const unique_ptr
<_Tp
, _Dp
>& __x
,
486 const unique_ptr
<_Up
, _Ep
>& __y
)
487 { return !(__x
.get() < __y
.get()); }
489 /// std::hash specialization for unique_ptr.
490 template<typename _Tp
, typename _Dp
>
491 struct hash
<unique_ptr
<_Tp
, _Dp
>>
492 : public std::unary_function
<unique_ptr
<_Tp
, _Dp
>, size_t>
495 operator()(const unique_ptr
<_Tp
, _Dp
>& __u
) const
497 typedef unique_ptr
<_Tp
, _Dp
> _UP
;
498 return std::hash
<typename
_UP::pointer
>()(__u
.get());
502 // @} group pointer_abstractions
504 _GLIBCXX_END_NAMESPACE
506 #endif /* _UNIQUE_PTR_H */