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;
88 typedef _Tp element_type;
89 typedef _Tp_Deleter deleter_type;
93 : _M_t(pointer(), deleter_type())
94 { static_assert(!std::is_pointer<deleter_type>::value,
95 "constructed with null function pointer deleter"); }
98 unique_ptr(pointer __p)
99 : _M_t(__p, deleter_type())
100 { static_assert(!std::is_pointer<deleter_type>::value,
101 "constructed with null function pointer deleter"); }
103 unique_ptr(pointer __p,
104 typename std::conditional<std::is_reference<deleter_type>::value,
105 deleter_type, const deleter_type&>::type __d)
108 unique_ptr(pointer __p,
109 typename std::remove_reference<deleter_type>::type&& __d)
110 : _M_t(std::move(__p), std::move(__d))
111 { static_assert(!std::is_reference<deleter_type>::value,
112 "rvalue deleter bound to reference"); }
114 // Move constructors.
115 unique_ptr(unique_ptr&& __u)
116 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
118 template<typename _Up, typename _Up_Deleter>
119 unique_ptr(unique_ptr<_Up, _Up_Deleter>&& __u)
120 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter()))
124 ~unique_ptr() { reset(); }
128 operator=(unique_ptr&& __u)
130 reset(__u.release());
131 get_deleter() = std::move(__u.get_deleter());
135 template<typename _Up, typename _Up_Deleter>
137 operator=(unique_ptr<_Up, _Up_Deleter>&& __u)
139 reset(__u.release());
140 get_deleter() = std::move(__u.get_deleter());
145 operator=(__unspecified_pointer_type)
152 typename std::add_lvalue_reference<element_type>::type
155 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
162 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
168 { return std::get<0>(_M_t); }
172 { return std::get<1>(_M_t); }
176 { return std::get<1>(_M_t); }
178 explicit operator bool() const
179 { return get() == pointer() ? false : true; }
186 std::get<0>(_M_t) = pointer();
191 reset(pointer __p = pointer())
194 swap(std::get<0>(_M_t), __p);
195 if (__p != pointer())
200 swap(unique_ptr& __u)
203 swap(_M_t, __u._M_t);
206 // Disable copy from lvalue.
207 unique_ptr(const unique_ptr&) = delete;
208 unique_ptr& operator=(const unique_ptr&) = delete;
214 /// 20.7.12.3 unique_ptr for array objects with a runtime length
215 // [unique.ptr.runtime]
216 // _GLIBCXX_RESOLVE_LIB_DEFECTS
217 // DR 740 - omit specialization for array objects with a compile time length
218 template<typename _Tp, typename _Tp_Deleter>
219 class unique_ptr<_Tp[], _Tp_Deleter>
221 typedef std::tuple<_Tp*, _Tp_Deleter> __tuple_type;
222 typedef _Tp* unique_ptr::* __unspecified_pointer_type;
225 typedef _Tp* pointer;
226 typedef _Tp element_type;
227 typedef _Tp_Deleter deleter_type;
231 : _M_t(pointer(), deleter_type())
232 { static_assert(!std::is_pointer<deleter_type>::value,
233 "constructed with null function pointer deleter"); }
236 unique_ptr(pointer __p)
237 : _M_t(__p, deleter_type())
238 { static_assert(!std::is_pointer<deleter_type>::value,
239 "constructed with null function pointer deleter"); }
241 unique_ptr(pointer __p,
242 typename std::conditional<std::is_reference<deleter_type>::value,
243 deleter_type, const deleter_type&>::type __d)
246 unique_ptr(pointer __p,
247 typename std::remove_reference<deleter_type>::type && __d)
248 : _M_t(std::move(__p), std::move(__d))
249 { static_assert(!std::is_reference<deleter_type>::value,
250 "rvalue deleter bound to reference"); }
252 // Move constructors.
253 unique_ptr(unique_ptr&& __u)
254 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
256 template<typename _Up, typename _Up_Deleter>
257 unique_ptr(unique_ptr<_Up, _Up_Deleter>&& __u)
258 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter()))
262 ~unique_ptr() { reset(); }
266 operator=(unique_ptr&& __u)
268 reset(__u.release());
269 get_deleter() = std::move(__u.get_deleter());
273 template<typename _Up, typename _Up_Deleter>
275 operator=(unique_ptr<_Up, _Up_Deleter>&& __u)
277 reset(__u.release());
278 get_deleter() = std::move(__u.get_deleter());
283 operator=(__unspecified_pointer_type)
290 typename std::add_lvalue_reference<element_type>::type
291 operator[](size_t __i) const
293 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
299 { return std::get<0>(_M_t); }
303 { return std::get<1>(_M_t); }
307 { return std::get<1>(_M_t); }
309 explicit operator bool() const
310 { return get() == pointer() ? false : true; }
317 std::get<0>(_M_t) = pointer();
322 reset(pointer __p = pointer())
325 swap(std::get<0>(_M_t), __p);
326 if (__p != pointer())
331 template<typename _Up>
332 void reset(_Up) = delete;
335 swap(unique_ptr& __u)
338 swap(_M_t, __u._M_t);
341 // Disable copy from lvalue.
342 unique_ptr(const unique_ptr&) = delete;
343 unique_ptr& operator=(const unique_ptr&) = delete;
345 // Disable construction from convertible pointer types.
346 // (N2315 - 20.6.5.3.1)
347 template<typename _Up>
348 unique_ptr(_Up*, typename
349 std::conditional<std::is_reference<deleter_type>::value,
350 deleter_type, const deleter_type&>::type,
351 typename std::enable_if<std::is_convertible<_Up*,
352 pointer>::value>::type* = 0) = delete;
354 template<typename _Up>
355 unique_ptr(_Up*, typename std::remove_reference<deleter_type>::type&&,
356 typename std::enable_if<std::is_convertible<_Up*,
357 pointer>::value>::type* = 0) = delete;
359 template<typename _Up>
361 unique_ptr(_Up*, typename std::enable_if<std::is_convertible<_Up*,
362 pointer>::value>::type* = 0) = delete;
368 template<typename _Tp, typename _Tp_Deleter>
370 swap(unique_ptr<_Tp, _Tp_Deleter>& __x,
371 unique_ptr<_Tp, _Tp_Deleter>& __y)
374 template<typename _Tp, typename _Tp_Deleter,
375 typename _Up, typename _Up_Deleter>
377 operator==(const unique_ptr<_Tp, _Tp_Deleter>& __x,
378 const unique_ptr<_Up, _Up_Deleter>& __y)
379 { return __x.get() == __y.get(); }
381 template<typename _Tp, typename _Tp_Deleter,
382 typename _Up, typename _Up_Deleter>
384 operator!=(const unique_ptr<_Tp, _Tp_Deleter>& __x,
385 const unique_ptr<_Up, _Up_Deleter>& __y)
386 { return !(__x.get() == __y.get()); }
388 template<typename _Tp, typename _Tp_Deleter,
389 typename _Up, typename _Up_Deleter>
391 operator<(const unique_ptr<_Tp, _Tp_Deleter>& __x,
392 const unique_ptr<_Up, _Up_Deleter>& __y)
393 { return __x.get() < __y.get(); }
395 template<typename _Tp, typename _Tp_Deleter,
396 typename _Up, typename _Up_Deleter>
398 operator<=(const unique_ptr<_Tp, _Tp_Deleter>& __x,
399 const unique_ptr<_Up, _Up_Deleter>& __y)
400 { return !(__y.get() < __x.get()); }
402 template<typename _Tp, typename _Tp_Deleter,
403 typename _Up, typename _Up_Deleter>
405 operator>(const unique_ptr<_Tp, _Tp_Deleter>& __x,
406 const unique_ptr<_Up, _Up_Deleter>& __y)
407 { return __y.get() < __x.get(); }
409 template<typename _Tp, typename _Tp_Deleter,
410 typename _Up, typename _Up_Deleter>
412 operator>=(const unique_ptr<_Tp, _Tp_Deleter>& __x,
413 const unique_ptr<_Up, _Up_Deleter>& __y)
414 { return !(__x.get() < __y.get()); }
416 // @} group pointer_abstractions
418 _GLIBCXX_END_NAMESPACE
420 #endif /* _UNIQUE_PTR_H */