Merged r156806 through r156977 into branch.
[official-gcc.git] / libstdc++-v3 / include / bits / unique_ptr.h
blob71348650ee66b53c636a033830af9e2ebf09709e
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 unique_ptr.h
26 * This is an internal header file, included by other library headers.
27 * You should not attempt to use it directly.
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 default_delete() { }
52 template<typename _Up>
53 default_delete(const default_delete<_Up>&) { }
55 void
56 operator()(_Tp* __ptr) const
58 static_assert(sizeof(_Tp)>0,
59 "can't delete pointer to incomplete type");
60 delete __ptr;
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[]>
70 void
71 operator()(_Tp* __ptr) const
73 static_assert(sizeof(_Tp)>0,
74 "can't delete pointer to incomplete type");
75 delete [] __ptr;
79 /// 20.7.12.2 unique_ptr for single objects.
80 template <typename _Tp, typename _Tp_Deleter = default_delete<_Tp> >
81 class unique_ptr
83 typedef std::tuple<_Tp*, _Tp_Deleter> __tuple_type;
84 typedef _Tp* unique_ptr::* __unspecified_pointer_type;
86 public:
87 typedef _Tp* pointer;
88 typedef _Tp element_type;
89 typedef _Tp_Deleter deleter_type;
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"); }
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"); }
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) { }
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"); }
114 // Move constructors.
115 unique_ptr(unique_ptr&& __u)
116 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
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()))
123 // Destructor.
124 ~unique_ptr() { reset(); }
126 // Assignment.
127 unique_ptr&
128 operator=(unique_ptr&& __u)
130 reset(__u.release());
131 get_deleter() = std::move(__u.get_deleter());
132 return *this;
135 template<typename _Up, typename _Up_Deleter>
136 unique_ptr&
137 operator=(unique_ptr<_Up, _Up_Deleter>&& __u)
139 reset(__u.release());
140 get_deleter() = std::move(__u.get_deleter());
141 return *this;
144 unique_ptr&
145 operator=(__unspecified_pointer_type)
147 reset();
148 return *this;
151 // Observers.
152 typename std::add_lvalue_reference<element_type>::type
153 operator*() const
155 _GLIBCXX_DEBUG_ASSERT(get() != 0);
156 return *get();
159 pointer
160 operator->() const
162 _GLIBCXX_DEBUG_ASSERT(get() != 0);
163 return get();
166 pointer
167 get() const
168 { return std::get<0>(_M_t); }
170 typename std::add_lvalue_reference<deleter_type>::type
171 get_deleter()
172 { return std::get<1>(_M_t); }
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); }
180 explicit operator bool() const
181 { return get() == 0 ? false : true; }
183 // Modifiers.
184 pointer
185 release()
187 pointer __p = get();
188 std::get<0>(_M_t) = 0;
189 return __p;
192 void
193 reset(pointer __p = pointer())
195 if (__p != get())
197 get_deleter()(get());
198 std::get<0>(_M_t) = __p;
202 void
203 swap(unique_ptr& __u)
205 using std::swap;
206 swap(_M_t, __u._M_t);
209 // Disable copy from lvalue.
210 unique_ptr(const unique_ptr&) = delete;
211 unique_ptr& operator=(const unique_ptr&) = delete;
213 private:
214 __tuple_type _M_t;
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>
224 typedef std::tuple<_Tp*, _Tp_Deleter> __tuple_type;
225 typedef _Tp* unique_ptr::* __unspecified_pointer_type;
227 public:
228 typedef _Tp* pointer;
229 typedef _Tp element_type;
230 typedef _Tp_Deleter deleter_type;
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"); }
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"); }
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) { }
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"); }
255 // Move constructors.
256 unique_ptr(unique_ptr&& __u)
257 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
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()))
264 // Destructor.
265 ~unique_ptr() { reset(); }
267 // Assignment.
268 unique_ptr&
269 operator=(unique_ptr&& __u)
271 reset(__u.release());
272 get_deleter() = std::move(__u.get_deleter());
273 return *this;
276 template<typename _Up, typename _Up_Deleter>
277 unique_ptr&
278 operator=(unique_ptr<_Up, _Up_Deleter>&& __u)
280 reset(__u.release());
281 get_deleter() = std::move(__u.get_deleter());
282 return *this;
285 unique_ptr&
286 operator=(__unspecified_pointer_type)
288 reset();
289 return *this;
292 // Observers.
293 typename std::add_lvalue_reference<element_type>::type
294 operator[](size_t __i) const
296 _GLIBCXX_DEBUG_ASSERT(get() != 0);
297 return get()[__i];
300 pointer
301 get() const
302 { return std::get<0>(_M_t); }
304 typename std::add_lvalue_reference<deleter_type>::type
305 get_deleter()
306 { return std::get<1>(_M_t); }
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); }
314 explicit operator bool() const
315 { return get() == 0 ? false : true; }
317 // Modifiers.
318 pointer
319 release()
321 pointer __p = get();
322 std::get<0>(_M_t) = 0;
323 return __p;
326 void
327 reset(pointer __p = pointer())
329 if (__p != get())
331 get_deleter()(get());
332 std::get<0>(_M_t) = __p;
336 // DR 821.
337 template<typename _Up>
338 void reset(_Up) = delete;
340 void
341 swap(unique_ptr& __u)
343 using std::swap;
344 swap(_M_t, __u._M_t);
347 // Disable copy from lvalue.
348 unique_ptr(const unique_ptr&) = delete;
349 unique_ptr& operator=(const unique_ptr&) = delete;
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;
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;
365 template<typename _Up>
366 explicit
367 unique_ptr(_Up*, typename std::enable_if<std::is_convertible<_Up*,
368 pointer>::value>::type* = 0) = delete;
370 private:
371 __tuple_type _M_t;
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); }
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(); }
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()); }
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(); }
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()); }
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(); }
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()); }
422 // @} group pointer_abstractions
424 _GLIBCXX_END_NAMESPACE
426 #endif /* _UNIQUE_PTR_H */