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
); }
170 typename
std::add_lvalue_reference
<deleter_type
>::type
172 { return std::get
<1>(_M_t
); }
174 typename
std::add_lvalue_reference
<
175 typename
std::add_const
<deleter_type
>::type
178 { return std::get
<1>(_M_t
); }
180 explicit operator bool() const
181 { return get() == pointer() ? false : true; }
188 std::get
<0>(_M_t
) = pointer();
193 reset(pointer __p
= pointer())
196 swap(std::get
<0>(_M_t
), __p
);
197 if (__p
!= pointer())
202 swap(unique_ptr
& __u
)
205 swap(_M_t
, __u
._M_t
);
208 // Disable copy from lvalue.
209 unique_ptr(const unique_ptr
&) = delete;
210 unique_ptr
& operator=(const unique_ptr
&) = delete;
216 /// 20.7.12.3 unique_ptr for array objects with a runtime length
217 // [unique.ptr.runtime]
218 // _GLIBCXX_RESOLVE_LIB_DEFECTS
219 // DR 740 - omit specialization for array objects with a compile time length
220 template<typename _Tp
, typename _Tp_Deleter
>
221 class unique_ptr
<_Tp
[], _Tp_Deleter
>
223 typedef std::tuple
<_Tp
*, _Tp_Deleter
> __tuple_type
;
224 typedef _Tp
* unique_ptr::* __unspecified_pointer_type
;
227 typedef _Tp
* pointer
;
228 typedef _Tp element_type
;
229 typedef _Tp_Deleter deleter_type
;
233 : _M_t(pointer(), deleter_type())
234 { static_assert(!std::is_pointer
<deleter_type
>::value
,
235 "constructed with null function pointer deleter"); }
238 unique_ptr(pointer __p
)
239 : _M_t(__p
, deleter_type())
240 { static_assert(!std::is_pointer
<deleter_type
>::value
,
241 "constructed with null function pointer deleter"); }
243 unique_ptr(pointer __p
,
244 typename
std::conditional
<std::is_reference
<deleter_type
>::value
,
245 deleter_type
, const deleter_type
&>::type __d
)
248 unique_ptr(pointer __p
,
249 typename
std::remove_reference
<deleter_type
>::type
&& __d
)
250 : _M_t(std::move(__p
), std::move(__d
))
251 { static_assert(!std::is_reference
<deleter_type
>::value
,
252 "rvalue deleter bound to reference"); }
254 // Move constructors.
255 unique_ptr(unique_ptr
&& __u
)
256 : _M_t(__u
.release(), std::forward
<deleter_type
>(__u
.get_deleter())) { }
258 template<typename _Up
, typename _Up_Deleter
>
259 unique_ptr(unique_ptr
<_Up
, _Up_Deleter
>&& __u
)
260 : _M_t(__u
.release(), std::forward
<deleter_type
>(__u
.get_deleter()))
264 ~unique_ptr() { reset(); }
268 operator=(unique_ptr
&& __u
)
270 reset(__u
.release());
271 get_deleter() = std::move(__u
.get_deleter());
275 template<typename _Up
, typename _Up_Deleter
>
277 operator=(unique_ptr
<_Up
, _Up_Deleter
>&& __u
)
279 reset(__u
.release());
280 get_deleter() = std::move(__u
.get_deleter());
285 operator=(__unspecified_pointer_type
)
292 typename
std::add_lvalue_reference
<element_type
>::type
293 operator[](size_t __i
) const
295 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
301 { return std::get
<0>(_M_t
); }
303 typename
std::add_lvalue_reference
<deleter_type
>::type
305 { return std::get
<1>(_M_t
); }
307 typename
std::add_lvalue_reference
<
308 typename
std::add_const
<deleter_type
>::type
311 { return std::get
<1>(_M_t
); }
313 explicit operator bool() const
314 { return get() == pointer() ? false : true; }
321 std::get
<0>(_M_t
) = pointer();
326 reset(pointer __p
= pointer())
329 swap(std::get
<0>(_M_t
), __p
);
330 if (__p
!= pointer())
335 template<typename _Up
>
336 void reset(_Up
) = delete;
339 swap(unique_ptr
& __u
)
342 swap(_M_t
, __u
._M_t
);
345 // Disable copy from lvalue.
346 unique_ptr(const unique_ptr
&) = delete;
347 unique_ptr
& operator=(const unique_ptr
&) = delete;
349 // Disable construction from convertible pointer types.
350 // (N2315 - 20.6.5.3.1)
351 template<typename _Up
>
352 unique_ptr(_Up
*, typename
353 std::conditional
<std::is_reference
<deleter_type
>::value
,
354 deleter_type
, const deleter_type
&>::type
,
355 typename
std::enable_if
<std::is_convertible
<_Up
*,
356 pointer
>::value
>::type
* = 0) = delete;
358 template<typename _Up
>
359 unique_ptr(_Up
*, typename
std::remove_reference
<deleter_type
>::type
&&,
360 typename
std::enable_if
<std::is_convertible
<_Up
*,
361 pointer
>::value
>::type
* = 0) = delete;
363 template<typename _Up
>
365 unique_ptr(_Up
*, typename
std::enable_if
<std::is_convertible
<_Up
*,
366 pointer
>::value
>::type
* = 0) = delete;
372 template<typename _Tp
, typename _Tp_Deleter
>
374 swap(unique_ptr
<_Tp
, _Tp_Deleter
>& __x
,
375 unique_ptr
<_Tp
, _Tp_Deleter
>& __y
)
378 template<typename _Tp
, typename _Tp_Deleter
,
379 typename _Up
, typename _Up_Deleter
>
381 operator==(const unique_ptr
<_Tp
, _Tp_Deleter
>& __x
,
382 const unique_ptr
<_Up
, _Up_Deleter
>& __y
)
383 { return __x
.get() == __y
.get(); }
385 template<typename _Tp
, typename _Tp_Deleter
,
386 typename _Up
, typename _Up_Deleter
>
388 operator!=(const unique_ptr
<_Tp
, _Tp_Deleter
>& __x
,
389 const unique_ptr
<_Up
, _Up_Deleter
>& __y
)
390 { return !(__x
.get() == __y
.get()); }
392 template<typename _Tp
, typename _Tp_Deleter
,
393 typename _Up
, typename _Up_Deleter
>
395 operator<(const unique_ptr
<_Tp
, _Tp_Deleter
>& __x
,
396 const unique_ptr
<_Up
, _Up_Deleter
>& __y
)
397 { return __x
.get() < __y
.get(); }
399 template<typename _Tp
, typename _Tp_Deleter
,
400 typename _Up
, typename _Up_Deleter
>
402 operator<=(const unique_ptr
<_Tp
, _Tp_Deleter
>& __x
,
403 const unique_ptr
<_Up
, _Up_Deleter
>& __y
)
404 { return !(__y
.get() < __x
.get()); }
406 template<typename _Tp
, typename _Tp_Deleter
,
407 typename _Up
, typename _Up_Deleter
>
409 operator>(const unique_ptr
<_Tp
, _Tp_Deleter
>& __x
,
410 const unique_ptr
<_Up
, _Up_Deleter
>& __y
)
411 { return __y
.get() < __x
.get(); }
413 template<typename _Tp
, typename _Tp_Deleter
,
414 typename _Up
, typename _Up_Deleter
>
416 operator>=(const unique_ptr
<_Tp
, _Tp_Deleter
>& __x
,
417 const unique_ptr
<_Up
, _Up_Deleter
>& __y
)
418 { return !(__x
.get() < __y
.get()); }
420 // @} group pointer_abstractions
422 _GLIBCXX_END_NAMESPACE
424 #endif /* _UNIQUE_PTR_H */