OSDN Git Service

2010-11-05 Paolo Carlini <paolo.carlini@oracle.com>
[pf3gnuchains/gcc-fork.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       constexpr default_delete() { }
51
52       template<typename _Up, typename = typename
53                std::enable_if<std::is_convertible<_Up*, _Tp*>::value>::type>
54         default_delete(const default_delete<_Up>&) { }
55
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       }
63     };
64
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[]>
70     {
71       constexpr default_delete() { }
72
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
82   /// 20.7.12.2 unique_ptr for single objects.
83   template <typename _Tp, typename _Dp = default_delete<_Tp> >
84     class unique_ptr
85     {
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
95         typedef typename remove_reference<_Dp>::type _Del;
96
97       public:
98         typedef decltype( __test<_Del>(0)) type;
99       };
100
101       typedef std::tuple<_Tp*, _Dp>     __tuple_type;
102       __tuple_type                      _M_t;
103
104     public:
105       typedef typename _Pointer::type   pointer;
106       typedef _Tp                       element_type;
107       typedef _Dp                       deleter_type;
108
109       static_assert(!std::is_pointer<deleter_type>::value,
110                     "constructed with null function pointer deleter");
111
112       // Constructors.
113       constexpr unique_ptr()
114       : _M_t()
115       { }
116
117       explicit
118       unique_ptr(pointer __p)
119       : _M_t(__p, deleter_type())
120       { static_assert(!std::is_pointer<deleter_type>::value,
121                      "constructed with null function pointer deleter"); }
122
123       unique_ptr(pointer __p,
124           typename std::conditional<std::is_reference<deleter_type>::value,
125             deleter_type, const deleter_type&>::type __d)
126       : _M_t(__p, __d) { }
127
128       unique_ptr(pointer __p,
129           typename std::remove_reference<deleter_type>::type&& __d)
130       : _M_t(std::move(__p), std::move(__d))
131       { static_assert(!std::is_reference<deleter_type>::value,
132                       "rvalue deleter bound to reference"); }
133
134       constexpr unique_ptr(nullptr_t)
135       : _M_t(pointer(), deleter_type())
136       { }
137
138       // Move constructors.
139       unique_ptr(unique_ptr&& __u)
140       : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
141
142       template<typename _Up, typename _Ep, typename = typename
143         std::enable_if
144           <std::is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
145                                pointer>::value
146            && !std::is_array<_Up>::value
147            && ((std::is_reference<_Dp>::value
148                 && std::is_same<_Ep, _Dp>::value)
149                || (!std::is_reference<_Dp>::value
150                    && std::is_convertible<_Ep, _Dp>::value))>
151              ::type>
152         unique_ptr(unique_ptr<_Up, _Ep>&& __u)
153         : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter()))
154         { }
155
156 #if _GLIBCXX_DEPRECATED
157       template<typename _Up, typename = typename
158         std::enable_if<std::is_convertible<_Up*, _Tp*>::value
159                        && std::is_same<_Dp,
160                                        default_delete<_Tp>>::value>::type>
161         unique_ptr(auto_ptr<_Up>&& __u)
162         : _M_t(__u.release(), deleter_type()) { }
163 #endif
164
165       // Destructor.
166       ~unique_ptr() { reset(); }
167
168       // Assignment.
169       unique_ptr&
170       operator=(unique_ptr&& __u)
171       {
172         reset(__u.release());
173         get_deleter() = std::move(__u.get_deleter());
174         return *this;
175       }
176
177       template<typename _Up, typename _Ep, typename = typename
178         std::enable_if
179           <std::is_convertible<typename unique_ptr<_Up, _Ep>::pointer,
180                                pointer>::value
181            && !std::is_array<_Up>::value>::type>
182         unique_ptr&
183         operator=(unique_ptr<_Up, _Ep>&& __u)
184         {
185           reset(__u.release());
186           get_deleter() = std::move(__u.get_deleter());
187           return *this;
188         }
189
190       unique_ptr&
191       operator=(nullptr_t)
192       {
193         reset();
194         return *this;
195       }
196
197       // Observers.
198       typename std::add_lvalue_reference<element_type>::type
199       operator*() const
200       {
201         _GLIBCXX_DEBUG_ASSERT(get() != pointer());
202         return *get();
203       }
204
205       pointer
206       operator->() const
207       {
208         _GLIBCXX_DEBUG_ASSERT(get() != pointer());
209         return get();
210       }
211
212       pointer
213       get() const
214       { return std::get<0>(_M_t); }
215
216       deleter_type&
217       get_deleter()
218       { return std::get<1>(_M_t); }
219
220       const deleter_type&
221       get_deleter() const
222       { return std::get<1>(_M_t); }
223
224       explicit operator bool() const
225       { return get() == pointer() ? false : true; }
226
227       // Modifiers.
228       pointer
229       release()
230       {
231         pointer __p = get();
232         std::get<0>(_M_t) = pointer();
233         return __p;
234       }
235
236       void
237       reset(pointer __p = pointer())
238       {
239         using std::swap;
240         swap(std::get<0>(_M_t), __p);
241         if (__p != pointer())
242           get_deleter()(__p);
243       }
244
245       void
246       swap(unique_ptr& __u)
247       {
248         using std::swap;
249         swap(_M_t, __u._M_t);
250       }
251
252       // Disable copy from lvalue.
253       unique_ptr(const unique_ptr&) = delete;
254       unique_ptr& operator=(const unique_ptr&) = delete;
255   };
256
257   /// 20.7.12.3 unique_ptr for array objects with a runtime length
258   // [unique.ptr.runtime]
259   // _GLIBCXX_RESOLVE_LIB_DEFECTS
260   // DR 740 - omit specialization for array objects with a compile time length
261   template<typename _Tp, typename _Dp>
262     class unique_ptr<_Tp[], _Dp>
263     {
264       typedef std::tuple<_Tp*, _Dp>     __tuple_type;
265       __tuple_type                      _M_t;
266
267     public:
268       typedef _Tp*                      pointer;
269       typedef _Tp                       element_type;
270       typedef _Dp                       deleter_type;
271
272       static_assert(!std::is_pointer<deleter_type>::value,
273                     "constructed with null function pointer deleter");
274
275       // Constructors.
276       constexpr unique_ptr()
277       : _M_t(pointer(), deleter_type())
278       { }
279
280       explicit
281       unique_ptr(pointer __p)
282       : _M_t(__p, deleter_type())
283       { }
284
285       unique_ptr(pointer __p,
286           typename std::conditional<std::is_reference<deleter_type>::value,
287               deleter_type, const deleter_type&>::type __d)
288       : _M_t(__p, __d) { }
289
290       unique_ptr(pointer __p,
291                  typename std::remove_reference<deleter_type>::type && __d)
292       : _M_t(std::move(__p), std::move(__d))
293       { static_assert(!std::is_reference<deleter_type>::value,
294                       "rvalue deleter bound to reference"); }
295
296       /* TODO: use delegating constructor */
297       constexpr unique_ptr(nullptr_t)
298       : _M_t(pointer(), deleter_type())
299       { }
300
301       // Move constructors.
302       unique_ptr(unique_ptr&& __u)
303       : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
304
305       template<typename _Up, typename _Ep>
306         unique_ptr(unique_ptr<_Up, _Ep>&& __u)
307         : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter()))
308         { }
309
310       // Destructor.
311       ~unique_ptr() { reset(); }
312
313       // Assignment.
314       unique_ptr&
315       operator=(unique_ptr&& __u)
316       {
317         reset(__u.release());
318         get_deleter() = std::move(__u.get_deleter());
319         return *this;
320       }
321
322       template<typename _Up, typename _Ep>
323         unique_ptr&
324         operator=(unique_ptr<_Up, _Ep>&& __u)
325         {
326           reset(__u.release());
327           get_deleter() = std::move(__u.get_deleter());
328           return *this;
329         }
330
331       unique_ptr&
332       operator=(nullptr_t)
333       {
334         reset();
335         return *this;
336       }
337
338       // Observers.
339       typename std::add_lvalue_reference<element_type>::type
340       operator[](size_t __i) const
341       {
342         _GLIBCXX_DEBUG_ASSERT(get() != pointer());
343         return get()[__i];
344       }
345
346       pointer
347       get() const
348       { return std::get<0>(_M_t); }
349
350       deleter_type&
351       get_deleter()
352       { return std::get<1>(_M_t); }
353
354       const deleter_type&
355       get_deleter() const
356       { return std::get<1>(_M_t); }
357
358       explicit operator bool() const
359       { return get() == pointer() ? false : true; }
360
361       // Modifiers.
362       pointer
363       release()
364       {
365         pointer __p = get();
366         std::get<0>(_M_t) = pointer();
367         return __p;
368       }
369
370       void
371       reset(pointer __p = pointer())
372       {
373         using std::swap;
374         swap(std::get<0>(_M_t), __p);
375         if (__p != nullptr)
376           get_deleter()(__p);
377       }
378
379       void
380       reset(nullptr_t)
381       {
382         pointer __p = get();
383         std::get<0>(_M_t) = pointer();
384         if (__p != nullptr)
385           get_deleter()(__p);
386       }
387
388       // DR 821.
389       template<typename _Up>
390         void reset(_Up) = delete;
391
392       void
393       swap(unique_ptr& __u)
394       {
395         using std::swap;
396         swap(_M_t, __u._M_t);
397       }
398
399       // Disable copy from lvalue.
400       unique_ptr(const unique_ptr&) = delete;
401       unique_ptr& operator=(const unique_ptr&) = delete;
402
403       // Disable construction from convertible pointer types.
404       // (N2315 - 20.6.5.3.1)
405       template<typename _Up>
406         unique_ptr(_Up*, typename
407                    std::conditional<std::is_reference<deleter_type>::value,
408                    deleter_type, const deleter_type&>::type,
409                    typename std::enable_if<std::is_convertible<_Up*,
410                    pointer>::value>::type* = 0) = delete;
411
412       template<typename _Up>
413         unique_ptr(_Up*, typename std::remove_reference<deleter_type>::type&&,
414                    typename std::enable_if<std::is_convertible<_Up*,
415                    pointer>::value>::type* = 0) = delete;
416
417       template<typename _Up>
418         explicit
419         unique_ptr(_Up*, typename std::enable_if<std::is_convertible<_Up*,
420                    pointer>::value>::type* = 0) = delete;
421   };
422
423   template<typename _Tp, typename _Dp>
424     inline void
425     swap(unique_ptr<_Tp, _Dp>& __x,
426          unique_ptr<_Tp, _Dp>& __y)
427     { __x.swap(__y); }
428
429   template<typename _Tp, typename _Dp,
430            typename _Up, typename _Ep>
431     inline bool
432     operator==(const unique_ptr<_Tp, _Dp>& __x,
433                const unique_ptr<_Up, _Ep>& __y)
434     { return __x.get() == __y.get(); }
435
436   template<typename _Tp, typename _Dp>
437     inline bool
438     operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
439     { return __x.get() == nullptr; }
440
441   template<typename _Tp, typename _Dp>
442     inline bool
443     operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __y)
444     { return nullptr == __y.get(); }
445
446   template<typename _Tp, typename _Dp,
447            typename _Up, typename _Ep>
448     inline bool
449     operator!=(const unique_ptr<_Tp, _Dp>& __x,
450                const unique_ptr<_Up, _Ep>& __y)
451     { return !(__x.get() == __y.get()); }
452
453   template<typename _Tp, typename _Dp>
454     inline bool
455     operator!=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
456     { return __x.get() != nullptr; }
457
458   template<typename _Tp, typename _Dp>
459     inline bool
460     operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __y)
461     { return nullptr != __y.get(); }
462
463   template<typename _Tp, typename _Dp,
464            typename _Up, typename _Ep>
465     inline bool
466     operator<(const unique_ptr<_Tp, _Dp>& __x,
467               const unique_ptr<_Up, _Ep>& __y)
468     { return __x.get() < __y.get(); }
469
470   template<typename _Tp, typename _Dp,
471            typename _Up, typename _Ep>
472     inline bool
473     operator<=(const unique_ptr<_Tp, _Dp>& __x,
474                const unique_ptr<_Up, _Ep>& __y)
475     { return !(__y.get() < __x.get()); }
476
477   template<typename _Tp, typename _Dp,
478            typename _Up, typename _Ep>
479     inline bool
480     operator>(const unique_ptr<_Tp, _Dp>& __x,
481               const unique_ptr<_Up, _Ep>& __y)
482     { return __y.get() < __x.get(); }
483
484   template<typename _Tp, typename _Dp,
485            typename _Up, typename _Ep>
486     inline bool
487     operator>=(const unique_ptr<_Tp, _Dp>& __x,
488                const unique_ptr<_Up, _Ep>& __y)
489     { return !(__x.get() < __y.get()); }
490
491   /// std::hash specialization for unique_ptr.
492   template<typename _Tp, typename _Dp>
493     struct hash<unique_ptr<_Tp, _Dp>>
494     : public std::unary_function<unique_ptr<_Tp, _Dp>, size_t>
495     {
496       size_t
497       operator()(const unique_ptr<_Tp, _Dp>& __u) const
498       {
499         typedef unique_ptr<_Tp, _Dp> _UP;
500         return std::hash<typename _UP::pointer>()(__u.get());
501       }
502     };
503
504   // @} group pointer_abstractions
505
506 _GLIBCXX_END_NAMESPACE
507
508 #endif /* _UNIQUE_PTR_H */