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() noexcept = 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>&) noexcept { }
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() noexcept = default;
76 operator()(_Tp* __ptr) const
78 static_assert(sizeof(_Tp)>0,
79 "can't delete pointer to incomplete type");
83 template<typename _Up> void operator()(_Up*) const = delete;
86 /// 20.7.12.2 unique_ptr for single objects.
87 template <typename _Tp, typename _Dp = default_delete<_Tp> >
90 // use SFINAE to determine whether _Del::pointer exists
93 template<typename _Up>
94 static typename _Up::pointer __test(typename _Up::pointer*);
96 template<typename _Up>
97 static _Tp* __test(...);
99 typedef typename remove_reference<_Dp>::type _Del;
102 typedef decltype( __test<_Del>(0)) type;
105 typedef std::tuple<typename _Pointer::type, _Dp> __tuple_type;
109 typedef typename _Pointer::type pointer;
110 typedef _Tp element_type;
111 typedef _Dp deleter_type;
114 constexpr unique_ptr() noexcept
116 { static_assert(!std::is_pointer<deleter_type>::value,
117 "constructed with null function pointer deleter"); }
120 unique_ptr(pointer __p) noexcept
121 : _M_t(__p, deleter_type())
122 { static_assert(!std::is_pointer<deleter_type>::value,
123 "constructed with null function pointer deleter"); }
125 unique_ptr(pointer __p,
126 typename std::conditional<std::is_reference<deleter_type>::value,
127 deleter_type, const deleter_type&>::type __d) noexcept
130 unique_ptr(pointer __p,
131 typename std::remove_reference<deleter_type>::type&& __d) noexcept
132 : _M_t(std::move(__p), std::move(__d))
133 { static_assert(!std::is_reference<deleter_type>::value,
134 "rvalue deleter bound to reference"); }
136 constexpr unique_ptr(nullptr_t) noexcept
138 { static_assert(!std::is_pointer<deleter_type>::value,
139 "constructed with null function pointer deleter"); }
141 // Move constructors.
142 unique_ptr(unique_ptr&& __u) noexcept
143 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
145 template<typename _Up, typename _Ep, typename = typename
147 <std::is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
149 && !std::is_array<_Up>::value
150 && ((std::is_reference<_Dp>::value
151 && std::is_same<_Ep, _Dp>::value)
152 || (!std::is_reference<_Dp>::value
153 && std::is_convertible<_Ep, _Dp>::value))>
155 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
156 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
159 #if _GLIBCXX_USE_DEPRECATED
160 template<typename _Up, typename = typename
161 std::enable_if<std::is_convertible<_Up*, _Tp*>::value
163 default_delete<_Tp>>::value>::type>
164 unique_ptr(auto_ptr<_Up>&& __u) noexcept
165 : _M_t(__u.release(), deleter_type()) { }
169 ~unique_ptr() noexcept { reset(); }
173 operator=(unique_ptr&& __u) noexcept
175 reset(__u.release());
176 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
180 template<typename _Up, typename _Ep, typename = typename
182 <std::is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
184 && !std::is_array<_Up>::value>::type>
186 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
188 reset(__u.release());
189 get_deleter() = std::forward<_Ep>(__u.get_deleter());
194 operator=(nullptr_t) noexcept
201 typename std::add_lvalue_reference<element_type>::type
204 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
209 operator->() const noexcept
211 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
217 { return std::get<0>(_M_t); }
220 get_deleter() noexcept
221 { return std::get<1>(_M_t); }
224 get_deleter() const noexcept
225 { return std::get<1>(_M_t); }
227 explicit operator bool() const noexcept
228 { return get() == pointer() ? false : true; }
235 std::get<0>(_M_t) = pointer();
240 reset(pointer __p = pointer()) noexcept
243 swap(std::get<0>(_M_t), __p);
244 if (__p != pointer())
249 swap(unique_ptr& __u) noexcept
252 swap(_M_t, __u._M_t);
255 // Disable copy from lvalue.
256 unique_ptr(const unique_ptr&) = delete;
257 unique_ptr& operator=(const unique_ptr&) = delete;
260 /// 20.7.12.3 unique_ptr for array objects with a runtime length
261 // [unique.ptr.runtime]
262 // _GLIBCXX_RESOLVE_LIB_DEFECTS
263 // DR 740 - omit specialization for array objects with a compile time length
264 template<typename _Tp, typename _Dp>
265 class unique_ptr<_Tp[], _Dp>
267 typedef std::tuple<_Tp*, _Dp> __tuple_type;
271 typedef _Tp* pointer;
272 typedef _Tp element_type;
273 typedef _Dp deleter_type;
276 constexpr unique_ptr() noexcept
278 { static_assert(!std::is_pointer<deleter_type>::value,
279 "constructed with null function pointer deleter"); }
282 unique_ptr(pointer __p) noexcept
283 : _M_t(__p, deleter_type())
284 { static_assert(!std::is_pointer<deleter_type>::value,
285 "constructed with null function pointer deleter"); }
287 unique_ptr(pointer __p,
288 typename std::conditional<std::is_reference<deleter_type>::value,
289 deleter_type, const deleter_type&>::type __d) noexcept
292 unique_ptr(pointer __p, typename
293 std::remove_reference<deleter_type>::type && __d) noexcept
294 : _M_t(std::move(__p), std::move(__d))
295 { static_assert(!std::is_reference<deleter_type>::value,
296 "rvalue deleter bound to reference"); }
298 constexpr unique_ptr(nullptr_t) noexcept
300 { static_assert(!std::is_pointer<deleter_type>::value,
301 "constructed with null function pointer deleter"); }
303 // Move constructors.
304 unique_ptr(unique_ptr&& __u) noexcept
305 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
307 template<typename _Up, typename _Ep>
308 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
309 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
313 ~unique_ptr() { reset(); }
317 operator=(unique_ptr&& __u) noexcept
319 reset(__u.release());
320 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
324 template<typename _Up, typename _Ep>
326 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
328 reset(__u.release());
329 get_deleter() = std::forward<_Ep>(__u.get_deleter());
334 operator=(nullptr_t) noexcept
341 typename std::add_lvalue_reference<element_type>::type
342 operator[](size_t __i) const
344 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
350 { return std::get<0>(_M_t); }
353 get_deleter() noexcept
354 { return std::get<1>(_M_t); }
357 get_deleter() const noexcept
358 { return std::get<1>(_M_t); }
360 explicit operator bool() const noexcept
361 { return get() == pointer() ? false : true; }
368 std::get<0>(_M_t) = pointer();
373 reset(pointer __p = pointer()) noexcept
376 swap(std::get<0>(_M_t), __p);
382 reset(nullptr_t) noexcept
385 std::get<0>(_M_t) = pointer();
391 template<typename _Up>
392 void reset(_Up) = delete;
395 swap(unique_ptr& __u) noexcept
398 swap(_M_t, __u._M_t);
401 // Disable copy from lvalue.
402 unique_ptr(const unique_ptr&) = delete;
403 unique_ptr& operator=(const unique_ptr&) = delete;
405 // Disable construction from convertible pointer types.
406 // (N2315 - 20.6.5.3.1)
407 template<typename _Up>
408 unique_ptr(_Up*, typename
409 std::conditional<std::is_reference<deleter_type>::value,
410 deleter_type, const deleter_type&>::type,
411 typename std::enable_if<std::is_convertible<_Up*,
412 pointer>::value>::type* = 0) = delete;
414 template<typename _Up>
415 unique_ptr(_Up*, typename std::remove_reference<deleter_type>::type&&,
416 typename std::enable_if<std::is_convertible<_Up*,
417 pointer>::value>::type* = 0) = delete;
419 template<typename _Up>
421 unique_ptr(_Up*, typename std::enable_if<std::is_convertible<_Up*,
422 pointer>::value>::type* = 0) = delete;
425 template<typename _Tp, typename _Dp>
427 swap(unique_ptr<_Tp, _Dp>& __x,
428 unique_ptr<_Tp, _Dp>& __y) noexcept
431 template<typename _Tp, typename _Dp,
432 typename _Up, typename _Ep>
434 operator==(const unique_ptr<_Tp, _Dp>& __x,
435 const unique_ptr<_Up, _Ep>& __y)
436 { return __x.get() == __y.get(); }
438 template<typename _Tp, typename _Dp>
440 operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
443 template<typename _Tp, typename _Dp>
445 operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
448 template<typename _Tp, typename _Dp,
449 typename _Up, typename _Ep>
451 operator!=(const unique_ptr<_Tp, _Dp>& __x,
452 const unique_ptr<_Up, _Ep>& __y)
453 { return __x.get() != __y.get(); }
455 template<typename _Tp, typename _Dp>
457 operator!=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
458 { return (bool)__x; }
460 template<typename _Tp, typename _Dp>
462 operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
463 { return (bool)__x; }
465 template<typename _Tp, typename _Dp,
466 typename _Up, typename _Ep>
468 operator<(const unique_ptr<_Tp, _Dp>& __x,
469 const unique_ptr<_Up, _Ep>& __y)
472 std::common_type<typename unique_ptr<_Tp, _Dp>::pointer,
473 typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
474 return std::less<_CT>()(__x.get(), __y.get());
477 template<typename _Tp, typename _Dp>
479 operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
480 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(),
483 template<typename _Tp, typename _Dp>
485 operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
486 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr,
489 template<typename _Tp, typename _Dp,
490 typename _Up, typename _Ep>
492 operator<=(const unique_ptr<_Tp, _Dp>& __x,
493 const unique_ptr<_Up, _Ep>& __y)
494 { return !(__y < __x); }
496 template<typename _Tp, typename _Dp>
498 operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
499 { return !(nullptr < __x); }
501 template<typename _Tp, typename _Dp>
503 operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
504 { return !(__x < nullptr); }
506 template<typename _Tp, typename _Dp,
507 typename _Up, typename _Ep>
509 operator>(const unique_ptr<_Tp, _Dp>& __x,
510 const unique_ptr<_Up, _Ep>& __y)
511 { return (__y < __x); }
513 template<typename _Tp, typename _Dp>
515 operator>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
516 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr,
519 template<typename _Tp, typename _Dp>
521 operator>(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
522 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(),
525 template<typename _Tp, typename _Dp,
526 typename _Up, typename _Ep>
528 operator>=(const unique_ptr<_Tp, _Dp>& __x,
529 const unique_ptr<_Up, _Ep>& __y)
530 { return !(__x < __y); }
532 template<typename _Tp, typename _Dp>
534 operator>=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
535 { return !(__x < nullptr); }
537 template<typename _Tp, typename _Dp>
539 operator>=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
540 { return !(nullptr < __x); }
542 /// std::hash specialization for unique_ptr.
543 template<typename _Tp, typename _Dp>
544 struct hash<unique_ptr<_Tp, _Dp>>
545 : public __hash_base<size_t, unique_ptr<_Tp, _Dp>>
548 operator()(const unique_ptr<_Tp, _Dp>& __u) const noexcept
550 typedef unique_ptr<_Tp, _Dp> _UP;
551 return std::hash<typename _UP::pointer>()(__u.get());
555 // @} group pointer_abstractions
557 _GLIBCXX_END_NAMESPACE_VERSION
560 #endif /* _UNIQUE_PTR_H */