2010-12-20 Tobias Burnus <burnus@net-b.de>
[official-gcc.git] / libstdc++-v3 / include / bits / unique_ptr.h
blob0b00fcb5703e8806d7b348f175e45e3e2ec5f15c
1 // unique_ptr implementation -*- C++ -*-
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.
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 bits/unique_ptr.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{memory}
30 #ifndef _UNIQUE_PTR_H
31 #define _UNIQUE_PTR_H 1
33 #include <bits/c++config.h>
34 #include <debug/debug.h>
35 #include <type_traits>
36 #include <utility>
37 #include <tuple>
39 _GLIBCXX_BEGIN_NAMESPACE(std)
41 /**
42 * @addtogroup pointer_abstractions
43 * @{
46 /// Primary template, default_delete.
47 template<typename _Tp>
48 struct default_delete
50 constexpr default_delete() { }
52 template<typename _Up, typename = typename
53 std::enable_if<std::is_convertible<_Up*, _Tp*>::value>::type>
54 default_delete(const default_delete<_Up>&) { }
56 void
57 operator()(_Tp* __ptr) const
59 static_assert(sizeof(_Tp)>0,
60 "can't delete pointer to incomplete type");
61 delete __ptr;
65 // _GLIBCXX_RESOLVE_LIB_DEFECTS
66 // DR 740 - omit specialization for array objects with a compile time length
67 /// Specialization, default_delete.
68 template<typename _Tp>
69 struct default_delete<_Tp[]>
71 constexpr default_delete() { }
73 void
74 operator()(_Tp* __ptr) const
76 static_assert(sizeof(_Tp)>0,
77 "can't delete pointer to incomplete type");
78 delete [] __ptr;
82 /// 20.7.12.2 unique_ptr for single objects.
83 template <typename _Tp, typename _Dp = default_delete<_Tp> >
84 class unique_ptr
86 // use SFINAE to determine whether _Del::pointer exists
87 class _Pointer
89 template<typename _Up>
90 static typename _Up::pointer __test(typename _Up::pointer*);
92 template<typename _Up>
93 static _Tp* __test(...);
95 typedef typename remove_reference<_Dp>::type _Del;
97 public:
98 typedef decltype( __test<_Del>(0)) type;
101 typedef std::tuple<_Tp*, _Dp> __tuple_type;
102 __tuple_type _M_t;
104 public:
105 typedef typename _Pointer::type pointer;
106 typedef _Tp element_type;
107 typedef _Dp deleter_type;
109 // Constructors.
110 constexpr unique_ptr()
111 : _M_t()
112 { static_assert(!std::is_pointer<deleter_type>::value,
113 "constructed with null function pointer deleter"); }
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"); }
121 unique_ptr(pointer __p,
122 typename std::conditional<std::is_reference<deleter_type>::value,
123 deleter_type, const deleter_type&>::type __d)
124 : _M_t(__p, __d) { }
126 unique_ptr(pointer __p,
127 typename std::remove_reference<deleter_type>::type&& __d)
128 : _M_t(std::move(__p), std::move(__d))
129 { static_assert(!std::is_reference<deleter_type>::value,
130 "rvalue deleter bound to reference"); }
132 constexpr unique_ptr(nullptr_t)
133 : _M_t()
134 { static_assert(!std::is_pointer<deleter_type>::value,
135 "constructed with null function pointer deleter"); }
137 // Move constructors.
138 unique_ptr(unique_ptr&& __u)
139 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
141 template<typename _Up, typename _Ep, typename = typename
142 std::enable_if
143 <std::is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
144 pointer>::value
145 && !std::is_array<_Up>::value
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))>
150 ::type>
151 unique_ptr(unique_ptr<_Up, _Ep>&& __u)
152 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter()))
155 #if _GLIBCXX_DEPRECATED
156 template<typename _Up, typename = typename
157 std::enable_if<std::is_convertible<_Up*, _Tp*>::value
158 && std::is_same<_Dp,
159 default_delete<_Tp>>::value>::type>
160 unique_ptr(auto_ptr<_Up>&& __u)
161 : _M_t(__u.release(), deleter_type()) { }
162 #endif
164 // Destructor.
165 ~unique_ptr() { reset(); }
167 // Assignment.
168 unique_ptr&
169 operator=(unique_ptr&& __u)
171 reset(__u.release());
172 get_deleter() = std::move(__u.get_deleter());
173 return *this;
176 template<typename _Up, typename _Ep, typename = typename
177 std::enable_if
178 <std::is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
179 pointer>::value
180 && !std::is_array<_Up>::value>::type>
181 unique_ptr&
182 operator=(unique_ptr<_Up, _Ep>&& __u)
184 reset(__u.release());
185 get_deleter() = std::move(__u.get_deleter());
186 return *this;
189 unique_ptr&
190 operator=(nullptr_t)
192 reset();
193 return *this;
196 // Observers.
197 typename std::add_lvalue_reference<element_type>::type
198 operator*() const
200 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
201 return *get();
204 pointer
205 operator->() const
207 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
208 return get();
211 pointer
212 get() const
213 { return std::get<0>(_M_t); }
215 deleter_type&
216 get_deleter()
217 { return std::get<1>(_M_t); }
219 const deleter_type&
220 get_deleter() const
221 { return std::get<1>(_M_t); }
223 explicit operator bool() const
224 { return get() == pointer() ? false : true; }
226 // Modifiers.
227 pointer
228 release()
230 pointer __p = get();
231 std::get<0>(_M_t) = pointer();
232 return __p;
235 void
236 reset(pointer __p = pointer())
238 using std::swap;
239 swap(std::get<0>(_M_t), __p);
240 if (__p != pointer())
241 get_deleter()(__p);
244 void
245 swap(unique_ptr& __u)
247 using std::swap;
248 swap(_M_t, __u._M_t);
251 // Disable copy from lvalue.
252 unique_ptr(const unique_ptr&) = delete;
253 unique_ptr& operator=(const unique_ptr&) = delete;
256 /// 20.7.12.3 unique_ptr for array objects with a runtime length
257 // [unique.ptr.runtime]
258 // _GLIBCXX_RESOLVE_LIB_DEFECTS
259 // DR 740 - omit specialization for array objects with a compile time length
260 template<typename _Tp, typename _Dp>
261 class unique_ptr<_Tp[], _Dp>
263 typedef std::tuple<_Tp*, _Dp> __tuple_type;
264 __tuple_type _M_t;
266 public:
267 typedef _Tp* pointer;
268 typedef _Tp element_type;
269 typedef _Dp deleter_type;
271 // Constructors.
272 constexpr unique_ptr()
273 : _M_t()
274 { static_assert(!std::is_pointer<deleter_type>::value,
275 "constructed with null function pointer deleter"); }
277 explicit
278 unique_ptr(pointer __p)
279 : _M_t(__p, deleter_type())
280 { static_assert(!std::is_pointer<deleter_type>::value,
281 "constructed with null function pointer deleter"); }
283 unique_ptr(pointer __p,
284 typename std::conditional<std::is_reference<deleter_type>::value,
285 deleter_type, const deleter_type&>::type __d)
286 : _M_t(__p, __d) { }
288 unique_ptr(pointer __p,
289 typename std::remove_reference<deleter_type>::type && __d)
290 : _M_t(std::move(__p), std::move(__d))
291 { static_assert(!std::is_reference<deleter_type>::value,
292 "rvalue deleter bound to reference"); }
294 constexpr unique_ptr(nullptr_t)
295 : _M_t()
296 { static_assert(!std::is_pointer<deleter_type>::value,
297 "constructed with null function pointer deleter"); }
299 // Move constructors.
300 unique_ptr(unique_ptr&& __u)
301 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
303 template<typename _Up, typename _Ep>
304 unique_ptr(unique_ptr<_Up, _Ep>&& __u)
305 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter()))
308 // Destructor.
309 ~unique_ptr() { reset(); }
311 // Assignment.
312 unique_ptr&
313 operator=(unique_ptr&& __u)
315 reset(__u.release());
316 get_deleter() = std::move(__u.get_deleter());
317 return *this;
320 template<typename _Up, typename _Ep>
321 unique_ptr&
322 operator=(unique_ptr<_Up, _Ep>&& __u)
324 reset(__u.release());
325 get_deleter() = std::move(__u.get_deleter());
326 return *this;
329 unique_ptr&
330 operator=(nullptr_t)
332 reset();
333 return *this;
336 // Observers.
337 typename std::add_lvalue_reference<element_type>::type
338 operator[](size_t __i) const
340 _GLIBCXX_DEBUG_ASSERT(get() != pointer());
341 return get()[__i];
344 pointer
345 get() const
346 { return std::get<0>(_M_t); }
348 deleter_type&
349 get_deleter()
350 { return std::get<1>(_M_t); }
352 const deleter_type&
353 get_deleter() const
354 { return std::get<1>(_M_t); }
356 explicit operator bool() const
357 { return get() == pointer() ? false : true; }
359 // Modifiers.
360 pointer
361 release()
363 pointer __p = get();
364 std::get<0>(_M_t) = pointer();
365 return __p;
368 void
369 reset(pointer __p = pointer())
371 using std::swap;
372 swap(std::get<0>(_M_t), __p);
373 if (__p != nullptr)
374 get_deleter()(__p);
377 void
378 reset(nullptr_t)
380 pointer __p = get();
381 std::get<0>(_M_t) = pointer();
382 if (__p != nullptr)
383 get_deleter()(__p);
386 // DR 821.
387 template<typename _Up>
388 void reset(_Up) = delete;
390 void
391 swap(unique_ptr& __u)
393 using std::swap;
394 swap(_M_t, __u._M_t);
397 // Disable copy from lvalue.
398 unique_ptr(const unique_ptr&) = delete;
399 unique_ptr& operator=(const unique_ptr&) = delete;
401 // Disable construction from convertible pointer types.
402 // (N2315 - 20.6.5.3.1)
403 template<typename _Up>
404 unique_ptr(_Up*, typename
405 std::conditional<std::is_reference<deleter_type>::value,
406 deleter_type, const deleter_type&>::type,
407 typename std::enable_if<std::is_convertible<_Up*,
408 pointer>::value>::type* = 0) = delete;
410 template<typename _Up>
411 unique_ptr(_Up*, typename std::remove_reference<deleter_type>::type&&,
412 typename std::enable_if<std::is_convertible<_Up*,
413 pointer>::value>::type* = 0) = delete;
415 template<typename _Up>
416 explicit
417 unique_ptr(_Up*, typename std::enable_if<std::is_convertible<_Up*,
418 pointer>::value>::type* = 0) = delete;
421 template<typename _Tp, typename _Dp>
422 inline void
423 swap(unique_ptr<_Tp, _Dp>& __x,
424 unique_ptr<_Tp, _Dp>& __y)
425 { __x.swap(__y); }
427 template<typename _Tp, typename _Dp,
428 typename _Up, typename _Ep>
429 inline bool
430 operator==(const unique_ptr<_Tp, _Dp>& __x,
431 const unique_ptr<_Up, _Ep>& __y)
432 { return __x.get() == __y.get(); }
434 template<typename _Tp, typename _Dp>
435 inline bool
436 operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
437 { return __x.get() == nullptr; }
439 template<typename _Tp, typename _Dp>
440 inline bool
441 operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __y)
442 { return nullptr == __y.get(); }
444 template<typename _Tp, typename _Dp,
445 typename _Up, typename _Ep>
446 inline bool
447 operator!=(const unique_ptr<_Tp, _Dp>& __x,
448 const unique_ptr<_Up, _Ep>& __y)
449 { return !(__x.get() == __y.get()); }
451 template<typename _Tp, typename _Dp>
452 inline bool
453 operator!=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
454 { return __x.get() != nullptr; }
456 template<typename _Tp, typename _Dp>
457 inline bool
458 operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __y)
459 { return nullptr != __y.get(); }
461 template<typename _Tp, typename _Dp,
462 typename _Up, typename _Ep>
463 inline bool
464 operator<(const unique_ptr<_Tp, _Dp>& __x,
465 const unique_ptr<_Up, _Ep>& __y)
466 { return __x.get() < __y.get(); }
468 template<typename _Tp, typename _Dp,
469 typename _Up, typename _Ep>
470 inline bool
471 operator<=(const unique_ptr<_Tp, _Dp>& __x,
472 const unique_ptr<_Up, _Ep>& __y)
473 { return !(__y.get() < __x.get()); }
475 template<typename _Tp, typename _Dp,
476 typename _Up, typename _Ep>
477 inline bool
478 operator>(const unique_ptr<_Tp, _Dp>& __x,
479 const unique_ptr<_Up, _Ep>& __y)
480 { return __y.get() < __x.get(); }
482 template<typename _Tp, typename _Dp,
483 typename _Up, typename _Ep>
484 inline bool
485 operator>=(const unique_ptr<_Tp, _Dp>& __x,
486 const unique_ptr<_Up, _Ep>& __y)
487 { return !(__x.get() < __y.get()); }
489 /// std::hash specialization for unique_ptr.
490 template<typename _Tp, typename _Dp>
491 struct hash<unique_ptr<_Tp, _Dp>>
492 : public std::unary_function<unique_ptr<_Tp, _Dp>, size_t>
494 size_t
495 operator()(const unique_ptr<_Tp, _Dp>& __u) const
497 typedef unique_ptr<_Tp, _Dp> _UP;
498 return std::hash<typename _UP::pointer>()(__u.get());
502 // @} group pointer_abstractions
504 _GLIBCXX_END_NAMESPACE
506 #endif /* _UNIQUE_PTR_H */