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() != 0);
162 _GLIBCXX_DEBUG_ASSERT(get() != 0);
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() == 0 ? false : true; }
188 std::get
<0>(_M_t
) = 0;
193 reset(pointer __p
= pointer())
197 get_deleter()(get());
198 std::get
<0>(_M_t
) = __p
;
203 swap(unique_ptr
& __u
)
206 swap(_M_t
, __u
._M_t
);
209 // Disable copy from lvalue.
210 unique_ptr(const unique_ptr
&) = delete;
211 unique_ptr
& operator=(const unique_ptr
&) = delete;
217 /// 20.7.12.3 unique_ptr for array objects with a runtime length
218 // [unique.ptr.runtime]
219 // _GLIBCXX_RESOLVE_LIB_DEFECTS
220 // DR 740 - omit specialization for array objects with a compile time length
221 template<typename _Tp
, typename _Tp_Deleter
>
222 class unique_ptr
<_Tp
[], _Tp_Deleter
>
224 typedef std::tuple
<_Tp
*, _Tp_Deleter
> __tuple_type
;
225 typedef _Tp
* unique_ptr::* __unspecified_pointer_type
;
228 typedef _Tp
* pointer
;
229 typedef _Tp element_type
;
230 typedef _Tp_Deleter deleter_type
;
234 : _M_t(pointer(), deleter_type())
235 { static_assert(!std::is_pointer
<deleter_type
>::value
,
236 "constructed with null function pointer deleter"); }
239 unique_ptr(pointer __p
)
240 : _M_t(__p
, deleter_type())
241 { static_assert(!std::is_pointer
<deleter_type
>::value
,
242 "constructed with null function pointer deleter"); }
244 unique_ptr(pointer __p
,
245 typename
std::conditional
<std::is_reference
<deleter_type
>::value
,
246 deleter_type
, const deleter_type
&>::type __d
)
249 unique_ptr(pointer __p
,
250 typename
std::remove_reference
<deleter_type
>::type
&& __d
)
251 : _M_t(std::move(__p
), std::move(__d
))
252 { static_assert(!std::is_reference
<deleter_type
>::value
,
253 "rvalue deleter bound to reference"); }
255 // Move constructors.
256 unique_ptr(unique_ptr
&& __u
)
257 : _M_t(__u
.release(), std::forward
<deleter_type
>(__u
.get_deleter())) { }
259 template<typename _Up
, typename _Up_Deleter
>
260 unique_ptr(unique_ptr
<_Up
, _Up_Deleter
>&& __u
)
261 : _M_t(__u
.release(), std::forward
<deleter_type
>(__u
.get_deleter()))
265 ~unique_ptr() { reset(); }
269 operator=(unique_ptr
&& __u
)
271 reset(__u
.release());
272 get_deleter() = std::move(__u
.get_deleter());
276 template<typename _Up
, typename _Up_Deleter
>
278 operator=(unique_ptr
<_Up
, _Up_Deleter
>&& __u
)
280 reset(__u
.release());
281 get_deleter() = std::move(__u
.get_deleter());
286 operator=(__unspecified_pointer_type
)
293 typename
std::add_lvalue_reference
<element_type
>::type
294 operator[](size_t __i
) const
296 _GLIBCXX_DEBUG_ASSERT(get() != 0);
302 { return std::get
<0>(_M_t
); }
304 typename
std::add_lvalue_reference
<deleter_type
>::type
306 { return std::get
<1>(_M_t
); }
308 typename
std::add_lvalue_reference
<
309 typename
std::add_const
<deleter_type
>::type
312 { return std::get
<1>(_M_t
); }
314 explicit operator bool() const
315 { return get() == 0 ? false : true; }
322 std::get
<0>(_M_t
) = 0;
327 reset(pointer __p
= pointer())
331 get_deleter()(get());
332 std::get
<0>(_M_t
) = __p
;
337 template<typename _Up
>
338 void reset(_Up
) = delete;
341 swap(unique_ptr
& __u
)
344 swap(_M_t
, __u
._M_t
);
347 // Disable copy from lvalue.
348 unique_ptr(const unique_ptr
&) = delete;
349 unique_ptr
& operator=(const unique_ptr
&) = delete;
351 // Disable construction from convertible pointer types.
352 // (N2315 - 20.6.5.3.1)
353 template<typename _Up
>
354 unique_ptr(_Up
*, typename
355 std::conditional
<std::is_reference
<deleter_type
>::value
,
356 deleter_type
, const deleter_type
&>::type
,
357 typename
std::enable_if
<std::is_convertible
<_Up
*,
358 pointer
>::value
>::type
* = 0) = delete;
360 template<typename _Up
>
361 unique_ptr(_Up
*, typename
std::remove_reference
<deleter_type
>::type
&&,
362 typename
std::enable_if
<std::is_convertible
<_Up
*,
363 pointer
>::value
>::type
* = 0) = delete;
365 template<typename _Up
>
367 unique_ptr(_Up
*, typename
std::enable_if
<std::is_convertible
<_Up
*,
368 pointer
>::value
>::type
* = 0) = delete;
374 template<typename _Tp
, typename _Tp_Deleter
>
376 swap(unique_ptr
<_Tp
, _Tp_Deleter
>& __x
,
377 unique_ptr
<_Tp
, _Tp_Deleter
>& __y
)
380 template<typename _Tp
, typename _Tp_Deleter
,
381 typename _Up
, typename _Up_Deleter
>
383 operator==(const unique_ptr
<_Tp
, _Tp_Deleter
>& __x
,
384 const unique_ptr
<_Up
, _Up_Deleter
>& __y
)
385 { return __x
.get() == __y
.get(); }
387 template<typename _Tp
, typename _Tp_Deleter
,
388 typename _Up
, typename _Up_Deleter
>
390 operator!=(const unique_ptr
<_Tp
, _Tp_Deleter
>& __x
,
391 const unique_ptr
<_Up
, _Up_Deleter
>& __y
)
392 { return !(__x
.get() == __y
.get()); }
394 template<typename _Tp
, typename _Tp_Deleter
,
395 typename _Up
, typename _Up_Deleter
>
397 operator<(const unique_ptr
<_Tp
, _Tp_Deleter
>& __x
,
398 const unique_ptr
<_Up
, _Up_Deleter
>& __y
)
399 { return __x
.get() < __y
.get(); }
401 template<typename _Tp
, typename _Tp_Deleter
,
402 typename _Up
, typename _Up_Deleter
>
404 operator<=(const unique_ptr
<_Tp
, _Tp_Deleter
>& __x
,
405 const unique_ptr
<_Up
, _Up_Deleter
>& __y
)
406 { return !(__y
.get() < __x
.get()); }
408 template<typename _Tp
, typename _Tp_Deleter
,
409 typename _Up
, typename _Up_Deleter
>
411 operator>(const unique_ptr
<_Tp
, _Tp_Deleter
>& __x
,
412 const unique_ptr
<_Up
, _Up_Deleter
>& __y
)
413 { return __y
.get() < __x
.get(); }
415 template<typename _Tp
, typename _Tp_Deleter
,
416 typename _Up
, typename _Up_Deleter
>
418 operator>=(const unique_ptr
<_Tp
, _Tp_Deleter
>& __x
,
419 const unique_ptr
<_Up
, _Up_Deleter
>& __y
)
420 { return !(__x
.get() < __y
.get()); }
422 // @} group pointer_abstractions
424 _GLIBCXX_END_NAMESPACE
426 #endif /* _UNIQUE_PTR_H */