1 // unique_ptr implementation -*- C++ -*-
3 // Copyright (C) 2008, 2009, 2010 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 unique_ptr.h
26 * This is an internal header file, included by other library headers.
27 * You should not attempt to use it directly.
31 #define _UNIQUE_PTR_H 1
33 #include <bits/c++config.h>
34 #include <debug/debug.h>
35 #include <type_traits>
39 _GLIBCXX_BEGIN_NAMESPACE(std)
42 * @addtogroup pointer_abstractions
46 /// Primary template, default_delete.
47 template<typename _Tp>
52 template<typename _Up>
53 default_delete(const default_delete<_Up>&) { }
56 operator()(_Tp* __ptr) const
58 static_assert(sizeof(_Tp)>0,
59 "can't delete pointer to incomplete type");
64 // _GLIBCXX_RESOLVE_LIB_DEFECTS
65 // DR 740 - omit specialization for array objects with a compile time length
66 /// Specialization, default_delete.
67 template<typename _Tp>
68 struct default_delete<_Tp[]>
71 operator()(_Tp* __ptr) const
73 static_assert(sizeof(_Tp)>0,
74 "can't delete pointer to incomplete type");
79 /// 20.7.12.2 unique_ptr for single objects.
80 template <typename _Tp, typename _Tp_Deleter = default_delete<_Tp> >
83 typedef std::tuple<_Tp*, _Tp_Deleter> __tuple_type;
84 typedef _Tp* unique_ptr::* __unspecified_pointer_type;
86 // use SFINAE to determine whether _Del::pointer exists
89 template<typename _Up>
90 static typename _Up::pointer __test(typename _Up::pointer*);
92 template<typename _Up>
93 static _Tp* __test(...);
95 typedef typename remove_reference<_Tp_Deleter>::type _Del;
98 typedef decltype( __test<_Del>(0) ) type;
102 typedef typename _Pointer::type pointer;
103 typedef _Tp element_type;
104 typedef _Tp_Deleter deleter_type;
108 : _M_t(pointer(), deleter_type())
109 { static_assert(!std::is_pointer<deleter_type>::value,
110 "constructed with null function pointer deleter"); }
113 unique_ptr(pointer __p)
114 : _M_t(__p, deleter_type())
115 { static_assert(!std::is_pointer<deleter_type>::value,
116 "constructed with null function pointer deleter"); }
118 unique_ptr(pointer __p,
119 typename std::conditional<std::is_reference<deleter_type>::value,
120 deleter_type, const deleter_type&>::type __d)
123 unique_ptr(pointer __p,
124 typename std::remove_reference<deleter_type>::type&& __d)
125 : _M_t(std::move(__p), std::move(__d))
126 { static_assert(!std::is_reference<deleter_type>::value,
127 "rvalue deleter bound to reference"); }
129 // Move constructors.
130 unique_ptr(unique_ptr&& __u)
131 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
133 template<typename _Up, typename _Up_Deleter>
134 unique_ptr(unique_ptr<_Up, _Up_Deleter>&& __u)
135 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter()))
139 ~unique_ptr() { reset(); }
143 operator=(unique_ptr&& __u)
145 reset(__u.release());
146 get_deleter() = std::move(__u.get_deleter());
150 template<typename _Up, typename _Up_Deleter>
152 operator=(unique_ptr<_Up, _Up_Deleter>&& __u)
154 reset(__u.release());
155 get_deleter() = std::move(__u.get_deleter());
160 operator=(__unspecified_pointer_type)
167 typename std::add_lvalue_reference<element_type>::type
170 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
177 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
183 { return std::get<0>(_M_t); }
187 { return std::get<1>(_M_t); }
191 { return std::get<1>(_M_t); }
193 explicit operator bool() const
194 { return get() == pointer() ? false : true; }
201 std::get<0>(_M_t) = pointer();
206 reset(pointer __p = pointer())
209 swap(std::get<0>(_M_t), __p);
210 if (__p != pointer())
215 swap(unique_ptr& __u)
218 swap(_M_t, __u._M_t);
221 // Disable copy from lvalue.
222 unique_ptr(const unique_ptr&) = delete;
223 unique_ptr& operator=(const unique_ptr&) = delete;
229 /// 20.7.12.3 unique_ptr for array objects with a runtime length
230 // [unique.ptr.runtime]
231 // _GLIBCXX_RESOLVE_LIB_DEFECTS
232 // DR 740 - omit specialization for array objects with a compile time length
233 template<typename _Tp, typename _Tp_Deleter>
234 class unique_ptr<_Tp[], _Tp_Deleter>
236 typedef std::tuple<_Tp*, _Tp_Deleter> __tuple_type;
237 typedef _Tp* unique_ptr::* __unspecified_pointer_type;
240 typedef _Tp* pointer;
241 typedef _Tp element_type;
242 typedef _Tp_Deleter deleter_type;
246 : _M_t(pointer(), deleter_type())
247 { static_assert(!std::is_pointer<deleter_type>::value,
248 "constructed with null function pointer deleter"); }
251 unique_ptr(pointer __p)
252 : _M_t(__p, deleter_type())
253 { static_assert(!std::is_pointer<deleter_type>::value,
254 "constructed with null function pointer deleter"); }
256 unique_ptr(pointer __p,
257 typename std::conditional<std::is_reference<deleter_type>::value,
258 deleter_type, const deleter_type&>::type __d)
261 unique_ptr(pointer __p,
262 typename std::remove_reference<deleter_type>::type && __d)
263 : _M_t(std::move(__p), std::move(__d))
264 { static_assert(!std::is_reference<deleter_type>::value,
265 "rvalue deleter bound to reference"); }
267 // Move constructors.
268 unique_ptr(unique_ptr&& __u)
269 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
271 template<typename _Up, typename _Up_Deleter>
272 unique_ptr(unique_ptr<_Up, _Up_Deleter>&& __u)
273 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter()))
277 ~unique_ptr() { reset(); }
281 operator=(unique_ptr&& __u)
283 reset(__u.release());
284 get_deleter() = std::move(__u.get_deleter());
288 template<typename _Up, typename _Up_Deleter>
290 operator=(unique_ptr<_Up, _Up_Deleter>&& __u)
292 reset(__u.release());
293 get_deleter() = std::move(__u.get_deleter());
298 operator=(__unspecified_pointer_type)
305 typename std::add_lvalue_reference<element_type>::type
306 operator[](size_t __i) const
308 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
314 { return std::get<0>(_M_t); }
318 { return std::get<1>(_M_t); }
322 { return std::get<1>(_M_t); }
324 explicit operator bool() const
325 { return get() == pointer() ? false : true; }
332 std::get<0>(_M_t) = pointer();
337 reset(pointer __p = pointer())
340 swap(std::get<0>(_M_t), __p);
341 if (__p != pointer())
346 template<typename _Up>
347 void reset(_Up) = delete;
350 swap(unique_ptr& __u)
353 swap(_M_t, __u._M_t);
356 // Disable copy from lvalue.
357 unique_ptr(const unique_ptr&) = delete;
358 unique_ptr& operator=(const unique_ptr&) = delete;
360 // Disable construction from convertible pointer types.
361 // (N2315 - 20.6.5.3.1)
362 template<typename _Up>
363 unique_ptr(_Up*, typename
364 std::conditional<std::is_reference<deleter_type>::value,
365 deleter_type, const deleter_type&>::type,
366 typename std::enable_if<std::is_convertible<_Up*,
367 pointer>::value>::type* = 0) = delete;
369 template<typename _Up>
370 unique_ptr(_Up*, typename std::remove_reference<deleter_type>::type&&,
371 typename std::enable_if<std::is_convertible<_Up*,
372 pointer>::value>::type* = 0) = delete;
374 template<typename _Up>
376 unique_ptr(_Up*, typename std::enable_if<std::is_convertible<_Up*,
377 pointer>::value>::type* = 0) = delete;
383 template<typename _Tp, typename _Tp_Deleter>
385 swap(unique_ptr<_Tp, _Tp_Deleter>& __x,
386 unique_ptr<_Tp, _Tp_Deleter>& __y)
389 template<typename _Tp, typename _Tp_Deleter,
390 typename _Up, typename _Up_Deleter>
392 operator==(const unique_ptr<_Tp, _Tp_Deleter>& __x,
393 const unique_ptr<_Up, _Up_Deleter>& __y)
394 { return __x.get() == __y.get(); }
396 template<typename _Tp, typename _Tp_Deleter,
397 typename _Up, typename _Up_Deleter>
399 operator!=(const unique_ptr<_Tp, _Tp_Deleter>& __x,
400 const unique_ptr<_Up, _Up_Deleter>& __y)
401 { return !(__x.get() == __y.get()); }
403 template<typename _Tp, typename _Tp_Deleter,
404 typename _Up, typename _Up_Deleter>
406 operator<(const unique_ptr<_Tp, _Tp_Deleter>& __x,
407 const unique_ptr<_Up, _Up_Deleter>& __y)
408 { return __x.get() < __y.get(); }
410 template<typename _Tp, typename _Tp_Deleter,
411 typename _Up, typename _Up_Deleter>
413 operator<=(const unique_ptr<_Tp, _Tp_Deleter>& __x,
414 const unique_ptr<_Up, _Up_Deleter>& __y)
415 { return !(__y.get() < __x.get()); }
417 template<typename _Tp, typename _Tp_Deleter,
418 typename _Up, typename _Up_Deleter>
420 operator>(const unique_ptr<_Tp, _Tp_Deleter>& __x,
421 const unique_ptr<_Up, _Up_Deleter>& __y)
422 { return __y.get() < __x.get(); }
424 template<typename _Tp, typename _Tp_Deleter,
425 typename _Up, typename _Up_Deleter>
427 operator>=(const unique_ptr<_Tp, _Tp_Deleter>& __x,
428 const unique_ptr<_Up, _Up_Deleter>& __y)
429 { return !(__x.get() < __y.get()); }
431 // @} group pointer_abstractions
433 _GLIBCXX_END_NAMESPACE
435 #endif /* _UNIQUE_PTR_H */