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
171 auto& __ptr = std::get<0>(_M_t);
172 if (__ptr != nullptr)
173 get_deleter()(__ptr);
179 operator=(unique_ptr&& __u) noexcept
181 reset(__u.release());
182 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
186 template<typename _Up, typename _Ep, typename = typename
188 <std::is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
190 && !std::is_array<_Up>::value>::type>
192 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
194 reset(__u.release());
195 get_deleter() = std::forward<_Ep>(__u.get_deleter());
200 operator=(nullptr_t) noexcept
207 typename std::add_lvalue_reference<element_type>::type
210 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
215 operator->() const noexcept
217 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
223 { return std::get<0>(_M_t); }
226 get_deleter() noexcept
227 { return std::get<1>(_M_t); }
230 get_deleter() const noexcept
231 { return std::get<1>(_M_t); }
233 explicit operator bool() const noexcept
234 { return get() == pointer() ? false : true; }
241 std::get<0>(_M_t) = pointer();
246 reset(pointer __p = pointer()) noexcept
249 swap(std::get<0>(_M_t), __p);
250 if (__p != pointer())
255 swap(unique_ptr& __u) noexcept
258 swap(_M_t, __u._M_t);
261 // Disable copy from lvalue.
262 unique_ptr(const unique_ptr&) = delete;
263 unique_ptr& operator=(const unique_ptr&) = delete;
266 /// 20.7.12.3 unique_ptr for array objects with a runtime length
267 // [unique.ptr.runtime]
268 // _GLIBCXX_RESOLVE_LIB_DEFECTS
269 // DR 740 - omit specialization for array objects with a compile time length
270 template<typename _Tp, typename _Dp>
271 class unique_ptr<_Tp[], _Dp>
273 typedef std::tuple<_Tp*, _Dp> __tuple_type;
277 typedef _Tp* pointer;
278 typedef _Tp element_type;
279 typedef _Dp deleter_type;
282 constexpr unique_ptr() noexcept
284 { static_assert(!std::is_pointer<deleter_type>::value,
285 "constructed with null function pointer deleter"); }
288 unique_ptr(pointer __p) noexcept
289 : _M_t(__p, deleter_type())
290 { static_assert(!std::is_pointer<deleter_type>::value,
291 "constructed with null function pointer deleter"); }
293 unique_ptr(pointer __p,
294 typename std::conditional<std::is_reference<deleter_type>::value,
295 deleter_type, const deleter_type&>::type __d) noexcept
298 unique_ptr(pointer __p, typename
299 std::remove_reference<deleter_type>::type && __d) noexcept
300 : _M_t(std::move(__p), std::move(__d))
301 { static_assert(!std::is_reference<deleter_type>::value,
302 "rvalue deleter bound to reference"); }
304 constexpr unique_ptr(nullptr_t) noexcept
306 { static_assert(!std::is_pointer<deleter_type>::value,
307 "constructed with null function pointer deleter"); }
309 // Move constructors.
310 unique_ptr(unique_ptr&& __u) noexcept
311 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
313 template<typename _Up, typename _Ep>
314 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
315 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
321 auto& __ptr = std::get<0>(_M_t);
322 if (__ptr != nullptr)
323 get_deleter()(__ptr);
329 operator=(unique_ptr&& __u) noexcept
331 reset(__u.release());
332 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
336 template<typename _Up, typename _Ep>
338 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
340 reset(__u.release());
341 get_deleter() = std::forward<_Ep>(__u.get_deleter());
346 operator=(nullptr_t) noexcept
353 typename std::add_lvalue_reference<element_type>::type
354 operator[](size_t __i) const
356 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
362 { return std::get<0>(_M_t); }
365 get_deleter() noexcept
366 { return std::get<1>(_M_t); }
369 get_deleter() const noexcept
370 { return std::get<1>(_M_t); }
372 explicit operator bool() const noexcept
373 { return get() == pointer() ? false : true; }
380 std::get<0>(_M_t) = pointer();
385 reset(pointer __p = pointer()) noexcept
388 swap(std::get<0>(_M_t), __p);
394 reset(nullptr_t) noexcept
397 std::get<0>(_M_t) = pointer();
403 template<typename _Up>
404 void reset(_Up) = delete;
407 swap(unique_ptr& __u) noexcept
410 swap(_M_t, __u._M_t);
413 // Disable copy from lvalue.
414 unique_ptr(const unique_ptr&) = delete;
415 unique_ptr& operator=(const unique_ptr&) = delete;
417 // Disable construction from convertible pointer types.
418 // (N2315 - 20.6.5.3.1)
419 template<typename _Up>
420 unique_ptr(_Up*, typename
421 std::conditional<std::is_reference<deleter_type>::value,
422 deleter_type, const deleter_type&>::type,
423 typename std::enable_if<std::is_convertible<_Up*,
424 pointer>::value>::type* = 0) = delete;
426 template<typename _Up>
427 unique_ptr(_Up*, typename std::remove_reference<deleter_type>::type&&,
428 typename std::enable_if<std::is_convertible<_Up*,
429 pointer>::value>::type* = 0) = delete;
431 template<typename _Up>
433 unique_ptr(_Up*, typename std::enable_if<std::is_convertible<_Up*,
434 pointer>::value>::type* = 0) = delete;
437 template<typename _Tp, typename _Dp>
439 swap(unique_ptr<_Tp, _Dp>& __x,
440 unique_ptr<_Tp, _Dp>& __y) noexcept
443 template<typename _Tp, typename _Dp,
444 typename _Up, typename _Ep>
446 operator==(const unique_ptr<_Tp, _Dp>& __x,
447 const unique_ptr<_Up, _Ep>& __y)
448 { return __x.get() == __y.get(); }
450 template<typename _Tp, typename _Dp>
452 operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
455 template<typename _Tp, typename _Dp>
457 operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
460 template<typename _Tp, typename _Dp,
461 typename _Up, typename _Ep>
463 operator!=(const unique_ptr<_Tp, _Dp>& __x,
464 const unique_ptr<_Up, _Ep>& __y)
465 { return __x.get() != __y.get(); }
467 template<typename _Tp, typename _Dp>
469 operator!=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
470 { return (bool)__x; }
472 template<typename _Tp, typename _Dp>
474 operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
475 { return (bool)__x; }
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)
484 std::common_type<typename unique_ptr<_Tp, _Dp>::pointer,
485 typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
486 return std::less<_CT>()(__x.get(), __y.get());
489 template<typename _Tp, typename _Dp>
491 operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
492 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(),
495 template<typename _Tp, typename _Dp>
497 operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
498 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr,
501 template<typename _Tp, typename _Dp,
502 typename _Up, typename _Ep>
504 operator<=(const unique_ptr<_Tp, _Dp>& __x,
505 const unique_ptr<_Up, _Ep>& __y)
506 { return !(__y < __x); }
508 template<typename _Tp, typename _Dp>
510 operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
511 { return !(nullptr < __x); }
513 template<typename _Tp, typename _Dp>
515 operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
516 { return !(__x < nullptr); }
518 template<typename _Tp, typename _Dp,
519 typename _Up, typename _Ep>
521 operator>(const unique_ptr<_Tp, _Dp>& __x,
522 const unique_ptr<_Up, _Ep>& __y)
523 { return (__y < __x); }
525 template<typename _Tp, typename _Dp>
527 operator>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
528 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr,
531 template<typename _Tp, typename _Dp>
533 operator>(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
534 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(),
537 template<typename _Tp, typename _Dp,
538 typename _Up, typename _Ep>
540 operator>=(const unique_ptr<_Tp, _Dp>& __x,
541 const unique_ptr<_Up, _Ep>& __y)
542 { return !(__x < __y); }
544 template<typename _Tp, typename _Dp>
546 operator>=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
547 { return !(__x < nullptr); }
549 template<typename _Tp, typename _Dp>
551 operator>=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
552 { return !(nullptr < __x); }
554 /// std::hash specialization for unique_ptr.
555 template<typename _Tp, typename _Dp>
556 struct hash<unique_ptr<_Tp, _Dp>>
557 : public __hash_base<size_t, unique_ptr<_Tp, _Dp>>
560 operator()(const unique_ptr<_Tp, _Dp>& __u) const noexcept
562 typedef unique_ptr<_Tp, _Dp> _UP;
563 return std::hash<typename _UP::pointer>()(__u.get());
567 // @} group pointer_abstractions
569 _GLIBCXX_END_NAMESPACE_VERSION
572 #endif /* _UNIQUE_PTR_H */