]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/unique_ptr.h
*: Use headername alias to associate private includes to public includes.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / unique_ptr.h
CommitLineData
ca0f8fd1
PC
1// unique_ptr implementation -*- C++ -*-
2
ab65a4c7 3// Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
ca0f8fd1
PC
4//
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
748086b7 8// Free Software Foundation; either version 3, or (at your option)
ca0f8fd1
PC
9// any later version.
10
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.
15
748086b7
JJ
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.
19
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/>.
ca0f8fd1 24
f910786b 25/** @file bits/unique_ptr.h
ca0f8fd1 26 * This is an internal header file, included by other library headers.
f910786b 27 * Do not attempt to use it directly. @headername{memory}
ca0f8fd1
PC
28 */
29
30#ifndef _UNIQUE_PTR_H
31#define _UNIQUE_PTR_H 1
32
ca0f8fd1 33#include <bits/c++config.h>
20d85a99 34#include <debug/debug.h>
ca0f8fd1
PC
35#include <type_traits>
36#include <utility>
37#include <tuple>
38
39_GLIBCXX_BEGIN_NAMESPACE(std)
40
5b9daa7e
BK
41 /**
42 * @addtogroup pointer_abstractions
43 * @{
44 */
45
ad68e9fc 46 /// Primary template, default_delete.
94a86be0 47 template<typename _Tp>
ca0f8fd1 48 struct default_delete
be9d3d73
PC
49 {
50 constexpr default_delete() { }
ca0f8fd1 51
be9d3d73
PC
52 template<typename _Up, typename = typename
53 std::enable_if<std::is_convertible<_Up*, _Tp*>::value>::type>
54 default_delete(const default_delete<_Up>&) { }
ca0f8fd1 55
be9d3d73
PC
56 void
57 operator()(_Tp* __ptr) const
58 {
59 static_assert(sizeof(_Tp)>0,
60 "can't delete pointer to incomplete type");
61 delete __ptr;
62 }
ca0f8fd1
PC
63 };
64
65 // _GLIBCXX_RESOLVE_LIB_DEFECTS
66 // DR 740 - omit specialization for array objects with a compile time length
ad68e9fc 67 /// Specialization, default_delete.
94a86be0 68 template<typename _Tp>
ca0f8fd1
PC
69 struct default_delete<_Tp[]>
70 {
be9d3d73
PC
71 constexpr default_delete() { }
72
ca0f8fd1
PC
73 void
74 operator()(_Tp* __ptr) const
75 {
76 static_assert(sizeof(_Tp)>0,
77 "can't delete pointer to incomplete type");
78 delete [] __ptr;
79 }
80 };
81
37d5c6ba 82 /// 20.7.12.2 unique_ptr for single objects.
94a86be0 83 template <typename _Tp, typename _Dp = default_delete<_Tp> >
ca0f8fd1
PC
84 class unique_ptr
85 {
7b65808d
JW
86 // use SFINAE to determine whether _Del::pointer exists
87 class _Pointer
88 {
89 template<typename _Up>
90 static typename _Up::pointer __test(typename _Up::pointer*);
91
92 template<typename _Up>
93 static _Tp* __test(...);
94
8fe286ea 95 typedef typename remove_reference<_Dp>::type _Del;
7b65808d
JW
96
97 public:
94a86be0 98 typedef decltype( __test<_Del>(0)) type;
7b65808d
JW
99 };
100
94a86be0
BK
101 typedef std::tuple<_Tp*, _Dp> __tuple_type;
102 __tuple_type _M_t;
103
ca0f8fd1 104 public:
7b65808d
JW
105 typedef typename _Pointer::type pointer;
106 typedef _Tp element_type;
3abeaf8f 107 typedef _Dp deleter_type;
ca0f8fd1 108
37d5c6ba 109 // Constructors.
be9d3d73
PC
110 constexpr unique_ptr()
111 : _M_t()
ef40b371
JW
112 { static_assert(!std::is_pointer<deleter_type>::value,
113 "constructed with null function pointer deleter"); }
ca0f8fd1
PC
114
115 explicit
116 unique_ptr(pointer __p)
117 : _M_t(__p, deleter_type())
118 { static_assert(!std::is_pointer<deleter_type>::value,
119 "constructed with null function pointer deleter"); }
120
121 unique_ptr(pointer __p,
94a86be0
BK
122 typename std::conditional<std::is_reference<deleter_type>::value,
123 deleter_type, const deleter_type&>::type __d)
ca0f8fd1
PC
124 : _M_t(__p, __d) { }
125
126 unique_ptr(pointer __p,
94a86be0 127 typename std::remove_reference<deleter_type>::type&& __d)
ca0f8fd1 128 : _M_t(std::move(__p), std::move(__d))
94a86be0 129 { static_assert(!std::is_reference<deleter_type>::value,
ca0f8fd1
PC
130 "rvalue deleter bound to reference"); }
131
be9d3d73 132 constexpr unique_ptr(nullptr_t)
14b846de 133 : _M_t()
ef40b371
JW
134 { static_assert(!std::is_pointer<deleter_type>::value,
135 "constructed with null function pointer deleter"); }
6d00745b 136
37d5c6ba 137 // Move constructors.
94a86be0 138 unique_ptr(unique_ptr&& __u)
ca0f8fd1
PC
139 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
140
8fe286ea 141 template<typename _Up, typename _Ep, typename = typename
5aadb69b 142 std::enable_if
8fe286ea 143 <std::is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
5aadb69b
PC
144 pointer>::value
145 && !std::is_array<_Up>::value
8fe286ea
PC
146 && ((std::is_reference<_Dp>::value
147 && std::is_same<_Ep, _Dp>::value)
148 || (!std::is_reference<_Dp>::value
149 && std::is_convertible<_Ep, _Dp>::value))>
94a86be0
BK
150 ::type>
151 unique_ptr(unique_ptr<_Up, _Ep>&& __u)
152 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter()))
ca0f8fd1
PC
153 { }
154
5aadb69b
PC
155#if _GLIBCXX_DEPRECATED
156 template<typename _Up, typename = typename
157 std::enable_if<std::is_convertible<_Up*, _Tp*>::value
8fe286ea 158 && std::is_same<_Dp,
5aadb69b 159 default_delete<_Tp>>::value>::type>
94a86be0 160 unique_ptr(auto_ptr<_Up>&& __u)
5aadb69b
PC
161 : _M_t(__u.release(), deleter_type()) { }
162#endif
163
37d5c6ba 164 // Destructor.
ca0f8fd1 165 ~unique_ptr() { reset(); }
5aadb69b 166
37d5c6ba 167 // Assignment.
ca0f8fd1
PC
168 unique_ptr&
169 operator=(unique_ptr&& __u)
94a86be0
BK
170 {
171 reset(__u.release());
172 get_deleter() = std::move(__u.get_deleter());
173 return *this;
ca0f8fd1
PC
174 }
175
8fe286ea 176 template<typename _Up, typename _Ep, typename = typename
94a86be0 177 std::enable_if
8fe286ea 178 <std::is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
5aadb69b 179 pointer>::value
94a86be0
BK
180 && !std::is_array<_Up>::value>::type>
181 unique_ptr&
182 operator=(unique_ptr<_Up, _Ep>&& __u)
ca0f8fd1 183 {
94a86be0
BK
184 reset(__u.release());
185 get_deleter() = std::move(__u.get_deleter());
186 return *this;
187 }
ca0f8fd1
PC
188
189 unique_ptr&
6d00745b 190 operator=(nullptr_t)
ca0f8fd1
PC
191 {
192 reset();
193 return *this;
194 }
195
37d5c6ba 196 // Observers.
d779a591
PC
197 typename std::add_lvalue_reference<element_type>::type
198 operator*() const
ca0f8fd1 199 {
3e2e1976 200 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
ca0f8fd1
PC
201 return *get();
202 }
203
204 pointer
205 operator->() const
206 {
3e2e1976 207 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
ca0f8fd1
PC
208 return get();
209 }
210
211 pointer
212 get() const
213 { return std::get<0>(_M_t); }
214
2ba34efc 215 deleter_type&
ca0f8fd1
PC
216 get_deleter()
217 { return std::get<1>(_M_t); }
218
2ba34efc 219 const deleter_type&
ca0f8fd1
PC
220 get_deleter() const
221 { return std::get<1>(_M_t); }
222
d29d4507 223 explicit operator bool() const
3e2e1976 224 { return get() == pointer() ? false : true; }
ca0f8fd1 225
37d5c6ba 226 // Modifiers.
ca0f8fd1 227 pointer
94a86be0 228 release()
ca0f8fd1
PC
229 {
230 pointer __p = get();
3e2e1976 231 std::get<0>(_M_t) = pointer();
ca0f8fd1
PC
232 return __p;
233 }
234
235 void
0d5f7a16 236 reset(pointer __p = pointer())
ca0f8fd1 237 {
3e2e1976
JW
238 using std::swap;
239 swap(std::get<0>(_M_t), __p);
240 if (__p != pointer())
241 get_deleter()(__p);
ca0f8fd1
PC
242 }
243
244 void
ff74fd13 245 swap(unique_ptr& __u)
0d5f7a16
PC
246 {
247 using std::swap;
ca0f8fd1
PC
248 swap(_M_t, __u._M_t);
249 }
250
37d5c6ba 251 // Disable copy from lvalue.
0d5f7a16 252 unique_ptr(const unique_ptr&) = delete;
0d5f7a16 253 unique_ptr& operator=(const unique_ptr&) = delete;
ca0f8fd1 254 };
94a86be0 255
37d5c6ba 256 /// 20.7.12.3 unique_ptr for array objects with a runtime length
ca0f8fd1
PC
257 // [unique.ptr.runtime]
258 // _GLIBCXX_RESOLVE_LIB_DEFECTS
259 // DR 740 - omit specialization for array objects with a compile time length
8fe286ea
PC
260 template<typename _Tp, typename _Dp>
261 class unique_ptr<_Tp[], _Dp>
ca0f8fd1 262 {
94a86be0
BK
263 typedef std::tuple<_Tp*, _Dp> __tuple_type;
264 __tuple_type _M_t;
0d5f7a16 265
ca0f8fd1 266 public:
94a86be0
BK
267 typedef _Tp* pointer;
268 typedef _Tp element_type;
269 typedef _Dp deleter_type;
270
37d5c6ba 271 // Constructors.
be9d3d73 272 constexpr unique_ptr()
14b846de 273 : _M_t()
ef40b371
JW
274 { static_assert(!std::is_pointer<deleter_type>::value,
275 "constructed with null function pointer deleter"); }
ca0f8fd1
PC
276
277 explicit
278 unique_ptr(pointer __p)
279 : _M_t(__p, deleter_type())
14b846de
JW
280 { static_assert(!std::is_pointer<deleter_type>::value,
281 "constructed with null function pointer deleter"); }
ca0f8fd1
PC
282
283 unique_ptr(pointer __p,
94a86be0
BK
284 typename std::conditional<std::is_reference<deleter_type>::value,
285 deleter_type, const deleter_type&>::type __d)
ca0f8fd1
PC
286 : _M_t(__p, __d) { }
287
288 unique_ptr(pointer __p,
289 typename std::remove_reference<deleter_type>::type && __d)
290 : _M_t(std::move(__p), std::move(__d))
94a86be0 291 { static_assert(!std::is_reference<deleter_type>::value,
ca0f8fd1
PC
292 "rvalue deleter bound to reference"); }
293
be9d3d73 294 constexpr unique_ptr(nullptr_t)
14b846de 295 : _M_t()
ef40b371
JW
296 { static_assert(!std::is_pointer<deleter_type>::value,
297 "constructed with null function pointer deleter"); }
6d00745b 298
37d5c6ba 299 // Move constructors.
94a86be0 300 unique_ptr(unique_ptr&& __u)
ca0f8fd1
PC
301 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
302
94a86be0
BK
303 template<typename _Up, typename _Ep>
304 unique_ptr(unique_ptr<_Up, _Ep>&& __u)
ca0f8fd1
PC
305 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter()))
306 { }
307
37d5c6ba 308 // Destructor.
ca0f8fd1
PC
309 ~unique_ptr() { reset(); }
310
37d5c6ba 311 // Assignment.
ca0f8fd1
PC
312 unique_ptr&
313 operator=(unique_ptr&& __u)
314 {
315 reset(__u.release());
94a86be0
BK
316 get_deleter() = std::move(__u.get_deleter());
317 return *this;
ca0f8fd1
PC
318 }
319
94a86be0
BK
320 template<typename _Up, typename _Ep>
321 unique_ptr&
322 operator=(unique_ptr<_Up, _Ep>&& __u)
ca0f8fd1 323 {
94a86be0
BK
324 reset(__u.release());
325 get_deleter() = std::move(__u.get_deleter());
326 return *this;
327 }
ca0f8fd1
PC
328
329 unique_ptr&
6d00745b 330 operator=(nullptr_t)
ca0f8fd1
PC
331 {
332 reset();
333 return *this;
334 }
335
37d5c6ba 336 // Observers.
94a86be0
BK
337 typename std::add_lvalue_reference<element_type>::type
338 operator[](size_t __i) const
ca0f8fd1 339 {
3e2e1976 340 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
ca0f8fd1
PC
341 return get()[__i];
342 }
343
344 pointer
345 get() const
346 { return std::get<0>(_M_t); }
347
94a86be0 348 deleter_type&
ca0f8fd1
PC
349 get_deleter()
350 { return std::get<1>(_M_t); }
351
2ba34efc 352 const deleter_type&
ca0f8fd1 353 get_deleter() const
94a86be0 354 { return std::get<1>(_M_t); }
ca0f8fd1 355
94a86be0 356 explicit operator bool() const
3e2e1976 357 { return get() == pointer() ? false : true; }
94a86be0 358
37d5c6ba 359 // Modifiers.
ca0f8fd1 360 pointer
94a86be0 361 release()
ca0f8fd1
PC
362 {
363 pointer __p = get();
3e2e1976 364 std::get<0>(_M_t) = pointer();
ca0f8fd1
PC
365 return __p;
366 }
367
368 void
94a86be0 369 reset(pointer __p = pointer())
ca0f8fd1 370 {
3e2e1976
JW
371 using std::swap;
372 swap(std::get<0>(_M_t), __p);
6d00745b
JW
373 if (__p != nullptr)
374 get_deleter()(__p);
375 }
376
377 void
378 reset(nullptr_t)
379 {
380 pointer __p = get();
381 std::get<0>(_M_t) = pointer();
382 if (__p != nullptr)
3e2e1976 383 get_deleter()(__p);
ca0f8fd1
PC
384 }
385
0d5f7a16
PC
386 // DR 821.
387 template<typename _Up>
94a86be0 388 void reset(_Up) = delete;
0d5f7a16 389
ca0f8fd1 390 void
ff74fd13 391 swap(unique_ptr& __u)
ca0f8fd1
PC
392 {
393 using std::swap;
394 swap(_M_t, __u._M_t);
395 }
396
37d5c6ba 397 // Disable copy from lvalue.
0d5f7a16
PC
398 unique_ptr(const unique_ptr&) = delete;
399 unique_ptr& operator=(const unique_ptr&) = delete;
ca0f8fd1 400
37d5c6ba 401 // Disable construction from convertible pointer types.
ca0f8fd1 402 // (N2315 - 20.6.5.3.1)
ca0f8fd1 403 template<typename _Up>
94a86be0 404 unique_ptr(_Up*, typename
0d5f7a16
PC
405 std::conditional<std::is_reference<deleter_type>::value,
406 deleter_type, const deleter_type&>::type,
94a86be0 407 typename std::enable_if<std::is_convertible<_Up*,
0d5f7a16
PC
408 pointer>::value>::type* = 0) = delete;
409
410 template<typename _Up>
94a86be0
BK
411 unique_ptr(_Up*, typename std::remove_reference<deleter_type>::type&&,
412 typename std::enable_if<std::is_convertible<_Up*,
0d5f7a16
PC
413 pointer>::value>::type* = 0) = delete;
414
415 template<typename _Up>
94a86be0
BK
416 explicit
417 unique_ptr(_Up*, typename std::enable_if<std::is_convertible<_Up*,
0d5f7a16 418 pointer>::value>::type* = 0) = delete;
ca0f8fd1 419 };
94a86be0
BK
420
421 template<typename _Tp, typename _Dp>
ca0f8fd1 422 inline void
8fe286ea
PC
423 swap(unique_ptr<_Tp, _Dp>& __x,
424 unique_ptr<_Tp, _Dp>& __y)
ca0f8fd1
PC
425 { __x.swap(__y); }
426
8fe286ea
PC
427 template<typename _Tp, typename _Dp,
428 typename _Up, typename _Ep>
ca0f8fd1 429 inline bool
8fe286ea
PC
430 operator==(const unique_ptr<_Tp, _Dp>& __x,
431 const unique_ptr<_Up, _Ep>& __y)
ca0f8fd1
PC
432 { return __x.get() == __y.get(); }
433
3abeaf8f
PC
434 template<typename _Tp, typename _Dp>
435 inline bool
436 operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
437 { return __x.get() == nullptr; }
438
439 template<typename _Tp, typename _Dp>
440 inline bool
441 operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __y)
442 { return nullptr == __y.get(); }
443
8fe286ea
PC
444 template<typename _Tp, typename _Dp,
445 typename _Up, typename _Ep>
ca0f8fd1 446 inline bool
8fe286ea
PC
447 operator!=(const unique_ptr<_Tp, _Dp>& __x,
448 const unique_ptr<_Up, _Ep>& __y)
ca0f8fd1
PC
449 { return !(__x.get() == __y.get()); }
450
3abeaf8f
PC
451 template<typename _Tp, typename _Dp>
452 inline bool
453 operator!=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
454 { return __x.get() != nullptr; }
455
456 template<typename _Tp, typename _Dp>
457 inline bool
458 operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __y)
459 { return nullptr != __y.get(); }
460
8fe286ea
PC
461 template<typename _Tp, typename _Dp,
462 typename _Up, typename _Ep>
ca0f8fd1 463 inline bool
8fe286ea
PC
464 operator<(const unique_ptr<_Tp, _Dp>& __x,
465 const unique_ptr<_Up, _Ep>& __y)
ca0f8fd1
PC
466 { return __x.get() < __y.get(); }
467
8fe286ea
PC
468 template<typename _Tp, typename _Dp,
469 typename _Up, typename _Ep>
ca0f8fd1 470 inline bool
8fe286ea
PC
471 operator<=(const unique_ptr<_Tp, _Dp>& __x,
472 const unique_ptr<_Up, _Ep>& __y)
ca0f8fd1
PC
473 { return !(__y.get() < __x.get()); }
474
8fe286ea
PC
475 template<typename _Tp, typename _Dp,
476 typename _Up, typename _Ep>
ca0f8fd1 477 inline bool
8fe286ea
PC
478 operator>(const unique_ptr<_Tp, _Dp>& __x,
479 const unique_ptr<_Up, _Ep>& __y)
ca0f8fd1
PC
480 { return __y.get() < __x.get(); }
481
8fe286ea
PC
482 template<typename _Tp, typename _Dp,
483 typename _Up, typename _Ep>
ca0f8fd1 484 inline bool
8fe286ea
PC
485 operator>=(const unique_ptr<_Tp, _Dp>& __x,
486 const unique_ptr<_Up, _Ep>& __y)
ca0f8fd1
PC
487 { return !(__x.get() < __y.get()); }
488
b0e788cc 489 /// std::hash specialization for unique_ptr.
8fe286ea
PC
490 template<typename _Tp, typename _Dp>
491 struct hash<unique_ptr<_Tp, _Dp>>
492 : public std::unary_function<unique_ptr<_Tp, _Dp>, size_t>
b0e788cc
PC
493 {
494 size_t
8fe286ea 495 operator()(const unique_ptr<_Tp, _Dp>& __u) const
b0e788cc 496 {
8fe286ea 497 typedef unique_ptr<_Tp, _Dp> _UP;
b0e788cc
PC
498 return std::hash<typename _UP::pointer>()(__u.get());
499 }
500 };
501
5b9daa7e
BK
502 // @} group pointer_abstractions
503
ca0f8fd1
PC
504_GLIBCXX_END_NAMESPACE
505
506#endif /* _UNIQUE_PTR_H */