]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/unique_ptr.h
re PR libstdc++/43183 (std::unique_ptr::reset() does not conform to N3035.)
[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
PC
24
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.
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.
ca0f8fd1
PC
47 template<typename _Tp>
48 struct default_delete
49 {
50 default_delete() { }
51
52 template<typename _Up>
53 default_delete(const default_delete<_Up>&) { }
54
55 void
56 operator()(_Tp* __ptr) const
57 {
58 static_assert(sizeof(_Tp)>0,
59 "can't delete pointer to incomplete type");
60 delete __ptr;
61 }
62 };
63
64 // _GLIBCXX_RESOLVE_LIB_DEFECTS
65 // DR 740 - omit specialization for array objects with a compile time length
ad68e9fc 66 /// Specialization, default_delete.
ca0f8fd1
PC
67 template<typename _Tp>
68 struct default_delete<_Tp[]>
69 {
70 void
71 operator()(_Tp* __ptr) const
72 {
73 static_assert(sizeof(_Tp)>0,
74 "can't delete pointer to incomplete type");
75 delete [] __ptr;
76 }
77 };
78
37d5c6ba 79 /// 20.7.12.2 unique_ptr for single objects.
ca0f8fd1
PC
80 template <typename _Tp, typename _Tp_Deleter = default_delete<_Tp> >
81 class unique_ptr
82 {
0d5f7a16 83 typedef std::tuple<_Tp*, _Tp_Deleter> __tuple_type;
37d5c6ba 84 typedef _Tp* unique_ptr::* __unspecified_pointer_type;
ca0f8fd1
PC
85
86 public:
37d5c6ba 87 typedef _Tp* pointer;
0d5f7a16
PC
88 typedef _Tp element_type;
89 typedef _Tp_Deleter deleter_type;
ca0f8fd1 90
37d5c6ba 91 // Constructors.
ca0f8fd1
PC
92 unique_ptr()
93 : _M_t(pointer(), deleter_type())
94 { static_assert(!std::is_pointer<deleter_type>::value,
95 "constructed with null function pointer deleter"); }
96
97 explicit
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"); }
102
103 unique_ptr(pointer __p,
104 typename std::conditional<std::is_reference<deleter_type>::value,
105 deleter_type, const deleter_type&>::type __d)
106 : _M_t(__p, __d) { }
107
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"); }
113
37d5c6ba
BK
114 // Move constructors.
115 unique_ptr(unique_ptr&& __u)
ca0f8fd1
PC
116 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
117
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()))
121 { }
122
37d5c6ba 123 // Destructor.
ca0f8fd1
PC
124 ~unique_ptr() { reset(); }
125
37d5c6ba 126 // Assignment.
ca0f8fd1
PC
127 unique_ptr&
128 operator=(unique_ptr&& __u)
129 {
130 reset(__u.release());
131 get_deleter() = std::move(__u.get_deleter());
132 return *this;
133 }
134
135 template<typename _Up, typename _Up_Deleter>
136 unique_ptr&
137 operator=(unique_ptr<_Up, _Up_Deleter>&& __u)
138 {
139 reset(__u.release());
140 get_deleter() = std::move(__u.get_deleter());
141 return *this;
142 }
143
144 unique_ptr&
145 operator=(__unspecified_pointer_type)
146 {
147 reset();
148 return *this;
149 }
150
37d5c6ba 151 // Observers.
d779a591
PC
152 typename std::add_lvalue_reference<element_type>::type
153 operator*() const
ca0f8fd1 154 {
3e2e1976 155 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
ca0f8fd1
PC
156 return *get();
157 }
158
159 pointer
160 operator->() const
161 {
3e2e1976 162 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
ca0f8fd1
PC
163 return get();
164 }
165
166 pointer
167 get() const
168 { return std::get<0>(_M_t); }
169
170 typename std::add_lvalue_reference<deleter_type>::type
171 get_deleter()
172 { return std::get<1>(_M_t); }
173
174 typename std::add_lvalue_reference<
175 typename std::add_const<deleter_type>::type
176 >::type
177 get_deleter() const
178 { return std::get<1>(_M_t); }
179
d29d4507 180 explicit operator bool() const
3e2e1976 181 { return get() == pointer() ? false : true; }
ca0f8fd1 182
37d5c6ba 183 // Modifiers.
ca0f8fd1
PC
184 pointer
185 release()
186 {
187 pointer __p = get();
3e2e1976 188 std::get<0>(_M_t) = pointer();
ca0f8fd1
PC
189 return __p;
190 }
191
192 void
0d5f7a16 193 reset(pointer __p = pointer())
ca0f8fd1 194 {
3e2e1976
JW
195 using std::swap;
196 swap(std::get<0>(_M_t), __p);
197 if (__p != pointer())
198 get_deleter()(__p);
ca0f8fd1
PC
199 }
200
201 void
ff74fd13 202 swap(unique_ptr& __u)
0d5f7a16
PC
203 {
204 using std::swap;
ca0f8fd1
PC
205 swap(_M_t, __u._M_t);
206 }
207
37d5c6ba 208 // Disable copy from lvalue.
0d5f7a16 209 unique_ptr(const unique_ptr&) = delete;
0d5f7a16 210 unique_ptr& operator=(const unique_ptr&) = delete;
ca0f8fd1 211
ca0f8fd1
PC
212 private:
213 __tuple_type _M_t;
214 };
215
37d5c6ba 216 /// 20.7.12.3 unique_ptr for array objects with a runtime length
ca0f8fd1
PC
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>
222 {
37d5c6ba 223 typedef std::tuple<_Tp*, _Tp_Deleter> __tuple_type;
37d5c6ba 224 typedef _Tp* unique_ptr::* __unspecified_pointer_type;
0d5f7a16 225
ca0f8fd1 226 public:
37d5c6ba 227 typedef _Tp* pointer;
0d5f7a16
PC
228 typedef _Tp element_type;
229 typedef _Tp_Deleter deleter_type;
230
37d5c6ba 231 // Constructors.
ca0f8fd1
PC
232 unique_ptr()
233 : _M_t(pointer(), deleter_type())
234 { static_assert(!std::is_pointer<deleter_type>::value,
235 "constructed with null function pointer deleter"); }
236
237 explicit
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"); }
242
243 unique_ptr(pointer __p,
244 typename std::conditional<std::is_reference<deleter_type>::value,
245 deleter_type, const deleter_type&>::type __d)
246 : _M_t(__p, __d) { }
247
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"); }
253
37d5c6ba 254 // Move constructors.
ca0f8fd1
PC
255 unique_ptr(unique_ptr&& __u)
256 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
257
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()))
261 { }
262
37d5c6ba 263 // Destructor.
ca0f8fd1
PC
264 ~unique_ptr() { reset(); }
265
37d5c6ba 266 // Assignment.
ca0f8fd1
PC
267 unique_ptr&
268 operator=(unique_ptr&& __u)
269 {
270 reset(__u.release());
271 get_deleter() = std::move(__u.get_deleter());
272 return *this;
273 }
274
275 template<typename _Up, typename _Up_Deleter>
276 unique_ptr&
277 operator=(unique_ptr<_Up, _Up_Deleter>&& __u)
278 {
279 reset(__u.release());
280 get_deleter() = std::move(__u.get_deleter());
281 return *this;
282 }
283
284 unique_ptr&
285 operator=(__unspecified_pointer_type)
286 {
287 reset();
288 return *this;
289 }
290
37d5c6ba 291 // Observers.
ca0f8fd1
PC
292 typename std::add_lvalue_reference<element_type>::type
293 operator[](size_t __i) const
294 {
3e2e1976 295 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
ca0f8fd1
PC
296 return get()[__i];
297 }
298
299 pointer
300 get() const
301 { return std::get<0>(_M_t); }
302
303 typename std::add_lvalue_reference<deleter_type>::type
304 get_deleter()
305 { return std::get<1>(_M_t); }
306
307 typename std::add_lvalue_reference<
308 typename std::add_const<deleter_type>::type
309 >::type
310 get_deleter() const
311 { return std::get<1>(_M_t); }
312
d29d4507 313 explicit operator bool() const
3e2e1976 314 { return get() == pointer() ? false : true; }
ca0f8fd1 315
37d5c6ba 316 // Modifiers.
ca0f8fd1
PC
317 pointer
318 release()
319 {
320 pointer __p = get();
3e2e1976 321 std::get<0>(_M_t) = pointer();
ca0f8fd1
PC
322 return __p;
323 }
324
325 void
0d5f7a16 326 reset(pointer __p = pointer())
ca0f8fd1 327 {
3e2e1976
JW
328 using std::swap;
329 swap(std::get<0>(_M_t), __p);
330 if (__p != pointer())
331 get_deleter()(__p);
ca0f8fd1
PC
332 }
333
0d5f7a16
PC
334 // DR 821.
335 template<typename _Up>
336 void reset(_Up) = delete;
337
ca0f8fd1 338 void
ff74fd13 339 swap(unique_ptr& __u)
ca0f8fd1
PC
340 {
341 using std::swap;
342 swap(_M_t, __u._M_t);
343 }
344
37d5c6ba 345 // Disable copy from lvalue.
0d5f7a16
PC
346 unique_ptr(const unique_ptr&) = delete;
347 unique_ptr& operator=(const unique_ptr&) = delete;
ca0f8fd1 348
37d5c6ba 349 // Disable construction from convertible pointer types.
ca0f8fd1 350 // (N2315 - 20.6.5.3.1)
ca0f8fd1 351 template<typename _Up>
0d5f7a16
PC
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;
357
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;
362
363 template<typename _Up>
364 explicit
365 unique_ptr(_Up*, typename std::enable_if<std::is_convertible<_Up*,
366 pointer>::value>::type* = 0) = delete;
367
ca0f8fd1
PC
368 private:
369 __tuple_type _M_t;
370 };
371
372 template<typename _Tp, typename _Tp_Deleter>
373 inline void
0d5f7a16
PC
374 swap(unique_ptr<_Tp, _Tp_Deleter>& __x,
375 unique_ptr<_Tp, _Tp_Deleter>& __y)
ca0f8fd1
PC
376 { __x.swap(__y); }
377
ca0f8fd1
PC
378 template<typename _Tp, typename _Tp_Deleter,
379 typename _Up, typename _Up_Deleter>
380 inline bool
0d5f7a16 381 operator==(const unique_ptr<_Tp, _Tp_Deleter>& __x,
ca0f8fd1
PC
382 const unique_ptr<_Up, _Up_Deleter>& __y)
383 { return __x.get() == __y.get(); }
384
385 template<typename _Tp, typename _Tp_Deleter,
386 typename _Up, typename _Up_Deleter>
387 inline bool
0d5f7a16 388 operator!=(const unique_ptr<_Tp, _Tp_Deleter>& __x,
ca0f8fd1
PC
389 const unique_ptr<_Up, _Up_Deleter>& __y)
390 { return !(__x.get() == __y.get()); }
391
392 template<typename _Tp, typename _Tp_Deleter,
393 typename _Up, typename _Up_Deleter>
394 inline bool
395 operator<(const unique_ptr<_Tp, _Tp_Deleter>& __x,
396 const unique_ptr<_Up, _Up_Deleter>& __y)
397 { return __x.get() < __y.get(); }
398
399 template<typename _Tp, typename _Tp_Deleter,
400 typename _Up, typename _Up_Deleter>
401 inline bool
402 operator<=(const unique_ptr<_Tp, _Tp_Deleter>& __x,
403 const unique_ptr<_Up, _Up_Deleter>& __y)
404 { return !(__y.get() < __x.get()); }
405
406 template<typename _Tp, typename _Tp_Deleter,
407 typename _Up, typename _Up_Deleter>
408 inline bool
409 operator>(const unique_ptr<_Tp, _Tp_Deleter>& __x,
410 const unique_ptr<_Up, _Up_Deleter>& __y)
411 { return __y.get() < __x.get(); }
412
413 template<typename _Tp, typename _Tp_Deleter,
414 typename _Up, typename _Up_Deleter>
415 inline bool
416 operator>=(const unique_ptr<_Tp, _Tp_Deleter>& __x,
417 const unique_ptr<_Up, _Up_Deleter>& __y)
418 { return !(__x.get() < __y.get()); }
419
5b9daa7e
BK
420 // @} group pointer_abstractions
421
ca0f8fd1
PC
422_GLIBCXX_END_NAMESPACE
423
424#endif /* _UNIQUE_PTR_H */