1 // unique_ptr implementation -*- C++ -*-
3 // Copyright (C) 2008-2012 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 #if _GLIBCXX_USE_DEPRECATED
49 template<typename> class auto_ptr;
52 /// Primary template, default_delete.
53 template<typename _Tp>
56 constexpr default_delete() noexcept = default;
58 template<typename _Up, typename = typename
59 std::enable_if<std::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");
71 // _GLIBCXX_RESOLVE_LIB_DEFECTS
72 // DR 740 - omit specialization for array objects with a compile time length
73 /// Specialization, default_delete.
74 template<typename _Tp>
75 struct default_delete<_Tp[]>
77 constexpr default_delete() noexcept = default;
80 operator()(_Tp* __ptr) const
82 static_assert(sizeof(_Tp)>0,
83 "can't delete pointer to incomplete type");
87 template<typename _Up> void operator()(_Up*) const = delete;
90 /// 20.7.12.2 unique_ptr for single objects.
91 template <typename _Tp, typename _Dp = default_delete<_Tp> >
94 // use SFINAE to determine whether _Del::pointer exists
97 template<typename _Up>
98 static typename _Up::pointer __test(typename _Up::pointer*);
100 template<typename _Up>
101 static _Tp* __test(...);
103 typedef typename remove_reference<_Dp>::type _Del;
106 typedef decltype( __test<_Del>(0)) type;
109 typedef std::tuple<typename _Pointer::type, _Dp> __tuple_type;
113 typedef typename _Pointer::type pointer;
114 typedef _Tp element_type;
115 typedef _Dp deleter_type;
118 constexpr unique_ptr() noexcept
120 { static_assert(!std::is_pointer<deleter_type>::value,
121 "constructed with null function pointer deleter"); }
124 unique_ptr(pointer __p) noexcept
125 : _M_t(__p, deleter_type())
126 { static_assert(!std::is_pointer<deleter_type>::value,
127 "constructed with null function pointer deleter"); }
129 unique_ptr(pointer __p,
130 typename std::conditional<std::is_reference<deleter_type>::value,
131 deleter_type, const deleter_type&>::type __d) noexcept
134 unique_ptr(pointer __p,
135 typename std::remove_reference<deleter_type>::type&& __d) noexcept
136 : _M_t(std::move(__p), std::move(__d))
137 { static_assert(!std::is_reference<deleter_type>::value,
138 "rvalue deleter bound to reference"); }
140 constexpr unique_ptr(nullptr_t) noexcept
142 { static_assert(!std::is_pointer<deleter_type>::value,
143 "constructed with null function pointer deleter"); }
145 // Move constructors.
146 unique_ptr(unique_ptr&& __u) noexcept
147 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
149 template<typename _Up, typename _Ep, typename = typename
151 <std::is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
153 && !std::is_array<_Up>::value
154 && ((std::is_reference<_Dp>::value
155 && std::is_same<_Ep, _Dp>::value)
156 || (!std::is_reference<_Dp>::value
157 && std::is_convertible<_Ep, _Dp>::value))>
159 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
160 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
163 #if _GLIBCXX_USE_DEPRECATED
164 template<typename _Up, typename = typename
165 std::enable_if<std::is_convertible<_Up*, _Tp*>::value
167 default_delete<_Tp>>::value>::type>
168 unique_ptr(auto_ptr<_Up>&& __u) noexcept;
172 ~unique_ptr() noexcept { reset(); }
176 operator=(unique_ptr&& __u) noexcept
178 reset(__u.release());
179 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
183 template<typename _Up, typename _Ep, typename = typename
185 <std::is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
187 && !std::is_array<_Up>::value>::type>
189 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
191 reset(__u.release());
192 get_deleter() = std::forward<_Ep>(__u.get_deleter());
197 operator=(nullptr_t) noexcept
204 typename std::add_lvalue_reference<element_type>::type
207 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
212 operator->() const noexcept
214 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
220 { return std::get<0>(_M_t); }
223 get_deleter() noexcept
224 { return std::get<1>(_M_t); }
227 get_deleter() const noexcept
228 { return std::get<1>(_M_t); }
230 explicit operator bool() const noexcept
231 { return get() == pointer() ? false : true; }
238 std::get<0>(_M_t) = pointer();
243 reset(pointer __p = pointer()) noexcept
246 swap(std::get<0>(_M_t), __p);
247 if (__p != pointer())
252 swap(unique_ptr& __u) noexcept
255 swap(_M_t, __u._M_t);
258 // Disable copy from lvalue.
259 unique_ptr(const unique_ptr&) = delete;
260 unique_ptr& operator=(const unique_ptr&) = delete;
263 /// 20.7.12.3 unique_ptr for array objects with a runtime length
264 // [unique.ptr.runtime]
265 // _GLIBCXX_RESOLVE_LIB_DEFECTS
266 // DR 740 - omit specialization for array objects with a compile time length
267 template<typename _Tp, typename _Dp>
268 class unique_ptr<_Tp[], _Dp>
270 typedef std::tuple<_Tp*, _Dp> __tuple_type;
274 typedef _Tp* pointer;
275 typedef _Tp element_type;
276 typedef _Dp deleter_type;
279 constexpr unique_ptr() noexcept
281 { static_assert(!std::is_pointer<deleter_type>::value,
282 "constructed with null function pointer deleter"); }
285 unique_ptr(pointer __p) noexcept
286 : _M_t(__p, deleter_type())
287 { static_assert(!std::is_pointer<deleter_type>::value,
288 "constructed with null function pointer deleter"); }
290 unique_ptr(pointer __p,
291 typename std::conditional<std::is_reference<deleter_type>::value,
292 deleter_type, const deleter_type&>::type __d) noexcept
295 unique_ptr(pointer __p, typename
296 std::remove_reference<deleter_type>::type && __d) noexcept
297 : _M_t(std::move(__p), std::move(__d))
298 { static_assert(!std::is_reference<deleter_type>::value,
299 "rvalue deleter bound to reference"); }
301 constexpr unique_ptr(nullptr_t) noexcept
303 { static_assert(!std::is_pointer<deleter_type>::value,
304 "constructed with null function pointer deleter"); }
306 // Move constructors.
307 unique_ptr(unique_ptr&& __u) noexcept
308 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
310 template<typename _Up, typename _Ep>
311 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
312 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
316 ~unique_ptr() { reset(); }
320 operator=(unique_ptr&& __u) noexcept
322 reset(__u.release());
323 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
327 template<typename _Up, typename _Ep>
329 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
331 reset(__u.release());
332 get_deleter() = std::forward<_Ep>(__u.get_deleter());
337 operator=(nullptr_t) noexcept
344 typename std::add_lvalue_reference<element_type>::type
345 operator[](size_t __i) const
347 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
353 { return std::get<0>(_M_t); }
356 get_deleter() noexcept
357 { return std::get<1>(_M_t); }
360 get_deleter() const noexcept
361 { return std::get<1>(_M_t); }
363 explicit operator bool() const noexcept
364 { return get() == pointer() ? false : true; }
371 std::get<0>(_M_t) = pointer();
376 reset(pointer __p = pointer()) noexcept
379 swap(std::get<0>(_M_t), __p);
385 reset(nullptr_t) noexcept
388 std::get<0>(_M_t) = pointer();
394 template<typename _Up>
395 void reset(_Up) = delete;
398 swap(unique_ptr& __u) noexcept
401 swap(_M_t, __u._M_t);
404 // Disable copy from lvalue.
405 unique_ptr(const unique_ptr&) = delete;
406 unique_ptr& operator=(const unique_ptr&) = delete;
408 // Disable construction from convertible pointer types.
409 // (N2315 - 20.6.5.3.1)
410 template<typename _Up>
411 unique_ptr(_Up*, typename
412 std::conditional<std::is_reference<deleter_type>::value,
413 deleter_type, const deleter_type&>::type,
414 typename std::enable_if<std::is_convertible<_Up*,
415 pointer>::value>::type* = 0) = delete;
417 template<typename _Up>
418 unique_ptr(_Up*, typename std::remove_reference<deleter_type>::type&&,
419 typename std::enable_if<std::is_convertible<_Up*,
420 pointer>::value>::type* = 0) = delete;
422 template<typename _Up>
424 unique_ptr(_Up*, typename std::enable_if<std::is_convertible<_Up*,
425 pointer>::value>::type* = 0) = delete;
428 template<typename _Tp, typename _Dp>
430 swap(unique_ptr<_Tp, _Dp>& __x,
431 unique_ptr<_Tp, _Dp>& __y) noexcept
434 template<typename _Tp, typename _Dp,
435 typename _Up, typename _Ep>
437 operator==(const unique_ptr<_Tp, _Dp>& __x,
438 const unique_ptr<_Up, _Ep>& __y)
439 { return __x.get() == __y.get(); }
441 template<typename _Tp, typename _Dp>
443 operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
446 template<typename _Tp, typename _Dp>
448 operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
451 template<typename _Tp, typename _Dp,
452 typename _Up, typename _Ep>
454 operator!=(const unique_ptr<_Tp, _Dp>& __x,
455 const unique_ptr<_Up, _Ep>& __y)
456 { return __x.get() != __y.get(); }
458 template<typename _Tp, typename _Dp>
460 operator!=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
461 { return (bool)__x; }
463 template<typename _Tp, typename _Dp>
465 operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
466 { return (bool)__x; }
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)
475 std::common_type<typename unique_ptr<_Tp, _Dp>::pointer,
476 typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
477 return std::less<_CT>()(__x.get(), __y.get());
480 template<typename _Tp, typename _Dp>
482 operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
483 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(),
486 template<typename _Tp, typename _Dp>
488 operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
489 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr,
492 template<typename _Tp, typename _Dp,
493 typename _Up, typename _Ep>
495 operator<=(const unique_ptr<_Tp, _Dp>& __x,
496 const unique_ptr<_Up, _Ep>& __y)
497 { return !(__y < __x); }
499 template<typename _Tp, typename _Dp>
501 operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
502 { return !(nullptr < __x); }
504 template<typename _Tp, typename _Dp>
506 operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
507 { return !(__x < nullptr); }
509 template<typename _Tp, typename _Dp,
510 typename _Up, typename _Ep>
512 operator>(const unique_ptr<_Tp, _Dp>& __x,
513 const unique_ptr<_Up, _Ep>& __y)
514 { return (__y < __x); }
516 template<typename _Tp, typename _Dp>
518 operator>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
519 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr,
522 template<typename _Tp, typename _Dp>
524 operator>(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
525 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(),
528 template<typename _Tp, typename _Dp,
529 typename _Up, typename _Ep>
531 operator>=(const unique_ptr<_Tp, _Dp>& __x,
532 const unique_ptr<_Up, _Ep>& __y)
533 { return !(__x < __y); }
535 template<typename _Tp, typename _Dp>
537 operator>=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
538 { return !(__x < nullptr); }
540 template<typename _Tp, typename _Dp>
542 operator>=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
543 { return !(nullptr < __x); }
545 /// std::hash specialization for unique_ptr.
546 template<typename _Tp, typename _Dp>
547 struct hash<unique_ptr<_Tp, _Dp>>
548 : public __hash_base<size_t, unique_ptr<_Tp, _Dp>>
551 operator()(const unique_ptr<_Tp, _Dp>& __u) const noexcept
553 typedef unique_ptr<_Tp, _Dp> _UP;
554 return std::hash<typename _UP::pointer>()(__u.get());
558 // @} group pointer_abstractions
560 _GLIBCXX_END_NAMESPACE_VERSION
563 #endif /* _UNIQUE_PTR_H */