1 // unique_ptr implementation -*- C++ -*-
3 // Copyright (C) 2008, 2009 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 #ifndef __GXX_EXPERIMENTAL_CXX0X__
34 # include <c++0x_warning.h>
37 #include <bits/c++config.h>
38 #include <debug/debug.h>
39 #include <type_traits>
43 _GLIBCXX_BEGIN_NAMESPACE(std
)
46 * @addtogroup pointer_abstractions
50 /// Primary template, default_delete.
51 template<typename _Tp
>
56 template<typename _Up
>
57 default_delete(const default_delete
<_Up
>&) { }
60 operator()(_Tp
* __ptr
) const
62 static_assert(sizeof(_Tp
)>0,
63 "can't delete pointer to incomplete type");
68 // _GLIBCXX_RESOLVE_LIB_DEFECTS
69 // DR 740 - omit specialization for array objects with a compile time length
70 /// Specialization, default_delete.
71 template<typename _Tp
>
72 struct default_delete
<_Tp
[]>
75 operator()(_Tp
* __ptr
) const
77 static_assert(sizeof(_Tp
)>0,
78 "can't delete pointer to incomplete type");
83 /// 20.7.12.2 unique_ptr for single objects.
84 template <typename _Tp
, typename _Tp_Deleter
= default_delete
<_Tp
> >
87 typedef std::tuple
<_Tp
*, _Tp_Deleter
> __tuple_type
;
88 typedef __tuple_type
unique_ptr::* __unspecified_bool_type
;
89 typedef _Tp
* unique_ptr::* __unspecified_pointer_type
;
93 typedef _Tp element_type
;
94 typedef _Tp_Deleter deleter_type
;
98 : _M_t(pointer(), deleter_type())
99 { static_assert(!std::is_pointer
<deleter_type
>::value
,
100 "constructed with null function pointer deleter"); }
103 unique_ptr(pointer __p
)
104 : _M_t(__p
, deleter_type())
105 { static_assert(!std::is_pointer
<deleter_type
>::value
,
106 "constructed with null function pointer deleter"); }
108 unique_ptr(pointer __p
,
109 typename
std::conditional
<std::is_reference
<deleter_type
>::value
,
110 deleter_type
, const deleter_type
&>::type __d
)
113 unique_ptr(pointer __p
,
114 typename
std::remove_reference
<deleter_type
>::type
&& __d
)
115 : _M_t(std::move(__p
), std::move(__d
))
116 { static_assert(!std::is_reference
<deleter_type
>::value
,
117 "rvalue deleter bound to reference"); }
119 // Move constructors.
120 unique_ptr(unique_ptr
&& __u
)
121 : _M_t(__u
.release(), std::forward
<deleter_type
>(__u
.get_deleter())) { }
123 template<typename _Up
, typename _Up_Deleter
>
124 unique_ptr(unique_ptr
<_Up
, _Up_Deleter
>&& __u
)
125 : _M_t(__u
.release(), std::forward
<deleter_type
>(__u
.get_deleter()))
129 ~unique_ptr() { reset(); }
133 operator=(unique_ptr
&& __u
)
135 reset(__u
.release());
136 get_deleter() = std::move(__u
.get_deleter());
140 template<typename _Up
, typename _Up_Deleter
>
142 operator=(unique_ptr
<_Up
, _Up_Deleter
>&& __u
)
144 reset(__u
.release());
145 get_deleter() = std::move(__u
.get_deleter());
150 operator=(__unspecified_pointer_type
)
157 typename
std::add_lvalue_reference
<element_type
>::type
operator*() const
159 _GLIBCXX_DEBUG_ASSERT(get() != 0);
166 _GLIBCXX_DEBUG_ASSERT(get() != 0);
172 { return std::get
<0>(_M_t
); }
174 typename
std::add_lvalue_reference
<deleter_type
>::type
176 { return std::get
<1>(_M_t
); }
178 typename
std::add_lvalue_reference
<
179 typename
std::add_const
<deleter_type
>::type
182 { return std::get
<1>(_M_t
); }
184 operator __unspecified_bool_type () const
185 { return get() == 0 ? 0 : &unique_ptr::_M_t
; }
192 std::get
<0>(_M_t
) = 0;
197 reset(pointer __p
= pointer())
201 get_deleter()(get());
202 std::get
<0>(_M_t
) = __p
;
207 swap(unique_ptr
&& __u
)
210 swap(_M_t
, __u
._M_t
);
213 // Disable copy from lvalue.
214 unique_ptr(const unique_ptr
&) = delete;
216 template<typename _Up
, typename _Up_Deleter
>
217 unique_ptr(const unique_ptr
<_Up
, _Up_Deleter
>&) = delete;
219 unique_ptr
& operator=(const unique_ptr
&) = delete;
221 template<typename _Up
, typename _Up_Deleter
>
222 unique_ptr
& operator=(const unique_ptr
<_Up
, _Up_Deleter
>&) = delete;
228 /// 20.7.12.3 unique_ptr for array objects with a runtime length
229 // [unique.ptr.runtime]
230 // _GLIBCXX_RESOLVE_LIB_DEFECTS
231 // DR 740 - omit specialization for array objects with a compile time length
232 template<typename _Tp
, typename _Tp_Deleter
>
233 class unique_ptr
<_Tp
[], _Tp_Deleter
>
235 typedef std::tuple
<_Tp
*, _Tp_Deleter
> __tuple_type
;
236 typedef __tuple_type
unique_ptr::* __unspecified_bool_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() != 0);
314 { return std::get
<0>(_M_t
); }
316 typename
std::add_lvalue_reference
<deleter_type
>::type
318 { return std::get
<1>(_M_t
); }
320 typename
std::add_lvalue_reference
<
321 typename
std::add_const
<deleter_type
>::type
324 { return std::get
<1>(_M_t
); }
326 operator __unspecified_bool_type () const
327 { return get() == 0 ? 0 : &unique_ptr::_M_t
; }
334 std::get
<0>(_M_t
) = 0;
339 reset(pointer __p
= pointer())
343 get_deleter()(get());
344 std::get
<0>(_M_t
) = __p
;
349 template<typename _Up
>
350 void reset(_Up
) = delete;
353 swap(unique_ptr
&& __u
)
356 swap(_M_t
, __u
._M_t
);
359 // Disable copy from lvalue.
360 unique_ptr(const unique_ptr
&) = delete;
361 unique_ptr
& operator=(const unique_ptr
&) = delete;
363 // Disable construction from convertible pointer types.
364 // (N2315 - 20.6.5.3.1)
365 template<typename _Up
>
366 unique_ptr(_Up
*, typename
367 std::conditional
<std::is_reference
<deleter_type
>::value
,
368 deleter_type
, const deleter_type
&>::type
,
369 typename
std::enable_if
<std::is_convertible
<_Up
*,
370 pointer
>::value
>::type
* = 0) = delete;
372 template<typename _Up
>
373 unique_ptr(_Up
*, typename
std::remove_reference
<deleter_type
>::type
&&,
374 typename
std::enable_if
<std::is_convertible
<_Up
*,
375 pointer
>::value
>::type
* = 0) = delete;
377 template<typename _Up
>
379 unique_ptr(_Up
*, typename
std::enable_if
<std::is_convertible
<_Up
*,
380 pointer
>::value
>::type
* = 0) = delete;
386 template<typename _Tp
, typename _Tp_Deleter
>
388 swap(unique_ptr
<_Tp
, _Tp_Deleter
>& __x
,
389 unique_ptr
<_Tp
, _Tp_Deleter
>& __y
)
392 template<typename _Tp
, typename _Tp_Deleter
>
394 swap(unique_ptr
<_Tp
, _Tp_Deleter
>&& __x
,
395 unique_ptr
<_Tp
, _Tp_Deleter
>& __y
)
398 template<typename _Tp
, typename _Tp_Deleter
>
400 swap(unique_ptr
<_Tp
, _Tp_Deleter
>& __x
,
401 unique_ptr
<_Tp
, _Tp_Deleter
>&& __y
)
404 template<typename _Tp
, typename _Tp_Deleter
,
405 typename _Up
, typename _Up_Deleter
>
407 operator==(const unique_ptr
<_Tp
, _Tp_Deleter
>& __x
,
408 const unique_ptr
<_Up
, _Up_Deleter
>& __y
)
409 { return __x
.get() == __y
.get(); }
411 template<typename _Tp
, typename _Tp_Deleter
,
412 typename _Up
, typename _Up_Deleter
>
414 operator!=(const unique_ptr
<_Tp
, _Tp_Deleter
>& __x
,
415 const unique_ptr
<_Up
, _Up_Deleter
>& __y
)
416 { return !(__x
.get() == __y
.get()); }
418 template<typename _Tp
, typename _Tp_Deleter
,
419 typename _Up
, typename _Up_Deleter
>
421 operator<(const unique_ptr
<_Tp
, _Tp_Deleter
>& __x
,
422 const unique_ptr
<_Up
, _Up_Deleter
>& __y
)
423 { return __x
.get() < __y
.get(); }
425 template<typename _Tp
, typename _Tp_Deleter
,
426 typename _Up
, typename _Up_Deleter
>
428 operator<=(const unique_ptr
<_Tp
, _Tp_Deleter
>& __x
,
429 const unique_ptr
<_Up
, _Up_Deleter
>& __y
)
430 { return !(__y
.get() < __x
.get()); }
432 template<typename _Tp
, typename _Tp_Deleter
,
433 typename _Up
, typename _Up_Deleter
>
435 operator>(const unique_ptr
<_Tp
, _Tp_Deleter
>& __x
,
436 const unique_ptr
<_Up
, _Up_Deleter
>& __y
)
437 { return __y
.get() < __x
.get(); }
439 template<typename _Tp
, typename _Tp_Deleter
,
440 typename _Up
, typename _Up_Deleter
>
442 operator>=(const unique_ptr
<_Tp
, _Tp_Deleter
>& __x
,
443 const unique_ptr
<_Up
, _Up_Deleter
>& __y
)
444 { return !(__x
.get() < __y
.get()); }
446 // @} group pointer_abstractions
448 _GLIBCXX_END_NAMESPACE
450 #endif /* _UNIQUE_PTR_H */