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");
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()
116 { static_assert(!std::is_pointer<deleter_type>::value,
117 "constructed with null function pointer deleter"); }
120 unique_ptr(pointer __p)
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)
130 unique_ptr(pointer __p,
131 typename std::remove_reference<deleter_type>::type&& __d)
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)
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)
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)
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)
165 : _M_t(__u.release(), deleter_type()) { }
169 ~unique_ptr() { reset(); }
173 operator=(unique_ptr&& __u)
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)
188 reset(__u.release());
189 get_deleter() = std::forward<_Ep>(__u.get_deleter());
201 typename std::add_lvalue_reference<element_type>::type
204 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
211 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
217 { return std::get<0>(_M_t); }
221 { return std::get<1>(_M_t); }
225 { return std::get<1>(_M_t); }
227 explicit operator bool() const
228 { return get() == pointer() ? false : true; }
235 std::get<0>(_M_t) = pointer();
240 reset(pointer __p = pointer())
243 swap(std::get<0>(_M_t), __p);
244 if (__p != pointer())
249 swap(unique_ptr& __u)
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()
278 { static_assert(!std::is_pointer<deleter_type>::value,
279 "constructed with null function pointer deleter"); }
282 unique_ptr(pointer __p)
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)
292 unique_ptr(pointer __p,
293 typename std::remove_reference<deleter_type>::type && __d)
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)
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)
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)
309 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
313 ~unique_ptr() { reset(); }
317 operator=(unique_ptr&& __u)
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)
328 reset(__u.release());
329 get_deleter() = std::forward<_Ep>(__u.get_deleter());
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); }
354 { return std::get<1>(_M_t); }
358 { return std::get<1>(_M_t); }
360 explicit operator bool() const
361 { return get() == pointer() ? false : true; }
368 std::get<0>(_M_t) = pointer();
373 reset(pointer __p = pointer())
376 swap(std::get<0>(_M_t), __p);
385 std::get<0>(_M_t) = pointer();
391 template<typename _Up>
392 void reset(_Up) = delete;
395 swap(unique_ptr& __u)
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)
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)
441 { return __x.get() == nullptr; }
443 template<typename _Tp, typename _Dp>
445 operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __y)
446 { return nullptr == __y.get(); }
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)
458 { return __x.get() != nullptr; }
460 template<typename _Tp, typename _Dp>
462 operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __y)
463 { return nullptr != __y.get(); }
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)
470 { return __x.get() < __y.get(); }
472 template<typename _Tp, typename _Dp,
473 typename _Up, typename _Ep>
475 operator<=(const unique_ptr<_Tp, _Dp>& __x,
476 const unique_ptr<_Up, _Ep>& __y)
477 { return !(__y.get() < __x.get()); }
479 template<typename _Tp, typename _Dp,
480 typename _Up, typename _Ep>
482 operator>(const unique_ptr<_Tp, _Dp>& __x,
483 const unique_ptr<_Up, _Ep>& __y)
484 { return __y.get() < __x.get(); }
486 template<typename _Tp, typename _Dp,
487 typename _Up, typename _Ep>
489 operator>=(const unique_ptr<_Tp, _Dp>& __x,
490 const unique_ptr<_Up, _Ep>& __y)
491 { return !(__x.get() < __y.get()); }
493 /// std::hash specialization for unique_ptr.
494 template<typename _Tp, typename _Dp>
495 struct hash<unique_ptr<_Tp, _Dp>>
496 : public std::unary_function<unique_ptr<_Tp, _Dp>, size_t>
499 operator()(const unique_ptr<_Tp, _Dp>& __u) const
501 typedef unique_ptr<_Tp, _Dp> _UP;
502 return std::hash<typename _UP::pointer>()(__u.get());
506 // @} group pointer_abstractions
508 _GLIBCXX_END_NAMESPACE_VERSION
511 #endif /* _UNIQUE_PTR_H */