unique_ptr.h: (unique_ptr<>:: unique_ptr(const unique_ptr<_Up...
[gcc.git] / libstdc++-v3 / include / bits / unique_ptr.h
1 // unique_ptr implementation -*- C++ -*-
2
3 // Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
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
8 // Free Software Foundation; either version 3, or (at your option)
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
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/>.
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
33 #include <bits/c++config.h>
34 #include <debug/debug.h>
35 #include <type_traits>
36 #include <utility>
37 #include <tuple>
38
39 _GLIBCXX_BEGIN_NAMESPACE(std)
40
41 /**
42 * @addtogroup pointer_abstractions
43 * @{
44 */
45
46 /// Primary template, default_delete.
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
66 /// Specialization, default_delete.
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
79 /// 20.7.12.2 unique_ptr for single objects.
80 template <typename _Tp, typename _Tp_Deleter = default_delete<_Tp> >
81 class unique_ptr
82 {
83 typedef std::tuple<_Tp*, _Tp_Deleter> __tuple_type;
84 typedef _Tp* unique_ptr::* __unspecified_pointer_type;
85
86 public:
87 typedef _Tp* pointer;
88 typedef _Tp element_type;
89 typedef _Tp_Deleter deleter_type;
90
91 // Constructors.
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
114 // Move constructors.
115 unique_ptr(unique_ptr&& __u)
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
123 // Destructor.
124 ~unique_ptr() { reset(); }
125
126 // Assignment.
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
151 // Observers.
152 typename std::add_lvalue_reference<element_type>::type
153 operator*() const
154 {
155 _GLIBCXX_DEBUG_ASSERT(get() != 0);
156 return *get();
157 }
158
159 pointer
160 operator->() const
161 {
162 _GLIBCXX_DEBUG_ASSERT(get() != 0);
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
180 explicit operator bool() const
181 { return get() == 0 ? false : true; }
182
183 // Modifiers.
184 pointer
185 release()
186 {
187 pointer __p = get();
188 std::get<0>(_M_t) = 0;
189 return __p;
190 }
191
192 void
193 reset(pointer __p = pointer())
194 {
195 if (__p != get())
196 {
197 get_deleter()(get());
198 std::get<0>(_M_t) = __p;
199 }
200 }
201
202 void
203 swap(unique_ptr& __u)
204 {
205 using std::swap;
206 swap(_M_t, __u._M_t);
207 }
208
209 // Disable copy from lvalue.
210 unique_ptr(const unique_ptr&) = delete;
211 unique_ptr& operator=(const unique_ptr&) = delete;
212
213 private:
214 __tuple_type _M_t;
215 };
216
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>
223 {
224 typedef std::tuple<_Tp*, _Tp_Deleter> __tuple_type;
225 typedef _Tp* unique_ptr::* __unspecified_pointer_type;
226
227 public:
228 typedef _Tp* pointer;
229 typedef _Tp element_type;
230 typedef _Tp_Deleter deleter_type;
231
232 // Constructors.
233 unique_ptr()
234 : _M_t(pointer(), deleter_type())
235 { static_assert(!std::is_pointer<deleter_type>::value,
236 "constructed with null function pointer deleter"); }
237
238 explicit
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"); }
243
244 unique_ptr(pointer __p,
245 typename std::conditional<std::is_reference<deleter_type>::value,
246 deleter_type, const deleter_type&>::type __d)
247 : _M_t(__p, __d) { }
248
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"); }
254
255 // Move constructors.
256 unique_ptr(unique_ptr&& __u)
257 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
258
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()))
262 { }
263
264 // Destructor.
265 ~unique_ptr() { reset(); }
266
267 // Assignment.
268 unique_ptr&
269 operator=(unique_ptr&& __u)
270 {
271 reset(__u.release());
272 get_deleter() = std::move(__u.get_deleter());
273 return *this;
274 }
275
276 template<typename _Up, typename _Up_Deleter>
277 unique_ptr&
278 operator=(unique_ptr<_Up, _Up_Deleter>&& __u)
279 {
280 reset(__u.release());
281 get_deleter() = std::move(__u.get_deleter());
282 return *this;
283 }
284
285 unique_ptr&
286 operator=(__unspecified_pointer_type)
287 {
288 reset();
289 return *this;
290 }
291
292 // Observers.
293 typename std::add_lvalue_reference<element_type>::type
294 operator[](size_t __i) const
295 {
296 _GLIBCXX_DEBUG_ASSERT(get() != 0);
297 return get()[__i];
298 }
299
300 pointer
301 get() const
302 { return std::get<0>(_M_t); }
303
304 typename std::add_lvalue_reference<deleter_type>::type
305 get_deleter()
306 { return std::get<1>(_M_t); }
307
308 typename std::add_lvalue_reference<
309 typename std::add_const<deleter_type>::type
310 >::type
311 get_deleter() const
312 { return std::get<1>(_M_t); }
313
314 explicit operator bool() const
315 { return get() == 0 ? false : true; }
316
317 // Modifiers.
318 pointer
319 release()
320 {
321 pointer __p = get();
322 std::get<0>(_M_t) = 0;
323 return __p;
324 }
325
326 void
327 reset(pointer __p = pointer())
328 {
329 if (__p != get())
330 {
331 get_deleter()(get());
332 std::get<0>(_M_t) = __p;
333 }
334 }
335
336 // DR 821.
337 template<typename _Up>
338 void reset(_Up) = delete;
339
340 void
341 swap(unique_ptr& __u)
342 {
343 using std::swap;
344 swap(_M_t, __u._M_t);
345 }
346
347 // Disable copy from lvalue.
348 unique_ptr(const unique_ptr&) = delete;
349 unique_ptr& operator=(const unique_ptr&) = delete;
350
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;
359
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;
364
365 template<typename _Up>
366 explicit
367 unique_ptr(_Up*, typename std::enable_if<std::is_convertible<_Up*,
368 pointer>::value>::type* = 0) = delete;
369
370 private:
371 __tuple_type _M_t;
372 };
373
374 template<typename _Tp, typename _Tp_Deleter>
375 inline void
376 swap(unique_ptr<_Tp, _Tp_Deleter>& __x,
377 unique_ptr<_Tp, _Tp_Deleter>& __y)
378 { __x.swap(__y); }
379
380 template<typename _Tp, typename _Tp_Deleter,
381 typename _Up, typename _Up_Deleter>
382 inline bool
383 operator==(const unique_ptr<_Tp, _Tp_Deleter>& __x,
384 const unique_ptr<_Up, _Up_Deleter>& __y)
385 { return __x.get() == __y.get(); }
386
387 template<typename _Tp, typename _Tp_Deleter,
388 typename _Up, typename _Up_Deleter>
389 inline bool
390 operator!=(const unique_ptr<_Tp, _Tp_Deleter>& __x,
391 const unique_ptr<_Up, _Up_Deleter>& __y)
392 { return !(__x.get() == __y.get()); }
393
394 template<typename _Tp, typename _Tp_Deleter,
395 typename _Up, typename _Up_Deleter>
396 inline bool
397 operator<(const unique_ptr<_Tp, _Tp_Deleter>& __x,
398 const unique_ptr<_Up, _Up_Deleter>& __y)
399 { return __x.get() < __y.get(); }
400
401 template<typename _Tp, typename _Tp_Deleter,
402 typename _Up, typename _Up_Deleter>
403 inline bool
404 operator<=(const unique_ptr<_Tp, _Tp_Deleter>& __x,
405 const unique_ptr<_Up, _Up_Deleter>& __y)
406 { return !(__y.get() < __x.get()); }
407
408 template<typename _Tp, typename _Tp_Deleter,
409 typename _Up, typename _Up_Deleter>
410 inline bool
411 operator>(const unique_ptr<_Tp, _Tp_Deleter>& __x,
412 const unique_ptr<_Up, _Up_Deleter>& __y)
413 { return __y.get() < __x.get(); }
414
415 template<typename _Tp, typename _Tp_Deleter,
416 typename _Up, typename _Up_Deleter>
417 inline bool
418 operator>=(const unique_ptr<_Tp, _Tp_Deleter>& __x,
419 const unique_ptr<_Up, _Up_Deleter>& __y)
420 { return !(__x.get() < __y.get()); }
421
422 // @} group pointer_abstractions
423
424 _GLIBCXX_END_NAMESPACE
425
426 #endif /* _UNIQUE_PTR_H */