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
;
109 static_assert(!std::is_pointer
<deleter_type
>::value
,
110 "constructed with null function pointer deleter");
113 constexpr unique_ptr()
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
)
135 : _M_t(pointer(), deleter_type())
138 // Move constructors.
139 unique_ptr(unique_ptr
&& __u
)
140 : _M_t(__u
.release(), std::forward
<deleter_type
>(__u
.get_deleter())) { }
142 template<typename _Up
, typename _Ep
, typename
= typename
144 <std::is_convertible
<typename unique_ptr
<_Up
, _Ep
>::pointer
,
146 && !std::is_array
<_Up
>::value
147 && ((std::is_reference
<_Dp
>::value
148 && std::is_same
<_Ep
, _Dp
>::value
)
149 || (!std::is_reference
<_Dp
>::value
150 && std::is_convertible
<_Ep
, _Dp
>::value
))>
152 unique_ptr(unique_ptr
<_Up
, _Ep
>&& __u
)
153 : _M_t(__u
.release(), std::forward
<deleter_type
>(__u
.get_deleter()))
156 #if _GLIBCXX_DEPRECATED
157 template<typename _Up
, typename
= typename
158 std::enable_if
<std::is_convertible
<_Up
*, _Tp
*>::value
160 default_delete
<_Tp
>>::value
>::type
>
161 unique_ptr(auto_ptr
<_Up
>&& __u
)
162 : _M_t(__u
.release(), deleter_type()) { }
166 ~unique_ptr() { reset(); }
170 operator=(unique_ptr
&& __u
)
172 reset(__u
.release());
173 get_deleter() = std::move(__u
.get_deleter());
177 template<typename _Up
, typename _Ep
, typename
= typename
179 <std::is_convertible
<typename unique_ptr
<_Up
, _Ep
>::pointer
,
181 && !std::is_array
<_Up
>::value
>::type
>
183 operator=(unique_ptr
<_Up
, _Ep
>&& __u
)
185 reset(__u
.release());
186 get_deleter() = std::move(__u
.get_deleter());
198 typename
std::add_lvalue_reference
<element_type
>::type
201 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
208 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
214 { return std::get
<0>(_M_t
); }
218 { return std::get
<1>(_M_t
); }
222 { return std::get
<1>(_M_t
); }
224 explicit operator bool() const
225 { return get() == pointer() ? false : true; }
232 std::get
<0>(_M_t
) = pointer();
237 reset(pointer __p
= pointer())
240 swap(std::get
<0>(_M_t
), __p
);
241 if (__p
!= pointer())
246 swap(unique_ptr
& __u
)
249 swap(_M_t
, __u
._M_t
);
252 // Disable copy from lvalue.
253 unique_ptr(const unique_ptr
&) = delete;
254 unique_ptr
& operator=(const unique_ptr
&) = delete;
257 /// 20.7.12.3 unique_ptr for array objects with a runtime length
258 // [unique.ptr.runtime]
259 // _GLIBCXX_RESOLVE_LIB_DEFECTS
260 // DR 740 - omit specialization for array objects with a compile time length
261 template<typename _Tp
, typename _Dp
>
262 class unique_ptr
<_Tp
[], _Dp
>
264 typedef std::tuple
<_Tp
*, _Dp
> __tuple_type
;
268 typedef _Tp
* pointer
;
269 typedef _Tp element_type
;
270 typedef _Dp deleter_type
;
272 static_assert(!std::is_pointer
<deleter_type
>::value
,
273 "constructed with null function pointer deleter");
276 constexpr unique_ptr()
277 : _M_t(pointer(), deleter_type())
281 unique_ptr(pointer __p
)
282 : _M_t(__p
, deleter_type())
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 /* TODO: use delegating constructor */
297 constexpr unique_ptr(nullptr_t
)
298 : _M_t(pointer(), deleter_type())
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
508 #endif /* _UNIQUE_PTR_H */