1 // unique_ptr implementation -*- C++ -*-
3 // Copyright (C) 2008, 2009, 2010, 2011 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 bits/unique_ptr.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{memory}
31 #define _UNIQUE_PTR_H 1
33 #include <bits/c++config.h>
34 #include <debug/debug.h>
35 #include <type_traits>
39 namespace std
_GLIBCXX_VISIBILITY(default)
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 * @addtogroup pointer_abstractions
48 /// Primary template, default_delete.
49 template<typename _Tp
>
52 constexpr default_delete() = default;
54 template<typename _Up
, typename
= typename
55 std::enable_if
<std::is_convertible
<_Up
*, _Tp
*>::value
>::type
>
56 default_delete(const default_delete
<_Up
>&) { }
59 operator()(_Tp
* __ptr
) const
61 static_assert(sizeof(_Tp
)>0,
62 "can't delete pointer to incomplete type");
67 // _GLIBCXX_RESOLVE_LIB_DEFECTS
68 // DR 740 - omit specialization for array objects with a compile time length
69 /// Specialization, default_delete.
70 template<typename _Tp
>
71 struct default_delete
<_Tp
[]>
73 constexpr default_delete() = default;
76 operator()(_Tp
* __ptr
) const
78 static_assert(sizeof(_Tp
)>0,
79 "can't delete pointer to incomplete type");
84 /// 20.7.12.2 unique_ptr for single objects.
85 template <typename _Tp
, typename _Dp
= default_delete
<_Tp
> >
88 // use SFINAE to determine whether _Del::pointer exists
91 template<typename _Up
>
92 static typename
_Up::pointer
__test(typename
_Up::pointer
*);
94 template<typename _Up
>
95 static _Tp
* __test(...);
97 typedef typename remove_reference
<_Dp
>::type _Del
;
100 typedef decltype( __test
<_Del
>(0)) type
;
103 typedef std::tuple
<_Tp
*, _Dp
> __tuple_type
;
107 typedef typename
_Pointer::type pointer
;
108 typedef _Tp element_type
;
109 typedef _Dp deleter_type
;
112 constexpr unique_ptr()
114 { static_assert(!std::is_pointer
<deleter_type
>::value
,
115 "constructed with null function pointer deleter"); }
118 unique_ptr(pointer __p
)
119 : _M_t(__p
, deleter_type())
120 { static_assert(!std::is_pointer
<deleter_type
>::value
,
121 "constructed with null function pointer deleter"); }
123 unique_ptr(pointer __p
,
124 typename
std::conditional
<std::is_reference
<deleter_type
>::value
,
125 deleter_type
, const deleter_type
&>::type __d
)
128 unique_ptr(pointer __p
,
129 typename
std::remove_reference
<deleter_type
>::type
&& __d
)
130 : _M_t(std::move(__p
), std::move(__d
))
131 { static_assert(!std::is_reference
<deleter_type
>::value
,
132 "rvalue deleter bound to reference"); }
134 constexpr unique_ptr(nullptr_t
)
136 { static_assert(!std::is_pointer
<deleter_type
>::value
,
137 "constructed with null function pointer deleter"); }
139 // Move constructors.
140 unique_ptr(unique_ptr
&& __u
)
141 : _M_t(__u
.release(), std::forward
<deleter_type
>(__u
.get_deleter())) { }
143 template<typename _Up
, typename _Ep
, typename
= typename
145 <std::is_convertible
<typename unique_ptr
<_Up
, _Ep
>::pointer
,
147 && !std::is_array
<_Up
>::value
148 && ((std::is_reference
<_Dp
>::value
149 && std::is_same
<_Ep
, _Dp
>::value
)
150 || (!std::is_reference
<_Dp
>::value
151 && std::is_convertible
<_Ep
, _Dp
>::value
))>
153 unique_ptr(unique_ptr
<_Up
, _Ep
>&& __u
)
154 : _M_t(__u
.release(), std::forward
<deleter_type
>(__u
.get_deleter()))
157 #if _GLIBCXX_USE_DEPRECATED
158 template<typename _Up
, typename
= typename
159 std::enable_if
<std::is_convertible
<_Up
*, _Tp
*>::value
161 default_delete
<_Tp
>>::value
>::type
>
162 unique_ptr(auto_ptr
<_Up
>&& __u
)
163 : _M_t(__u
.release(), deleter_type()) { }
167 ~unique_ptr() { reset(); }
171 operator=(unique_ptr
&& __u
)
173 reset(__u
.release());
174 get_deleter() = std::move(__u
.get_deleter());
178 template<typename _Up
, typename _Ep
, typename
= typename
180 <std::is_convertible
<typename unique_ptr
<_Up
, _Ep
>::pointer
,
182 && !std::is_array
<_Up
>::value
>::type
>
184 operator=(unique_ptr
<_Up
, _Ep
>&& __u
)
186 reset(__u
.release());
187 get_deleter() = std::move(__u
.get_deleter());
199 typename
std::add_lvalue_reference
<element_type
>::type
202 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
209 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
215 { return std::get
<0>(_M_t
); }
219 { return std::get
<1>(_M_t
); }
223 { return std::get
<1>(_M_t
); }
225 explicit operator bool() const
226 { return get() == pointer() ? false : true; }
233 std::get
<0>(_M_t
) = pointer();
238 reset(pointer __p
= pointer())
241 swap(std::get
<0>(_M_t
), __p
);
242 if (__p
!= pointer())
247 swap(unique_ptr
& __u
)
250 swap(_M_t
, __u
._M_t
);
253 // Disable copy from lvalue.
254 unique_ptr(const unique_ptr
&) = delete;
255 unique_ptr
& operator=(const unique_ptr
&) = delete;
258 /// 20.7.12.3 unique_ptr for array objects with a runtime length
259 // [unique.ptr.runtime]
260 // _GLIBCXX_RESOLVE_LIB_DEFECTS
261 // DR 740 - omit specialization for array objects with a compile time length
262 template<typename _Tp
, typename _Dp
>
263 class unique_ptr
<_Tp
[], _Dp
>
265 typedef std::tuple
<_Tp
*, _Dp
> __tuple_type
;
269 typedef _Tp
* pointer
;
270 typedef _Tp element_type
;
271 typedef _Dp deleter_type
;
274 constexpr unique_ptr()
276 { static_assert(!std::is_pointer
<deleter_type
>::value
,
277 "constructed with null function pointer deleter"); }
280 unique_ptr(pointer __p
)
281 : _M_t(__p
, deleter_type())
282 { static_assert(!std::is_pointer
<deleter_type
>::value
,
283 "constructed with null function pointer deleter"); }
285 unique_ptr(pointer __p
,
286 typename
std::conditional
<std::is_reference
<deleter_type
>::value
,
287 deleter_type
, const deleter_type
&>::type __d
)
290 unique_ptr(pointer __p
,
291 typename
std::remove_reference
<deleter_type
>::type
&& __d
)
292 : _M_t(std::move(__p
), std::move(__d
))
293 { static_assert(!std::is_reference
<deleter_type
>::value
,
294 "rvalue deleter bound to reference"); }
296 constexpr unique_ptr(nullptr_t
)
298 { static_assert(!std::is_pointer
<deleter_type
>::value
,
299 "constructed with null function pointer deleter"); }
301 // Move constructors.
302 unique_ptr(unique_ptr
&& __u
)
303 : _M_t(__u
.release(), std::forward
<deleter_type
>(__u
.get_deleter())) { }
305 template<typename _Up
, typename _Ep
>
306 unique_ptr(unique_ptr
<_Up
, _Ep
>&& __u
)
307 : _M_t(__u
.release(), std::forward
<deleter_type
>(__u
.get_deleter()))
311 ~unique_ptr() { reset(); }
315 operator=(unique_ptr
&& __u
)
317 reset(__u
.release());
318 get_deleter() = std::move(__u
.get_deleter());
322 template<typename _Up
, typename _Ep
>
324 operator=(unique_ptr
<_Up
, _Ep
>&& __u
)
326 reset(__u
.release());
327 get_deleter() = std::move(__u
.get_deleter());
339 typename
std::add_lvalue_reference
<element_type
>::type
340 operator[](size_t __i
) const
342 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
348 { return std::get
<0>(_M_t
); }
352 { return std::get
<1>(_M_t
); }
356 { return std::get
<1>(_M_t
); }
358 explicit operator bool() const
359 { return get() == pointer() ? false : true; }
366 std::get
<0>(_M_t
) = pointer();
371 reset(pointer __p
= pointer())
374 swap(std::get
<0>(_M_t
), __p
);
383 std::get
<0>(_M_t
) = pointer();
389 template<typename _Up
>
390 void reset(_Up
) = delete;
393 swap(unique_ptr
& __u
)
396 swap(_M_t
, __u
._M_t
);
399 // Disable copy from lvalue.
400 unique_ptr(const unique_ptr
&) = delete;
401 unique_ptr
& operator=(const unique_ptr
&) = delete;
403 // Disable construction from convertible pointer types.
404 // (N2315 - 20.6.5.3.1)
405 template<typename _Up
>
406 unique_ptr(_Up
*, typename
407 std::conditional
<std::is_reference
<deleter_type
>::value
,
408 deleter_type
, const deleter_type
&>::type
,
409 typename
std::enable_if
<std::is_convertible
<_Up
*,
410 pointer
>::value
>::type
* = 0) = delete;
412 template<typename _Up
>
413 unique_ptr(_Up
*, typename
std::remove_reference
<deleter_type
>::type
&&,
414 typename
std::enable_if
<std::is_convertible
<_Up
*,
415 pointer
>::value
>::type
* = 0) = delete;
417 template<typename _Up
>
419 unique_ptr(_Up
*, typename
std::enable_if
<std::is_convertible
<_Up
*,
420 pointer
>::value
>::type
* = 0) = delete;
423 template<typename _Tp
, typename _Dp
>
425 swap(unique_ptr
<_Tp
, _Dp
>& __x
,
426 unique_ptr
<_Tp
, _Dp
>& __y
)
429 template<typename _Tp
, typename _Dp
,
430 typename _Up
, typename _Ep
>
432 operator==(const unique_ptr
<_Tp
, _Dp
>& __x
,
433 const unique_ptr
<_Up
, _Ep
>& __y
)
434 { return __x
.get() == __y
.get(); }
436 template<typename _Tp
, typename _Dp
>
438 operator==(const unique_ptr
<_Tp
, _Dp
>& __x
, nullptr_t
)
439 { return __x
.get() == nullptr; }
441 template<typename _Tp
, typename _Dp
>
443 operator==(nullptr_t
, const unique_ptr
<_Tp
, _Dp
>& __y
)
444 { return nullptr == __y
.get(); }
446 template<typename _Tp
, typename _Dp
,
447 typename _Up
, typename _Ep
>
449 operator!=(const unique_ptr
<_Tp
, _Dp
>& __x
,
450 const unique_ptr
<_Up
, _Ep
>& __y
)
451 { return !(__x
.get() == __y
.get()); }
453 template<typename _Tp
, typename _Dp
>
455 operator!=(const unique_ptr
<_Tp
, _Dp
>& __x
, nullptr_t
)
456 { return __x
.get() != nullptr; }
458 template<typename _Tp
, typename _Dp
>
460 operator!=(nullptr_t
, const unique_ptr
<_Tp
, _Dp
>& __y
)
461 { return nullptr != __y
.get(); }
463 template<typename _Tp
, typename _Dp
,
464 typename _Up
, typename _Ep
>
466 operator<(const unique_ptr
<_Tp
, _Dp
>& __x
,
467 const unique_ptr
<_Up
, _Ep
>& __y
)
468 { return __x
.get() < __y
.get(); }
470 template<typename _Tp
, typename _Dp
,
471 typename _Up
, typename _Ep
>
473 operator<=(const unique_ptr
<_Tp
, _Dp
>& __x
,
474 const unique_ptr
<_Up
, _Ep
>& __y
)
475 { return !(__y
.get() < __x
.get()); }
477 template<typename _Tp
, typename _Dp
,
478 typename _Up
, typename _Ep
>
480 operator>(const unique_ptr
<_Tp
, _Dp
>& __x
,
481 const unique_ptr
<_Up
, _Ep
>& __y
)
482 { return __y
.get() < __x
.get(); }
484 template<typename _Tp
, typename _Dp
,
485 typename _Up
, typename _Ep
>
487 operator>=(const unique_ptr
<_Tp
, _Dp
>& __x
,
488 const unique_ptr
<_Up
, _Ep
>& __y
)
489 { return !(__x
.get() < __y
.get()); }
491 /// std::hash specialization for unique_ptr.
492 template<typename _Tp
, typename _Dp
>
493 struct hash
<unique_ptr
<_Tp
, _Dp
>>
494 : public std::unary_function
<unique_ptr
<_Tp
, _Dp
>, size_t>
497 operator()(const unique_ptr
<_Tp
, _Dp
>& __u
) const
499 typedef unique_ptr
<_Tp
, _Dp
> _UP
;
500 return std::hash
<typename
_UP::pointer
>()(__u
.get());
504 // @} group pointer_abstractions
506 _GLIBCXX_END_NAMESPACE_VERSION
509 #endif /* _UNIQUE_PTR_H */