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<typename _Pointer::type, _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) noexcept
441 template<typename _Tp, typename _Dp>
443 operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
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) noexcept
456 { return (bool)__x; }
458 template<typename _Tp, typename _Dp>
460 operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
461 { return (bool)__x; }
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)
470 std::common_type<typename unique_ptr<_Tp, _Dp>::pointer,
471 typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
472 return std::less<_CT>()(__x.get(), __y.get());
475 template<typename _Tp, typename _Dp>
477 operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
478 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(),
481 template<typename _Tp, typename _Dp>
483 operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
484 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr,
487 template<typename _Tp, typename _Dp,
488 typename _Up, typename _Ep>
490 operator<=(const unique_ptr<_Tp, _Dp>& __x,
491 const unique_ptr<_Up, _Ep>& __y)
492 { return !(__y < __x); }
494 template<typename _Tp, typename _Dp>
496 operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
497 { return !(nullptr < __x); }
499 template<typename _Tp, typename _Dp>
501 operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
502 { return !(__x < nullptr); }
504 template<typename _Tp, typename _Dp,
505 typename _Up, typename _Ep>
507 operator>(const unique_ptr<_Tp, _Dp>& __x,
508 const unique_ptr<_Up, _Ep>& __y)
509 { return (__y < __x); }
511 template<typename _Tp, typename _Dp>
513 operator>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
514 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr,
517 template<typename _Tp, typename _Dp>
519 operator>(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
520 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(),
523 template<typename _Tp, typename _Dp,
524 typename _Up, typename _Ep>
526 operator>=(const unique_ptr<_Tp, _Dp>& __x,
527 const unique_ptr<_Up, _Ep>& __y)
528 { return !(__x < __y); }
530 template<typename _Tp, typename _Dp>
532 operator>=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
533 { return !(__x < nullptr); }
535 template<typename _Tp, typename _Dp>
537 operator>=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
538 { return !(nullptr < __x); }
540 /// std::hash specialization for unique_ptr.
541 template<typename _Tp, typename _Dp>
542 struct hash<unique_ptr<_Tp, _Dp>>
543 : public std::unary_function<unique_ptr<_Tp, _Dp>, size_t>
546 operator()(const unique_ptr<_Tp, _Dp>& __u) const
548 typedef unique_ptr<_Tp, _Dp> _UP;
549 return std::hash<typename _UP::pointer>()(__u.get());
553 // @} group pointer_abstractions
555 _GLIBCXX_END_NAMESPACE_VERSION
558 #endif /* _UNIQUE_PTR_H */