OSDN Git Service

2008-09-28 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 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 2, 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 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING.  If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 /** @file unique_ptr.h
31  *  This is an internal header file, included by other library headers.
32  *  You should not attempt to use it directly.
33  */
34
35 #ifndef _UNIQUE_PTR_H
36 #define _UNIQUE_PTR_H 1
37
38 #ifndef __GXX_EXPERIMENTAL_CXX0X__
39 # include <c++0x_warning.h>
40 #endif
41
42 #include <bits/c++config.h>
43 #include <debug/debug.h>
44 #include <type_traits>
45 #include <utility>
46 #include <tuple>
47
48 _GLIBCXX_BEGIN_NAMESPACE(std)
49
50   /// Primary template, default_delete.
51   template<typename _Tp> 
52     struct default_delete
53       {
54         default_delete() { }
55
56         template<typename _Up>
57           default_delete(const default_delete<_Up>&) { }
58
59         void
60         operator()(_Tp* __ptr) const
61         {
62           static_assert(sizeof(_Tp)>0,
63                         "can't delete pointer to incomplete type");
64           delete __ptr;
65         }
66     };
67
68   // _GLIBCXX_RESOLVE_LIB_DEFECTS
69   // DR 740 - omit specialization for array objects with a compile time length
70   /// Specialization, default_delete.
71   template<typename _Tp> 
72     struct default_delete<_Tp[]>
73     {
74       void
75       operator()(_Tp* __ptr) const
76       {
77         static_assert(sizeof(_Tp)>0,
78                       "can't delete pointer to incomplete type");
79         delete [] __ptr;
80       }
81     };
82
83   /// 20.6.11.2 unique_ptr for single objects.
84   template <typename _Tp, typename _Tp_Deleter = default_delete<_Tp> > 
85     class unique_ptr
86     {
87       typedef unique_ptr<_Tp, _Tp_Deleter>   __this_type;
88       typedef std::tuple<_Tp*, _Tp_Deleter>  __tuple_type;
89       typedef __tuple_type __this_type::*    __unspecified_bool_type;
90       typedef _Tp* __this_type::*            __unspecified_pointer_type;
91
92     public:
93       typedef _Tp*                    pointer;
94       typedef _Tp                element_type;      
95       typedef _Tp_Deleter        deleter_type;
96
97       // constructors
98       unique_ptr()
99       : _M_t(pointer(), deleter_type())
100       { static_assert(!std::is_pointer<deleter_type>::value,
101                       "constructed with null function pointer deleter"); }
102
103       explicit
104       unique_ptr(pointer __p)
105       : _M_t(__p, deleter_type())
106       { static_assert(!std::is_pointer<deleter_type>::value,
107                      "constructed with null function pointer deleter"); }
108
109       unique_ptr(pointer __p,
110           typename std::conditional<std::is_reference<deleter_type>::value, 
111             deleter_type, const deleter_type&>::type __d)
112       : _M_t(__p, __d) { }
113
114       unique_ptr(pointer __p,
115           typename std::remove_reference<deleter_type>::type&& __d)
116       : _M_t(std::move(__p), std::move(__d))
117       { static_assert(!std::is_reference<deleter_type>::value, 
118                       "rvalue deleter bound to reference"); }
119
120       // move constructors
121       unique_ptr(unique_ptr && __u) 
122       : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
123
124       template<typename _Up, typename _Up_Deleter> 
125         unique_ptr(unique_ptr<_Up, _Up_Deleter>&& __u) 
126         : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter()))
127         { }
128
129       // destructor
130       ~unique_ptr() { reset(); }
131     
132       // assignment
133       unique_ptr&
134       operator=(unique_ptr&& __u)
135       { 
136         reset(__u.release()); 
137         get_deleter() = std::move(__u.get_deleter()); 
138         return *this;
139       }
140
141       template<typename _Up, typename _Up_Deleter> 
142         unique_ptr&
143         operator=(unique_ptr<_Up, _Up_Deleter>&& __u)
144         {
145           reset(__u.release()); 
146           get_deleter() = std::move(__u.get_deleter()); 
147           return *this;
148         }
149
150       unique_ptr&
151       operator=(__unspecified_pointer_type) 
152       {
153         reset();
154         return *this;
155       }
156
157       // observers
158       typename std::add_lvalue_reference<element_type>::type operator*() const
159       {
160         _GLIBCXX_DEBUG_ASSERT(get() != 0);
161         return *get();
162       }
163
164       pointer
165       operator->() const
166       {
167         _GLIBCXX_DEBUG_ASSERT(get() != 0);
168         return get();
169       }
170
171       pointer
172       get() const
173       { return std::get<0>(_M_t); }
174
175       typename std::add_lvalue_reference<deleter_type>::type
176       get_deleter()
177       { return std::get<1>(_M_t); }
178
179       typename std::add_lvalue_reference<
180           typename std::add_const<deleter_type>::type
181               >::type
182       get_deleter() const
183       { return std::get<1>(_M_t); }
184
185       operator __unspecified_bool_type () const
186       { return get() == 0 ? 0 : &__this_type::_M_t; }
187
188       // modifiers
189       pointer
190       release() 
191       {
192         pointer __p = get();
193         std::get<0>(_M_t) = 0;
194         return __p;
195       }
196
197       void
198       reset(pointer __p = pointer())
199       {
200         if (__p != get())
201           {
202             get_deleter()(get());
203             std::get<0>(_M_t) = __p;
204           }
205       }
206
207       void
208       swap(unique_ptr&& __u)
209       {
210         using std::swap;
211         swap(_M_t, __u._M_t);
212       }
213
214       // disable copy from lvalue
215       unique_ptr(const unique_ptr&) = delete;
216
217       template<typename _Up, typename _Up_Deleter> 
218         unique_ptr(const unique_ptr<_Up, _Up_Deleter>&) = delete;
219
220       unique_ptr& operator=(const unique_ptr&) = delete;
221
222       template<typename _Up, typename _Up_Deleter> 
223         unique_ptr& operator=(const unique_ptr<_Up, _Up_Deleter>&) = delete;
224
225     private:
226       __tuple_type _M_t;
227   };
228  
229   /// 20.6.11.3 unique_ptr for array objects with a runtime length
230   // [unique.ptr.runtime]
231   // _GLIBCXX_RESOLVE_LIB_DEFECTS
232   // DR 740 - omit specialization for array objects with a compile time length
233   template<typename _Tp, typename _Tp_Deleter> 
234     class unique_ptr<_Tp[], _Tp_Deleter>
235     {
236       typedef unique_ptr<_Tp[], _Tp_Deleter>  __this_type;
237       typedef std::tuple<_Tp*, _Tp_Deleter>   __tuple_type;
238       typedef __tuple_type __this_type::*     __unspecified_bool_type;
239       typedef _Tp* __this_type::*             __unspecified_pointer_type;
240
241     public:
242       typedef _Tp*                    pointer;
243       typedef _Tp                element_type;      
244       typedef _Tp_Deleter        deleter_type;
245
246       // constructors
247       unique_ptr()
248       : _M_t(pointer(), deleter_type())
249       { static_assert(!std::is_pointer<deleter_type>::value,
250                       "constructed with null function pointer deleter"); }
251
252       explicit
253       unique_ptr(pointer __p)
254       : _M_t(__p, deleter_type())
255       { static_assert(!std::is_pointer<deleter_type>::value,
256                       "constructed with null function pointer deleter"); }
257
258       unique_ptr(pointer __p,
259           typename std::conditional<std::is_reference<deleter_type>::value, 
260               deleter_type, const deleter_type&>::type __d) 
261       : _M_t(__p, __d) { }
262
263       unique_ptr(pointer __p,
264                  typename std::remove_reference<deleter_type>::type && __d)
265       : _M_t(std::move(__p), std::move(__d))
266       { static_assert(!std::is_reference<deleter_type>::value, 
267                       "rvalue deleter bound to reference"); }
268
269       // move constructors
270       unique_ptr(unique_ptr&& __u) 
271       : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
272
273       template<typename _Up, typename _Up_Deleter> 
274         unique_ptr(unique_ptr<_Up, _Up_Deleter>&& __u) 
275         : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter()))
276         { }
277
278       // destructor
279       ~unique_ptr() { reset(); }
280
281       // assignment
282       unique_ptr&
283       operator=(unique_ptr&& __u)
284       {
285         reset(__u.release());
286         get_deleter() = std::move(__u.get_deleter()); 
287         return *this; 
288       }
289
290       template<typename _Up, typename _Up_Deleter> 
291         unique_ptr&
292         operator=(unique_ptr<_Up, _Up_Deleter>&& __u)
293         {
294           reset(__u.release());
295           get_deleter() = std::move(__u.get_deleter()); 
296           return *this;
297         }
298
299       unique_ptr&
300       operator=(__unspecified_pointer_type)
301       {
302         reset();
303         return *this;
304       }
305
306       // observers
307       typename std::add_lvalue_reference<element_type>::type 
308       operator[](size_t __i) const 
309       {
310         _GLIBCXX_DEBUG_ASSERT(get() != 0);
311         return get()[__i];
312       }
313
314       pointer
315       get() const
316       { return std::get<0>(_M_t); }
317
318       typename std::add_lvalue_reference<deleter_type>::type 
319       get_deleter()
320       { return std::get<1>(_M_t); }
321
322       typename std::add_lvalue_reference<
323           typename std::add_const<deleter_type>::type
324               >::type 
325       get_deleter() const
326       { return std::get<1>(_M_t); }    
327
328       operator __unspecified_bool_type () const 
329       { return get() == 0 ? 0 : &__this_type::_M_t; }
330     
331       // modifiers
332       pointer
333       release() 
334       {
335         pointer __p = get();
336         std::get<0>(_M_t) = 0;
337         return __p;
338       }
339
340       void
341       reset(pointer __p = pointer()) 
342       {
343         if (__p != get())
344         {
345           get_deleter()(get());
346           std::get<0>(_M_t) = __p;
347         }
348       }
349
350       // DR 821.
351       template<typename _Up>
352         void reset(_Up) = delete;
353
354       void
355       swap(unique_ptr&& __u)
356       {
357         using std::swap;
358         swap(_M_t, __u._M_t);
359       }
360
361       // disable copy from lvalue
362       unique_ptr(const unique_ptr&) = delete;
363       unique_ptr& operator=(const unique_ptr&) = delete;
364
365       // disable construction from convertible pointer types
366       // (N2315 - 20.6.5.3.1)
367       template<typename _Up>
368         unique_ptr(_Up*, typename
369                    std::conditional<std::is_reference<deleter_type>::value,
370                    deleter_type, const deleter_type&>::type,
371                    typename std::enable_if<std::is_convertible<_Up*, 
372                    pointer>::value>::type* = 0) = delete;
373
374       template<typename _Up>
375         unique_ptr(_Up*, typename std::remove_reference<deleter_type>::type&&,
376                    typename std::enable_if<std::is_convertible<_Up*, 
377                    pointer>::value>::type* = 0) = delete;
378
379       template<typename _Up>
380         explicit
381         unique_ptr(_Up*, typename std::enable_if<std::is_convertible<_Up*, 
382                    pointer>::value>::type* = 0) = delete;
383
384     private:
385       __tuple_type _M_t;
386   };
387   
388   template<typename _Tp, typename _Tp_Deleter> 
389     inline void
390     swap(unique_ptr<_Tp, _Tp_Deleter>& __x,
391          unique_ptr<_Tp, _Tp_Deleter>& __y)
392     { __x.swap(__y); }
393
394   template<typename _Tp, typename _Tp_Deleter> 
395     inline void
396     swap(unique_ptr<_Tp, _Tp_Deleter>&& __x,
397          unique_ptr<_Tp, _Tp_Deleter>& __y)
398     { __x.swap(__y); }
399
400   template<typename _Tp, typename _Tp_Deleter> 
401     inline void
402     swap(unique_ptr<_Tp, _Tp_Deleter>& __x,
403          unique_ptr<_Tp, _Tp_Deleter>&& __y)
404     { __x.swap(__y); }
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 __x.get() == __y.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
420   template<typename _Tp, typename _Tp_Deleter,
421            typename _Up, typename _Up_Deleter>
422     inline bool
423     operator<(const unique_ptr<_Tp, _Tp_Deleter>& __x,
424               const unique_ptr<_Up, _Up_Deleter>& __y)
425     { return __x.get() < __y.get(); }
426
427   template<typename _Tp, typename _Tp_Deleter,
428            typename _Up, typename _Up_Deleter>
429     inline bool
430     operator<=(const unique_ptr<_Tp, _Tp_Deleter>& __x,
431                const unique_ptr<_Up, _Up_Deleter>& __y)
432     { return !(__y.get() < __x.get()); }
433
434   template<typename _Tp, typename _Tp_Deleter,
435            typename _Up, typename _Up_Deleter>
436     inline bool
437     operator>(const unique_ptr<_Tp, _Tp_Deleter>& __x,
438               const unique_ptr<_Up, _Up_Deleter>& __y)
439     { return __y.get() < __x.get(); }
440
441   template<typename _Tp, typename _Tp_Deleter,
442            typename _Up, typename _Up_Deleter>
443     inline bool
444     operator>=(const unique_ptr<_Tp, _Tp_Deleter>& __x,
445                const unique_ptr<_Up, _Up_Deleter>& __y)
446     { return !(__x.get() < __y.get()); }
447
448 _GLIBCXX_END_NAMESPACE
449
450 #endif /* _UNIQUE_PTR_H */